如何从USB外设读取数据?

问题描述:

我拿起了一个即将被扔掉的wii drumset。它有一个USB端口,我想将它连接到我的MacBook上进行一个项目。我的目标基本上是能够检测何时在设备上敲击某个鼓。根据我到目前为止云集,我需要做下面的步骤:如何从USB外设读取数据?

  1. 连接设备,确定它是TTY端口
  2. 使用“屏幕”命令打印出来的数据来自鼓从屏幕命令到一些代码,为我的项目产生有用的输出
  3. 输出数据

我想我明白了怎么办2和3,但我越来越挂在步骤1中

如果我在终端中运行“ioreg -Src IOUSBDevice”,我可以确定设备已连接,但我不知道它在哪个tty端口上。 (请参阅下面的输出)

另外,一般情况下,是否可以从连接到USB端口的任何USB外设以字节流的形式打印数据?

编辑:我应该补充说,如果我运行“ls /dev/tty.*”,我没有看到任何tty.usb项目,只有tty.bluetooth的东西。

+-o Harmonix Drum Controller for Nintendo [email protected] <class IOUSBDevice, id $ 
 
    | { 
 
    | "sessionID" = 2111009401078 
 
    | "iManufacturer" = 1 
 
    | "bNumConfigurations" = 1 
 
    | "idProduct" = 5 
 
    | "bcdDevice" = 4096 
 
    | "Bus Power Available" = 250 
 
    | "bMaxPacketSize0" = 64 
 
    | "USB Product Name" = "Harmonix Drum Controller for Nintendo Wii" 
 
    | "iProduct" = 2 
 
    | "iSerialNumber" = 0 
 
    | "USB Address" = 4 
 
    | "bDeviceClass" = 0 
 
    | "locationID" = 337641472 
 
    | "bDeviceSubClass" = 0 
 
    | "IOUserClientClass" = "IOUSBDeviceUserClientV2" 
 
    | "PortNum" = 2 
 
    | "IOCFPlugInTypes" = {"9dc7b780-9ec0-11d4-a54f-000a27052861"="IOUSBFamily.$ 
 
    | "bDeviceProtocol" = 0 
 
    | "USB Vendor Name" = "Licensed by Nintendo of America " 
 
    | "Device Speed" = 1 
 
    | "idVendor" = 7085 
 
    | "Requested Power" = 50 
 
    | "IOGeneralInterest" = "IOCommand is not serializable" 
 
    | "Low Power Displayed" = No 
 
    | } 
 
    | 
 
    +-o IOUSBCompositeDriver <class IOUSBCompositeDriver, id 0x100000ebe, !regis$ 
 
    +-o [email protected] <class IOUSBInterface, id 0x100000ebf, registered, matc$ 
 
+-o IOUSBHIDDriver <class IOUSBHIDDriver, id 0x100000ec2, registered, matc$ 
 
    +-o IOHIDInterface <class IOHIDInterface, id 0x100000ec4, registered, ma$ 
 
    +-o IOHIDLibUserClient <class IOHIDLibUserClient, id 0x100000ec5, !regis$ 
 
    +-o IOHIDLibUserClient <class IOHIDLibUserClient, id 0x100000ec7, !regis$

到目前为止,

我没有说你正在寻找确切的答案,但我跑了类似的问题,试图从连接到GPS设备读取数据我的MacBook Pro。在我的情况下,发出命令后:

ioreg -w0 -l -p IOUSB 

我有(限幅不相关的部分):

+-o u-blox 7 - GPS/GNSS [email protected] <class IOUSBDevice, id 0x1000026b0, registered, matched, active, busy 0 (60 ms), retain 12> 
    | { 
    |  "sessionID" = 17488341785377 
    |  "idProduct" = 423 
    |  "bNumConfigurations" = 1 
    |  "iManufacturer" = 1 
    |  "bcdDevice" = 256 
    |  "Bus Power Available" = 250 
    |  "bMaxPacketSize0" = 64 
    |  "USB Product Name" = "u-blox 7 - GPS/GNSS Receiver" 
    |  "iProduct" = 2 
    |  "iSerialNumber" = 0 
    |  "USB Address" = 12 
    |  "IOUserClientClass" = "IOUSBDeviceUserClientV2" 
    |  "bDeviceSubClass" = 0 
    |  "bDeviceClass" = 2 
    |  "bcdUSB" = 272 
    |  "locationID" = 336658432 
    |  "PortNum" = 1 
    |  "IOCFPlugInTypes" = {"9dc7b780-9ec0-11d4-a54f-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
    |  "bDeviceProtocol" = 0 
    |  "USB Vendor Name" = "u-blox AG - www.u-blox.com" 
    |  "Device Speed" = 1 
    |  "idVendor" = 5446 
    |  "Requested Power" = 50 
    |  "IOGeneralInterest" = "IOCommand is not serializable" 
    |  "Low Power Displayed" = No 
    | } 
    | 

这并没有告诉太多上tty.*设备被分配所以我做了什么在拔下设备之前和之后是ls /dev/tty.*。通过这样做,我意识到终端/dev/tty.usbmodem14111消失了。也许你不会有tty.usbmodem*你的情况,但请注意14111部分,这与我的设备的ioreg行的第一部分(u-blox 7 - GPS/GNSS [email protected])部分重合。也许你可以在你的案例中使用这个提示,看看你是否发现了tty设备。

如果您发现更通用的方式来发现tty,请分享!祝你好运!

它看起来像您的鼓套件标识为HID设备,而不是串行端口/ TTY。 OSX具有用户空间HID事件API,可让您检查报告描述符并捕获HID报告,即事件。检查出IOHIDDeviceGetReportWithCallback。或者,HID transaction API可能更容易处理,因为它处理为您分析报告和描述符的混乱业务。