问题描述
请让我知道是否有任何API,系统调用函数(在Linux上),我可以用我的C程序动态检测COM端口,也就是说,每当一个USB调制解调器软件狗插入口我会能够使用API或系统调用,或函数在我的C程序才能检测到COM端口。
Please let me know if there is any API, system call, function (in Linux) which I can use my C program to detect a COM port dynamically, i.e., whenever a USB modem dongle is inserted in the port I will be able to detect that COM port using the API, or system call, or function in my C program.
推荐答案
根据您的调制解调器,USB串口设备可能会显示为的/ dev / ttyUSBn
或的/ dev / ttyACMn
,其中 N
一些数量从 0
Depending on your modem, USB serial port device may show up as /dev/ttyUSBn
or /dev/ttyACMn
, where n
is some number starting from 0
.
您可以配置的udev
规则自动对设备的反应被插入或删除。
You can configure udev
rule to automatically react on device being inserted or removed.
如果你想这样做你自己在C,你需要使用网络链路(7)
插座。如果你不想与他们不甘示弱,可能更容易的方法是简单地使用工具 udevadm
由udev包提供(udevadm使用网络链路
内部),这样的事情:
If you want to do it on your own in C, you need to make use of netlink(7)
sockets. If you don't want to fiddle with them, probably easier approach is to simply use utility udevadm
provided by udev package (udevadm is using netlink
internally), something like that:
udevadm monitor --kernel
如果你要在你的C程序中使用它,只需调用它像这条管道:
If you are going to use it in your C program, simply call it in pipe like this:
stdbuf -o0 udevadm monitor --kernel
和设置选择
循环,所以它可以立即做出反应。包装在 stdbuf
是必要的,以避免udevadm提供缓冲。
and setup select
loop, so it can react immediately. Wrapping in stdbuf
is necessary to avoid buffering provided by udevadm.
这篇关于在Linux的COM端口检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!