{"id":390,"date":"2015-03-01T15:33:21","date_gmt":"2015-03-01T14:33:21","guid":{"rendered":"http:\/\/heppg.de\/ikg\/wordpress\/?p=390"},"modified":"2015-03-01T21:15:55","modified_gmt":"2015-03-01T20:15:55","slug":"barcode-scanner-for-scratch","status":"publish","type":"post","link":"https:\/\/heppg.de\/ikg\/wordpress\/?p=390","title":{"rendered":"Barcode scanner for scratch"},"content":{"rendered":"<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/03\/barcode.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-391\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/03\/barcode.jpg\" alt=\"barcode\" width=\"400\" height=\"195\" \/><\/a><\/p>\n<p>Connecting a barcode scanner to scratch<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<h3>Install pyusb<\/h3>\n<p>Here I found a problem. The usual install did not workwith \u201epip install pyusb\u201c. This resulted in a &#8216;backend not found&#8217; exception and \u201eundefined symbol: libusb_strerror\u201c<br \/>\nWhen I tried to download walac-pyusb-50b1490 from <a href=\"https:\/\/github.com\/walac\/pyusb\">https:\/\/github.com\/walac\/pyusb<\/a>, this worked, but I had to uninstall the pip-installed code first \u201esudo pip uninstall pyusb\u201c.<\/p>\n<p>The needed backend packages are already available in raspbian, you can check this with<\/p>\n<pre>apt-cache pkgnames | grep libusb<\/pre>\n<p>You should find libusb-1.0-0 in the list.<\/p>\n<h3>Setup Scanner for USB and CR-suffix<\/h3>\n<p>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 &#8216;start programming&#8217; code, scan the appropriate setup code and exit programming with an &#8216;end programming&#8217; code.<\/p>\n<p>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.<\/p>\n<h3>Configure idVendor and idProduct<\/h3>\n<p>Another preparation is the configuration of idVendor and idProduct in the adapter&#8217;s config file. Use the utility enum.py to list the devices available<\/p>\n<pre>cd ~\/scratchClient\r\npython tools\/usb\/enum.py<\/pre>\n<p>Here the output for my scanner<\/p>\n<pre>DEVICE ID 0c2e:0200 on Bus 001 Address 007 =================\r\nbLength : 0x12 (18 bytes)\r\nbDescriptorType : 0x1 Device\r\nbcdUSB : 0x110 USB 1.1\r\nbDeviceClass : 0x0 Specified at interface\r\nbDeviceSubClass : 0x0\r\nbDeviceProtocol : 0x0\r\nbMaxPacketSize0 : 0x8 (8 bytes)\r\n<b><span style=\"color: #0000ff;\">idVendor<\/span> : 0x0c2e\r\n<span style=\"color: #0000ff;\">idProduct<\/span> : 0x0200\r\n<\/b>bcdDevice : 0x5881 Device 88.81\r\niManufacturer : 0x1 Honeywell Scanning and Mobility\r\niProduct : 0x2 Honeywell Scanning and Mobility Scanner\r\niSerialNumber : 0x0\r\nbNumConfigurations : 0x1\r\nCONFIGURATION 1: 300 mA==================================\r\nbLength : 0x9 (9 bytes)\r\nbDescriptorType : 0x2 Configuration\r\nwTotalLength : 0x22 (34 bytes)\r\nbNumInterfaces : 0x1\r\nbConfigurationValue : 0x1\r\n<span style=\"color: #0000ff;\">i<\/span><b><span style=\"color: #0000ff;\">Configuration<\/span> : 0x3 HID Keyboard\r\n<\/b>bmAttributes : 0x80 Bus Powered\r\nbMaxPower : 0x96 (300 mA)\r\nINTERFACE 0: Human Interface Device ====================\r\nbLength : 0x9 (9 bytes)\r\nbDescriptorType : 0x4 Interface\r\nbInterfaceNumber : 0x0\r\nbAlternateSetting : 0x0\r\nbNumEndpoints : 0x1\r\nbInterfaceClass : 0x3 Human Interface Device\r\nbInterfaceSubClass : 0x1\r\nbInterfaceProtocol : 0x1\r\niInterface : 0x0\r\nENDPOINT 0x81: Interrupt IN ==========================\r\nbLength : 0x7 (7 bytes)\r\nbDescriptorType : 0x5 Endpoint\r\nbEndpointAddress : 0x81 IN\r\nbmAttributes : 0x3 Interrupt\r\nwMaxPacketSize : 0x8 (8 bytes)\r\nbInterval : 0xa<\/pre>\n<p>What you also should check is the iConfiguration to be a HID Keyboard.<\/p>\n<p>For verification open a text editor like leafpad: when scanning a code, this should be entered into the editor as a text string.<\/p>\n<p>Edit config\/config_barcode.xml and adjust the vendor\/product id there.<\/p>\n<p>In my environment, I use a powered USB hub to connect the scanner.<\/p>\n<h3>Start scratchClient<\/h3>\n<pre>cd ~\/scratchClient\r\nsudo python src\/scratchClient.py -c config_barcode<\/pre>\n<p>The file\u00a0config\/config_barcode.xml is a starting point for a project. Add other adapters as needed.<br \/>\nWhen scanning barcodes, these are send to scratch. Do not forget to enable remote sensor connections.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/03\/scratch_barcode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-397\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/03\/scratch_barcode.png\" alt=\"scratch_barcode\" width=\"363\" height=\"239\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,3],"tags":[],"class_list":["post-390","post","type-post","status-publish","format-standard","hentry","category-scratch","category-scratchclient"],"_links":{"self":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=390"}],"version-history":[{"count":4,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/390\/revisions"}],"predecessor-version":[{"id":398,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/390\/revisions\/398"}],"wp:attachment":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}