From 3da03c011eb3f4a4d902f0cc51cee83dc122a2c7 Mon Sep 17 00:00:00 2001 From: uhi22 Date: Mon, 19 Dec 2022 14:48:23 +0100 Subject: [PATCH] feature: show inlet voltage in the GUI --- hardwareInterface.py | 27 +++++++++++++++++++++++++-- pyPlc.py | 6 ++++++ pyPlcWorker.py | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/hardwareInterface.py b/hardwareInterface.py index 91215c5..866ab75 100644 --- a/hardwareInterface.py +++ b/hardwareInterface.py @@ -88,12 +88,14 @@ class hardwareInterface(): return self.simulatedSoc - def __init__(self, callbackAddToTrace=None): + def __init__(self, callbackAddToTrace=None, callbackShowStatus=None): self.callbackAddToTrace = callbackAddToTrace + self.callbackShowStatus = callbackShowStatus self.loopcounter = 0 self.outvalue = 0 self.simulatedSoc = 20.0 # percent self.inletVoltage = 0.0 # volts + self.rxbuffer = "" self.findSerialPort() def resetSimulation(self): @@ -107,6 +109,22 @@ class hardwareInterface(): def close(self): if (self.isInterfaceOk): self.ser.close() + + def evaluateReceivedData(self, s): + self.rxbuffer += s + x=self.rxbuffer.find("A0=") + if (x>=0): + s = self.rxbuffer[x+3:x+7] + if (len(s)==4): + try: + self.uInlet_V = int(s) / 1024.0 * 1.08 * (6250) / (4.7+4.7) + + self.callbackShowStatus(format(self.uInlet_V,".1f"), "uInlet") + except: + # keep last known value, if nothing new valid was received. + pass + #self.addToTrace("RX data ok " + s) + self.rxbuffer = self.rxbuffer[x+3:] # consume the receive buffer entry def mainfunction(self): if (self.simulatedSoc<100): @@ -122,7 +140,12 @@ class hardwareInterface(): self.ser.write(bytes("do"+s+"\n", "utf-8")) # set outputs of dieter, see https://github.com/uhi22/dieter s = self.ser.read(100) if (len(s)>0): - self.addToTrace(str(len(s)) + " bytes received: " + str(s, 'utf-8').strip()) + try: + s = str(s, 'utf-8').strip() + except: + s = "" # for the case we received corrupted data (not convertable as utf-8) + self.addToTrace(str(len(s)) + " bytes received: " + s) + self.evaluateReceivedData(s) def myPrintfunction(s): print("myprint " + s) diff --git a/pyPlc.py b/pyPlc.py index 9c9232e..8771578 100644 --- a/pyPlc.py +++ b/pyPlc.py @@ -36,6 +36,9 @@ def cbShowStatus(s, selection=""): if (selection == "pevmac"): lblPevMac['text']="PEV MAC " + s s="" + if (selection == "uInlet"): + lblUInlet['text']= "UInlet " + s + "V" + s="" if (len(s)>0): lblStatus['text']=s root.update() @@ -69,6 +72,9 @@ lblStatus = tk.Label(root, text="(Status)") lblStatus.pack() lblPevMac = tk.Label(root, text="(pev mac)") lblPevMac.pack() +lblUInlet = tk.Label(root, text="(U Inlet)") +lblUInlet.config(font=('Helvetica bold', 26)) +lblUInlet.pack() lblMode = tk.Label(root, text="(mode)") lblMode.pack() diff --git a/pyPlcWorker.py b/pyPlcWorker.py index ca8aab7..723ab9f 100644 --- a/pyPlcWorker.py +++ b/pyPlcWorker.py @@ -29,7 +29,7 @@ class pyPlcWorker(): self.oldAvlnStatus = 0 self.isSimulationMode = isSimulationMode self.hp = pyPlcHomeplug.pyPlcHomeplug(self.workerAddToTrace, self.callbackShowStatus, self.mode, self.addressManager, self.callbackReadyForTcp, self.isSimulationMode) - self.hardwareInterface = hardwareInterface.hardwareInterface(self.workerAddToTrace) + self.hardwareInterface = hardwareInterface.hardwareInterface(self.workerAddToTrace, self.callbackShowStatus) self.hp.printToUdp("pyPlcWorker init") # Find out the version number, using git. # see https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script