Saturday 1 February 2014

COM port detection in Linux

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.

Answers:-

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 -i0 udevadm monitor --kernel
and setup select loop, so it can react immediately. Wrapping in stdbuf is necessary to avoid buffering provided by udevadm.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More