对不起,我的英语不好。我尝试使用 pyModbusTCP lib 通过 Modbus 在 python 的 plc 寄存器中写入和读取浮点数。这是我的代码,不幸的是没有去......
from pyModbusTCP.client import ModbusClient
from pyModbusTCP import utils
class FloatModbusClient(ModbusClient):
def read_float(self, address, number=1):
reg_l = self.read_holding_registers(address, number * 2)
if reg_l:
return [utils.decode_ieee(f) for f in utils.word_list_to_long(reg_l)]
else:
return None
def write_float(self, address, floats_list):
b32_l = [utils.encode_ieee(f) for f in floats_list]
b16_l = utils.long_list_to_word(b32_l)
return self.write_multiple_registers(address, b16_l)
c = FloatModbusClient(host=ip, port=porta, auto_open=True)
# write 10.0 at @0
c.write_float(registrow, [var])
print("write ok")
# read @0 to 9
float_l = c.read_float(registror)
print(float_l)
c.close()
有人能帮我吗?