用VC++实现USB接口读写数据的程序
生活随笔
收集整理的這篇文章主要介紹了
用VC++实现USB接口读写数据的程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用一個GUIDguidHID_1查找并打開一個USB設(shè)備
extern "C" int PASCAL SearchUSBDevice()
{HANDLE hUsb;int nCount, i, j;//標記同一設(shè)備個數(shù)HDEVINFO hDevInfoSet;BOOL bResult;PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail =NULL;memset(m_sysversion, 0, 20);GetSysVersion(m_sysversion);// 檢索相關(guān)GUID的USB設(shè)備總設(shè)備個數(shù)if (!GetUSBList()){return 0;}// 取得一個該GUID相關(guān)的設(shè)備信息集句柄hDevInfoSet = ::SetupDiGetClassDevs((LPGUID)&guidHID_1,//GUID_CLASS_USB_DEVICE, // class GUID NULL, // 無關(guān)鍵字 NULL, // 不指定父窗口句柄 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的設(shè)備// 失敗...if (hDevInfoSet == INVALID_HANDLE_VALUE){return NULL;}// 申請設(shè)備接口數(shù)據(jù)空間nCount = 0;bResult = TRUE;for (i=0; i< 34; i++){bDeviceOpen[i] = FALSE;memset(m_DeviceDesc[i], 0, 256);}SP_DEVICE_INTERFACE_DATA ifdata;// 設(shè)備序號=0,1,2... 逐一測試設(shè)備接口,到失敗為止while (bResult){ifdata.cbSize = sizeof(ifdata);// 枚舉符合該GUID的設(shè)備接口bResult = ::SetupDiEnumDeviceInterfaces(hDevInfoSet, // 設(shè)備信息集句柄NULL, // 不需額外的設(shè)備描述(LPGUID)&guidHID_1,//GUID_CLASS_USB_DEVICE, // GUID(ULONG)nCount, // 設(shè)備信息集里的設(shè)備序號&ifdata); // 設(shè)備接口信息if (bResult){ULONG predictedLength = 0;ULONG requiredLength = 0;// 取得該設(shè)備接口的細節(jié)(設(shè)備路徑)bResult = SetupDiGetInterfaceDeviceDetail(hDevInfoSet, // 設(shè)備信息集句柄&ifdata, // 設(shè)備接口信息NULL, // 設(shè)備接口細節(jié)(設(shè)備路徑)0, // 輸出緩沖區(qū)大小&requiredLength, // 不需計算輸出緩沖區(qū)大小(直接用設(shè)定值)NULL); // 不需額外的設(shè)備描述// 取得該設(shè)備接口的細節(jié)(設(shè)備路徑)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, // 設(shè)備信息集句柄&ifdata, // 設(shè)備接口信息pDetail, // 設(shè)備接口細節(jié)(設(shè)備路徑)predictedLength, // 輸出緩沖區(qū)大小&requiredLength, // 不需計算輸出緩沖區(qū)大小(直接用設(shè)定值)NULL); // 不需額外的設(shè)備描述if (bResult){// 復(fù)制設(shè)備路徑到輸出緩沖區(qū)//::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 securityTRUE, // explicit reset reqFALSE, // initial event resetNULL ) ; // no nameif (READ_OS.hEvent == NULL) {break;}WRITE_OS.hEvent = CreateEvent( NULL, // no securityTRUE, // explicit reset reqFALSE, // initial event resetNULL ) ; // no nameif (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;}}// 比較驅(qū)動版本}// 比較操作系統(tǒng)版本else{memset( &READ_OS, 0, sizeof( OVERLAPPED ) ) ; memset( &WRITE_OS, 0, sizeof( OVERLAPPED ) ) ; READ_OS.hEvent = CreateEvent( NULL, // no securityTRUE, // explicit reset reqFALSE, // initial event resetNULL ) ; // no nameif (READ_OS.hEvent == NULL) {break;}WRITE_OS.hEvent = CreateEvent( NULL, // no securityTRUE, // explicit reset reqFALSE, // initial event resetNULL ) ; // no nameif (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;
}
}
}
}
}
// 釋放設(shè)備接口數(shù)據(jù)空間
::GlobalFree(pDetail);// 關(guān)閉設(shè)備信息集句柄
::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<packet; 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;
}
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<len+3;j++)
{
xorcode^=sendpacket[j+1];
}
sendpacket[len+4] = (char)xorcode;
sendpacket[len+5] = 0x23;
PurgeComm(m_gphdCom[index],PURGE_RXCLEAR|PURGE_TXCLEAR);
// Sleep(10);
fWriteStat = WriteFile(m_gphdCom[index], sendpacket, len+6,&ret, NULL);
if (!fWriteStat)
{
if(GetLastError() == ERROR_IO_PENDING)
{
dwError = GetLastError();
// an error occurred, try to recover
wsprintf( szError, "\n\r <CE-%u>", dwError ) ;
OutputDebugString(szError);
ClearCommError(m_gphdCom[index], &dwErrorFlags, &ComStat ) ;
if (dwErrorFlags >0)
{
wsprintf( szError, "\n\r <CE-%u>", dwErrorFlags ) ;
OutputDebugString(szError);
}
}
else
{
// some other error occurred
ClearCommError(m_gphdCom[index], &dwErrorFlags, &ComStat ) ;
if (dwErrorFlags > 0)
{
wsprintf( szError, "\n\r <CE-%u>", 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, &dwLength, TRUE )){dwError = GetLastError();if(dwError == ERROR_IO_INCOMPLETE) continue;else{// an error occurred, try to recoverClearCommError(m_gphdCom[index],&dwErrorFlags, &ComStat ) ;break;}}}else // end-----if (GetLastError() == ERROR_IO_PENDING){// some other error occurreddwLength = 0 ;ClearCommError(m_gphdCom[index], &dwErrorFlags, &ComStat ) ;if (dwErrorFlags >0){wsprintf( szError, "\n\r <CE-%u>", dwErrorFlags ) ;OutputDebugString(szError);}}} // end-----if (!fReadStat) } // end-----if (olap==TRUE) else{fReadStat = ReadFile( m_gphdCom[index],buf, dwLength, &dwLength, NULL ) ;if (!fReadStat){dwError = GetLastError();ClearCommError(m_gphdCom[index],&dwErrorFlags, &ComStat ) ;if (dwErrorFlags >0){wsprintf( szError, "\n\r <CE-%u>", dwErrorFlags ) ;OutputDebugString(szError);}}PurgeComm(m_gphdCom[index],PURGE_RXCLEAR|PURGE_TXCLEAR);}}return dwLength;
}
總結(jié)
以上是生活随笔為你收集整理的用VC++实现USB接口读写数据的程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: usb数据的接收和打印,除了问题,接收数
- 下一篇: string 与char *的区别