简体   繁体   中英

COM port detection in Linux

请让我知道是否有任何API,系统调用,函数(在Linux中),我可以使用我的C程序来动态检测COM端口,即,只要将USB调制解调器加密狗插入端口,我就可以检测到使用API​​或系统调用或C语言程序中的功能的COM端口。

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 .

You can configure udev rule to automatically react on device being inserted or removed.

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

If you are going to use it in your C program, simply call it in pipe like this:

 stdbuf -o0 udevadm monitor --kernel

and setup select loop, so it can react immediately. Wrapping in stdbuf is necessary to avoid buffering provided by udevadm.

In Linux the serial ports aren't called COMx. They (real serial ports) are generally named /dev/ttySx where x is a number, starting at 0 for COM1, 1 for COM2, etc.

If you want a list, you should look in /dev for ttyS devices.

Here is a very detailed page for serial port programming on Linux:

Finally, note that a quick Google search goes a long way for general questions like this!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM