IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> usb hid 设备 -> 正文阅读

[C++知识库]usb hid 设备

#pragma once
#ifndef USB_DEVICE_H
#define USB_DEVICE_H


class UsbDevice
{
?? ?int mvid;
?? ?int mpid;
?? ?void* dev_handler;
?? ?bool usb_ok = false;
public:
?? ?UsbDevice();
?? ?~UsbDevice();
?? ?int open(int pid, int vid);
?? ?int close();
?? ?int write(const unsigned char* buf, const int l);
?? ?int read(unsigned char* buf, const int l, const int timeout_ms);
?? ?int reconnect();

private:
?? ?static void* ?getDeviceHandle(unsigned short vendor, unsigned short product, unsigned int MI = 0x0000);
?? ?static bool?? ?IsMatchingDevice(wchar_t *pDeviceID, unsigned int uiVID, unsigned int uiPID, unsigned int uiMI);
};

#endif

///---------------------------------------------usbdevice.cpp--------------------------------------------------------

///------------------------------------------------------------------------------------------------------------------------


#include <cstdio>
#include <sstream>
#include <wchar.h>
#include <string.h>
#include <Windows.h>
#include <hidsdi.h>
#include <SetupAPI.h>
#include <cfgmgr32.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <Windows.h>
#include "UsbDevice.h"

#pragma comment(lib, "hid.lib")
#pragma comment(lib, "setupapi.lib")

HANDLE UsbDevice::getDeviceHandle(unsigned short uiVID, unsigned short uiPID, unsigned int uiMI) {
?? ?printf("Attempting to open: vid=%04x pid=%04x mid=%04x\n", uiVID, uiPID, uiMI);
?? ?const GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2L, 0xF16F, 0x11CF, 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 };
?? ?HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_HID, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
?? ?//std::cout << hDevInfo << std::endl;
?? ?if (hDevInfo == INVALID_HANDLE_VALUE)
?? ??? ?return 0;

?? ?HANDLE hReturn = 0;

?? ?SP_DEVINFO_DATA deviceData = { 0 };
?? ?deviceData.cbSize = sizeof(SP_DEVINFO_DATA);

?? ?for (unsigned int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &deviceData); ++i) {
?? ??? ?wchar_t wszDeviceID[MAX_DEVICE_ID_LEN];
?? ??? ?if (CM_Get_Device_IDW(deviceData.DevInst, wszDeviceID, MAX_DEVICE_ID_LEN, 0))
?? ??? ??? ?continue;

?? ??? ?if (!IsMatchingDevice(wszDeviceID, uiVID, uiPID, uiMI))
?? ??? ??? ?continue;

?? ??? ?SP_INTERFACE_DEVICE_DATA interfaceData = { 0 };
?? ??? ?interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

?? ??? ?if (!SetupDiEnumDeviceInterfaces(hDevInfo, &deviceData, &GUID_DEVINTERFACE_HID, 0, &interfaceData))
?? ??? ??? ?break;

?? ??? ?DWORD dwRequiredSize = 0;
?? ??? ?SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, 0, 0, &dwRequiredSize, 0);

?? ??? ?SP_INTERFACE_DEVICE_DETAIL_DATA *pData = (SP_INTERFACE_DEVICE_DETAIL_DATA *)new unsigned char[dwRequiredSize];
?? ??? ?pData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

?? ??? ?if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, pData, dwRequiredSize, 0, 0))
?? ??? ?{
?? ??? ??? ?delete pData;
?? ??? ??? ?break;
?? ??? ?}

?? ??? ?// get device handle(with and without overlapping mode)
?? ??? ?HANDLE hDevice = CreateFile(pData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, NULL, 0);
?? ??? ?//HANDLE hDevice = CreateFile(pData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);

?? ??? ?if (hDevice == INVALID_HANDLE_VALUE)
?? ??? ?{
?? ??? ??? ?delete pData;
?? ??? ??? ?printf(" ? ?Failed open: vid=%04x pid=%04x mid=%04x\n", uiVID, uiPID, uiMI);
?? ??? ??? ?break;
?? ??? ?}

?? ??? ?hReturn = hDevice;
?? ??? ?printf("!!! Opened: vid=%04x pid=%04x mid=%04x\n", uiVID, uiPID, uiMI);
?? ??? ?break;
?? ?}

?? ?SetupDiDestroyDeviceInfoList(hDevInfo);

?? ?return hReturn;
}

//void UsbDevice::readDataFromDevice(HANDLE hFile, unsigned char* buf, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped = NULL) {
//?? ?ReadFile(hFile, buf, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped);
//}

bool UsbDevice::IsMatchingDevice(wchar_t *pDeviceID, unsigned int uiVID, unsigned int uiPID, unsigned int uiMI) {
?? ?unsigned int uiLocalVID = 0, uiLocalPID = 0, uiLocalMI = 0;
?? ?LPWSTR pszNextToken = 0;
?? ?LPWSTR pszToken = wcstok(pDeviceID, L"\\#&");
?? ?while (pszToken) {
?? ??? ?std::wstring tokenStr(pszToken);
?? ??? ?if (tokenStr.find(L"VID_", 0, 4) != std::wstring::npos) {
?? ??? ??? ?std::wistringstream iss(tokenStr.substr(4));
?? ??? ??? ?iss >> std::hex >> uiLocalVID;
?? ??? ?}
?? ??? ?else if (tokenStr.find(L"PID_", 0, 4) != std::wstring::npos) {
?? ??? ??? ?std::wistringstream iss(tokenStr.substr(4));
?? ??? ??? ?iss >> std::hex >> uiLocalPID;
?? ??? ?}
?? ??? ?else if (tokenStr.find(L"MI_", 0, 3) != std::wstring::npos) {
?? ??? ??? ?std::wistringstream iss(tokenStr.substr(3));
?? ??? ??? ?iss >> std::hex >> uiLocalMI;
?? ??? ?}
?? ??? ?pszToken = wcstok(0, L"\\#&");
?? ?}
?? ?if (uiVID != uiLocalVID || uiPID != uiLocalPID || uiMI != uiLocalMI)
?? ??? ?return false;
?? ?return true;
}


UsbDevice::UsbDevice() {
?? ?usb_ok = false;
?? ?mvid = 0;
?? ?mpid = 0;
?? ?dev_handler = 0;
}
UsbDevice::~UsbDevice() {
?? ?close();
}
int UsbDevice::open(int vid, int pid) {
?? ?mvid = vid;
?? ?mpid = pid;
?? ?dev_handler = getDeviceHandle(vid, pid);
?? ?if (dev_handler == 0) {
?? ??? ?usb_ok = false;
?? ??? ?return -1;
?? ?}
?? ?usb_ok = true;
?? ?return 1;
}


int UsbDevice::reconnect() {
?? ?if (!usb_ok) {
?? ??? ?if (open(mvid, mpid) < 0)
?? ??? ??? ?return -1;
?? ??? ?else
?? ??? ??? ?return 1;
?? ?}
?? ?return 1;
}


int UsbDevice::close() {
?? ?if (dev_handler) {
?? ??? ?int res = CloseHandle(dev_handler);
?? ??? ?if (res) {
?? ??? ??? ?dev_handler = 0;
?? ??? ??? ?usb_ok = false;
?? ??? ?}
?? ??? ?return res;
?? ?}

?? ?return 0;
}

int UsbDevice::write(const unsigned char* buf, const int l) {

?? ?DWORD bytes_written=-1;
?? ?unsigned char innerbuf[65];
?? ?memset(innerbuf, 0, sizeof(innerbuf));
?? ?if (l <= sizeof(innerbuf)) {
?? ??? ?memcpy(innerbuf, buf, l);
?? ?} else {
?? ??? ?memcpy(innerbuf, buf, sizeof(innerbuf));
?? ?}

?? ?BOOL res = WriteFile(dev_handler, innerbuf, sizeof(innerbuf), &bytes_written,0);
?? ?if (res == 0) {
?? ??? ?printf("WriteFile error=%d\n", (int)GetLastError());
?? ?}
?? ?FlushFileBuffers(WriteFile);
//?? ?if (!res) {
//?? ??? ?if (GetLastError() != ERROR_IO_PENDING) {
//?? ??? ??? ?/* WriteFile() failed. Return error. */
//?? ??? ??? ?bytes_written = -1;
//?? ??? ??? ?goto end_of_function;
//?? ??? ?}
//?? ?}
//?? ?/* Wait here until the write is done. This makes
//?? ? ? hid_write() synchronous. */
//?? ?res = GetOverlappedResult(dev_handler, &ol, &bytes_written, TRUE/*wait*/);
//?? ?if (!res) {
//?? ??? ?/* The Write operation failed. */
//?? ??? ?bytes_written = -1;
//?? ??? ?goto end_of_function;
//?? ?}
//end_of_function:

?? ?return bytes_written;
}
int UsbDevice::read(unsigned char* buf, const int l, const int timeout_ms) {
?? ?DWORD read_byte = 0;
?? ?//readDataFromDevice(dev_handler, buf, l, &read_byte);
?? ?if (0 == ReadFile(dev_handler, buf, l, &read_byte, 0)) {
?? ??? ?return -1;
?? ?}
?? ?return read_byte;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-02-22 20:23:18  更:2022-02-22 20:25:49 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 7:50:40-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码