0

I'm developing on an am335x system with ubuntu and the last kernel released from TI (vendor). I'm using a virtual tty device (ttyUSB0) for comunicate with a remote device. After about one hour of continuous comunication (cyclic open-transmit-receive-close) I get a strange behaviour of read(). If the UART is opened in blocking mode the read hangs forever (no matter what value I set on VMIN&VTIME). If I open it in non-blocking mode it return -1 for ever (after 1 hour). Now I'm using select() to check if there is data to be read. In case I receive a negative result from select, how can I handle the error? What is a good practice? I have to restart the service?

This code is a part of a service that start at boot time(with upstart). When it hangs, if I restart it, it works again. The restart do not have any effect on the device with which I'm communicating. It works properly.

This is a piece of code, just for completeness:

        FD_ZERO(&set); /* clear the set */
        FD_SET(tty_fileDescriptor, &set); /* add our file descriptor to the set */
        timeout.tv_sec = 10;
        timeout.tv_usec = 0;
        rv = select(tty_fileDescriptor + 1, &set, NULL, NULL, &timeout);
        if(rv>0){
            letti=read(tty_fileDescriptor,payLoadTMP,300);
        }if(rv<0){
                  perror("select")
                   //what to do here to re-stablish communication?
        }

The perror's output is:

select: Resource temporarily unavailable

this is a grep on dmesg

usb 1-1: cp210x converter now attached to ttyUSB0

any ideas? How to re-stablish connection?

JosephITA
  • 502
  • 2
  • 11
  • 21
  • 1
    You should emit `errno` value. `perror("select");` would also shed some light on why `select()` returns with -1. – yegorich Feb 03 '14 at 11:39
  • Ok, Ill'add it. Thanks. Let's see what happen – JosephITA Feb 03 '14 at 11:41
  • I get select: Resource temporarily unavailable.. I edit the answer – JosephITA Feb 03 '14 at 12:13
  • Seems like USB was disconnected. Could you try to connect it via self-powered USB hub? The only way to re-establish connection is to close file descriptor and open `ttyUSB0` again. – yegorich Feb 03 '14 at 12:21
  • 1
    how often do you run the transmit-receive procedure? – kirill Feb 03 '14 at 12:25
  • @yegorich few methods later I close the fd and I reopen it only if I know that there is some data (I get an IRQ and then I start the communication). – JosephITA Feb 03 '14 at 12:41
  • @kirill a lot of times in a second (about 4 complete open-trasmit-receive-close cycle in a second). It works properly for about 1 hour – JosephITA Feb 03 '14 at 12:42
  • 2
    may you try not to open\close the port every time? just open it once and close it once on exit. And flush the input buffer before transmit. If this would help that means you have a problem with closing. – kirill Feb 03 '14 at 13:29
  • @kirill In the first version I opened and closed it one but had the same error so I moved to the open-close-each-time version. I'm trying right now whit flush before transmit. Thanks for the hint – JosephITA Feb 03 '14 at 14:03

0 Answers0