input working
This commit is contained in:
@ -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()
|
||||
|
Reference in New Issue
Block a user