From 69dc97ea0700b66f5380435b7a68c8fc0edae34c Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Sat, 23 Mar 2024 21:46:01 +0100 Subject: [PATCH] make units in string optional --- peaktech.py | 72 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/peaktech.py b/peaktech.py index f3f30e7..7f997ce 100644 --- a/peaktech.py +++ b/peaktech.py @@ -3,7 +3,7 @@ from math import inf import sys import binascii -def crap_to_number(num, unit, acdc): +def crap_to_number(num, unit, acdc, include_unit): number_str = "" if(num[0]&0x10): @@ -29,42 +29,44 @@ def crap_to_number(num, unit, acdc): } number_str+=switch.get(elem&0xEF, "-") - if (unit[0] & 0x1): - number_str+="µ" - if (unit[0] & 0x2): - number_str+="n" - if (unit[0] & 0x4): - number_str+="k" - if (unit[1] & 0x1): - number_str+="m" - if (unit[1] & 0x2): - number_str+="%" - if (unit[1] & 0x4): - number_str+="M" - if (unit[2] & 0x1): - number_str+="F" - if (unit[2] & 0x2): - number_str+="Ω" - if (unit[3] & 0x1): - number_str+="A" - if (unit[3] & 0x2): - number_str+="V" - if (unit[3] & 0x4): - number_str+="Hz" - if (unit[4] & 0x2): - number_str+="°C" - if (unit[4] & 0x1): - number_str+="°F" - - if(acdc&0xb == 0x0a): - number_str+=" DC" - if(acdc&0xb == 0x09): - number_str+=" AC" + + if(include_unit): + if (unit[0] & 0x1): + number_str+="µ" + if (unit[0] & 0x2): + number_str+="n" + if (unit[0] & 0x4): + number_str+="k" + if (unit[1] & 0x1): + number_str+="m" + if (unit[1] & 0x2): + number_str+="%" + if (unit[1] & 0x4): + number_str+="M" + if (unit[2] & 0x1): + number_str+="F" + if (unit[2] & 0x2): + number_str+="Ω" + if (unit[3] & 0x1): + number_str+="A" + if (unit[3] & 0x2): + number_str+="V" + if (unit[3] & 0x4): + number_str+="Hz" + if (unit[4] & 0x2): + number_str+="°C" + if (unit[4] & 0x1): + number_str+="°F" + + if(acdc&0xb == 0x0a): + number_str+=" DC" + if(acdc&0xb == 0x09): + number_str+=" AC" return number_str -def read_peaktech( ser ): +def read_peaktech(ser, include_unit=True): line = ser.read_until( b'\xf1' ) #print(binascii.hexlify(line)) @@ -117,7 +119,7 @@ def read_peaktech( ser ): #if line[0:2] == b'\x18\x2e': # print("Temp") - return crap_to_number(num, unit, acdc) + return crap_to_number(num, unit, acdc, include_unit) #if (len(line) == 14) and (line[5] == 0x20): # try: # value = float( line[0:5].decode("ascii") ) @@ -167,7 +169,7 @@ def read_peaktech( ser ): def read(): ser = Serial("/dev/ttyUSB0", 2400) - res = read_peaktech(ser) + res = read_peaktech(ser, False) try: res = int(res) except: