Module | USB |
In: |
usb.c
lib/usb.rb |
USB module is a binding for libusb.
It needs appropriate privilege to access USB. For example, the process should be an member of plugdev group on Debin GNU/Linux (etch).
require 'usb' require 'pp' pp USB.devices #=> [#<USB::Device 001/001 0000:0000 Linux 2.6.17-2-486 uhci_hcd UHCI Host Controller 0000:00:1d.0 (Full speed Hub)>, #<USB::Device 002/001 0000:0000 Linux 2.6.17-2-486 uhci_hcd UHCI Host Controller 0000:00:1d.1 (Full speed Hub)>, #<USB::Device 003/001 0000:0000 Linux 2.6.17-2-486 uhci_hcd UHCI Host Controller 0000:00:1d.2 (Full speed Hub)>, #<USB::Device 004/001 0000:0000 Linux 2.6.17-2-486 ehci_hcd EHCI Host Controller 0000:00:1d.7 (Hi-speed Hub with single TT)>]
# find the device "004/001" in the above list. dev = USB.find_bus(4).find_device(1) p dev #=> #<USB::Device 004/001 0000:0000 Linux 2.6.17-2-486 ehci_hcd EHCI Host Controller 0000:00:1d.7 (Hi-speed Hub with single TT)>
dev.open {|handle| p handle } #=> #<USB::DevHandle:0xa7d94688>
searches devices by USB device class, subclass and protocol.
# find hubs. USB.each_device_by_class(USB::USB_CLASS_HUB) {|d| p d }' # find Full speed Hubs USB.each_device_by_class(USB::USB_CLASS_HUB, 0, 0) {|d| p d }' # find Hi-speed Hubs with single TT USB.each_device_by_class(USB::USB_CLASS_HUB, 0, 1) {|d| p d }' # find Hi-speed Hubs with multiple TT USB.each_device_by_class(USB::USB_CLASS_HUB, 0, 2) {|d| p d }'