Connecting a barcode scanner to scratch
Some time ago I aquired a barcode scanner, which I use to manage a CD collection based on the EAN-code printed on the packages.
These scanners typically can send codes by emulating HID-class devices. This makes usage from programs quite easy, but you loose input when the program goes to background and other programs get the focus.
Currently I try to bring this code to raspberry pi, using python and pyusb library. This library allows access to usb devices and, very important, can grab devices to be used exclusively by one program. As a side result from this work, I added a barcode scanner adapter to scratchClient.
Install pyusb
Here I found a problem. The usual install did not workwith „pip install pyusb“. This resulted in a ‘backend not found’ exception and „undefined symbol: libusb_strerror“
When I tried to download walac-pyusb-50b1490 from https://github.com/walac/pyusb, this worked, but I had to uninstall the pip-installed code first „sudo pip uninstall pyusb“.
The needed backend packages are already available in raspbian, you can check this with
apt-cache pkgnames | grep libusb
You should find libusb-1.0-0 in the list.
Setup Scanner for USB and CR-suffix
A few preparations are needed with the scanner to enable HID mode. With my scanner I got a handbook with a huge amount of programming codes. You start programming with a ‘start programming’ code, scan the appropriate setup code and exit programming with an ‘end programming’ code.
For my sample, I have setup HID mode, and added exit code/suffix CR. This is needed to detect complete sequences. The adapter in scratchClient relies on this.
Configure idVendor and idProduct
Another preparation is the configuration of idVendor and idProduct in the adapter’s config file. Use the utility enum.py to list the devices available
cd ~/scratchClient python tools/usb/enum.py
Here the output for my scanner
DEVICE ID 0c2e:0200 on Bus 001 Address 007 ================= bLength : 0x12 (18 bytes) bDescriptorType : 0x1 Device bcdUSB : 0x110 USB 1.1 bDeviceClass : 0x0 Specified at interface bDeviceSubClass : 0x0 bDeviceProtocol : 0x0 bMaxPacketSize0 : 0x8 (8 bytes) idVendor : 0x0c2e idProduct : 0x0200 bcdDevice : 0x5881 Device 88.81 iManufacturer : 0x1 Honeywell Scanning and Mobility iProduct : 0x2 Honeywell Scanning and Mobility Scanner iSerialNumber : 0x0 bNumConfigurations : 0x1 CONFIGURATION 1: 300 mA================================== bLength : 0x9 (9 bytes) bDescriptorType : 0x2 Configuration wTotalLength : 0x22 (34 bytes) bNumInterfaces : 0x1 bConfigurationValue : 0x1 iConfiguration : 0x3 HID Keyboard bmAttributes : 0x80 Bus Powered bMaxPower : 0x96 (300 mA) INTERFACE 0: Human Interface Device ==================== bLength : 0x9 (9 bytes) bDescriptorType : 0x4 Interface bInterfaceNumber : 0x0 bAlternateSetting : 0x0 bNumEndpoints : 0x1 bInterfaceClass : 0x3 Human Interface Device bInterfaceSubClass : 0x1 bInterfaceProtocol : 0x1 iInterface : 0x0 ENDPOINT 0x81: Interrupt IN ========================== bLength : 0x7 (7 bytes) bDescriptorType : 0x5 Endpoint bEndpointAddress : 0x81 IN bmAttributes : 0x3 Interrupt wMaxPacketSize : 0x8 (8 bytes) bInterval : 0xa
What you also should check is the iConfiguration to be a HID Keyboard.
For verification open a text editor like leafpad: when scanning a code, this should be entered into the editor as a text string.
Edit config/config_barcode.xml and adjust the vendor/product id there.
In my environment, I use a powered USB hub to connect the scanner.
Start scratchClient
cd ~/scratchClient sudo python src/scratchClient.py -c config_barcode
The file config/config_barcode.xml is a starting point for a project. Add other adapters as needed.
When scanning barcodes, these are send to scratch. Do not forget to enable remote sensor connections.