from serial import Serial from math import inf import sys import binascii def crap_to_number(crap): number_str = "" if(crap[0]&0x10): number_str+="-" crap[0] &= 0xEF for elem in crap: if(elem & 0x10 != 0): number_str+="." switch={ 0xcf:"9", 0xef:"8", 0x8a:"7", 0xe7:"6", 0xc7:"5", 0x4e:"4", 0x8f:"3", 0xad:"2", 0x0a:"1", 0xeb:"0", 0x61:"L", 0x00:" " } number_str+=switch.get(elem&0xEF, "-") return number_str def read_peaktech( ser ): line = ser.read_until( b'\xf1' ) #print(binascii.hexlify(line)) good=1 if(len(line) == 15): for i in range(0, 0xf): if i+1 != ((line[i]&0xF0)>>4): good=0 else: good=0 bytearr = bytearray(line) if(good==1): for i in range(0, 0xf): bytearr[i] = (bytearr[i] & 0x0F) print(binascii.hexlify(bytearr)) num1 = (bytearr[1]<<4) | bytearr[2] num2 = (bytearr[3]<<4) | bytearr[4] num3 = (bytearr[5]<<4) | bytearr[6] num4 = (bytearr[7]<<4) | bytearr[8] if line[0:2] == b'\x1C\x20': print("Ohm") if line[0:2] == b'\x18\x20': print("beep") if line[0:1] == b'\x1e': print("V DC") if line[0:1] == b'\x1d': print("V AC") if line[0:2] == b'\x1c\x2e': print("Hz") if line[0:2] == b'\x18\x2e': print("Temp") return crap_to_number([num1, num2, num3, num4]) #if (len(line) == 14) and (line[5] == 0x20): # try: # value = float( line[0:5].decode("ascii") ) # if line[6] == 0x31: # value /= 10 # elif line[6] == 0x32: # value /= 100 # elif line[6] == 0x33: # value /= 1000 # elif line[6] == 0x34: # value /= 10000 # except: # value = inf # if (line[9] & 0x80) != 0: # prefix = "µ" # elif (line[9] & 0x40) != 0: # prefix = "m" # elif (line[9] & 0x20) != 0: # prefix = "k" # elif (line[9] & 0x20) != 0: # prefix = "M" # else: # prefix = "" # if (line[10] & 0x80) != 0: # unit = "V" # elif (line[10] & 0x40) != 0: # unit = "A" # elif (line[10] & 0x20) != 0: # unit = "Ω" # elif (line[10] & 0x10) != 0: # unit = "hFE" # elif (line[10] & 0x08) != 0: # unit = "Hz" # elif (line[10] & 0x04) != 0: # unit = "F" # elif (line[10] & 0x02) != 0: # unit = "℃" # elif (line[10] & 0x01) != 0: # unit = "℉" # else: # unit = "" # print( f"{value}{prefix}{unit}" ) if __name__ == "__main__": port = "/dev/ttyUSB0" if len(sys.argv) > 1: port = sys.argv[1] try: with Serial( port , 2400) as ser: print(ser.name) while True: print(read_peaktech(ser)) except KeyboardInterrupt: pass except Exception as ex: print( "Exception:", ex )