236 lines
5.1 KiB
C++
236 lines
5.1 KiB
C++
#include "usb_interface.h"
|
|
#include <iostream>
|
|
|
|
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <usbioctl.h>
|
|
#include <usbprint.h>
|
|
|
|
extern "C"{
|
|
#include <hidsdi.h>
|
|
#include <setupapi.h>
|
|
#include <hidsdi.h>
|
|
#include <usbprint.h>
|
|
#include <initguid.h>
|
|
#include <fileapi.h>
|
|
#include "usbscan.h"
|
|
}
|
|
|
|
|
|
DEFINE_GUID (GUID_DEVINTERFACE_USBPRINT, 0x28d78fad, 0x5a12, 0x11d1, 0xae, 0x5b, 0x0, 0x0, 0xf8, 0x3, 0xa8, 0xc2);
|
|
|
|
using namespace std;
|
|
|
|
_USB_VAR USB_Var;
|
|
HANDLE hUsb = INVALID_HANDLE_VALUE;
|
|
char DeviceName[MAX_PATH];
|
|
OVERLAPPED overlap;
|
|
|
|
|
|
|
|
Usb_Interface::Usb_Interface() {}
|
|
|
|
//USB获取当前的设备信息
|
|
bool Usb_Interface::Usb_Exsit(uint16_t vid,uint16_t pid)
|
|
{
|
|
// uint8_t vid_buf[8];
|
|
// uint8_t pid_buf[8];
|
|
|
|
// //vid & pid 组合成字符串
|
|
// sprintf((char *)(&vid_buf[0]),"vid_%04x",vid);
|
|
// sprintf((char *)(&pid_buf[0]),"pid_%04x",pid);
|
|
|
|
// Get handle to the devices
|
|
HDEVINFO hInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVINTERFACE_USBPRINT, NULL, NULL,
|
|
DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
|
|
|
|
if (hInfo == INVALID_HANDLE_VALUE)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
SP_INTERFACE_DEVICE_DATA Interface_Info;
|
|
Interface_Info.cbSize = sizeof(Interface_Info); // Enumerate device
|
|
if (!SetupDiEnumInterfaceDevice(hInfo, NULL, (LPGUID) &GUID_DEVINTERFACE_USBPRINT,0, &Interface_Info))
|
|
{
|
|
SetupDiDestroyDeviceInfoList(hInfo);
|
|
return false;
|
|
}
|
|
|
|
DWORD needed; // get the required length
|
|
SetupDiGetInterfaceDeviceDetail(hInfo, &Interface_Info, NULL, 0, &needed, NULL);
|
|
PSP_INTERFACE_DEVICE_DETAIL_DATA detail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc(needed);
|
|
if (!detail)
|
|
{
|
|
SetupDiDestroyDeviceInfoList(hInfo);
|
|
return false;
|
|
}
|
|
// fill the device details
|
|
detail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
|
|
if (!SetupDiGetInterfaceDeviceDetail(hInfo, &Interface_Info, detail, needed,NULL, NULL))
|
|
{
|
|
free((PVOID) detail);
|
|
SetupDiDestroyDeviceInfoList(hInfo);
|
|
return false;;
|
|
}
|
|
|
|
|
|
char *ptr = (char *)(detail->DevicePath);
|
|
DevicePath = QString("PID:0x8800,VID:0x0fe6");
|
|
memset(DeviceName,0x00,MAX_PATH);
|
|
for(int i = 0; i<MAX_PATH; i++)
|
|
{
|
|
DeviceName[i] = ptr[i] ;
|
|
if('}' == DeviceName[i])
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
free((PVOID) detail);
|
|
SetupDiDestroyDeviceInfoList(hInfo);
|
|
|
|
|
|
// for(int i = 0; i<MAX_PATH; i++)
|
|
// {
|
|
// qDebug() << DeviceName[i];
|
|
// }
|
|
|
|
|
|
//判断是否是要找的VID & PID
|
|
// if(! strstr((char *)(&DeviceName[0]),(char *)(&vid_buf[0])))
|
|
// {
|
|
// return false;
|
|
// }
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
//打开USB
|
|
bool Usb_Interface::Usb_Open(uint16_t vid,uint16_t pid)
|
|
{
|
|
UINT success;
|
|
int siz, nBytes;
|
|
|
|
memset(&overlap, 0, sizeof(overlap));
|
|
|
|
//搜索设备
|
|
if(! Usb_Exsit(vid,pid))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
//读VID和PID
|
|
hUsb = CreateFile((LPCWSTR)(DeviceName),
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
FILE_SHARE_WRITE | FILE_SHARE_READ,
|
|
NULL,
|
|
OPEN_EXISTING,
|
|
FILE_FLAG_OVERLAPPED,
|
|
NULL);
|
|
|
|
if(hUsb == INVALID_HANDLE_VALUE )
|
|
{
|
|
return false;
|
|
}
|
|
|
|
|
|
USB_Var.UsbOpen = true;
|
|
|
|
return true;
|
|
}
|
|
|
|
//关闭USB
|
|
bool Usb_Interface::Usb_Close(void)
|
|
{
|
|
if(INVALID_HANDLE_VALUE != hUsb )
|
|
{
|
|
CloseHandle(hUsb);
|
|
}
|
|
|
|
hUsb = INVALID_HANDLE_VALUE;
|
|
|
|
USB_Var.UsbOpen = false;
|
|
|
|
return true;
|
|
}
|
|
|
|
//usb数据发送
|
|
bool Usb_Interface::Usb_Data_Send(LPCVOID lpBuffer, DWORD dwSize)
|
|
{
|
|
UINT success;
|
|
int nBytes;
|
|
DWORD numread;
|
|
|
|
//如果非法句柄 则不能用
|
|
if(INVALID_HANDLE_VALUE == hUsb )
|
|
{
|
|
return false;
|
|
}
|
|
|
|
success = WriteFile(hUsb,lpBuffer,dwSize,(PULONG) &nBytes,&overlap);
|
|
|
|
//状态判断
|
|
if (success)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if (GetLastError() == ERROR_IO_PENDING)
|
|
{
|
|
WaitForSingleObject(hUsb,800);
|
|
success = GetOverlappedResult(hUsb, &overlap, &numread, FALSE);
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// qDebug() << "Send Success: " << success;
|
|
|
|
return true;
|
|
}
|
|
|
|
//usb数据接收
|
|
bool Usb_Interface::Usb_Data_Read(LPVOID lpBuffer, DWORD dwSize)
|
|
{
|
|
UINT success;
|
|
int nBytes;
|
|
DWORD numread;
|
|
|
|
//如果非法句柄 则不能用
|
|
if(INVALID_HANDLE_VALUE == hUsb )
|
|
{
|
|
return false;
|
|
}
|
|
|
|
success = ReadFile(hUsb,lpBuffer,dwSize,(PULONG) &nBytes,&overlap);
|
|
|
|
//状态判断
|
|
if (success)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if (GetLastError() == ERROR_IO_PENDING)
|
|
{
|
|
WaitForSingleObject(hUsb,500);
|
|
success = GetOverlappedResult(hUsb, &overlap, &numread, FALSE);
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// qDebug() << "read Success: " << success;
|
|
|
|
return success;
|
|
}
|