Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import math
- import serial
- import binascii
- import subprocess
- import time
- from bitstring import BitArray
- tags = {}
- com_port = '/dev/ttyUSB0'
- baud_rate = 115200
- ser = serial.Serial(com_port, baud_rate, timeout=1)
- def checksum(data):
- cSum = 0x00
- for byte in data:
- cSum += byte
- return (((~cSum) + 1) + (1 << 8)) % (1 << 8)
- def send(cmd, data=None):
- msg = [0xA0, 0x00, 0x01, cmd]
- if data:
- msg.extend(data)
- msg[1] = len(msg) - 1
- msg.append(checksum(msg))
- ser.write(msg)
- def ean_checksum(data):
- even = False
- cSum = 0
- for i in map(int,reversed(str(data))):
- if even:
- cSum += i
- even = False
- else:
- cSum += i * 3
- even = True
- return (int(math.ceil(cSum / 10.0)) * 10) - cSum
- send(0x76,data=[0x14])
- time.sleep(1)
- ans = ser.read(ser.read(2)[1])
- send(0x7a, data=[0x00])
- ans = ser.read(ser.read(2)[1])
- send(0x89, data=[0xff])
- timer = time.time()
- while True:
- try:
- ans = BitArray(ser.read(ser.read(2)[1])[5:])
- if ans.hex[:2] == '30':
- filt = BitArray(ans[8:11])
- part = BitArray(ans[11:14])
- ans = ans[14:]
- if part.uint == 0:
- company_len = 40
- indicator_len = 4
- elif part.uint == 1:
- company_len = 37
- indicator_len = 7
- elif part.uint == 2:
- company_len = 34
- indicator_len = 10
- elif part.uint == 3:
- company_len = 30
- indicator_len = 14
- elif part.uint == 4:
- company_len = 27
- indicator_len = 17
- elif part.uint == 5:
- company_len = 24
- indicator_len = 20
- elif part.uint == 6:
- company_len = 20
- indicator_len = 24
- company = BitArray(ans[:company_len])
- indicator = BitArray(ans[company_len:company_len+indicator_len])
- #serNum = BitArray(ans[company_len+indicator_len:company_len+indicator_len+38])
- barcode = (company.uint * 100000 + indicator.uint) * 10 + ean_checksum(company.uint * 100000 + indicator.uint)
- #if ans[-16:][:8].uint <= 55:
- # print(str(barcode), end=' - ')
- # print(ans[-16:][:8].uint)
- # if barcode not in tags:
- # tags[barcode] = [int(time.time()), 0]
- # else:
- # tags[barcode][1] += 1
- # for tag in tags.keys():
- # if int(time.time()) - tags[tag][0] <= 3:
- # if tags[tag][1] >= 3:
- # subprocess.call(["xdotool", "type", str(barcode)])
- # subprocess.call(["xdotool", "key", "Return"])
- # print (tag, end=' - ')
- # print(int(time.time()))
- # del tags[tag]
- # else:
- # del tags[tag]
- subprocess.call(["xdotool", "type", str(barcode)])
- subprocess.call(["xdotool", "key", "Return"])
- # print(str(barcode) + '*', end=' - ')
- # print(ans[-16:][:8].uint)
- except Exception as e:
- #print(e)
- if time.time() - timer > 3600:
- send(0x70)
- timer = time.time()
- send(0x89, data=[0xff])
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement