简体   繁体   English

将USB序列号与Qt和libusb的端口名匹配

[英]Match USB serial # to port name with Qt and libusb

I have a device that acts as a serial port connected over USB. 我有一台充当通过USB连接的串行端口的设备。 I have a class that interfaces with the device through a QSerialPort . 我有一个通过QSerialPort与设备连接的QSerialPort Soon I will have multiple such devices and the program will need to reliably distinguish them to assign configured values to instances of the class. 很快我将拥有多个这样的设备,程序将需要可靠地区分它们,以便将配置的值分配给该类的实例。

Qt identifies serial devices by the portName field of the QSerialPortInfo helper object. Qt通过QSerialPortInfo帮助器对象的portName字段标识串行设备。 This name is derived from the /dev/ path (eg /dev/tty.foo42 is port name "foo42" ) and this path does not remain associated with a particular device over reboots or deployment to a different machine. 该名称是从/ dev /路径派生的(例如/dev/tty.foo42是端口名"foo42" ),并且在重新引导或部署到另一台计算机时,此路径不会与特定设备保持关联。 However, through libusb I can get a serial number, which is stable across reboots and different machines. 但是,通过libusb,我可以获得一个序列号,该序列号在重新启动和不同计算机上均稳定。

What I am still missing is a way to match the serial number, which can be identified in advance and recorded in a configuration file, to a port name, which can be used to open a QSerialPort . 我仍然缺少的是一种将序列号与端口名进行匹配的方法,该序列号可以预先识别并记录在配置文件中,该端口名可以用于打开QSerialPort Currently deploying to MacOS, but in the future we may wish to deploy to Linux. 当前正在部署到MacOS,但将来我们可能希望部署到Linux。

Searching existing questions mostly turns up ways to configure a Linux system to always assign a particular dev path to a particular device. 搜索现有问题通常可以找到一种配置Linux系统的方式,以便始终为特定设备分配特定的开发路径。 A solution that involves configuring the operating system is acceptable, but something the application can do without that is preferable. 涉及配置操作系统的解决方案是可以接受的,但如果不这样做,应用程序可以做的事情是可取的。 A Mac specific solution is acceptable, but something that also works on Linux is preferable. 可以使用Mac专用解决方案,但最好在Linux上也可以使用。

The documentation for QSerialPortInfo::serialNumber() (new in Qt 5.3) sounds like what you're after: QSerialPortInfo :: serialNumber()的文档(Qt 5.3中的新增功能)听起来像您所追求的:

QString QSerialPortInfo::serialNumber() const QString QSerialPortInfo :: serialNumber()常量

Returns the serial number string of the serial port, if available; 返回串口的序列号字符串(如果有); otherwise returns an empty string. 否则返回一个空字符串。

Note: The serial number may include letters. 注意:序列号可能包含字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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