编写在USB接口读写数据的C++程序.docx

上传人:牧羊曲112 文档编号:3125490 上传时间:2023-03-11 格式:DOCX 页数:10 大小:39.03KB
返回 下载 相关 举报
编写在USB接口读写数据的C++程序.docx_第1页
第1页 / 共10页
编写在USB接口读写数据的C++程序.docx_第2页
第2页 / 共10页
编写在USB接口读写数据的C++程序.docx_第3页
第3页 / 共10页
编写在USB接口读写数据的C++程序.docx_第4页
第4页 / 共10页
编写在USB接口读写数据的C++程序.docx_第5页
第5页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《编写在USB接口读写数据的C++程序.docx》由会员分享,可在线阅读,更多相关《编写在USB接口读写数据的C++程序.docx(10页珍藏版)》请在三一办公上搜索。

1、编写在USB接口读写数据的C+程序使用一个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; memset(m_sysversion, 0, 20); GetSysVersion(m_sysversion); / 检索相关GUID的USB设备总设备个数 if (!GetUSBLi

2、st) return 0; / 取得一个该GUID相关的设备信息集句柄 hDevInfoSet = :SetupDiGetClassDevs(LPGUID)&guidHID_1,/GUID_CLASS_USB_DEVICE, / class GUID NULL, / 无关键字 NULL, / 不指定父窗口句柄 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); / 目前存在的设备 / 失败. if (hDevInfoSet = INVALID_HANDLE_VALUE) return NULL; / 申请设备接口数据空间 nCount = 0; bResult =

3、TRUE; for (i=0; icbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); bResult = SetupDiGetInterfaceDeviceDetail( hDevInfoSet, / 设备信息集句柄 &ifdata, / 设备接口信息 pDetail, / 设备接口细节(设备路径) predictedLength, / 输出缓冲区大小 &requiredLength, / 不需计算输出缓冲区大小(直接用设定值) NULL); / 不需额外的设备描述 if (bResult) / 复制设备路径到输出缓冲区 /:strcpy(psz

4、DevicePathnCount, pDetail-DevicePath); if (strcmp(m_sysversion, winnt)=0) char ch18; for(i=0;iDevicePath+8+i); ch17=0; if (strcmp(ch,vid_0471&pid_0666)=0)/比较版本号,防止意外出错 memset( &READ_OS, 0, sizeof( OVERLAPPED ) ) ; memset( &WRITE_OS, 0, sizeof( OVERLAPPED ) ) ; READ_OS.hEvent = CreateEvent( NULL, / n

5、o security TRUE, / 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.hEven

6、t ); break; hUsb=CreateFile(pDetail-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; memset(id, 0, 30); i=0; do idi=*(pDetail-Devic

7、ePath+26+i); i+; while(idi-1!=#); idi-1 = 0; for (j=0; jDevicePath); m_USBPositionMapnCount = j+1; break; CloseHandle(hUsb); nCount+; / break; / 比较驱动版本 / 比较操作系统版本 else memset( &READ_OS, 0, sizeof( OVERLAPPED ) ) ; memset( &WRITE_OS, 0, sizeof( OVERLAPPED ) ) ; READ_OS.hEvent = CreateEvent( NULL, / n

8、o security TRUE, / 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.hEven

9、t ); break; hUsb=CreateFile(pDetail-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) if(strcmp(pDetail-DevicePath, m_USBListj)=0) sprintf(m_DeviceDescj+1, %s, pDetail-

10、DevicePath); m_USBPositionMapnCount = j+1; break; CloseHandle(hUsb); nCount+; / break; / 释放设备接口数据空间 :GlobalFree(pDetail); / 关闭设备信息集句柄 :SetupDiDestroyDeviceInfoList(hDevInfoSet); iDeviceCount = nCount; return nCount; / 写 BOOL Writestr(char *buf,int buflen, int index) BOOL fWriteStat; DWORD dwErrorFla

11、gs; DWORD dwError; COMSTAT ComStat; char szError 10 ; DWORD ret; int len, i, j, packet; div_t div_result; BYTE sendpacket65; BYTE xorcode=0x00; if (m_gphdComindex = NULL) / no usb device(jk100c) return -1; div_result = div(buflen, 58); if (div_result.rem = 0) packet = div_result.quot; else packet =

12、div_result.quot+1; for (i=0; ipacket; i+) memset(sendpacket, 0, 65); if(i=packet-1) / end packet if (div_result.rem = 0) len = 58; else len = div_result.rem; else len = 58; sendpacket0 = 0x13; sendpacket1 = 3+len; sendpacket2 = 0x01; sendpacket3 = packet*16+i+1; memcpy(sendpacket+4, buf+(i*58), len)

13、; for(j=0;jlen+3;j+) xorcode=sendpacketj+1; sendpacketlen+4 = (char)xorcode; sendpacketlen+5 = 0x23; PurgeComm(m_gphdComindex,PURGE_RXCLEAR|PURGE_TXCLEAR); / Sleep(10); fWriteStat = WriteFile(m_gphdComindex, sendpacket, len+6,&ret, NULL); if (!fWriteStat) if(GetLastError = ERROR_IO_PENDING) dwError

14、= GetLastError; / an error occurred, try to recover wsprintf( szError, nr , dwError ) ; OutputDebugString(szError); ClearCommError(m_gphdComindex, &dwErrorFlags, &ComStat ) ; if (dwErrorFlags 0) wsprintf( szError, nr , dwErrorFlags ) ; OutputDebugString(szError); else / some other error occurred Cle

15、arCommError(m_gphdComindex, &dwErrorFlags, &ComStat ) ; if (dwErrorFlags 0) wsprintf( szError, nr , dwErrorFlags ) ; OutputDebugString(szError); return FALSE; if (i != packet-1) / should be receive ack if (ReceivePacketAnswer(index) != 0) return FALSE; return TRUE; / 读 int Readstr(char *buf,int nMax

16、Length, int index) BOOL fReadStat ; COMSTAT ComStat; DWORD dwErrorFlags; DWORD dwLength; DWORD dwError; char szError 10 ; if (fCOMMOpened=0) return FALSE; /串口未打开 / only try to read number of bytes in queue ClearCommError(m_gphdComindex, &dwErrorFlags, &ComStat) ; /dwLength = min( (DWORD) nMaxLength,

17、 ComStat.cbInQue ) ; dwLength=nMaxLength; if (dwLength 0) if (olap=TRUE) fReadStat = ReadFile(m_gphdComindex,buf, dwLength, &dwLength,&READ_OS) ; if (!fReadStat) if (GetLastError = ERROR_IO_PENDING) OutputDebugString(nrIO Pending); while(!GetOverlappedResult(m_gphdComindex, &READ_OS, &dwLength, TRUE

18、 ) dwError = GetLastError; if(dwError = ERROR_IO_INCOMPLETE) continue; else / an error occurred, try to recover ClearCommError(m_gphdComindex,&dwErrorFlags, &ComStat ) ; break; else / end-if (GetLastError = ERROR_IO_PENDING) / some other error occurred dwLength = 0 ; ClearCommError(m_gphdComindex, &

19、dwErrorFlags, &ComStat ) ; if (dwErrorFlags 0) wsprintf( szError, nr , dwErrorFlags ) ; OutputDebugString(szError); / end-if (!fReadStat) / end-if (olap=TRUE) else fReadStat = ReadFile( m_gphdComindex,buf, dwLength, &dwLength, NULL ) ; if (!fReadStat) dwError = GetLastError; ClearCommError(m_gphdComindex,&dwErrorFlags, &ComStat ) ; if (dwErrorFlags 0) wsprintf( szError, nr , dwErrorFlags ) ; OutputDebugString(szError); PurgeComm(m_gphdComindex,PURGE_RXCLEAR|PURGE_TXCLEAR); return dwLength;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号