做网站如何自己寻找客户,wordpress怎么运行,吉林seo排名公司,html模板网先右键【此电脑-管理- 设备管理器-端口#xff08;COM和LPT#xff09;】中找到我们插入的某个设备的物理序号#xff0c;如下图红色矩形框出的信息#xff0c;这个就是已插入设备的物理序号#xff08;就是插在哪个USB口的意思#xff09;。 在Linux下我们可以通过往/et… 先右键【此电脑-管理- 设备管理器-端口COM和LPT】中找到我们插入的某个设备的物理序号如下图红色矩形框出的信息这个就是已插入设备的物理序号就是插在哪个USB口的意思。 在Linux下我们可以通过往/etc/udev/rules.d文件夹中放入串口绑定规则文件将指定物理序号devpath上的设备绑定一个固定名称参考我的文章Linux绑定串口名称而在windows下没有这种方式。 那么当我们在Qt开发过程中需要获取windows系统下特定物理USB口的序号如Port_#0001.Hub_#0002和串口名称如COM3把它作为某个设备的专用启动位置就可以按下面的方法得到我用的Qt5.14.2是直接可以用的这是windows的api理论上受Qt版本影响不大。 QSerialPort本身不提供相关的接口所以我们要使用windows api。
#include windows.h
#include setupapi.h
#include regstr.h// 定义全局变量
GUID GuidSerialPort { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };// 使用SetupDiGetDeviceRegistryProperty获取设备属性
bool GetDeviceProperty(HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, DWORD Property, LPWSTR Buffer, DWORD BufferLength, PDWORD RequiredSize) {DWORD DataT;LONG Result;DeviceInfoData-cbSize sizeof(SP_DEVINFO_DATA);// 获取属性Result SetupDiGetDeviceRegistryPropertyW(DeviceInfoSet, DeviceInfoData, Property, DataT, (PBYTE)Buffer, BufferLength, RequiredSize);if (Result) {return true;} else {if (GetLastError() ERROR_INSUFFICIENT_BUFFER) {return false;} else {return false;}}
}void serialPortListFresh(){HDEVINFO DeviceInfoSet INVALID_HANDLE_VALUE;SP_DEVINFO_DATA DeviceInfoData;DWORD i;WCHAR Buffer[256];DWORD RequiredSize;QString portName;QString locationInfo;DeviceInfoSet SetupDiGetClassDevs(GuidSerialPort, 0, 0, DIGCF_PRESENT);if (DeviceInfoSet INVALID_HANDLE_VALUE) {qDebug() Error getting device information set;}for (i 0; i 10; i) {// 清空缓冲区ZeroMemory(Buffer, sizeof(Buffer));RequiredSize sizeof(Buffer);// 获取设备实例路径if (GetDeviceProperty(DeviceInfoSet, DeviceInfoData, SPDRP_LOCATION_INFORMATION, Buffer, sizeof(Buffer), RequiredSize)) {locationInfo QString::fromWCharArray(Buffer);qDebug() Location Info: locationInfo;// 获取设备描述通常是串口名如COM1ZeroMemory(Buffer, sizeof(Buffer));RequiredSize sizeof(Buffer);if (GetDeviceProperty(DeviceInfoSet, DeviceInfoData, SPDRP_DEVICEDESC, Buffer, sizeof(Buffer), RequiredSize)) {portName QString::fromWCharArray(Buffer);qDebug() Port Name: portName;// 输出配对信息qDebug() portName is located at locationInfo;}if (GetDeviceProperty(DeviceInfoSet, DeviceInfoData, SPDRP_FRIENDLYNAME, Buffer, sizeof(Buffer), RequiredSize)) {portName QString::fromWCharArray(Buffer);qDebug() Port Name: portName \n;QString portCom portName.mid(portName.indexOf((), portName.indexOf()) - portName.indexOf(());portlist.append(portCom);ui-comboBox_SerialName_Rail-addItem(ports.at(i).portName());if(locationInfo Port_#0001.Hub_#0002) {ui-comboBox_SerialName_Rail-setCurrentIndex(i); //选取}ui-comboBox_Lidar_SerialName_1-addItem(ports.at(i).portName());if(locationInfo Port_#0006.Hub_#0002) {ui-comboBox_Lidar_SerialName_1-setCurrentIndex(i); //选取}}}if (!SetupDiEnumDeviceInfo(DeviceInfoSet, i, DeviceInfoData)) {break;}}SetupDiDestroyDeviceInfoList(DeviceInfoSet);}可以得到如下打印 这样我们就知道插到Port_#0001.Hub_#0002举例的USB口上的设备的端口号是COM3了那么接下来就可以用QSerialPort打开COM3设备进行通信了。
注更多串口信息参数详见微软官网
SetupDiGetDeviceRegistryPropertyA 函数