编写在USB接口读写数据的C++程序Word格式.docx
《编写在USB接口读写数据的C++程序Word格式.docx》由会员分享,可在线阅读,更多相关《编写在USB接口读写数据的C++程序Word格式.docx(9页珍藏版)》请在冰豆网上搜索。
NULL,//不指定父窗口句柄
DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
//目前存在的设备
//失败...
if(hDevInfoSet==INVALID_HANDLE_VALUE)
returnNULL;
//申请设备接口数据空间
nCount=0;
bResult=TRUE;
for(i=0;
i<
34;
i++)
bDeviceOpen[i]=FALSE;
memset(m_DeviceDesc[i],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(
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);
pDetail,//设备接口细节(设备路径)
predictedLength,//输出缓冲区大小
//复制设备路径到输出缓冲区
//:
strcpy(pszDevicePath[nCount],pDetail->
DevicePath);
if(strcmp(m_sysversion,"
winnt"
)==0)
charch[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));
WRITE_OS,0,sizeof(OVERLAPPED));
READ_OS.hEvent=CreateEvent(NULL,//nosecurity
TRUE,//explicitresetreq
FALSE,//initialeventreset
NULL);
//noname
if(READ_OS.hEvent==NULL)
break;
WRITE_OS.hEvent=CreateEvent(NULL,//nosecurity
if(NULL==WRITE_OS.hEvent)
CloseHandle(READ_OS.hEvent);
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*/,
if(hUsb!
=NULL)
//比较定位找到的USB在哪个USBPORT上
charid[30];
memset(id,0,30);
i=0;
do
id[i]=*(pDetail->
DevicePath+26+i);
i++;
while(id[i-1]!
='
#'
);
id[i-1]='
for(j=0;
j<
34;
j++)
if(strcmp(id,m_USBList[j])==0)
sprintf(m_DeviceDesc[j+1],"
%s"
pDetail->
m_USBPositionMap[nCount]=j+1;
CloseHandle(hUsb);
nCount++;
//break;
}//比较驱动版本
}//比较操作系统版本
else
if(strcmp(pDetail->
DevicePath,m_USBList[j])==0)
//释放设备接口数据空间
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;
packet=div_result.quot+1;
packet;
memset(sendpacket,0,65);
if(i==packet-1)
//endpacket
len=58;
len=div_result.rem;
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);
fWriteStat)
if(GetLastError()==ERROR_IO_PENDING)
dwError=GetLastError();
//anerroroccurred,trytorecover
wsprintf(szError,"
\n\r<
CE-%u>
"
dwError);
OutputDebugString(szError);
ClearCommError(m_gphdCom[index],&
dwErrorFlags,&
ComStat);
if(dwErrorFlags>
0)
dwErrorFlags);
{
//someothererroroccurred
0)
returnFALSE;
if(i!
=packet-1)
//shouldbereceiveack
if(ReceivePacketAnswer(index)!
=0)
returnTRUE;
//读
intReadstr(char*buf,intnMaxLength,intindex)
BOOLfReadStat;
DWORDdwLength;
charszError[10];
if(fCOMMOpened==0)
//串口未打开
//onlytrytoreadnumberofbytesinqueue
ComStat);
//dwLength=min((DWORD)nMaxLength,ComStat.cbInQue);
dwLength=nMaxLength;
if(dwLength>
if(olap==TRUE)
fReadStat=ReadFile(m_gphdCom[index],buf,dwLength,&
dwLength,&
READ_OS);
fReadStat)
if(GetLastError()==ERROR_IO_PENDING)
OutputDebugString("
\n\rIOPending"
while(!
GetOverlappedResult(m_gphdCom[index],&
READ_OS,
dwLength,TRUE))
if(dwError==ERROR_IO_INCOMPLETE)continue;
ClearCommError(m_gphdCom[index],&
else//end-----if(GetLastError()==ERROR_IO_PENDING)
dwLength=0;
}//end-----if(!
fReadStat)
}//end-----if(olap==TRUE)
fReadStat=ReadFile(m_gphdCom[index],buf,dwLength,&
dwLength,NULL);
returndwLength;