问题描述
我正在Linux中使用termios api与串行设备进行通信.我正在尝试检测设备是否已断开连接,因此我可以尝试在超时后重新连接.我有以下示例代码:
I am using the termios api in Linux to communicate with a serial device. I'm trying to detect if the device has disconnected so I can try to reconnect after some timeout. I have the following example code:
while(1)
{
FD_ZERO(&rfds);
FD_SET(tty_fd, &rfds);
// have tried checking fcntl(tty_fd, F_GETFL); too
// Blocking call to wait until we have data
select(tty_fd+1, &rfds, NULL, NULL, NULL);
// While we have data, collect it
while (read(tty_fd, &c, 1)>0 && bytesRead++<200)
{
serialBuffer.push_back(c);
}
bytesRead = 0;
// Try to parse it
BufferParse();
}
在ttyUSB设备物理断开连接后,我实际上没有看到select()或fcntl返回错误值(-1).我当然可以检查/dev/中的文件是否存在,但是我希望有一个更优雅的解决方案.
I'm not actually seeing select() or fcntl return error values (-1) after the ttyUSB device is physically disconnected. I could, of course, check to see if the file in /dev/ exists, but I was hoping there was a more elegant solution.
将感谢您的任何建议,谢谢!
Would appreciate any advice, thanks!
推荐答案
首先值得一提的是,serial-usb的行为如下:
First of all it worth mentioning, that the behavior serial-usb is following:
在USB设备上,拔出的断开连接称为
On usb device unplugged disconnect is called
在我们的例子中是usb_serial_disconnect(struct usb_interface * interface)
in our case it is usb_serial_disconnect(struct usb_interface *interface)
调用usb_serial_console_disconnect(serial),调用tty_hangup ...,依此类推.
which calles usb_serial_console_disconnect(serial), which calles tty_hangup ... and so on.
您可以按照从此处开始的链条进行操作: http://lxr.free-electrons.com /source/drivers/usb/serial/usb-serial.c#L1091
You can follow chain started from here:http://lxr.free-electrons.com/source/drivers/usb/serial/usb-serial.c#L1091
简而言之,这是以下经典方式:
In short this results in following classic manner:
pselect表示文件描述符已准备好,并且ioctl(fd,FIONREAD和& len)返回零len.
pselect signals that file descriptor is ready and ioctl(fd, FIONREAD, &len) returns zero len.
就是这样,您拔出了设备的电源.
That's it you unplugged the device.
汇总(从您的代码中得出):
Summurizing (derived from your code) :
while(1)
{
FD_ZERO(&rfds);
FD_SET(tty_fd, &rfds);
// have tried checking fcntl(tty_fd, F_GETFL); too
// Blocking call to wait until we have data
int ready = select(tty_fd + 1, &rfds, NULL, NULL, NULL);
if(ready && FD_ISSET(tty_fd, &rfds)) {
size_t len = 0;
ioctl(tty_fd, FIONREAD, &len);
errsv = errno;
if(len == 0)
{
printf("prog_name: zero read from the device: %s.", strerror(errsv));
/* close fd and cleanup or reconnect etc...*/
exit(EXIT_FAILURE);
}
// While we have data, collect it
while (read(tty_fd, &c, 1)>0 && bytesRead++<200)
{
serialBuffer.push_back(c);
}
bytesRead = 0;
// Try to parse it
BufferParse();
}
}
很遗憾您没有说明您使用的是哪种设备.
It's a pity that you did not say what kind of device you are using.
如果您的设备能够进行RTS/CTS流量控制,则还可以检测到线路中断.
In case if your device is capable of RTS/CTS flow control it is also possbile to detect line break.
这篇关于在Linux中使用termios API(C ++)检测字符设备是否已断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!