input working

This commit is contained in:
2021-08-23 16:24:40 +02:00
parent a37f4945d8
commit 17e5b6b012
6 changed files with 167 additions and 17 deletions

View File

@ -2,15 +2,20 @@ from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.exceptions import ModbusIOException
from time import sleep
import threading
from loguru import logger
MODBUS_CLIENT = '172.16.2.157'
MODBUS_REFRESH_PERIOD = 0.25
def modbusStart(config, processImage):
modbusThread = threading.Thread(target=modbusHandler, args=[config, processImage])
modbusThread.start()
def modbusHandler(config, processImage):
modbusClient = config["modbus"]["client"]
modbusRefreshPeriod = config["modbus"]["scanrate"]
def modbusHandler(processImage):
client = ModbusClient(MODBUS_CLIENT)
client = ModbusClient(modbusClient)
try:
client.connect()
@ -23,7 +28,7 @@ def modbusHandler(processImage):
raise Exception("Unexpected number of registers for process image ({})".format(len(res.registers)))
(analogOutputBits, analogInputBits, digitalOutputBits, digitalInputBits) = res.registers
print(f"AO: {analogOutputBits}, AI: {analogInputBits}, DO: {digitalOutputBits}, DI: {digitalInputBits}")
logger.debug(f"AO: {analogOutputBits}, AI: {analogInputBits}, DO: {digitalOutputBits}, DI: {digitalInputBits}")
processImage.init(digitalOutputBits, digitalInputBits, analogInputBits)
@ -63,12 +68,12 @@ def modbusHandler(processImage):
raise Exception(reg)
except Exception as e:
print("Exception in inner modbus handler loop: {}".format(e))
logger.error("Exception in inner modbus handler loop: {}".format(e))
client.close()
finally:
sleep(MODBUS_REFRESH_PERIOD)
sleep(modbusRefreshPeriod)
except Exception as e:
print("Exception in modbus handler: {}".format(e))
logger.error("Exception in modbus handler: {}".format(e))
finally:
client.close()