Linux, termios: how to handle negative result of s

2019-08-31 06:53发布

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?

0条回答
登录 后发表回答