mirror of
https://github.com/uhi22/pyPLC.git
synced 2024-11-10 01:05:42 +00:00
hardwareInterface integrated into Pev
This commit is contained in:
parent
10ff8176bf
commit
658e022e2e
3 changed files with 80 additions and 24 deletions
|
@ -204,8 +204,8 @@ class fsmPev():
|
||||||
# (B) The charger finished to tell the charge parameters.
|
# (B) The charger finished to tell the charge parameters.
|
||||||
if (strConverterResult.find('"EVSEProcessing": "Finished"')>0):
|
if (strConverterResult.find('"EVSEProcessing": "Finished"')>0):
|
||||||
self.addToTrace("It is Finished. Will change to state C and send CableCheckReq.")
|
self.addToTrace("It is Finished. Will change to state C and send CableCheckReq.")
|
||||||
# todo: pull the CP line to state C here.
|
# pull the CP line to state C here:
|
||||||
# self.hardwareInterface.changeToStateC()
|
self.hardwareInterface.setStateC()
|
||||||
msg = addV2GTPHeader(exiEncode("EDF_"+self.sessionId)) # EDF for Encode, Din, CableCheck
|
msg = addV2GTPHeader(exiEncode("EDF_"+self.sessionId)) # EDF for Encode, Din, CableCheck
|
||||||
self.addToTrace("responding " + prettyHexMessage(msg))
|
self.addToTrace("responding " + prettyHexMessage(msg))
|
||||||
self.Tcp.transmit(msg)
|
self.Tcp.transmit(msg)
|
||||||
|
@ -320,15 +320,17 @@ class fsmPev():
|
||||||
def reInit(self):
|
def reInit(self):
|
||||||
self.addToTrace("re-initializing fsmPev")
|
self.addToTrace("re-initializing fsmPev")
|
||||||
self.Tcp.disconnect()
|
self.Tcp.disconnect()
|
||||||
|
self.hardwareInterface.setStateB()
|
||||||
self.state = stateConnecting
|
self.state = stateConnecting
|
||||||
self.cyclesInState = 0
|
self.cyclesInState = 0
|
||||||
self.rxData = []
|
self.rxData = []
|
||||||
|
|
||||||
def __init__(self, addressManager, callbackAddToTrace):
|
def __init__(self, addressManager, callbackAddToTrace, hardwareInterface):
|
||||||
self.callbackAddToTrace = callbackAddToTrace
|
self.callbackAddToTrace = callbackAddToTrace
|
||||||
self.addToTrace("initializing fsmPev")
|
self.addToTrace("initializing fsmPev")
|
||||||
self.Tcp = pyPlcTcpSocket.pyPlcTcpClientSocket(self.callbackAddToTrace)
|
self.Tcp = pyPlcTcpSocket.pyPlcTcpClientSocket(self.callbackAddToTrace)
|
||||||
self.addressManager = addressManager
|
self.addressManager = addressManager
|
||||||
|
self.hardwareInterface = hardwareInterface
|
||||||
self.state = stateNotYetInitialized
|
self.state = stateNotYetInitialized
|
||||||
self.sessionId = "DEAD55AADEAD55AA"
|
self.sessionId = "DEAD55AADEAD55AA"
|
||||||
self.cyclesInState = 0
|
self.cyclesInState = 0
|
||||||
|
|
|
@ -10,24 +10,75 @@ import serial # the pyserial
|
||||||
from serial.tools.list_ports import comports
|
from serial.tools.list_ports import comports
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
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.")
|
||||||
|
self.outvalue = 0
|
||||||
|
|
||||||
|
def setStateC(self):
|
||||||
|
self.addToTrace("Setting CP line into state C.")
|
||||||
|
self.outvalue = 1
|
||||||
|
|
||||||
|
def __init__(self, callbackAddToTrace=None):
|
||||||
|
self.callbackAddToTrace = callbackAddToTrace
|
||||||
|
self.loopcounter = 0
|
||||||
|
self.outvalue = 0
|
||||||
|
self.findSerialPort()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if (self.isInterfaceOk):
|
||||||
|
self.ser.close()
|
||||||
|
|
||||||
|
def mainfunction(self):
|
||||||
|
self.loopcounter+=1
|
||||||
|
if (self.isInterfaceOk):
|
||||||
|
if (self.loopcounter>15):
|
||||||
|
self.loopcounter=0
|
||||||
|
# self.ser.write(b'hello world\n')
|
||||||
|
self.ser.write(bytes("out"+str(self.outvalue)+"\n", "utf-8"))
|
||||||
|
s = self.ser.read(100)
|
||||||
|
if (len(s)>0):
|
||||||
|
self.addToTrace(str(len(s)) + " bytes received: " + str(s, 'utf-8').strip())
|
||||||
|
|
||||||
|
def myPrintfunction(s):
|
||||||
|
print("myprint " + s)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
nFail=0
|
|
||||||
print("Testing hardwareInterface...")
|
print("Testing hardwareInterface...")
|
||||||
print('Available ports:')
|
hw = hardwareInterface(myPrintfunction)
|
||||||
ports = []
|
for i in range(0, 300):
|
||||||
for n, (port, desc, hwid) in enumerate(sorted(comports()), 1):
|
hw.mainfunction()
|
||||||
print('{:2}: {:20} {!r}'.format(n, port, desc))
|
if (i==100):
|
||||||
ports.append(port)
|
hw.setStateC()
|
||||||
if (len(ports)<1):
|
if (i==200):
|
||||||
print("no ports, we cannot test anything.")
|
hw.setStateB()
|
||||||
exit()
|
if (i==250):
|
||||||
print("ok, we take the first port, " + ports[0])
|
hw.setStateC()
|
||||||
ser = serial.Serial(ports[0], 19200, timeout=0)
|
if (i==280):
|
||||||
for i in range(0, 5):
|
hw.setStateB()
|
||||||
ser.write(b'hello world\n')
|
sleep(0.03)
|
||||||
sleep(0.5)
|
hw.close()
|
||||||
s = ser.read(100)
|
|
||||||
if (len(s)>0):
|
|
||||||
print(str(len(s)) + " bytes received: " + str(s, 'utf-8'))
|
|
||||||
ser.close()
|
|
||||||
print("finished.")
|
print("finished.")
|
|
@ -13,6 +13,7 @@ from pyPlcModes import *
|
||||||
import addressManager
|
import addressManager
|
||||||
import time
|
import time
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import hardwareInterface
|
||||||
|
|
||||||
|
|
||||||
class pyPlcWorker():
|
class pyPlcWorker():
|
||||||
|
@ -28,6 +29,7 @@ class pyPlcWorker():
|
||||||
self.oldAvlnStatus = 0
|
self.oldAvlnStatus = 0
|
||||||
self.isSimulationMode = isSimulationMode
|
self.isSimulationMode = isSimulationMode
|
||||||
self.hp = pyPlcHomeplug.pyPlcHomeplug(self.workerAddToTrace, self.callbackShowStatus, self.mode, self.addressManager, self.callbackReadyForTcp, self.isSimulationMode)
|
self.hp = pyPlcHomeplug.pyPlcHomeplug(self.workerAddToTrace, self.callbackShowStatus, self.mode, self.addressManager, self.callbackReadyForTcp, self.isSimulationMode)
|
||||||
|
self.hardwareInterface = hardwareInterface.hardwareInterface(self.workerAddToTrace)
|
||||||
self.hp.printToUdp("pyPlcWorker init")
|
self.hp.printToUdp("pyPlcWorker init")
|
||||||
# Find out the version number, using git.
|
# Find out the version number, using git.
|
||||||
# see https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script
|
# see https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script
|
||||||
|
@ -39,7 +41,7 @@ class pyPlcWorker():
|
||||||
if (self.mode == C_EVSE_MODE):
|
if (self.mode == C_EVSE_MODE):
|
||||||
self.evse = fsmEvse.fsmEvse(self.workerAddToTrace)
|
self.evse = fsmEvse.fsmEvse(self.workerAddToTrace)
|
||||||
if (self.mode == C_PEV_MODE):
|
if (self.mode == C_PEV_MODE):
|
||||||
self.pev = fsmPev.fsmPev(self.addressManager, self.workerAddToTrace)
|
self.pev = fsmPev.fsmPev(self.addressManager, self.workerAddToTrace, self.hardwareInterface)
|
||||||
|
|
||||||
def workerAddToTrace(self, s):
|
def workerAddToTrace(self, s):
|
||||||
# The central logging function. All logging messages from the different parts of the project
|
# The central logging function. All logging messages from the different parts of the project
|
||||||
|
@ -69,7 +71,8 @@ class pyPlcWorker():
|
||||||
def mainfunction(self):
|
def mainfunction(self):
|
||||||
self.nMainFunctionCalls+=1
|
self.nMainFunctionCalls+=1
|
||||||
#self.showStatus("pyPlcWorker loop " + str(self.nMainFunctionCalls))
|
#self.showStatus("pyPlcWorker loop " + str(self.nMainFunctionCalls))
|
||||||
self.hp.mainfunction() # call the lower-level worker
|
self.hp.mainfunction() # call the lower-level workers
|
||||||
|
self.hardwareInterface.mainfunction()
|
||||||
if (self.mode == C_EVSE_MODE):
|
if (self.mode == C_EVSE_MODE):
|
||||||
self.evse.mainfunction() # call the evse state machine
|
self.evse.mainfunction() # call the evse state machine
|
||||||
if (self.mode == C_PEV_MODE):
|
if (self.mode == C_PEV_MODE):
|
||||||
|
@ -86,7 +89,7 @@ class pyPlcWorker():
|
||||||
self.hp.enterPevMode()
|
self.hp.enterPevMode()
|
||||||
if (not hasattr(self, 'pev')):
|
if (not hasattr(self, 'pev')):
|
||||||
print("creating pev")
|
print("creating pev")
|
||||||
self.pev = fsmPev.fsmPev(self.addressManager, self.workerAddToTrace)
|
self.pev = fsmPev.fsmPev(self.addressManager, self.workerAddToTrace, self.hardwareInterface)
|
||||||
self.pev.reInit()
|
self.pev.reInit()
|
||||||
if (strAction == "E"):
|
if (strAction == "E"):
|
||||||
print("switching to EVSE mode")
|
print("switching to EVSE mode")
|
||||||
|
|
Loading…
Reference in a new issue