串口linux c ++ [英] serial ports linux c++

查看:96
本文介绍了串口linux c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够列出计算机中的每个端口并连接到最后一个可用的串行端口。如果我有com1,2,3,它应该连接到端口3,如果断开连接,应用程序应该连接到端口2,依此类推。

如果有人可以给我一个例子,我会非常感激。

I want to be able to list every port in a computer and connect to the last serial port available. If I have com1, 2, 3, it should connect to port 3, if it is disconnected, the app should connect to port 2 and so on.
If someone could give me an example, I would greatly appreciate it.

推荐答案

对于最新的Linux内核,将有目录 / dev / serial 包含指向串行设备的链接。这仅包含指向现有设备的链接。



经典串行端口链接为 / dev / ttySx ,USB连接到串行适配器链接为 / dev / ttyUSBx 其中 x 枚举从零开始的端口。



特别是对于不存在的端口, / dev / ttySx 链接也可能存在。所以必须检查这些。这可以在命令行上使用

With recent Linux kernels, there will be the directory /dev/serial containing links to the serial devices. This contains only links to existing devices.

Classic serial ports are linked as /dev/ttySx and USB to serial adapters are linked as /dev/ttyUSBx where x is enumerating the ports starting at zero.

Especially the /dev/ttySx links may be also present for not existing ports. So these must be checked. This can be done on the command line using
setserial -g /dev/ttySx





要以编程方式检查存在,请打开设备并发出 ioctl TIOCGSERIAL 请求:



To check the existance programmatically, open the device and issue an ioctl TIOCGSERIAL request:

int fd = open(device, O_RDWR | O_NONBLOCK);
if (fd != -1)
{
    serial_struct info;
    // Port exists when this returns zero
    ioctl(fd, TIOCGSERIAL, &info);
    close(fd);
}


这篇关于串口linux c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆