【转】簡單講講 USB Human Interface Device
原地址http://213style.blogspot.com/2013/09/usb-human-interface-device.html
恩,發本文的原因是看到了以前畢業的朋友在旁邊的對話框問了一些問題,我想這些問題
Report Size (8) | 75 08 |
Report Count (8) | 95 08 |
Logical Maximum (255) | 26 FF 00 |
Report ID (50) | 85 32 |
Usage (Vendor-Defined 195) | 09 C3 |
Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Buf) |
B2 02 01 |
#include "stdafx.h"
#include "HIDGet.h"
#include "HIDGetDlg.h"
#include "Process.h" #ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif #define WM_THREADDATA WM_USER + 1
#define WM_READDATA WM_USER + 2 /////////////////////////////////////////////////////////////////////////////
// CHIDGetDlg dialog
struct _ThreadData
{
HWND hWnd;
HANDLE hDev;
char cBuf[9];
HANDLE hReadFinished;
}ThreadData; LRESULT CALLBACK ReadThreadWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
BOOL bRet; ResetEvent(ThreadData.hReadFinished);
OVERLAPPED ol;
ol.hEvent = ThreadData.hReadFinished;
ol.Offset = 0;
ol.OffsetHigh = 0; DWORD wResult, wByteRead;
switch(msg)
{
case WM_CLOSE:
CancelIo(ThreadData.hDev);
CloseHandle(ThreadData.hReadFinished);
DestroyWindow(hWnd);
break; case WM_DESTROY:
PostQuitMessage(0);
break; case WM_READDATA:
//ReadFile(ThreadData.hDev, ThreadData.cBuf, 9, &wByteRead, &ol);
ThreadData.cBuf[0] = 50;
bRet = HidD_GetFeature(ThreadData.hDev, ThreadData.cBuf, 9);
//wResult = WaitForSingleObject(ThreadData.hReadFinished, 1000);
if(/*wResult == WAIT_OBJECT_0*/ bRet == TRUE)
::PostMessage(ThreadData.hWnd, WM_THREADDATA, 0, 0);
else if(/*wResult == WAIT_TIMEOUT*/ bRet == FALSE)
::PostMessage(hWnd, WM_READDATA, 0, 0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
} return 0;
} void ReadThread(CHIDGetDlg* pDlg)
{
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0 ;
wndclass.hbrBackground = NULL;
wndclass.hCursor = NULL;
wndclass.hIcon = NULL;
wndclass.hIconSm = NULL;
wndclass.hInstance = GetModuleHandle(NULL);
wndclass.lpfnWndProc = ReadThreadWindowProc;
wndclass.lpszClassName = "ReadThread";
wndclass.lpszMenuName = NULL;
wndclass.style = 0 ;
RegisterClassEx(&wndclass); HWND hReadThreadWindow = ::CreateWindow("ReadThread", "", WS_POPUP,
0, 0, 0, 0, NULL, NULL,
GetModuleHandle(NULL), NULL);
pDlg->SetReadThreadHWND(hReadThreadWindow);
ThreadData.hReadFinished = CreateEvent(NULL, TRUE, FALSE, NULL);
SetEvent(pDlg->m_hReadThreadCreated); // Start the message loop
MSG msg;
while(GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} CHIDGetDlg::CHIDGetDlg(CWnd* pParent /*=NULL*/) : CDialog(CHIDGetDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHIDGetDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
} void CHIDGetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHIDGetDlg)
DDX_Control(pDX, IDC_DATA_ED, m_edData);
//}}AFX_DATA_MAP
} BEGIN_MESSAGE_MAP(CHIDGetDlg, CDialog)
//{{AFX_MSG_MAP(CHIDGetDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////
// CHIDGetDlg message handlers void CHIDGetDlg::SetReadThreadHWND(HWND hWnd)
{
m_hReadThread = hWnd;
} void CHIDGetDlg::CreateReadThread()
{
ThreadData.hWnd = m_hWnd;
m_hReadThreadCreated = CreateEvent(NULL, TRUE, FALSE, NULL);
if(m_hReadThreadCreated)
{
DWORD threadID;
if(_beginthreadex(NULL, 0, (unsigned int (WINAPI*)(PVOID))ReadThread,
this, 0, (unsigned int*)&threadID) != 0)
WaitForSingleObject(m_hReadThreadCreated, INFINITE); CloseHandle(m_hReadThreadCreated);
}
} BOOL CHIDGetDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here
GetDeviceHandle();
CreateReadThread();
::PostMessage(m_hReadThread, WM_READDATA, 0, 0); return TRUE; // return TRUE unless you set the focus to a control
} // If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework. void CHIDGetDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
} // The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CHIDGetDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
} HANDLE CHIDGetDlg::GetDeviceHandle(GUID guid, HANDLE hDev, DWORD wDevice)
{
SP_DEVICE_INTERFACE_DATA interfaceDev;
interfaceDev.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); //Get interface
DWORD wSize = 0;
if(!SetupDiEnumDeviceInterfaces(hDev, NULL, &guid, wDevice, &interfaceDev)
|| SetupDiGetDeviceInterfaceDetail(hDev, &interfaceDev, NULL, 0, &wSize, NULL))
return INVALID_HANDLE_VALUE; //Create buffer
SP_INTERFACE_DEVICE_DETAIL_DATA *pDeviceDetail;
pDeviceDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*)malloc(wSize);
pDeviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if(!SetupDiGetDeviceInterfaceDetail(hDev, &interfaceDev, pDeviceDetail, wSize, &wSize, NULL))
{
free(pDeviceDetail);
return INVALID_HANDLE_VALUE;
} //Get device handle
HANDLE hDevice = CreateFile(pDeviceDetail->DevicePath, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL); free(pDeviceDetail);
return hDevice;
} void CHIDGetDlg::Show(CString str,long n)
{
CString strN;
strN.Format("%d", n);
m_edData.ReplaceSel(str + ":" + strN + "\r\n");
} void CHIDGetDlg::Show(CString str, CString s)
{
m_edData.ReplaceSel(str + ": " + s + "\r\n");
} void CHIDGetDlg::Clear()
{
int nStart, nStop;
m_edData.GetSel(nStart, nStop);
m_edData.SetSel(0, nStop);
m_edData.Clear();
} void CHIDGetDlg::GetDeviceHandle()
{
//Get HID GUID
GUID guid;
HidD_GetHidGuid(&guid); //Get all present HID interface
HDEVINFO hDeviceInfo = SetupDiGetClassDevs(&guid, NULL, NULL,
DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
if(hDeviceInfo == INVALID_HANDLE_VALUE) return; Clear();
DWORD w = 0;
while(1) /*for(DWORD w=0; w<20; w++)*/
{
if((ThreadData.hDev = GetDeviceHandle(guid, hDeviceInfo, /*w*/w++))!=INVALID_HANDLE_VALUE)
{
HIDD_ATTRIBUTES att;
if(HidD_GetAttributes(ThreadData.hDev, &att))
{
PHIDP_PREPARSED_DATA pPreData;
if(HidD_GetPreparsedData(ThreadData.hDev, &pPreData))
{
HIDP_CAPS cap;
if(HidP_GetCaps(pPreData, &cap)==HIDP_STATUS_SUCCESS)
{
if(att.VendorID==0x056D && att.ProductID==0x0002 && att.VersionNumber==0x7530)
{
if(cap.Usage==0x01 && cap.UsagePage==0x80)
{
HidD_FreePreparsedData(pPreData);
break;
}
}
}
HidD_FreePreparsedData(pPreData);
}
}
CloseHandle(ThreadData.hDev);
ThreadData.hDev = INVALID_HANDLE_VALUE;
} if(w > 65536)
{
CloseHandle(ThreadData.hDev);
ThreadData.hDev = INVALID_HANDLE_VALUE;
break;
}
} SetupDiDestroyDeviceInfoList(hDeviceInfo);
} void CHIDGetDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
::SendMessage(m_hReadThread, WM_CLOSE, 0, 0);
if(ThreadData.hDev!=INVALID_HANDLE_VALUE)
CloseHandle(ThreadData.hDev); CDialog::OnClose();
} LRESULT CHIDGetDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
static long nCount = 0; if(message == WM_THREADDATA)
{
CString str;
str.Format("# of packet = %d, report id =%d, LCD = %c %c %c %c %c %c %c %c",
nCount++,
ThreadData.cBuf[0], // report id = 50
ThreadData.cBuf[1],
ThreadData.cBuf[2],
ThreadData.cBuf[3],
ThreadData.cBuf[4],
ThreadData.cBuf[5],
ThreadData.cBuf[6],
ThreadData.cBuf[7],
ThreadData.cBuf[8]); Show("Data", str);
if(nCount > 500)
{
Clear();
nCount = 0;
} ::PostMessage(m_hReadThread, WM_READDATA, 0, 0);
} return CDialog::DefWindowProc(message, wParam, lParam);
}
case WM_READDATA:
//ReadFile(ThreadData.hDev, ThreadData.cBuf, 9, &wByteRead, &ol);
ThreadData.cBuf[0] = 50;
bRet = HidD_GetFeature(ThreadData.hDev, ThreadData.cBuf, 9);
//wResult = WaitForSingleObject(ThreadData.hReadFinished, 1000);
if(/*wResult == WAIT_OBJECT_0*/ bRet == TRUE)
::PostMessage(ThreadData.hWnd, WM_THREADDATA, 0, 0);
else if(/*wResult == WAIT_TIMEOUT*/ bRet == FALSE)
::PostMessage(hWnd, WM_READDATA, 0, 0);
break;
用 GetMessage 收到該訊息,相反的就是 Post 另外一個自訂訊息 WM_READDATA,這樣
就是你剛剛放進去的訊息,反正 GetMessage 輪詢個不停 總有會被處理到的時候。
事實上,主要我還有宣告一個隱形的視窗,然後才在這個隱形的視窗下的 WndProc 用訊息
Registering for Raw Input
Performing a Standard Read of Raw Input
Performing a Buffered Read of Raw Input
Performing a Standard Read of Raw Input
read of raw input from either a keyboard or mouse Human Interface Device
(HID) and then prints out various information from the device.
【转】簡單講講 USB Human Interface Device的更多相关文章
- Human Interface Device (HID) Class Decoder
http://www.usblyzer.com/usb-human-interface-device-hid-class-decoder.htm Human Interface Device (H ...
- [转]講講 John Carmack 的快速反平方根演算法
講講 John Carmack 的快速反平方根演算法 原地址http://213style.blogspot.com/2014/07/john-carmack.html 本篇的主題很簡單,講講怎麼快 ...
- 转:[ASP.NET]重構之路系列v4 – 簡單使用interface之『你也會IoC』
前言 上次v3版本,我們將Entity, Service, Dao, Utility都放到了類別庫裡面,讓我們可以輕鬆的在不同專案中用同一份組件.雖然文章沒有獲得太多的讚賞,不過相信那一定是太多人會這 ...
- [Xamarin] 簡單使用Fragment 靜態篇 (转帖)
新的Android 開發,非常會使用到Fragment,不過官方範例有點小複雜,對初學者來說有點難消化,所以就記錄一下心得,這邊部落格將使用靜態的方法使用Fragment,Fragment 有自己的生 ...
- 用JavaScript做一個簡單的計算器
今天繼續學習JavaScript,視頻講的確實挺差勁的.還是只能跟著W3School自己慢慢摸索著弄了.自己百度了一下,參考了一個大佬寫的一個簡單的計算器代碼.代碼能跑通,但是做出來的樣子實在是感覺太 ...
- 簡單工廠模式-之-什麼是產品線 And 抽象工廠模式-之-什麼是產品族
簡單工廠模式-之-什麼是產品線 簡單工廠模式中,有一個概念就是使用了多層次的產品結構,那麼什麼是產品結構或者說什麼是產品線? 假定我們有一個基準的產品標準Product,那麼所有繼承該基類或者傳遞基類 ...
- [Xamarin] 簡單使用AlertDialog (转帖)
這東西跟Toast 很像,有方便提示的作用 像是Windows 上面的MessageBox 或是 Javascript 的 Alert 會先阻斷使用者並且下一個決定 很簡單我就不贅述,基本上透過 Al ...
- 簡單SQL存儲過程實例
簡單SQL存儲過程實例 摘自:http://blog.csdn.net/libra6956/article/details/5589173 实例1:只返回单一记录集的存储过程. 银行存款表(bankM ...
- 【译】iOS人性化界面指南(iOS Human Interface Guidelines)(一)
1. 引言1.1 译者自述 我是一个表达能力一般的开发员,不管是书面表达,还是语言表达.在很早以前其实就有通过写博客锻炼这方面能力的想法,但水平有限实在没有什么拿得出手的东西分享.自2015年7月以来 ...
随机推荐
- Hive在集群环境配置
本文转载自:https://blog.csdn.net/hanjin7278/article/details/53035739 一.简介 hive是基于Hadoop的一个数据仓库工具,可以将结构化的数 ...
- Neo4j 第三篇:Cypher查询入门
本文转载自:https://www.cnblogs.com/ljhdo/p/5516793.html Neo4j使用Cypher查询图形数据,Cypher是描述性的图形查询语言,语法简单,功能强大,由 ...
- vue-router总结2
在上一篇总结了vue-router中的路由切换.重定向和路由传参等知识,因为篇幅的原因,便将剩下的路由模块化.路由嵌套.history模式.路由守卫等放在这里来写了.因为是继续前面的写.所以代码也还是 ...
- 服务网关zuul之六:Zuul高可用
我们实际使用Zuul的方式如上图,不同的客户端使用不同的负载将请求分发到后端的Zuul,Zuul在通过Eureka调用后端服务,最后对外输出.因此为了保证Zuul的高可用性,前端可以同时启动多个Zuu ...
- 如何修改CSV文件的分隔符
Excel转成CSV文件,在Windows下默认的列表分隔符是逗号(,).在某些情况下,我们可能需要更改列表分隔符,如某个单元格内容包含逗号,而这可能导致使用它的程序出错,这样就需要更改列表分隔符:再 ...
- Python下发送定时消息给微信好友
""" Description:时间可以改长一点 一分钟一个 Author:Nod Date: Record: #---------------------------- ...
- Jmeter(八)HTTPCookie管理器
Cookie绝对是日常工作以及技术中一个绕不过去的‘角色’,正常各种各样的业务需要Cookie的存在.Jmeter中也有支持发送Cookie的组件,但是,仅是后话:在此还是有必要先记一记Cookie到 ...
- Aspose.Word 输出表格后空格字符丢失的解决方法
将datatable输出到word,执行如下代码. doc.MailMerge.ExecuteWithRegions(outDt); 执行完后发现第一列含前缀空格的字段值,空格字符被自动清除了. 解决 ...
- (转)利用WPF的ListView进行大数据量异步加载
原文:http://www.cnblogs.com/scy251147/archive/2012/01/08/2305319.html 由于之前利用Winform的ListView进行大数据量加载的时 ...
- 通过表达式树把datareader和datatable转换为实体
续上两篇文章,使用emit构造dynamic method,把 datareader转换为实体,以避免直接使用反射来实现带来的性能损失.代码看似没有纰漏,但是实际上我在framwork4下运行时,调用 ...