【发布时间】:2021-04-06 03:47:25
【问题描述】:
对不起,我的英语不好。 我尝试使用 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()
有人可以帮我吗?
【问题讨论】:
-
你能用更多信息更新这个问题吗?什么不起作用。该功能无法正确读取/写入。有错误,还有别的吗?
-
从 write_float 方法中,我希望在我给它的寄存器中写入一个浮点数(var),它不会给我错误,但它不会写任何东西。从 read_float 我希望从特定寄存器中读取浮点数,但结果我总是“无”
标签: python modbus plc pymodbustcp