Skip to content

Instantly share code, notes, and snippets.

@micahwalter
Created March 21, 2014 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micahwalter/9689732 to your computer and use it in GitHub Desktop.
Save micahwalter/9689732 to your computer and use it in GitHub Desktop.
import sys
import struct
import select
def parse_scanner_data(scanner_data):
upc_chars = []
for i in range(0, len(scanner_data), 16):
chunk = scanner_data[i:i+16]
# __ __ __ __ __ __ __ __ 01 00 __ 00 00 00 00 00
if chunk[8:10] != '\x01\x00' or chunk[11:] != '\x00\x00\x00\x00\x00':
continue
digit_int = struct.unpack('>h', chunk[9:11])[0]
upc_chars.append(str((digit_int - 1) % 10))
return ''.join(upc_chars)
f = open('/dev/hidraw0', 'rb')
while True:
print 'Waiting for scanner data'
scan_complete = False
scanner_data = ''
while True:
rlist, _wlist, _elist = select.select([f], [], [], 0.1)
if rlist != []:
new_data = ''
while not new_data.endswith('\x01\x00\x1c\x00\x01\x00\x00\x00'):
new_data = rlist[0].read(16)
scanner_data += new_data
[rlist[0].read(16) for i in range(4)]
scan_complete = True
if scan_complete:
break
barcode = parse_scanner_data(scanner_data)
print "Scanned barcode '{0}'".format(barcode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment