logo资料库

怎样编写一个在USB接口读写数据的程序.doc

第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
资料共10页,剩余部分请下载后查看
使用一个 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 (!GetUSBList()) { return 0; } // 取得一个该 GUID 相关的设备信息集句柄 hDevInfoSet = ::SetupDiGetClassDevs((LPGUID)&guidHID_1,//GUID_CLASS_USB_DEVICE, // class GUI D NULL, // 无关键字 NULL, // 不指定父窗口句柄 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备 // 失败... if (hDevInfoSet == INVALID_HANDLE_VALUE) { return NULL; } // 申请设备接口数据空间 nCount = 0; bResult = TRUE; for (i=0; i< 34; i++) { bDeviceOpen[i] = FALSE; memset(m_DeviceDesc[i], 0, 256); }
SP_DEVICE_INTERFACE_DATA ifdata; // 设备序号=0,1,2... 逐一测试设备接口,到失败为止 while (bResult) { ifdata.cbSize = sizeof(ifdata); // 枚举符合该 GUID 的设备接口 bResult = ::SetupDiEnumDeviceInterfaces( hDevInfoSet, // 设备信息集句柄 NULL, // 不需额外的设备描述 (LPGUID)&guidHID_1,//GUID_CLASS_USB_DEVICE, // GUID (ULONG)nCount, // 设备信息集里的设备序号 &ifdata); // 设备接口信息 if (bResult) { ULONG predictedLength = 0; ULONG requiredLength = 0; // 取得该设备接口的细节(设备路径) bResult = SetupDiGetInterfaceDeviceDetail( hDevInfoSet, // 设备信息集句柄 &ifdata, // 设备接口信息 NULL, // 设备接口细节(设备路径) 0, // 输出缓冲区大小 &requiredLength, // 不需计算输出缓冲区大小(直接用设定值) NULL); // 不需额外的设备描述 // 取得该设备接口的细节(设备路径) predictedLength=requiredLength; // if(pDetail) // { // pDetail =NULL; // } pDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, predictedLength); pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); bResult = SetupDiGetInterfaceDeviceDetail( hDevInfoSet, // 设备信息集句柄 &ifdata, // 设备接口信息 pDetail, // 设备接口细节(设备路径) predictedLength, // 输出缓冲区大小 &requiredLength, // 不需计算输出缓冲区大小(直接用设定值) NULL); // 不需额外的设备描述 if (bResult)
{ // 复制设备路径到输出缓冲区 //::strcpy(pszDevicePath[nCount], pDetail->DevicePath); if (strcmp(m_sysversion, "winnt")==0) { char ch[18]; for(i=0;i<17;i++){ ch[i]=*(pDetail->DevicePath+8+i); } ch[17]='\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, // no 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.hEvent ); 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 id[30]; memset(id, 0, 30); i=0; do { id[i]=*(pDetail->DevicePath+26+i); i++; } while(id[i-1]!='#'); id[i-1] = '\0'; for (j=0; j<34; j++) { if(strcmp(id, m_USBList[j])==0) { sprintf(m_DeviceDesc[j+1], "%s", pDetail->DevicePath); m_USBPositionMap[nCount] = 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, // no 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.hEvent ); 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_USBList[j])==0) { sprintf(m_DeviceDesc[j+1], "%s", pDetail->DevicePath); m_USBPositionMap[nCount] = 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 dwErrorFlags; DWORD dwError; COMSTAT ComStat; char szError[ 10 ] ; DWORD ret; int len, i, j, packet; div_t div_result; BYTE sendpacket[65]; BYTE xorcode=0x00; if (m_gphdCom[index] == NULL) // no usb device(jk100c) { return -1; } div_result = div(buflen, 58); if (div_result.rem == 0) { packet = div_result.quot; } else { packet = div_result.quot+1; } for (i=0; i
{ len = 58; } sendpacket[0] = 0x13; sendpacket[1] = 3+len; sendpacket[2] = 0x01; sendpacket[3] = packet*16+i+1; memcpy(sendpacket+4, buf+(i*58), len); for(j=0;j", dwError ) ; OutputDebugString(szError); ClearCommError(m_gphdCom[index], &dwErrorFlags, &ComStat ) ; if (dwErrorFlags >0) { wsprintf( szError, "\n\r ", dwErrorFlags ) ; OutputDebugString(szError); } } else { // some other error occurred ClearCommError(m_gphdCom[index], &dwErrorFlags, &ComStat ) ; if (dwErrorFlags > 0) { wsprintf( szError, "\n\r ", 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 nMaxLength, 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_gphdCom[index], &dwErrorFlags, &ComStat) ; //dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ; dwLength=nMaxLength; if (dwLength > 0) { if (olap==TRUE) { fReadStat = ReadFile(m_gphdCom[index],buf, dwLength, &dwLength,&READ_OS) ; if (!fReadStat) { if (GetLastError() == ERROR_IO_PENDING) { OutputDebugString("\n\rIO Pending"); while(!GetOverlappedResult(m_gphdCom[index], &READ_OS,
分享到:
收藏