Seeking USB Serial Com Port in Windows Automatically : via PID VID
After you read previous article, you might know how to operate a com port in Windows.
But that example requires programmer (or user, if you modified that example being able to support inputting command line) to set a com port number, it is not consummate. I will fill the flaw in here.
You need to check the device enumeration path zeroth.In here, I use CH340 (USB com port chip from China), I assure there is only one CH340 connected to the computer, I could seek the comport via pid(product id) and vid (vendor
id).
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzYwNjE3MA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">
If you do not know what is vid/pid : Briefly say, it is USB device firmware replying computer what device it is in seral number. We could use pid/vid + google to indentify the device, even there is no correspending driver.
You need to set hSerial as invalid in the begining of that code:
hSerial = INVALID_HANDLE_VALUE;
And you should replace that as the code below:
hSerial = CreateFile(&comPortName[0], GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
as
if(0 == strncmp(&comPortName[0], "\\\\.\\COM0", MAX_STR_LEN))
{
unsigned int i; COMMCONFIG comConfig;
DWORD dwSize;
dwSize = sizeof(comConfig); ZeroMemory(&comConfig, sizeof(COMMCONFIG)); for(i = 1; i< 256; i++){
TCHAR comName[16]; sprintf(&comName[0], "COM%d", i); if(FALSE != GetDefaultCommConfig(&comName[0], &comConfig, &dwSize))
printf("found %s\n", &comName[0]);
}/*for */ } if(0 == strncmp(&comPortName[0], "\\\\.\\COM0", MAX_STR_LEN))
{
DWORD dwGuids;
GUID *pGuids; HDEVINFO hDevInfo;
SP_DEVICE_INTERFACE_DATA devInterfaceData;
SP_DEVINFO_DATA devInfoData;
unsigned int index; dwGuids = 0; isSuc = SetupDiClassGuidsFromName("Ports", NULL, 0, &dwGuids); pGuids = (GUID*)malloc(dwGuids*sizeof(GUID));
ZeroMemory(pGuids, dwGuids*sizeof(GUID));
isSuc = SetupDiClassGuidsFromName("Ports", pGuids, dwGuids, &dwGuids); hDevInfo = SetupDiGetClassDevs(pGuids, NULL, NULL,
/*DIGCF_ALLCLASSES | DIGCF_PRESENT |*/ DIGCF_DEVICEINTERFACE); index = 0; ZeroMemory(&devInfoData, sizeof(SP_DEVINFO_DATA));
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); while(SetupDiEnumDeviceInfo(hDevInfo, index, &devInfoData))
{ TCHAR szDeviceInstanceID[1024]; index++; isSuc = CM_Get_Device_ID(devInfoData.DevInst,
&szDeviceInstanceID[0] , sizeof(szDeviceInstanceID), 0); //printf("%s\n", &szDeviceInstanceID[0]); if(0 == strncmp(&szDeviceInstanceID[0], "USB\\VID_1A86&PID_7523",
strlen("USB\\VID_1A86&PID_7523")) )
{ DWORD requiredSize;
GUID classGuid; SP_DEVICE_INTERFACE_DATA devInterfaceData;
SP_DEVICE_INTERFACE_DETAIL_DATA *pDevInterfaceDetailData; printf("&szDeviceInstanceID[0] = %s\n", &szDeviceInstanceID[0]); ZeroMemory(&devInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA)); devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
classGuid = devInfoData.ClassGuid; isSuc = SetupDiEnumDeviceInterfaces(hDevInfo, &devInfoData, pGuids,
0, &devInterfaceData); isSuc = SetupDiGetDeviceInterfaceDetail(hDevInfo,
&devInterfaceData, NULL, NULL, &requiredSize, NULL); //printf ("%s\n", GetLastErrorMessage( GetLastError() ) ); pDevInterfaceDetailData = (SP_INTERFACE_DEVICE_DETAIL_DATA*)malloc(requiredSize); pDevInterfaceDetailData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); isSuc = SetupDiGetDeviceInterfaceDetail( hDevInfo,
&devInterfaceData, pDevInterfaceDetailData, requiredSize,
&requiredSize, &devInfoData); printf("devInterfaceDetailData.DevicePath = %s\n",
pDevInterfaceDetailData->DevicePath); hSerial = CreateFile(pDevInterfaceDetailData->DevicePath,
GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL ); free(pDevInterfaceDetailData); pDevInterfaceDetailData = NULL; if(INVALID_HANDLE_VALUE != hSerial)
break;
}
} free(pGuids); pGuids = NULL;
SetupDiDestroyDeviceInfoList(hDevInfo); if(INVALID_HANDLE_VALUE == hSerial)
{
printf("auto seeking com number fail!!\n");
return 0;
}
}
else /*non auto seeking com port*/
{
hSerial = CreateFile(&comPortName[0], GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
}/*if auto seeking com port*/
("\\\\.\\COM0" be auto-seeking mode in here.) blogID=6543025294777699729" target="_blank" style="clear:right; margin-bottom:1em; margin-left:1em; float:right">
You have to modify the USB\\VID_1A86&PID_7523 as your device's.
( If you use FTDI's FT232B or FT232R, that should be
FTDIBUS\\VID_0403+PID_6001
)
That would support auto-seeking com-port.
Note the line :
while(SetupDiEnumDeviceInfo(hDevInfo, index, &devInfoData))
You would find NOT ONLY one device as VID_1A86&PID_7523, even you have plugged only one CH340. If you print that seeking result (it is just the line : printf("&szDeviceInstanceID[0] = %s\n", &szDeviceInstanceID[0]);), you would watch the printing as: blogID=6543025294777699729" target="_blank" style="clear:left; margin-right:1em; margin-bottom:1em; float:left"> blogID=6543025294777699729" target="_blank" style="clear:left; margin-right:1em; margin-bottom:1em; float:left">
&szDeviceInstanceID[0] = USB\VID_1A86&PID_7523\6&123E8975&0&1
devInterfaceDetailData.DevicePath = \\?\usb#vid_1a86&pid_7523#6&123e8975&0&1#{4d
36e978-e325-11ce-bfc1-08002be10318}
&szDeviceInstanceID[0] = USB\VID_1A86&PID_7523\6&123E8975&0&2
devInterfaceDetailData.DevicePath = \\? \usb#vid_1a86&pid_7523#6&123e8975&0&2#{4d
36e978-e325-11ce-bfc1-08002be10318}
&szDeviceInstanceID[0] = USB\VID_1A86&PID_7523\6&123E8975&0&3
devInterfaceDetailData.DevicePath = \\? \usb#vid_1a86&pid_7523#6&123e8975&0&3#{4d
36e978-e325-11ce-bfc1-08002be10318}
&szDeviceInstanceID[0] = USB\VID_1A86&PID_7523\6&123E8975&0&4
devInterfaceDetailData.DevicePath = \\? \usb#vid_1a86&pid_7523#6&123e8975&0&4#{4d
36e978-e325-11ce-bfc1-08002be10318}
&szDeviceInstanceID[0] = USB\VID_1A86&PID_7523\6&123E8975&0&5
devInterfaceDetailData.DevicePath = \\?\usb#vid_1a86&pid_7523#6&123e8975&0&5#{4d
36e978-e325-11ce-bfc1-08002be10318}
&szDeviceInstanceID[0] = USB\VID_1A86&PID_7523\7&107B0B49&0&2
devInterfaceDetailData.DevicePath = \\?\usb#vid_1a86&pid_7523#7&107b0b49&0&2#{4d
36e978-e325-11ce-bfc1-08002be10318}
&szDeviceInstanceID[0] = USB\VID_1A86&PID_7523\7&14F85601&0&1
devInterfaceDetailData.DevicePath = \\?\usb#vid_1a86&pid_7523#7&14f85601&0&1#{4d
36e978-e325-11ce-bfc1-08002be10318}
I do not know why Windows lists so much device path. AlI I could do, is to try opening each. you could find that the actual one's last 4 characters (like &0&4, &0&2..etc) would change with port number, that depends on which
USB socket you plug.
Seeking USB Serial Com Port in Windows Automatically : via PID VID的更多相关文章
- Linux/drivers/usb/serial/ftdi_sio.c
Linux/drivers/usb/serial/ftdi_sio.h /* 2 * Driver definitions for the FTDI USB Single Port Serial Co ...
- Methods Collection of Enumerating Com Port in Windows, by C
According to this stack overflow thread, PJ Naughter has implemented 9 methods to emunerate com port ...
- UEFI Bootable USB Flash Drive - Create in Windows(WIN7 WIN8)
How to Create a Bootable UEFI USB Flash Drive for Installing Windows 7, Windows 8, or Windows 8.1 In ...
- gentoo usb serial ch340 16进制读写
首先安装包含 lsusb 命令的 usbutils, emerge -v usbutils. 使用 lsusb后,可以查看到 ch340 的信息: Bus 001 Device 004: ID 1a8 ...
- usb serial for android
/******************************************************************** * usb serial for android * 说明: ...
- install usb serial
Install driver for USB-UART bridge converter on Linux Ubuntu12.04 Ubuntu下USB转串口芯片驱动程序安装,支持cp210x,pl2 ...
- 将cocos2dx+lua创建的游戏port到windows phone
在整个Port的过程中遇到的问题总结例如以下 1.一定要使用最新版本号的cocos2dx,原因大家看一下changelog就知道了,近期的cocos2dx版本号都是在修windows phone上的b ...
- Serial Fluent UDF on Windows
test test Table of Contents 1. Serial UDF on Windows OS 1 Serial UDF on Windows OS Note: Udf has to ...
- kill 8080 port on windows
1. 查找PID netstat -ano | findstr :yourPortNumber 2. kill进程 taskkill /PID typeyourPIDhere /F
随机推荐
- jQuery插件综合应用(三)发布文章页面
一.使用的插件 一个折叠的功能导航,由Akordeon插件实现.Nanoscroller插件与Tagit插件主要用于美化页面.这里只是测试,其实还可以综合使用其它的插件,例如将Akordeon插件换成 ...
- php引用(&)详解及注意事项
php的引用(就是在变量或者函数.对象等前面加上&符号) 在PHP 中引用的意思是:不同的名字访问同一个变量内容. 与C语言中的指针是有差别的.C语言中的指针里面存储的是变量的内容,在内存中存 ...
- 【转】使用spring @Scheduled注解执行定时任务
http://blog.csdn.net/sd4000784/article/details/7745947 以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在s ...
- Memento:客户端瘦身
说是客户端瘦身,其实备忘录模式的本质让调用客户端职责减轻,将客户端的对于实现比如数据恢复之类细节的内容封装在操作类之中.其实面向对象的一重要方面就是划分清楚职责,这样可以减少改到造成的影响,便于扩展. ...
- iOS应用如何支持IPV6-b
果然是苹果打个哈欠,iOS行业内就得起一次风暴呀.自从5月初Apple明文规定所有开发者在6月1号以后提交新版本需要支持IPV6-Only的网络,大家便开始热火朝天的研究如何支持IPV6,以及应用中哪 ...
- http server v0.1_http_reponse.c
#include <string.h> #include <sys/stat.h> #include <sys/mman.h> #include <fcntl ...
- Ubuntu14.04 和 Windows7 双系统安装
用了一个暑假,我原来的Ubuntu终于挂了,连gnome桌面器都进不去了,索性重装整个Ubuntu.至少这次我知道什么都升级是一个很糟糕的行为. 由于笔者的电脑原来是Win8预装机,所以各种地方都是的 ...
- iOS7新特性-NSURLSession详解
前言:本文由DevDiv版主@jas 原创翻译,转载请注明出处!原文:http://www.shinobicontrols.com/b ... day-1-nsurlsession/ 大家都知道,过去 ...
- IndexedDB
http://www.tfan.org/indexeddb/ http://fnvfox.appspot.com/thankyou.html http://www.tfan.org/wechat-on ...
- java Clone()克隆
转自:http://www.blogjava.net/orangelizq/archive/2007/10/17/153573.html 现在Clone已经不是一个新鲜词语了,伴随着“多莉”的产生这个 ...