版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、文档编码 : CM5N7B4Q2S5 HN3E10T10S8L5 ZQ1O4V5X1G10使用一个 GUIDguidHID_1 查找并打开一个 USB设备 extern C int PASCAL SearchUSBDevice HANDLE hUsb; int nCount, i, j;/ 标记同一设备个数 HDEVINFO hDevInfoSet; BOOL bResult; PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail =NULL; memsetm_sysversion, 0, 20; GetSysVersionm_sysversion; / 检索相关
2、GUID的 USB设备总设备个数 if .GetUSBList return 0; / 取得一个该GUID相关的设备信息集句柄hDevInfoSet = :SetupDiGetClassDevsLPGUID&guidHID_1,/GUID_CLASS_USB_DEVICE, / class GUI D NULL, / 无关键字 NULL, / 不指定父窗口句柄 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE; / 目前存在的设备/ 失败 . if hDevInfoSet = INVALID_HANDLE_VALUE return NULL; / 申请设备接口数据空间
3、nCount = 0; bResult = TRUE; for i=0; icbSize = sizeofSP_DEVICE_INTERFACE_DETAIL_DATA; bResult = SetupDiGetInterfaceDeviceDetail hDevInfoSet, / 设备信息集句柄&ifdata, / 设备接口信息pDetail, / 设备接口细节 设备路径 predictedLength, / 输出缓冲区大小&requiredLength, / 不需运算输出缓冲区大小 直接用设定值 NULL; / 不需额外的设备描述if bResult / 复制设备路径到输出缓冲区 /:s
4、trcpypszDevicePathnCount, pDetail-DevicePath; if strcmpm_sysversion, winnt=0 char ch18; fori=0;iDevicePath+8+i; ch17=0; if strcmpch,vid_0471&pid_0666=0/ 比较版本号,防止意外出错 memset &READ_OS, 0, sizeof OVERLAPPED ; memset &WRITE_OS, 0, sizeof OVERLAPPED ; READ_OS.hEvent = CreateEvent NULL, / no security TRUE
5、, / explicit reset req FALSE, / initial event reset NULL ; / no name if READ_OS.hEvent = NULL break; WRITE_OS.hEvent = CreateEvent NULL, / no security TRUE, / explicit reset req FALSE, / initial event reset NULL ; / no name if NULL = WRITE_OS.hEvent CloseHandle READ_OS.hEvent ; break; hUsb=CreateFil
6、epDetail-DevicePath,/&guidHID_1,/ GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL/*| FILE_FLAG_OVERLAPPED*/, NULL; if hUsb .= NULL / 比较定位找到的USB在哪个 USB PORT上char id30; memsetid, 0, 30; i=0; do idi=*pDetail-DevicePath+26+i; i+; whileidi-1.=#; id
7、i-1 = 0; for j=0; jDevicePath; m_USBPositionMapnCount = j+1; break; CloseHandlehUsb; nCount+; / break; / 比较驱动版本 / 比较操作系统版本 else memset &READ_OS, 0, sizeof OVERLAPPED ; memset &WRITE_OS, 0, sizeof OVERLAPPED ; READ_OS.hEvent = CreateEvent NULL, / no security TRUE, / explicit reset req FALSE, / initia
8、l event reset NULL ; / no name if READ_OS.hEvent = NULL break; WRITE_OS.hEvent = CreateEvent NULL, / no security TRUE, / explicit reset req FALSE, / initial event reset NULL ; / no name if NULL = WRITE_OS.hEvent CloseHandle READ_OS.hEvent ; break; hUsb=CreateFilepDetail-DevicePath,/&guidHID_1,/ GENE
9、RIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL/*| FILE_FLAG_OVERLAPPED*/, NULL; if hUsb .= NULL ifstrcmppDetail-DevicePath, m_USBListj=0 sprintfm_DeviceDescj+1, %s, pDetail-DevicePath; m_USBPositionMapnCount = j+1; break; CloseHandlehUsb; nCount+
10、; / break; / 释放设备接口数据空间:GlobalFreepDetail; / 关闭设备信息集句柄:SetupDiDestroyDeviceInfoListhDevInfoSet; iDeviceCount = nCount; return nCount; / 写 BOOL Writestrchar *buf,int buflen, int index BOOL fWriteStat; DWORD dwErrorFlags; DWORD dwError; COMSTAT ComStat; char szError 10 ; DWORD ret; int len, i, j, pack
11、et; div_t div_result; BYTE sendpacket65; BYTE xorcode=0 x00; if m_gphdComindex = NULL / no usb devicejk100c return -1; div_result = divbuflen, 58; if div_result.rem = 0 packet = div_result.quot; else packet = div_result.quot+1; for i=0; ipacket; i+ memsetsendpacket, 0, 65; ifi=packet-1 / end packet
12、if div_result.rem = 0 len = 58; else len = div_result.rem; else len = 58; sendpacket0 = 0 x13; sendpacket1 = 3+len; sendpacket2 = 0 x01; sendpacket3 = packet*16+i+1; memcpysendpacket+4, buf+i*58, len; forj=0;jlen+3;j+ xorcode=sendpacketj+1; sendpacketlen+4 = charxorcode; sendpacketlen+5 = 0 x23; Pur
13、geCommm_gphdComindex,PURGE_RXCLEAR|PURGE_TXCLEAR; / Sleep10; fWriteStat = WriteFilem_gphdComindex, sendpacket, len+6,&ret, NULL; if .fWriteStat ifGetLastError = ERROR_IO_PENDING dwError = GetLastError; / an error occurred, try to recover wsprintf szError, nr , dwError ; OutputDebugStringszError; Cle
14、arCommErrorm_gphdComindex, &dwErrorFlags, &ComStat ; if dwErrorFlags 0 wsprintf szError, nr , dwErrorFlags ; OutputDebugStringszError; else / some other error occurred ClearCommErrorm_gphdComindex, &dwErrorFlags, &ComStat ; if dwErrorFlags 0 wsprintf szError, nr , dwErrorFlags ; OutputDebugStringszE
15、rror; return FALSE; if i .= packet-1 / should be receive ack if ReceivePacketAnswerindex .= 0 return FALSE; return TRUE; / 读 int Readstrchar *buf,int nMaxLength, int index BOOL fReadStat ; COMSTAT ComStat; DWORD dwErrorFlags; DWORD dwLength; DWORD dwError; char szError 10 ; if fCOMMOpened=0 return F
16、ALSE; / 串口未打开 / only try to read number of bytes in queue ClearCommErrorm_gphdComindex, &dwErrorFlags, &ComStat ; /dwLength = min DWORD nMaxLength, ComStat.cbInQue ; dwLength=nMaxLength; if dwLength 0 if olap=TRUE fReadStat = ReadFilem_gphdComindex,buf, dwLength, &dwLength,&READ_OS ; if .fReadStat i
17、f GetLastError = ERROR_IO_PENDING OutputDebugStringnrIO Pending; while.GetOverlappedResultm_gphdComindex, &READ_OS, &dwLength, TRUE dwError = GetLastError; ifdwError = ERROR_IO_INCOMPLETE continue; else / an error occurred, try to recover ClearCommErrorm_gphdComindex,&dwErrorFlags, &ComStat ; break;
18、 else / end-if GetLastError = ERROR_IO_PENDING / some other error occurred dwLength = 0 ; ClearCommErrorm_gphdComindex, &dwErrorFlags, &ComStat ; if dwErrorFlags 0 wsprintf szError, nr , dwErrorFlags ; OutputDebugStringszError; / end-if .fReadStat / end-if olap=TRUE else fReadStat = ReadFile m_gphdCo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年汽车维修配件采购与质量控制指南
- 高新技术企业认定与优惠政策指南
- 建筑施工质量管理体系手册
- 企业内部审计人员培训手册(标准版)
- 信息技术服务标准与操作手册(标准版)
- 工厂员工培训制度
- 干部教育培训重要制度
- 2026年酒店前厅经理面试题及管理技巧参考
- 房地产公司现行培训制度
- 幼儿岗前培训制度
- cobas-h-232心肌标志物床边检测仪操作培训
- 第六讲通量观测方法与原理
- 林规发防护林造林工程投资估算指标
- GB/T 23821-2022机械安全防止上下肢触及危险区的安全距离
- GB/T 5563-2013橡胶和塑料软管及软管组合件静液压试验方法
- GB/T 16895.6-2014低压电气装置第5-52部分:电气设备的选择和安装布线系统
- GB/T 11018.1-2008丝包铜绕组线第1部分:丝包单线
- GA/T 765-2020人血红蛋白检测金标试剂条法
- 武汉市空调工程毕业设计说明书正文
- 麻风病防治知识课件整理
- 安全安全应急救援预案(沟槽开挖)
评论
0/150
提交评论