WinUsb读取设备描述符及端点读写示例
2022-08-17
728
0
void Cusbdemo2Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
DEVICE_DATA deviceData;
HRESULT hr;
BOOL noDevice;
USB_DEVICE_DESCRIPTOR deviceDesc;
BOOL bResult;
ULONG lengthReceived;
hr = OpenDevice(&deviceData, &noDevice);
if (FAILED(hr)) {
if (noDevice) {
//TRACE(_T("Device not connected or driver not installed\n"));
MessageBox(_T("请检查USB是否连接好!"), _T("USB通信"), MB_OKCANCEL | MB_ICONQUESTION);
} else {
// TRACE(_T("Failed looking for device, HRESULT 0x%x\n"), hr);
MessageBox(_T("连接失败!"), _T("USB通信"), MB_OKCANCEL | MB_ICONQUESTION);
}
}
else{
//MessageBox(_T("连接成功!"), _T("USB通信"), MB_OKCANCEL | MB_ICONQUESTION);
}
bResult = WinUsb_GetDescriptor(deviceData.WinusbHandle,
USB_DEVICE_DESCRIPTOR_TYPE,
0,
0,
(PBYTE) &deviceDesc,
sizeof(deviceDesc),
&lengthReceived);
if (FALSE == bResult || lengthReceived != sizeof(deviceDesc)) {
MessageBox(_T("获取设备信息失败!"), _T("USB通信"), MB_OKCANCEL | MB_ICONQUESTION);
}
//
// Print a few parts of the device descriptor
//
int vID,pID;
CString str;
vID=deviceDesc.idVendor;
pID=deviceDesc.idVendor;
str.Format(L"vID=%04X",vID);
SetDlgItemText(IDC_STATIC1,str);
str.Format(L"pID=%04X",pID);
SetDlgItemText(IDC_STATIC2,str);
UCHAR buff[512]={0};
ULONG rtn = 0;
memset(buff,0x11,sizeof(buff));
if(!WinUsb_WritePipe(deviceData.WinusbHandle,0x01,buff,sizeof(buff),&rtn,NULL))
{
DWORD e = GetLastError();
e = e;
}
memset(buff,0x00,sizeof(buff));
if(!WinUsb_ReadPipe(deviceData.WinusbHandle,0x81,buff,sizeof(buff),&rtn,NULL))
{
DWORD e = GetLastError();
e = e;
}
printf("dd");
CloseDevice(&deviceData);
}
HID人机交互QQ群:564808376
UAC音频QQ群:218581009
UVC相机QQ群:331552032
BOT&UASP大容量存储QQ群:258159197
STC-USB单片机QQ群:315457461
USB技术交流QQ群2:580684376
USB技术交流QQ群:952873936