Linux, termios: how to handle negative result of select()

喜你入骨 提交于 2019-12-25 02:24:12

问题


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?

来源:https://stackoverflow.com/questions/21525654/linux-termios-how-to-handle-negative-result-of-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!