Skip to content

Instantly share code, notes, and snippets.

@jayliew
Created March 22, 2018 22:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jayliew/d3da282cb8fef44ddcb0d34227c9891e to your computer and use it in GitHub Desktop.
Save jayliew/d3da282cb8fef44ddcb0d34227c9891e to your computer and use it in GitHub Desktop.
USB barcode scanner read in Python on Raspberry Pi Raspbian
# Props to user brechmos for sharing the code here: https://www.raspberrypi.org/forums/viewtopic.php?t=55100
# Tested with the following barcode scanner
# macbook# ioreg -p IOUSB
# <snip>
# | +-o WIT 122-UFS V2.03@14200000 <class AppleUSBDevice, id 0x10000c3c4, registered, matched, active, busy 0 (6 ms), retain 14>
# WIT 122-UFS V2.03:
# Product ID: 0x1010
# Vendor ID: 0x05fe (CHIC TECHNOLOGY CORP)
# Version: 0.01
# Speed: Up to 12 Mb/sec
# Manufacturer: WIT Electron Company
# Location ID: 0x14200000 / 4
# Current Available (mA): 500
# Current Required (mA): 500
# Extra Operating Current (mA): 0
import sys
hid = { 4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12: 'i', 13: 'j', 14: 'k', 15: 'l', 16: 'm', 17: 'n', 18: 'o', 19: 'p', 20: 'q', 21: 'r', 22: 's', 23: 't', 24: 'u', 25: 'v', 26: 'w', 27: 'x', 28: 'y', 29: 'z', 30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7', 37: '8', 38: '9', 39: '0', 44: ' ', 45: '-', 46: '=', 47: '[', 48: ']', 49: '\\', 51: ';' , 52: '\'', 53: '~', 54: ',', 55: '.', 56: '/' }
hid2 = { 4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'E', 9: 'F', 10: 'G', 11: 'H', 12: 'I', 13: 'J', 14: 'K', 15: 'L', 16: 'M', 17: 'N', 18: 'O', 19: 'P', 20: 'Q', 21: 'R', 22: 'S', 23: 'T', 24: 'U', 25: 'V', 26: 'W', 27: 'X', 28: 'Y', 29: 'Z', 30: '!', 31: '@', 32: '#', 33: '$', 34: '%', 35: '^', 36: '&', 37: '*', 38: '(', 39: ')', 44: ' ', 45: '_', 46: '+', 47: '{', 48: '}', 49: '|', 51: ':' , 52: '"', 53: '~', 54: '<', 55: '>', 56: '?' }
fp = open('/dev/hidraw0', 'rb')
ss = ""
shift = False
done = False
while not done:
## Get the character from the HID
buffer = fp.read(8)
for c in buffer:
if ord(c) > 0:
## 40 is carriage return which signifies
## we are done looking for characters
if int(ord(c)) == 40:
done = True
break;
## If we are shifted then we have to
## use the hid2 characters.
if shift:
## If it is a '2' then it is the shift key
if int(ord(c)) == 2 :
shift = True
## if not a 2 then lookup the mapping
else:
ss += hid2[ int(ord(c)) ]
shift = False
## If we are not shifted then use
## the hid characters
else:
## If it is a '2' then it is the shift key
if int(ord(c)) == 2 :
shift = True
## if not a 2 then lookup the mapping
else:
ss += hid[ int(ord(c)) ]
print ss
@romeoGG
Copy link

romeoGG commented Dec 16, 2019

hi,

I'm trying this on my raspberry pi. But I get an error 13 permission denied.

What I'm I doing wrong? Or what should I do to make it work?

Thanks

@jayliew
Copy link
Author

jayliew commented Dec 16, 2019

A permission issue doesn't sound like an issue with the code. Check if your user has permission to execute (sudo and chmod?)

@romeoGG
Copy link

romeoGG commented Dec 16, 2019

I know it does not have anything to do with the code.
But how can I change the permissions on my raspberry pi?

Copy link

ghost commented Feb 9, 2020

@romeoGG run it as sudo

@habib700
Copy link

try executing below command in terminal:

sudo python Filename.py

Replace python with python3 if using Python 3 or above version.
Filename.py with your filename

@habib700
Copy link

I know it does not have anything to do with the code.
But how can I change the permissions on my raspberry pi?

first know the your hid device using below command:
ls -l /dev/hidraw*

than type following to change permission
sudo 777 /dev/hidraw*

replace * with your device number, like /dev/hidraw0

@JeanCarlosChavarriaHughes

sudo chown $USER: /dev/hidraw0
This command will assign your user as the owner of the device. However it will be reverted once the device is rebooted.
This is supposing the device is /dev/hidraw0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment