Windows与USB的通信

搞了三天,终于把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;
}


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值