2022-12-02 20:25:20 +00:00
|
|
|
|
|
|
|
# For serial (including USB-to-serial) interfaces:
|
|
|
|
# https://pyserial.readthedocs.io/en/latest/pyserial.html
|
|
|
|
# Install pyserial library:
|
|
|
|
# python -m pip install pyserial
|
|
|
|
# List ports:
|
|
|
|
# python -m serial.tools.list_ports
|
|
|
|
|
|
|
|
import serial # the pyserial
|
|
|
|
from serial.tools.list_ports import comports
|
|
|
|
from time import sleep
|
|
|
|
|
2022-12-06 17:35:17 +00:00
|
|
|
class hardwareInterface():
|
|
|
|
def findSerialPort(self):
|
|
|
|
ports = []
|
|
|
|
self.addToTrace('Available serial ports:')
|
|
|
|
for n, (port, desc, hwid) in enumerate(sorted(comports()), 1):
|
|
|
|
self.addToTrace('{:2}: {:20} {!r}'.format(n, port, desc))
|
|
|
|
ports.append(port)
|
|
|
|
if (len(ports)<1):
|
|
|
|
self.addToTrace("ERROR: No serial ports found. No hardware interaction possible.")
|
|
|
|
self.ser = None
|
|
|
|
self.isInterfaceOk = False
|
|
|
|
else:
|
|
|
|
self.addToTrace("ok, we take the first port, " + ports[0])
|
|
|
|
try:
|
|
|
|
self.ser = serial.Serial(ports[0], 19200, timeout=0)
|
|
|
|
self.isInterfaceOk = True
|
|
|
|
except:
|
|
|
|
self.addToTrace("ERROR: Could not open serial port.")
|
|
|
|
self.ser = None
|
|
|
|
self.isInterfaceOk = False
|
|
|
|
|
|
|
|
def addToTrace(self, s):
|
|
|
|
self.callbackAddToTrace("[HARDWAREINTERFACE] " + s)
|
|
|
|
|
|
|
|
def setStateB(self):
|
|
|
|
self.addToTrace("Setting CP line into state B.")
|
2022-12-08 23:22:18 +00:00
|
|
|
self.outvalue &= ~1
|
2022-12-06 17:35:17 +00:00
|
|
|
|
|
|
|
def setStateC(self):
|
|
|
|
self.addToTrace("Setting CP line into state C.")
|
2022-12-08 23:22:18 +00:00
|
|
|
self.outvalue |= 1
|
|
|
|
|
|
|
|
def setPowerRelayOn(self):
|
|
|
|
self.addToTrace("Switching PowerRelay ON.")
|
|
|
|
self.outvalue |= 2
|
|
|
|
|
|
|
|
def setPowerRelayOff(self):
|
|
|
|
self.addToTrace("Switching PowerRelay OFF.")
|
|
|
|
self.outvalue &= ~2
|
2022-12-16 12:03:36 +00:00
|
|
|
|
|
|
|
def setRelay2On(self):
|
|
|
|
self.addToTrace("Switching PowerRelay ON.")
|
|
|
|
self.outvalue |= 4
|
|
|
|
|
|
|
|
def setRelay2Off(self):
|
|
|
|
self.addToTrace("Switching PowerRelay OFF.")
|
|
|
|
self.outvalue &= ~4
|
2022-12-08 23:22:18 +00:00
|
|
|
|
|
|
|
def getInletVoltage(self):
|
|
|
|
#todo: get real measured voltage from the inlet
|
2022-12-10 16:08:51 +00:00
|
|
|
self.inletVoltage = self.simulatedInletVoltage
|
2022-12-08 23:22:18 +00:00
|
|
|
return self.inletVoltage
|
|
|
|
|
|
|
|
def getAccuVoltage(self):
|
|
|
|
#todo: get real measured voltage from the accu
|
|
|
|
self.accuVoltage = 230
|
|
|
|
return self.accuVoltage
|
|
|
|
|
|
|
|
def getAccuMaxCurrent(self):
|
|
|
|
#todo: get max charging current from the BMS
|
|
|
|
self.accuMaxCurrent = 10
|
|
|
|
return self.accuMaxCurrent
|
|
|
|
|
2022-12-09 13:23:32 +00:00
|
|
|
def getAccuMaxVoltage(self):
|
|
|
|
#todo: get max charging voltage from the BMS
|
|
|
|
self.accuMaxVoltage = 230
|
|
|
|
return self.accuMaxVoltage
|
|
|
|
|
2022-12-08 23:22:18 +00:00
|
|
|
def getIsAccuFull(self):
|
|
|
|
#todo: get "full" indication from the BMS
|
|
|
|
self.IsAccuFull = (self.simulatedSoc >= 98)
|
|
|
|
return self.IsAccuFull
|
2022-12-09 13:23:32 +00:00
|
|
|
|
|
|
|
def getSoc(self):
|
|
|
|
#todo: get SOC from the BMS
|
|
|
|
return self.simulatedSoc
|
2022-12-08 23:22:18 +00:00
|
|
|
|
2022-12-06 17:35:17 +00:00
|
|
|
|
2022-12-19 13:48:23 +00:00
|
|
|
def __init__(self, callbackAddToTrace=None, callbackShowStatus=None):
|
2022-12-06 17:35:17 +00:00
|
|
|
self.callbackAddToTrace = callbackAddToTrace
|
2022-12-19 13:48:23 +00:00
|
|
|
self.callbackShowStatus = callbackShowStatus
|
2022-12-06 17:35:17 +00:00
|
|
|
self.loopcounter = 0
|
|
|
|
self.outvalue = 0
|
2022-12-08 23:22:18 +00:00
|
|
|
self.simulatedSoc = 20.0 # percent
|
|
|
|
self.inletVoltage = 0.0 # volts
|
2022-12-19 13:48:23 +00:00
|
|
|
self.rxbuffer = ""
|
2022-12-06 17:35:17 +00:00
|
|
|
self.findSerialPort()
|
2022-12-10 16:08:51 +00:00
|
|
|
|
|
|
|
def resetSimulation(self):
|
|
|
|
self.simulatedInletVoltage = 0.0 # volts
|
|
|
|
self.simulatedSoc = 20.0 # percent
|
|
|
|
|
|
|
|
def simulatePreCharge(self):
|
|
|
|
if (self.simulatedInletVoltage<230):
|
|
|
|
self.simulatedInletVoltage = self.simulatedInletVoltage + 1.0 # simulate increasing voltage during PreCharge
|
2022-12-06 17:35:17 +00:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if (self.isInterfaceOk):
|
|
|
|
self.ser.close()
|
2022-12-19 13:48:23 +00:00
|
|
|
|
|
|
|
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
|
2022-12-06 17:35:17 +00:00
|
|
|
|
|
|
|
def mainfunction(self):
|
2022-12-08 23:22:18 +00:00
|
|
|
if (self.simulatedSoc<100):
|
|
|
|
if ((self.outvalue & 2)!=0):
|
2022-12-12 19:54:17 +00:00
|
|
|
# while the relay is closed, simulate increasing SOC
|
|
|
|
self.simulatedSoc = self.simulatedSoc + 0.05
|
2022-12-06 17:35:17 +00:00
|
|
|
self.loopcounter+=1
|
|
|
|
if (self.isInterfaceOk):
|
|
|
|
if (self.loopcounter>15):
|
|
|
|
self.loopcounter=0
|
|
|
|
# self.ser.write(b'hello world\n')
|
2022-12-12 07:26:18 +00:00
|
|
|
s = "000" + str(self.outvalue)
|
|
|
|
self.ser.write(bytes("do"+s+"\n", "utf-8")) # set outputs of dieter, see https://github.com/uhi22/dieter
|
2022-12-06 17:35:17 +00:00
|
|
|
s = self.ser.read(100)
|
|
|
|
if (len(s)>0):
|
2022-12-19 13:48:23 +00:00
|
|
|
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)
|
2022-12-06 17:35:17 +00:00
|
|
|
|
|
|
|
def myPrintfunction(s):
|
|
|
|
print("myprint " + s)
|
|
|
|
|
2022-12-02 20:25:20 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
print("Testing hardwareInterface...")
|
2022-12-06 17:35:17 +00:00
|
|
|
hw = hardwareInterface(myPrintfunction)
|
2022-12-16 12:03:36 +00:00
|
|
|
for i in range(0, 350):
|
2022-12-06 17:35:17 +00:00
|
|
|
hw.mainfunction()
|
2022-12-16 12:03:36 +00:00
|
|
|
if (i==50):
|
|
|
|
hw.setStateC()
|
2022-12-06 17:35:17 +00:00
|
|
|
if (i==100):
|
2022-12-16 12:03:36 +00:00
|
|
|
hw.setStateB()
|
|
|
|
if (i==150):
|
2022-12-06 17:35:17 +00:00
|
|
|
hw.setStateC()
|
2022-12-16 12:03:36 +00:00
|
|
|
hw.setPowerRelayOn()
|
2022-12-06 17:35:17 +00:00
|
|
|
if (i==200):
|
|
|
|
hw.setStateB()
|
2022-12-16 12:03:36 +00:00
|
|
|
hw.setPowerRelayOff()
|
2022-12-06 17:35:17 +00:00
|
|
|
if (i==250):
|
2022-12-16 12:03:36 +00:00
|
|
|
hw.setRelay2On()
|
|
|
|
if (i==300):
|
|
|
|
hw.setRelay2Off()
|
2022-12-06 17:35:17 +00:00
|
|
|
sleep(0.03)
|
|
|
|
hw.close()
|
2022-12-02 20:25:20 +00:00
|
|
|
print("finished.")
|