根据PID和VID得到USB转串口的串口号
/*******************************************************************************
*
* FindAppUART.cpp - PC command line utility for enumerating MSP430 EVM's
* Application UARTs.
*
* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/ //------------------------------------------------------------------------------
// Desc: PC command line utility for enumerating MSP430 EVM's Application UARTs.
// The application returns the COM port number of the first UART that is
// found. If this is successful the error code is set to '0'. In case of
// the UART string could not be determined '1' is returned.
//
// The code was developed with the Express Edition of Visual C++ 2008
// http://www.microsoft.com/express/
//
// Ver.: 0.1 (February 2011)
// - Alpha version
//
// Auth: Andreas Dannenberg
// MSP430 Applications
// Texas Instruments, Inc.
//------------------------------------------------------------------------------ // Windows Header Files
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff
// from Windows headers
#include <windows.h>
#include <tchar.h>
#include <shellapi.h>
#include <setupapi.h> // "setupapi.lib" must be linked
// to the project
// C Runtime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h> //------------------------------------------------------------------------------
// DWORD WINAPI EnumComPorts(DWORD dwIndex, LPTSTR lptszName,
// DWORD dwNumOfElements)
//
// User-mode code fragment to identify attached VCP COMnn port[s] with a
// specific device instance ID based on USB VID and PID and returns the COMnn
// port that the OS embeds into the device instance ID. When called with
// dwIndex = 0, the function will enumerate all COMPORT class devices and
// return the name of the first one that was found. Subsequent calls using an
// incremented dwIndex parameter can be performed until ERROR_NO_MORE_ITEMS
// is returned.
//
// IN: dwIndex COMPORT class device # 0, 1, 2, ... to check
// dwNumOfElements The size of the lptszName buffer
// OUT: lptszName COMnn name of given device #
// return() ERROR_SUCCESS - lpszName is valid
// ERROR_NO_MORE_ITEMS - End of device list reached
//------------------------------------------------------------------------------
DWORD WINAPI EnumComPorts(DWORD dwIndex, LPTSTR lptszName, DWORD dwNumOfElements)
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
TCHAR *pcParse; // Create a HDEVINFO with all present devices
hDevInfo = SetupDiGetClassDevs(
NULL,
, // Enumerator
,
DIGCF_PRESENT | DIGCF_ALLCLASSES); if (INVALID_HANDLE_VALUE == hDevInfo)
{
return GetLastError();
} // Enumerate through all devices in set
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (i = ; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
LPTSTR buffer = NULL;
DWORD buffersize = ; // Get the device instance ID that is associated with the device information element
while (!SetupDiGetDeviceInstanceId(
hDevInfo,
&DeviceInfoData,
buffer,
buffersize,
&buffersize))
{
if (buffer)
{
LocalFree(buffer);
} if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
{
// Change the buffer size. Double the size to avoid problems on
// W2K MBCS systems per KB 888609.
buffer = (LPTSTR)LocalAlloc(LPTR, buffersize * );
}
else
{
// Error: could not get device instance ID
// Cleanup and return error code
SetupDiDestroyDeviceInfoList(hDevInfo);
return GetLastError();
}
} if (buffer)
{
// Look for the "Application UART" of common MSP430 EVMs. The application UART
// has an USB VID of 0x0451 (Texas Instruments) and an PID of 0xF432.
const TCHAR testval[] = _T("USB\\VID_0451&PID_F432&MI_00"); if (NULL != _tcsstr(buffer, testval))
{
TCHAR szFriendlyName[MAX_PATH]; if (SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,
NULL,
(PBYTE)szFriendlyName,
sizeof(szFriendlyName) - ,
NULL))
{
// Check if we have reached the dwIndex-th element, if not keep looking
if (dwIndex == )
{
// Find pointer to "COM" substring (secure)
szFriendlyName[sizeof(szFriendlyName) - ] = 0x00;
pcParse = _tcsstr(szFriendlyName, _T("COM")); if (pcParse != NULL)
{
// Zero-terminate COM port string after last digit
if (!isdigit(pcParse[])) {
pcParse[] = ;
}
else if (!isdigit(pcParse[])) {
pcParse[] = ;
}
else {
pcParse[] = ;
} // Pass string to the return parameter
_tcscpy_s(lptszName, dwNumOfElements, pcParse); // Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo); return ERROR_SUCCESS;
}
}
else
{
dwIndex--;
}
}
}
}
} // Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo); return ERROR_NO_MORE_ITEMS;
} //------------------------------------------------------------------------------
// Main application entry point. Simply return the first Application UART
// COM port that was found to STDOUT.
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szDeviceName[MAX_PATH];
DWORD dwReturnValue; dwReturnValue = EnumComPorts(, szDeviceName, _countof(szDeviceName)); if (dwReturnValue == ERROR_SUCCESS) {
_ftprintf_s(stdout, _T("%s\r\n"), szDeviceName);
return ;
} return ;
}
根据PID和VID得到USB转串口的串口号的更多相关文章
- RK3288 USB触摸屏无法使用,需要添加PID和VID
RK3288 Android5.1 现象:USB 接口触摸屏插到板子上,触摸屏无法使用,有可能出现更奇葩的,同一套代码,有的板子可以用,有的板子不能用. 1.打开串口调试,插上触摸屏,读取触摸屏的 ...
- 通过串口设备vid,pid自动获得该设备所对应的串口号
用C#做串口通讯很方便,因为dotfx2.0已经集成了Serial Port控件,此控件使用上比MSComm控件更简单,当然它也有一个小bug (RecievedBytesThreshold设置有时候 ...
- ubuntu下minicom和USB转串口(转)
ubuntu下minicom和USB转串口(转) minicom是linux下串口通信的软件,它的使用完全依靠键盘的操作,虽然没有“超级终端”那么易用,但是使用习惯之后读者将会体会到它的高效与便利 ...
- [驱动]内核添加USB转串口驱动支持
转自:http://blog.csdn.net/gatieme/article/details/49491325 目录 1. 问题 2. 驱动源码 3. 内核配置 4. 编译内核和模块驱动 5. 加载 ...
- 【小技巧】9针USB转串口简易连通性测试,附25针转9针
Part 1 前言 最近用SecureCRT连接串口,因为是笔记本用的USB转串口,好多次出现安装驱动OK,连接上了,但是没有串口打印.无法进行控制的问题:所以不清楚是USB串口的驱动问题,还是转接用 ...
- Ubuntu系统下USB转串口的使用
PC系统是Ubuntu12.04,与路由器开发板之间用USB转串口线连接. 一.硬件连接 确认Ubuntu对USB转串口设备的支持. 1.# lsmod | grep usbserial如果有usbs ...
- 单片机usb转串口的时灵时不灵的解答
写这篇博客,首先检讨一下自己,因为以前串口的程序,也和步进电机一样,时灵时不灵,我现在终于知道这是为什么了,因为51上有三个串口,一个公口,一个母口,一个usb转串口,这样的话,串口有3个了,我手头上 ...
- 在MAC OS X下安装usb转串口驱动(PL2303主控芯片)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 因为最近手里有一块STM32Discovery开发板,所以想搞一下STM32的开发,我前面的 ...
- mac usb转串口 连接树莓PI
USB 转串口是淘宝买的 http://item.taobao.com/item.htm?spm=a1z09.2.9.50.YOJBwG&id=38963495468&_u=4m1dr ...
随机推荐
- ajax读取json数据
首先建立json.txt文件 { "programmers": [ { "firstName": "Brett", "lastNa ...
- CMake实践(3)
一,本期目标 [sun@localhost t3]$ cat README t3:静态库(.a)与动态库(.so)构建 任务:1,建立一个静态库和动态库,提供HelloFunc函数供其他程序编程 ...
- RandomAccessFile、FileChannel、MappedByteBuffer读写文件
s package com.nio; import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IO ...
- linux下使用go-oci8
地址:https://github.com/wendal/go-oci8 它是 https://github.com/mattn/go-oci8 的分支. win下安装步骤参考:http://www. ...
- cocoa NSFileManager
NSFileManager中包含了用来查询单词库目录.创建.重命名.删除目录以及获取/设置文件属性的方法(可读性,可编写性等等). 每个程序都会有它自己的沙盒,通过它你可以阅读/编写文件.写入沙盒的文 ...
- hdu 5253 最小生成树
赤裸裸最小生成树,没啥说的,我用kruskal过的 /* * Author : ben */ #include <cstdio> #include <cstdlib> #inc ...
- FreeMarker笔记 第四章 其它
4.1 自定义指令 4.1.1 简介 自定义指令可以使用macro指令来定义.Java程序员若不想在模板中实现定义指令,而是在Java语言中实现指令的定义,这时可以使用freemarker.templ ...
- grails中报Cannot create a session after the response has been committed异常的解决办法
在grails中,我们在layouts\main.gsp中使用类似如下的代码来判断当前用户处于登录状态时显示相关的登录信息: <g:if test="${session.users}& ...
- HDU5873:Football Games
题目链接: Football Games 分析: 先将分数排序,然后 设当前队编号为p,设个指针为p+1,然后p>1,每次p-=2,指针右移一位p==1,指针指向的队-=1p==0,从指针开始到 ...
- 如何调试最新的asp.net mvc源码
vs2013调试 一.源码当前为5.2.0.0,按下面改为5.0.0.1 二./web.config 版本为5.0.0.0 改为5.0.0.1 三.vs2013 x86 本机工具命令提示 sn.exe ...