Linux中的COM端口检测 [英] COM port detection in Linux

查看:268
本文介绍了Linux中的COM端口检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请让我知道,如果有任何API,系统调用,函数(在Linux中),我可以使用我的C程序来动态检测COM端口,即,每当USB调制解调器加密狗插入端口,我将能够在我的C程序中使用API​​或系统调用或函数来检测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.

如果您想在C中自己执行此操作,请使用c $ c> udev 您需要使用 netlink(7)套接字。如果你不想干扰他们,可能更容易的方法是简单地使用由udev包提供的实用程序 udevadm使用 netlink 内部),像这样:

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端口检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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