隨着越來越多的設備通過USB接口與主機連接,我們需要讀取USB設備的數據。現就幾種不同讀取USB Device Data的方法進行探討。非常歡迎網友補充。
方法一:讀取/dev/input/eventX
此方法只對HID設備有效。只要USB設備註明是HID Input設備,則它的數據就會被解析爲輸入設備並填入/dev/input/eventX.
對此設備的操作和數據讀取。見:
http://blog.sina.com.cn/s/blog_602f87700101dno6.html
要系統自動創建/dev/input/eventX. 則需要在Kernel配置中作如下動作:
Device Drivers ---> Input device support ---> -*- Generic input layer (needed for keyboard, mouse, ...)
Device Drivers ---> Input device support ---> <<font color="#ed1c24">*> Event interface
優點:ioctl衆多,可以得到輸入設備非常多的信息。且數據結構明顯。讀取數據方便。
缺點:數據只包括HID Input 數據,其它非Input Event數據如Raw Data。 則無法從這裏得到。
方法二:讀取/dev/hidrawX 設備:
/dev/hidrawX設備解釋:
當支持不是標準的User interface Device的HID Device時。/dev/hidraw與稍候會提到的/dev/hiddev的區別是:這個Device不處理hid Event. not parsing, no lookups.
要避免使用用戶層庫 libhid/libusb. (Sam對Kernel中這個說明也不太明白,因爲發現hidraw與libusb可以同時使用的)
要支持/dev/hidraw. 則需要Kernel作如下配置:
Device Drivers ---> [*] HID Devices ---> [*] /dev/hidraw raw HID device support
讀取過程:
首先看其數據結構:
#hexdump /dev/hidraw2
0000000 fe04 0054 0000 0000 0001 0000 0000 0000
0000010 0000 0000 0000 ff00 00ff 0000 2300 005a
0000020 fe04 0054 0000 0000 0001 0000 0000 0000
0000030 0000 0000 0000 ff00 00ff 0000 2300 005a
0000040 fe04 0054 0000 0000 0001 0000 0000 0000
0000050 0000 0000 0000 ff00 00ff 0000 2300 005a
以Sam這個設備爲例:可以發現每32個字節,數據有個重複。所以推測USB設備每個數據包32字節。
所以可以使用open,read 等方式得到USB Device Raw Data.