列出机器的串行端口
我有一个基于 Arduino 的设备,通过 USB 连接。
我想在插入时使用 QExtSerialPort(或任何必要的方法)从我的 Qt 4 应用程序中检测它。
如果这是不可能的,我想我可以以某种方式获得一个列表系统的端口名称,然后尝试所有这些名称来搜索我的 Arduino(我将在其中实现某种握手程序以使其正确检测)。我对这种方法的担忧是,如果我以不同的波特率发送某种握手确认,我不确定设备(例如打印机)是否会损坏。
所以,我真的不知道从哪里开始。哪一个是最好的方法?我将如何实施它?
I have an Arduino-based device which connects through USB.
I'd like to detect it from my Qt 4 application, using QExtSerialPort
(or whatever necessary), when it's plugged in.
If this weren't possible, I thought I could somehow get a list of the system's port names and just try all of them in search for my Arduino (where I'd implement some kind of handshaking procedure for it to detect it correctly). My concern in this approach is that I'm not sure if a device (for example, printer) would get damaged if I send some kind of handshaking ack at a different baud rate.
So, I don't really know where to start for any of them. Which would be the best approach? How would I implement it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您可以通过查看来找到 Windows 上的串行端口列表
注册表项
UNIX 系统上的每个串行端口都有一个或多个与其关联的设备文件(/dev 目录中的文件):有关
POSIX 系统上串行编程的更多详细信息 < a href="http://www.easysw.com/~mike/serial/serial.html#2_5_1" rel="nofollow noreferrer">此处
I believe you can find list of serial ports on Windows by looking into
registry key
Each serial port on a UNIX system has one or more device files (files in the /dev directory) associated with it:
more details on serial programing on POSIX systems here
由于您的设备是 USB,因此您的 UART 端口将由其硬件中的某种转换器进行模拟。因此,首先您必须了解您的系统上正在使用什么驱动程序。
最常见的SERIAL->USB转换器使用PL2303/PL2301芯片,因此它会在/dev上创建一个路径,如果它是第一个设备,它将显示为“/dev/ttyUSB0”,但您也可能会看到列表读取proc 路径(如“cat /proc/bus/usb/devices”)。
在Windows下,它通常会创建一个虚拟的“COM”,只需进入设备管理器并检查端口即可。
当您确定硬件如何与您的系统通信时,您可以使用 QExtSerialPort 来包装系统 API 并与设备通信。
Since your device is USB, your UART port will be emulated by some kind of conversor in his hardware. So first you must understand what driver is being used on your system.
The most common SERIAL->USB conversor uses PL2303/PL2301 chip, so it would create a path on /dev, if its the first device, it will appear as "/dev/ttyUSB0", but you may also see the list reading the proc path (like "cat /proc/bus/usb/devices").
Under Windows it usually creates a virtual "COM", just go to device manager and check the port.
When you are sure about how the HW talks to your system, you may use QExtSerialPort for wrapping the system API and talk to the device.
方式太难,太特定于平台,使用奇怪的 Windows 注册表项或依赖 Linux 上的硬连线设备节点。
你走在正确的道路上。获取 QextSerialPort 或 QSerialDevice(我在项目中更喜欢它,因为它集成在 Qt5 中),查看示例并简单地使用它。在这两个库中,您都会获得某种端口枚举器类,它会返回所有配置串行端口的列表。您只需手动执行特定于平台/设备的设置(例如在我当前的嵌入式项目中以半双工模式获取 RS485),但“标准”问题完美地封装在 QIODevice 实现中。
您可以像文件一样使用 QextSerialPort 和 QSerialDevice。打开它(而不是指定设备名称的文件名,即 Windows 上的“COM1”或 Linux 上的“/dev/tty0”,具体取决于您的配置),然后像使用普通 QFile、QBuffer 一样进行读取或写入操作, Qwhatever 继承自 QIODevice。
如果您在打开端口和通信时遇到任何问题,请随时询问! :)
Way too hard and too platform specific, using weird Windows Registry keys or rely on hard wired device nodes on Linux.
You are on the right way. Get QextSerialPort or QSerialDevice (which I preffer in my projects, because it got integrated in Qt5), have a look at the examples and simply use it. In both libraries you get some kind of port enumerator class which returns you a list of all configures serial ports. Only platform/device specific settings you will have to do manually (like getting RS485 in half-duplex mode on my current embedded project), but "standard" problems are perfectly encapsulated in a QIODevice implementation.
You can use both QextSerialPort and QSerialDevice like a file. Open it (instead of a filename you specify the device name ie. "COM1" on Windows or "/dev/tty0" on Linux, depending on your configuration) and then read or write like you are doing it with an ordinary QFile, QBuffer, Qwhatever-inherits-from-QIODevice.
If you have any problems opening the port and communicating, don't hesitate to ask! :)