您的位置:首页 > 编程语言 > C语言/C++

Windows与USB的通信

2016-07-21 11:03 399 查看
搞了三天,终于把windows的USB程序搞定了,虽然还存在一些问题,

mark一下方便以后使用。

#include <windows.h> //一定要加入该头文件
#include <iostream>

extern "C"{
#include <hidsdi.h>
#include <setupapi.h>
#include <hidsdi.h>
}

using namespace std;

BOOL DeviceOpen(HANDLE&handle, WORD wVID, WORD wPID)
{
BOOL bRet = FALSE;
GUID hidGuid;
HDEVINFO hardwareDeviceInfo;
SP_INTERFACE_DEVICE_DATA deviceInfoData;
PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;
ULONG predictedLength = 0;
ULONG requiredLength = 0;
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
deviceInfoData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
HidD_GetHidGuid(&hidGuid);
hardwareDeviceInfo = SetupDiGetClassDevs(&hidGuid, NULL,NULL, (DIGCF_PRESENT|DIGCF_DEVICEINTERFACE));
for (int i=0; i<128; i++)
{
if (!SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, 0,&hidGuid, i, &deviceInfoData)) continue;
SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo, &deviceInfoData,NULL, 0, &requiredLength, NULL);
predictedLength = requiredLength;
functionClassDeviceData =(PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(predictedLength);
if (!functionClassDeviceData) continue;
functionClassDeviceData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfo,&deviceInfoData, functionClassDeviceData, predictedLength,&requiredLength, NULL)) break;
handle = CreateFile(functionClassDeviceData->DevicePath,GENERIC_READ|GENERIC_WRITE, 0, NULL,OPEN_EXISTING, 0, NULL);
// cout <<"devicePath"<<functionClassDeviceData->DevicePath<<endl;
if (handle != INVALID_HANDLE_VALUE)
{
HIDD_ATTRIBUTES attri;
HidD_GetAttributes(handle, &attri);
if ((attri.VendorID == wVID) && (attri.ProductID == wPID))
{
cout <<"handle="<<handle<<",err="<<GetLastError()<<endl;
printf("%x,%x\n",attri.VendorID,attri.ProductID);
bRet = TRUE;
break;
}
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
// cout <<attri.VendorID<<" "<<attri.ProductID<<" "<<attri.VendorID<<" "<<attri.VersionNumber<<endl;
}
}
SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
return bRet;
}

void DeviceClose(HANDLE &handle)
{

CloseHandle(handle);

handle = INVALID_HANDLE_VALUE;
}

BOOL DeviceWrite(HANDLE &handle, LPCVOID lpBuffer, DWORD dwSize)
{
if(handle==INVALID_HANDLE_VALUE)
{
cout <<"DeviceWrite failed"<<endl;
return 0;
}
BYTE rBuffer[64] = {0};
DWORD dwRet;
BOOL bRet;

PHIDP_PREPARSED_DATA PreparsedData;
HIDP_CAPS Capabilities;

HidD_GetPreparsedData(handle,&PreparsedData);
HidP_GetCaps(PreparsedData, &Capabilities);
cout <<"Capabilities.OutputReportByteLength="<<Capabilities.OutputReportByteLength<<endl;

rBuffer[0] = 0x0; //第一个字节为report Id,一定需要
#if 0
rBuffer[1] = 50;
rBuffer[2] = 88;
rBuffer[3] = 2;
rBuffer[4] = 88;
#else
memcpy(rBuffer+1,lpBuffer,min(10,dwSize));
#endif
//这里写的长度一定要是Capabilities.OutputReportByteLength,还不知道什么原因
bRet = WriteFile(handle, rBuffer, Capabilities.OutputReportByteLength, &dwRet, NULL);
cout <<"bRet="<<bRet<<" GetLastError="<<GetLastError()<<" dwRet="<<dwRet<<endl;
return bRet;
}

BOOL DeviceRead(HANDLE &handle, LPVOID lpBuffer, DWORD dwSize)
{
if(handle==INVALID_HANDLE_VALUE)
{
cout <<"DeviceWrite failed"<<endl;
return 0;
}
BYTE rBuffer[128] = {0};
DWORD dwRet;
BOOL bRet;

PHIDP_PREPARSED_DATA PreparsedData;
HIDP_CAPS Capabilities;
HidD_GetPreparsedData(handle,&PreparsedData);
HidP_GetCaps(PreparsedData, &Capabilities);
//memcpy(&wBuffer[2], lpBuffer, min(6, dwSize));
COMMTIMEOUTS timeout;
timeout.ReadIntervalTimeout = 0;
timeout.ReadTotalTimeoutConstant = 0;
timeout.ReadTotalTimeoutMultiplier = 1000;
SetCommTimeouts(handle,&timeout);
cout <<"timeout err="<<GetLastError()<<endl;

cout <<"read Capabilities.OutputReportByteLength="<<Capabilities.OutputReportByteLength<<endl;
//读的长度也一定要是Capabilities.OutputReportByteLength
bRet = ReadFile(handle, rBuffer, Capabilities.OutputReportByteLength, &dwRet, NULL);
cout <<"bRet="<<bRet<<" GetLastError="<<GetLastError()<<" dwRet="<<dwRet<<endl;
for (int i=0; i<dwRet; i++)
{
printf("%x ",rBuffer[i]);
}
cout <<endl;
// memcpy(lpBuffer, &rBuffer[1], min(7, dwSize));
return bRet;
}

int main(int argc, char* argv[])
{

HANDLE handle=NULL;
cout <<"handle="<<handle<<endl;
//这两个参数的值能识别哪一个是你的设备
if(DeviceOpen(handle,0x9219,0x1312)==FALSE)
{
// cout <<"Device Open failed!"<<endl;
}

cout <<"handle="<<handle<<endl;

char data[10]={0};

if(handle==INVALID_HANDLE_VALUE)
{
cout <<"open device failed"<<endl;
return 1;
}
data[0] = 50;
data[1] = 88;
data[2] = 2;
data[3] = 88;
DeviceWrite(handle,data,4);
DeviceRead(handle,data,5);

DeviceClose(handle);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ USB windows