用VC++实现USB接口读写数据的程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

用VC++实现USB接口读写数据的程序2009-08-1511:55用VC++实现USB接口读写数据的程序使用一个GUIDguidHID_1查找并打开一个USB设备externCintPASCALSearchUSBDevice(){HANDLEhUsb;intnCount,i,j;//标记同一设备个数HDEVINFOhDevInfoSet;BOOLbResult;PSP_DEVICE_INTERFACE_DETAIL_DATApDetail=NULL;memset(m_sysversion,0,20);GetSysVersion(m_sysversion);//检索相关GUID的USB设备总设备个数if(!GetUSBList()){return0;}//取得一个该GUID相关的设备信息集句柄hDevInfoSet=::SetupDiGetClassDevs((LPGUID)&guidHID_1,//GUID_CLASS_USB_DEVICE,//classGUIDNULL,//无关键字NULL,//不指定父窗口句柄DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);//目前存在的设备//失败...if(hDevInfoSet==INVALID_HANDLE_VALUE){returnNULL;}//申请设备接口数据空间nCount=0;bResult=TRUE;for(i=0;i34;i++){bDeviceOpen=FALSE;memset(m_DeviceDesc,0,256);}SP_DEVICE_INTERFACE_DATAifdata;//设备序号=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){ULONGpredictedLength=0;ULONGrequiredLength=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){charch[18];for(i=0;i17;i++){ch=*(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,//nosecurityTRUE,//explicitresetreqFALSE,//initialeventresetNULL);//nonameif(READ_OS.hEvent==NULL){break;}WRITE_OS.hEvent=CreateEvent(NULL,//nosecurityTRUE,//explicitresetreqFALSE,//initialeventresetNULL);//nonameif(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在哪个USBPORT上charid[30];memset(id,0,30);i=0;do{id=*(pDetail-DevicePath+26+i);i++;}while(id!='#');id='\0';for(j=0;j34;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,//nosecurityTRUE,//explicitresetreqFALSE,//initialeventresetNULL);//nonameif(READ_OS.hEvent==NULL){break;}WRITE_OS.hEvent=CreateEvent(NULL,//nosecurityTRUE,//explicitresetreqFALSE,//initialeventresetNULL);//nonameif(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;returnnCount;}//写BOOLWritestr(char*buf,intbuflen,intindex){BOOLfWriteStat;DWORDdwErrorFlags;DWORDdwError;COMSTATComStat;charszError[10];DWORDret;intlen,i,j,packet;div_tdiv_result;BYTEsendpacket[65];BYTExorcode=0x00;if(m_gphdCom[index]==NULL)//nousbdevice(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;ipacket;i++){memset(sendpacket,0,65);if(i==packet-1){//endpacketif(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;jlen+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();//anerroroccurred,trytorecoverwsprintf(szError,\n\rCE-%u,dwError);OutputDebugString(szError);ClearCommError(m_gphdCom[index],&dwErrorFlags,&ComStat);if(dwErrorFlags0){wsprintf(szError,\n\rCE-%u,dwErrorFlags);OutputDebugString(szError);}}else{//someothererroroccurredClearCommError(m_gphdCom[index],&dwErrorFlags,&ComStat);if(dwErrorFlags0){wsprintf(szError,\n\rCE-%u,dwErrorFlags);OutputDebugString(szError);}returnFALSE;}}if(i!=packet-1){//shouldberec

1 / 14
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功