C++MFC界面读写USBHID设备数据程序文件.docx
《C++MFC界面读写USBHID设备数据程序文件.docx》由会员分享,可在线阅读,更多相关《C++MFC界面读写USBHID设备数据程序文件.docx(14页珍藏版)》请在冰豆网上搜索。
![C++MFC界面读写USBHID设备数据程序文件.docx](https://file1.bdocx.com/fileroot1/2023-6/14/71229223-d8f9-482e-a60f-f406c8e67a93/71229223-d8f9-482e-a60f-f406c8e67a931.gif)
C++MFC界面读写USBHID设备数据程序文件
第一步:
列举所有的HID设备:
m_ctllHIDdevices.ResetContent();//这是MFC里面一个list控件,用来显示所有的HID设备的,如果你没有界面,可以不需要此行
UpdateData(FALSE);//更新界面
CStringtemp;
intCount=0;//Totalnumberofdevicesfound
DWORDstrSize=0,requiredSize=0;
BOOLresult1,result2;
ULONGDeviceInterfaceDetailDataSize;
//定义一些变量,以后会用到
SP_DEVINFO_DATADeviceInfoData;
SP_DEVICE_INTERFACE_DATADeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATADeviceInterfaceDetailData;
//PSP_DEVICE_INTERFACE_DETAIL_DATAtest;
//第一步:
获取deviceID
GUIDdeviceId;
HidD_GetHidGuid(&deviceId);
//第二步:
获取设备信息
HDEVINFOhandle;
handle=SetupDiGetClassDevs(&deviceId,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);//GetonlyHIDdevices
//第三步:
对所有的设备进行枚举
//SetupDiEnumDeviceInterfaces();
result1=false;//定义一些变量
result2=false;
CStringtemp11="";
do
{
DeviceInterfaceData.cbSize=sizeof(SP_DEVICE_INTERFACE_DATA);
result1=SetupDiEnumDeviceInterfaces(
handle,
NULL,//INPSP_DEVINFO_DATADeviceInfoData,OPTIONAL
&deviceId,
Count,
&DeviceInterfaceData
);
//获得设备详细数据(初步)
SetupDiGetDeviceInterfaceDetail(handle,
&DeviceInterfaceData,
NULL,
0,
&strSize,
NULL);
requiredSize=strSize;
DeviceInterfaceDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);
DeviceInterfaceDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
DeviceInfoData.cbSize=s第一步:
列举所有的HID设备:
m_ctllHIDdevices.ResetContent();//这是MFC里面一个list控件,用来显示所有的HID设备的,如果你没有界面,可以不需要此行
UpdateData(FALSE);//更新界面
CStringtemp;
intCount=0;//Totalnumberofdevicesfound
DWORDstrSize=0,requiredSize=0;
BOOLresult1,result2;
ULONGDeviceInterfaceDetailDataSize;
//定义一些变量,以后会用到
SP_DEVINFO_DATADeviceInfoData;
SP_DEVICE_INTERFACE_DATADeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATADeviceInterfaceDetailData;
//PSP_DEVICE_INTERFACE_DETAIL_DATAtest;
//第一步:
获取deviceID
GUIDdeviceId;
HidD_GetHidGuid(&deviceId);
//第二步:
获取设备信息
HDEVINFOhandle;
handle=SetupDiGetClassDevs(&deviceId,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);//GetonlyHIDdevices
//第三步:
对所有的设备进行枚举
//SetupDiEnumDeviceInterfaces();
result1=false;//定义一些变量
result2=false;
CStringtemp11="";
do
{
DeviceInterfaceData.cbSize=sizeof(SP_DEVICE_INTERFACE_DATA);
result1=SetupDiEnumDeviceInterfaces(
handle,
NULL,//INPSP_DEVINFO_DATADeviceInfoData,OPTIONAL
&deviceId,
Count,
&DeviceInterfaceData
);
//获得设备详细数据(初步)
SetupDiGetDeviceInterfaceDetail(handle,
&DeviceInterfaceData,
NULL,
0,
&strSize,
NULL);
requiredSize=strSize;
DeviceInterfaceDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);
DeviceInterfaceDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
DeviceInfoData.cbSize=sizeof(SP_DEVINFO_DATA);
//再次获得详细数据
result2=SetupDiGetDeviceInterfaceDetail(handle,
&DeviceInterfaceData,
DeviceInterfaceDetailData,
strSize,
&requiredSize,
&DeviceInfoData);
//获得设备路径(最重要的部分)
temp=DeviceInterfaceDetailData->DevicePath;
UpdateData(FALSE);
m_ctllHIDdevices.AddString(temp);
Count++;
}while(result1);
UpdateData(false);izeof(SP_DEVINFO_DATA);
//再次获得详细数据
result2=SetupDiGetDeviceInterfaceDetail(handle,
&DeviceInterfaceData,
DeviceInterfaceDetailData,
strSize,
&requiredSize,
&DeviceInfoData);
//获得设备路径(最重要的部分)
temp=DeviceInterfaceDetailData->DevicePath;
UpdateData(FALSE);
m_ctllHIDdevices.AddString(temp);
Count++;
}while(result1);
UpdateData(false);
第二步:
循环读取HID设备数据(根据用户提供的HID的vendorID和productID),并且把字节解码成二进制,在MFC界面上用LED展示:
为了不影响主线程的运行,我把读取数据的操作,放在一个子线程里!
每隔50ms去读取一次数据!
首先创建一个线程:
HANDLEhThread1;
bStopHID=false;//这个变量,以后用来停止线程
UpdateData(true);//更新界面,获取变量
UpdateData(false);
hThread1=CreateThread(NULL,0,Thread_Enable_Read,(LPVOID)this,NULL,NULL);
在线程的程序里:
CusbhidDlg*p=(CusbhidDlg*)pvParam;//获取主窗口的指针,用来调用主窗口的变量和函数
p->UpdateData(true);
p->bStopHID=false;
CStringtemp;
CStringDevicePath;
temp="";
intCount=0;//Totalnumberofdevicesfound
DWORDstrSize=0,requiredSize=0;
BOOLresult1,result2;
ULONGDeviceInterfaceDetailDataSize;
SP_DEVINFO_DATADeviceInfoData;
SP_DEVICE_INTERFACE_DATADeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATADeviceInterfaceDetailData;
//PSP_DEVICE_INTERFACE_DETAIL_DATAtest;
//1
GUIDdeviceId;
HidD_GetHidGuid(&deviceId);
intvenderID=p->v_eVendorID;//从窗口里获取用户输入的VendorID
intproductID=p->v_eProductID;//从窗口里获取用户输入的ProductID
unsignedcharinbuffer[2];//用来存放读取的数据,请在这里定义你自己需要的长度,我每次读一个字节进来
unsignedlongnumBytesReturned;
HIDD_ATTRIBUTESdevAttr;
PHIDP_PREPARSED_DATAPreparsedData;
HIDP_CAPSCapabilities;
intreadValue;
boolLED;
intflag=0;
//2
HDEVINFOhandle;
handle=SetupDiGetClassDevs(&deviceId,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);//GetonlyHIDdevices
inti=0;
intj=p->m_ctllHIDdevices.GetCount();
for(i=0;im_ctllHIDdevices.GetCount();i++)
{
p->m_ctllHIDdevices.GetText(i,temp);
DevicePath=temp;
//CreateFile是非常重要的一步,用来建立于HID通信的句柄
HANDLEhCom=CreateFile(
DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,0,
NULL);
if(hCom==INVALID_HANDLE_VALUE)
{
//AfxMessageBox("InvalideDevicePath...");
continue;
}
devAttr.Size=sizeof(HIDD_ATTRIBUTES);
if(!
HidD_GetAttributes(hCom,&devAttr))
{
CloseHandle(hCom);
AfxMessageBox("CannotgettheparametersoftheHID...");
return0;
}
//temp.Format("VendorID:
%d,ProductID:
%d",devAttr.VendorID,devAttr.ProductID);//ComparewiththeVendorIDandProductIDfromNakamura-san
//AfxMessageBox(temp);
if(!
HidD_GetPreparsedData(hCom,&PreparsedData))
{
CloseHandle(hCom);
AfxMessageBox("CannotgetthePreparsedData...");
return0;
}
if(!
HidP_GetCaps(PreparsedData,&Capabilities))
{
CloseHandle(hCom);
AfxMessageBox("CannotgettheCapData...");
return0;
}
if(devAttr.VendorID==venderID&&devAttr.ProductID==productID)
{
while
(1)
{
result1=ReadFile(hCom,&inbuffer[0],Capabilities.InputReportByteLength,&numBytesReturned,0);
temp=inbuffer;
//p->m_eDataRead=CString(inbuffer);
//p->UpdateData(false);
if(!
result1)
{
AfxMessageBox("CannotReadData...");
return0;
}
readValue=inbuffer[1];
p->m_eDataRead.Format("%d",readValue);
//下面是我把数据从10进制转换成二进制,并且点亮LED(一个字节有8个bits,可以点亮8个LED
for(intk=0;k<8;k++)
{
flag=readValue%2;
readValue=readValue/2;
if(k==0)
{
if(flag==0)
p->m_sDynLED0.SwitchOff();
else
p->m_sDynLED0.SwitchOn();
}
elseif(k==1)
{
if(flag==0)
p->m_sDynLED1.SwitchOff();
else
p->m_sDynLED1.SwitchOn();
}
elseif(k==2)
{
if(flag==0)
p->m_sDynLED2.SwitchOff();
else
p->m_sDynLED2.SwitchOn();
}
elseif(k==3)
{
if(flag==0)
p->m_sDynLED3.SwitchOff();
else
p->m_sDynLED3.SwitchOn();
}
elseif(k==4)
{
if(flag==0)
p->m_sDynLED4.SwitchOff();
else
p->m_sDynLED4.SwitchOn();
}
elseif(k==5)
{
if(flag==0)
p->m_sDynLED5.SwitchOff();
else
p->m_sDynLED5.SwitchOn();
}
elseif(k==6)
{
if(flag==0)
p->m_sDynLED6.SwitchOff();
else
p->m_sDynLED6.SwitchOn();
}
elseif(k==7)
{
if(flag==0)
p->m_sDynLED7.SwitchOff();
else
p->m_sDynLED7.SwitchOn();
}
}
p->UpdateData(false);
:
:
Sleep(50);
//判断用户是否点击停止按钮,若是,则退出
if(p->bStopHID)
{
AfxMessageBox("stopped...");
return0;
}
}
}
}
if(i==j)
{
AfxMessageBox("ThereisnosuchHIDdevice...");
}
return0;
第三步:
向HID设备写数据(根据用户提供的HID的vendorID和productID),用户输入的是二进制数据:
与读的程序一样,唯一区别就是红色那部分!
UpdateData(true);
bStopHID=false;
CStringtemp;
CStringDevicePath;
temp="";
intCount=0;//Totalnumberofdevicesfound
DWORDstrSize=0,requiredSize=0;
BOOLresult1,result2;
ULONGDeviceInterfaceDetailDataSize;
SP_DEVINFO_DATADeviceInfoData;
SP_DEVICE_INTERFACE_DATADeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATADeviceInterfaceDetailData;
//PSP_DEVICE_INTERFACE_DETAIL_DATAtest;
//1
GUIDdeviceId;
HidD_GetHidGuid(&deviceId);
intvenderID=v_eVendorID;
intproductID=v_eProductID;
unsignedcharinbuffer[2];
unsignedlongnumBytesReturned;
HIDD_ATTRIBUTESdevAttr;
PHIDP_PREPARSED_DATAPreparsedData;
HIDP_CAPSCapabilities;
intreadValue;
boolLED;
intflag=0;
inbuffer[0]=0;
//把界面里的二进制转换成10进制
inbuffer[1]=m_eByte0*1+m_eByte1*2+m_eByte2*4+m_eByte3*8+m_eByte4*16+m_eByte5*32+m_eByte6*64+m_eByte7*128;
v_eDataToWrite=inbuffer[1];
UpdateData(false);
//2
HDEVINFOhandle;
handle=SetupDiGetClassDevs(&deviceId,NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);//GetonlyHIDdevices
inti=0;
intj=m_ctllHIDdevices.GetCount();
for(i=0;i{
m_ctllHIDdevices.GetText(i,temp);
DevicePath=temp;
HANDLEhCom=CreateFile(
DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,0,
NULL);
if(hCom==INVALID_HANDLE_VALUE)
{
//AfxMessageBox("InvalideDevicePath...");
continue;
}
devAttr.Size=sizeof(HIDD_ATTRIBUTES);
if(!
HidD_GetAttributes(hCom,&devAttr))
{
CloseHandle(hCom);
AfxMessageBox("CannotgettheparametersoftheHID...");
return;
}
//temp.Format("VendorID:
%d,ProductID:
%d",devAttr.VendorID,devAttr.ProductID);//ComparewiththeVendorIDandProductIDfromNakamura-san
//AfxMessageBox(temp);
if(!
HidD_GetPreparsedData(hCom,&PreparsedData))
{
CloseHandle(hCom);
AfxMessageBox("CannotgetthePreparsedData...");
return;
}
if(!
HidP_GetCaps(PreparsedData,&Capabilities))
{
CloseHandle(hCom);
AfxMessageBox("CannotgettheCapData...");
return;
}
//WriteFile
if(devAttr.VendorID==venderID&&devAttr.ProductID==productID)
{
result1=WriteFile(hCom,inbuffer,2,&numBytesReturned,NULL);
//temp=inbuffer;
//p->m_eDataRead=CString(inbuffer);
//p->UpdateData(false);
if(!
result1)
{
AfxMessageBox("CannotWriteData...");
return;
}
AfxMessageBox("Suncess...");
break;
}
}
if(i==j)
{
AfxMessageBox("Thereisnosuch