我正在使用pymodbus库来创建Modbus服务器。我发现了你可以找到的所有例子here。我遇到了与在服务器(从属)端写入浮点值有关的问题。有一个updating_writer
函数,我想在其中将float值= 22.34
写入2个持有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)
...
但是当我从Modbus主站(客户端)连接并尝试读取这2个registor时出现错误:
...
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
我应该如何将float值写入某些上下文?我找不到任何例子。