windows 与 USB设备进行数据交换实现

2019-12-5 C++

需求:上位机通过USB与下位机进行数据交互

实现:

打开设备:


static LONG HidOpen(DWORD HidUsbMian, DWORD HidUsbSub)
{
//hid设备GUID
CString strHidPath(_T(""));
HDEVINFO deviceInfoSet = INVALID_HANDLE_VALUE;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData = NULL;
SP_DEVINFO_DATA devinfoData;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
ZeroMemory(&deviceInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
ZeroMemory(&devinfoData, sizeof(SP_DEVINFO_DATA));
devinfoData.cbSize = sizeof(SP_DEVINFO_DATA);
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
GUID interfaceClassGuid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30} };
deviceInfoSet = SetupDiGetClassDevsA(&interfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
int deviceIndex = 0;
BOOL bExit = FALSE;
for ( ; bExit == FALSE; )
{
do
{
HANDLE writeHandle = INVALID_HANDLE_VALUE;
DWORD dwRequiredSize = 0;
BOOL res = SetupDiEnumDeviceInterfaces(deviceInfoSet,
NULL,
&interfaceClassGuid,
deviceIndex,
&deviceInterfaceData);
if (res == FALSE)
{
bExit = TRUE;
break;
}

BOOL bFind = FALSE;
for (int i = 0; ; ++i)
{
TCHAR szDriverName[MAX_PATH] = { 0 };
res = SetupDiEnumDeviceInfo(deviceInfoSet, i, &devinfoData);
if (res == FALSE)
{
break;
}
res = SetupDiGetDeviceRegistryProperty(deviceInfoSet, &devinfoData,
   SPDRP_CLASS, NULL, reinterpret_cast<PBYTE>(szDriverName), sizeof(szDriverName), NULL);
if (res == FALSE)
{
break;
}
if (_tcscmp(szDriverName, _T("HIDClass")) == 0) 
{
res = SetupDiGetDeviceRegistryProperty(deviceInfoSet, &devinfoData,
   SPDRP_DRIVER, NULL, reinterpret_cast<PBYTE>(szDriverName), sizeof(szDriverName), NULL);
if (res == TRUE)
{
bFind = TRUE;
break;
}
}
}
//不是有效的设备的继续
if (FALSE == bFind)
{
break;
}
res = SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
&deviceInterfaceData,
NULL,
0,
&dwRequiredSize,
NULL);
pDeviceInterfaceDetailData = reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(malloc(dwRequiredSize));
pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
res = SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
&deviceInterfaceData,
pDeviceInterfaceDetailData,
dwRequiredSize,
NULL,
NULL);

writeHandle = CreateFile(pDeviceInterfaceDetailData->DevicePath, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
/*FILE_FLAG_OVERLAPPED,/*FILE_ATTRIBUTE_NORMAL,*/
if (writeHandle == INVALID_HANDLE_VALUE) 
{
break;
}
HIDD_ATTRIBUTES attrib;
attrib.Size = sizeof(HIDD_ATTRIBUTES);
HidD_GetAttributes(writeHandle, &attrib);
/*
if ((HidUsbMian == 0x0 || attrib.VendorID == HidUsbMian)
&& (HidUsbSub == 0x0 || attrib.ProductID == HidUsbSub))
*/
if (attrib.VendorID == HidUsbMian && attrib.ProductID == HidUsbSub)
{
strHidPath = pDeviceInterfaceDetailData->DevicePath;
}
CloseHandle(writeHandle);
} while (0);
if (pDeviceInterfaceDetailData != NULL)
{
free(pDeviceInterfaceDetailData);
pDeviceInterfaceDetailData = NULL;
}
if (strHidPath != _T(""))
{
break;
}
++deviceIndex;
}
if (strHidPath == _T(""))
{
return 0;
}

HANDLE openHandle = CreateFile(strHidPath, 
GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 
FILE_FLAG_OVERLAPPED, 0);
HANDLE readHandle = CreateFile(strHidPath, 
GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 
0, 0);
/*FILE_FLAG_OVERLAPPED,/*FILE_ATTRIBUTE_NORMAL,*/
if (openHandle == INVALID_HANDLE_VALUE)
{
return NULL;
}
do
{
PHIDP_PREPARSED_DATA ppData = NULL;
HIDP_CAPS caps;
if (0)//0 == HidD_SetNumInputBuffers(openHandle, 512))
{
::OutputDebugString(_T("-------------HidD_SetNumInputBuffers Failed-------\r\n"));
break;
}
if (0 == HidD_GetPreparsedData(openHandle, &ppData))
{
::OutputDebugString(_T("-------------HidD_GetPreparsedData Failed-------\r\n"));
break;
}

NTSTATUS ntStatus = HidP_GetCaps(ppData, &caps);
if (ntStatus != HIDP_STATUS_SUCCESS)
{
::OutputDebugString(_T("-------------HidP_GetCaps Failed-------\r\n"));
break;
}
{
CString strMsg(_T(""));
strMsg.Format(_T("--Input Length[%d]---Output Length[%d]----\r\n"), caps.InputReportByteLength, caps.OutputReportByteLength);
::OutputDebugString(strMsg);
}
HidD_FreePreparsedData(ppData);
PHidDevice dev = NewHidDevice();
if (dev != NULL)
{
dev->deviceHandle = openHandle;
dev->deviceReadHandle = readHandle;
int nBufLen = (strHidPath.GetLength() + 1);
dev->devicePath = new TCHAR[nBufLen];
if (dev->devicePath != NULL)
{
memset(dev->devicePath, 0, nBufLen * sizeof(TCHAR));
_tcscpy_s(dev->devicePath, nBufLen, strHidPath.GetString());
}
}
return reinterpret_cast<long>(dev);
} while (0);

if (openHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(openHandle);
openHandle = INVALID_HANDLE_VALUE;
}
if (readHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(readHandle);
readHandle = INVALID_HANDLE_VALUE;
}
return 0;
}
读数据:

ReadFile(handle, buf, 18, &dwReaded, NULL) ;

写数据:

OVERLAPPED ol;
memset(&ol, 0, sizeof(OVERLAPPED));
ol.hEvent = ::CreateEventA(NULL, TRUE, FALSE, NULL);
do
{
int nCurLen = nBufSize >= 8 ? 8 : nBufSize;
memset(buf, 0, 11);
buf[0] = 0x00;
buf[1] = LOBYTE(nCurLen);
buf[2] = 0x00;
memcpy(buf + 3, pTmp, nCurLen);
ResetEvent(ol.hEvent);
BOOL bRet = WriteFile(dev->deviceHandle, reinterpret_cast<LPVOID>(buf), 11, &dwWrited, &ol);
if (bRet == FALSE)
{
DWORD dwTmp = 0;
GetOverlappedResult(dev->deviceHandle, &ol, &dwTmp, TRUE);
}

pTmp += nCurLen;
nBufSize -= nCurLen;
} while (nBufSize > 0);

if (ol.hEvent != NULL)
{
CloseHandle(ol.hEvent);
}
坑1:

传输大数据时,在有的电脑,会出现卡死情况

解决方式:

读跟写句柄分开创建,具体原因不明  

标签: c++ USB 设备读写

评论(0) 浏览(751)

Powered by EMLOG Copyright @ 狼酒 版权所有. 闽ICP备14012694号-2