#!/usr/bin/env python3 """ Finding UHF RFID tags with a rubik/WYUAN RD905UW. """ import serial, crcmod, time #BAUDRATE = 115200 #BAUDRATE = 9600 BAUDRATE = 57600 class UHF: def __init__(self): self.ser = serial.Serial('/dev/ttyUSB0', BAUDRATE) def crc16(self, s): crc16 = crcmod.predefined.Crc('crc-16-mcrf4xx') crc16.update(s) r = crc16.digest() r = r[::-1] #print(repr(r)) #exit() #r = b'\xdb\x4b' return r def recv_with_crc16(self): count_byte = self.ser.read(1) count = ord(count_byte) s = self.ser.read(count) s, c = s[:-2], s[-2:] c2 = self.crc16(count_byte+s) #print('s', repr(s)) #print('c0', repr(c)) #print('c2', repr(c2)) if (c != c2): print('Checksum MISMATCH') return s pass def send_with_crc16(self, s): print(repr(s)) s = bytes([len(s)+2]) + s c = self.crc16(s) r = s + c #print(repr(r)) #exit() self.ser.write(r) pass def get_info(self): #self.send_with_crc16(b'\xff\x21') self.send_with_crc16(b'\x00\x01') #r = self.ser.read(5) r = self.recv_with_crc16() print(repr(r)) pass def get_info_loop(self): while True: self.get_info() time.sleep(0.05) # not really needed pass def run(self): #self.get_info() self.get_info_loop() def mainfunc(): uhf = UHF() uhf.run() def mainfunc_loop(): while True: try: mainfunc() except serial.serialutil.SerialException: pass if (__name__ == '__main__'): #mainfunc() mainfunc_loop()