66 lines
2.1 KiB
Python
Executable File
66 lines
2.1 KiB
Python
Executable File
#!python
|
|
from pymodbus.client import ModbusSerialClient, ModbusTcpClient
|
|
from pymodbus.pdu.register_read_message import ReadInputRegistersResponse
|
|
from configModel import Algo, Metrique, Capteur, ModbusType
|
|
import struct, time, logging
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
# log = logging.getLogger('pymodbus.protocol.*')
|
|
# log.setLevel(logging.DEBUG)
|
|
|
|
# client = ModbusTcpClient(host="shellyproem50-08f9e0e79718") # grarage (borne et pac)
|
|
# client = ModbusTcpClient(host="ShellyPro3EM-FCE8C0D97664") # bureau (prises 1 à 3)
|
|
client = ModbusSerialClient(
|
|
port="/dev/ttyUSB1",
|
|
baudrate=9600,
|
|
# stopbits=capteur.comConfig.stopbits,
|
|
# bytesize=capteur.comConfig.bytesize,
|
|
# parity=capteur.comConfig.parity
|
|
)
|
|
|
|
def ieee754(registres:list):
|
|
float_bytes = struct.pack('HH', registres[0], registres[1])
|
|
return struct.unpack('f', float_bytes)[0]
|
|
|
|
def uint32(a, b):
|
|
ret:int = (a << 16) | b
|
|
return ret
|
|
|
|
def lireMetrique(metrique:Metrique, registre:int, slave:int = 1, nom:str = "sans nom"):
|
|
client.connect()
|
|
try:
|
|
data:ReadInputRegistersResponse = client.read_input_registers(registre, 1, slave=slave)
|
|
if data:
|
|
registres = data.registers
|
|
if metrique.algo == Algo.NORMAL:
|
|
val = registres[0]
|
|
if metrique.algo == Algo.IEEE2:
|
|
val = round(ieee754(registres[0:2]), metrique.precision)
|
|
if metrique.algo == Algo.UINT32:
|
|
val = uint32(registres[0], registres[1])
|
|
if metrique.algo == Algo.STRING:
|
|
val = ''.join(chr(register) for register in data.registers)
|
|
if metrique.algo == Algo.BOOLEAN:
|
|
val = registres[0]
|
|
print(f"--> Registre {registre} du périmérique {slave} ({nom}) : {val}")
|
|
except Exception as e:
|
|
print(f"Erreur lors de la lecture du registre {registre} du périmérique {slave} ({nom}) : {e}")
|
|
finally:
|
|
client.close()
|
|
|
|
slave = 0
|
|
|
|
if __name__ == '__main__':
|
|
while True:
|
|
# slave = slave + 1
|
|
m:Metrique = Metrique(
|
|
idMetrique=0,
|
|
indexRegistreDepart=0,
|
|
precision=3,
|
|
algo=Algo.NORMAL
|
|
)
|
|
lireMetrique(m, 1, slave=1)
|
|
time.sleep(1)
|
|
# lireMetrique(m, 0, 2, "orientation")
|
|
# time.sleep(1)
|