I'm using the pymodbus library for creating the Modbus server. I've discovered all examples which you can find here. I faced with problem related to writing float value on the server (slave) side. There is an updating_writer
function in which I want to write the float value = 22.34
to 2 holding resitors:
...
def updating_writer(a):
context = a[0]
register = 3
slave_id = 0x00
address = 0x0
builder = BinaryPayloadBuilder(endian=Endian.Little)
builder.add_32bit_float(22.34)
res = builder.build()
context[slave_id].setValues(register, address, res)
store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [17]*100),
co = ModbusSequentialDataBlock(0, [17]*100),
hr = ModbusSequentialDataBlock(0, [17]*100),
ir = ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)
...
But when I have a connection from Modbus master (client) and try to read these 2 registor I get an error:
...
File "/home/workstation/devel/modbus_example/env_modbus/local/lib/python2.7/site-packages/pymodbus/server/async.py", line 83, in _send
pdu = self.framer.buildPacket(message)
File "/home/workstation/devel/modbus_example/env_modbus/local/lib/python2.7/site-packages/pymodbus/transaction.py", line 354, in buildPacket
data = message.encode()
File "/home/workstation/devel/modbus_example/env_modbus/local/lib/python2.7/site-packages/pymodbus/register_read_message.py", line 71, in encode
result += struct.pack('>H', register)
struct.error: cannot convert argument to integer
How I should write the float value to some context? I cannot find any examples.