From 20a227cc53ca36307c9b9e8d1da3f49d3714b127 Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Thu, 21 Mar 2024 08:32:08 +0100 Subject: [PATCH] initial commit --- peaktech.py | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 peaktech.py diff --git a/peaktech.py b/peaktech.py new file mode 100644 index 0000000..3294c67 --- /dev/null +++ b/peaktech.py @@ -0,0 +1,125 @@ +from serial import Serial +from math import inf +import sys +import binascii + +def crap_to_number(crap): + number_str = "" + for elem in crap: + switch={ + 0xcf:"9", + 0xef:"8", + 0x8a:"7", + 0xe7:"6", + 0xc7:"5", + 0x4e:"4", + 0x8f:"3", + 0xad:"2", + 0x0a:"1", + 0xeb:"0" + } + number_str+=switch.get(elem, "-") + print(number_str) + + +def read_peaktech( port ): + try: + with Serial( port , 2400) as ser: + print(ser.name) + while True: + 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] + + crap_to_number([num1, num2, num3, num4]) + + 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") + + #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}" ) + except KeyboardInterrupt: + pass + except Exception as ex: + print( "Exception:", ex ) + +if __name__ == "__main__": + port = "/dev/ttyUSB0" + + if len(sys.argv) > 1: + port = sys.argv[1] + + read_peaktech( port )