python modbus RTU 03功能码和10功能码

from pymodbus.client.sync import ModbusSerialClient#pip3 install pymodbus==2.5.3

创建Modbus RTU客户端

client = ModbusSerialClient(method=‘rtu’, port=‘COM7’, baudrate=115200, stopbits=1, bytesize=8, parity=‘N’)

连接Modbus设备

client.connect()

读取保持寄存器

result = client.read_holding_registers(address=0, count=30, unit=1)
if result.isError():
print(“读取保持寄存器失败:”, result)
else:
print(“保持寄存器值:”, result.registers)

写入多个寄存器

register_values = [1, 2, 3, 4, 5] # 要写入的寄存器值列表
starting_address = 90 # 要写入的寄存器起始地址
result = client.write_registers(address=starting_address, values=register_values, unit=1)
if result.isError():
print(“写入寄存器失败:”, result)
else:
print(“寄存器写入成功”)

关闭连接

client.close()

你可能感兴趣的:(python,开发语言)