Hello,
I need to integrate using Modbus function code 0x17 (23), which in the pymodbus library is referred to as readwrite_registers
. I’ve written a small Python script that works quite well and allows me to communicate with the field device.
Here’s a snippet of the script:
busId = 1
read_address = 0x0000
read_quantity = 6
write_address = 0x0001 # 0-read, 1 write
# Building the data to write
bytes_to_send = [
0x06, # ID0
0x00, # ID1
0xB5, # ID2
0x36, # ID3
0x00,
0x00, # Number of table
0x00, # Number of register_High
0x13, # Number of register_Low
lo, # VALUE_float LO IEEE 754, from value
mi, # VALUE_float Mi IEEE 754, from value
hi, # VALUE_float HI IEEE 754, from value
exponent # VALUE_float EXPONENT IEEE 754, from value
]
registers_to_write = [bytes_to_send[i] << 8 | bytes_to_send[i + 1] for i in range(0, len(bytes_to_send), 2)]
write_quantity = len(registers_to_write)
write_request = client.readwrite_registers(
read_address=read_address,
read_count=read_quantity,
write_address=write_address,
values=registers_to_write,
slave=busId
)
if write_request.isError():
print(f"Error : {write_request}")
else:
print(f"OK")
read_registers = write_request.registers # Response
client.close()
read_bytes = []
for reg in read_registers:
high_byte = (reg >> 8) & 0xFF # Byte H
low_byte = reg & 0xFF # Byte L
read_bytes.append(high_byte)
read_bytes.append(low_byte)
# Print HEX
read_bytes_hex = [f'{byte:02X}' for byte in read_bytes]
print(f"Byte HEX: {read_bytes_hex}")
lo = read_bytes_hex[8]
mi = read_bytes_hex[9]
hi = read_bytes_hex[10]
exponent = read_bytes_hex[11]
result = custom_bytes_to_float(lo, mi, hi, exponent)
print(result) # Output: 230.49436950683594
Do you think it’s feasible to integrate this way, or would it be simpler to develop a standalone integration?