问题
I want to write to PLC input registers using pymodbus. I am able to read them :
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('10.10.10.32')
client.connect()
reg = client.read_input_registers(1,5)
print(reg.registers)
But I still did not found any way how to write any value to them. I appreciate any help. Thanks.
回答1:
Input registers are read-only. You can write to holding registers, using Modbus functions Write Single Register or Write Multiple Registers (ModbusTcpClient.write_register or ModbusTcpClient.write_registers in pymodbus).
回答2:
Example using a Festo MODBUS/TCP:
# First digital input address
address = 40003
# Written value
value = 255
# It will send '11111111' to the output
client.write_register(address, value)
According to the documentation:
http://pymodbus.readthedocs.org/en/latest/library/sync-client.html
// function
write_register(address, value)
// Parameters
address – The starting address to write to
value – The value to write to the specified address
回答3:
PLC's have a dedicated set of registers for you to read and a set for you to write to. The set of write registers differ by PLC. You read registers, for instance could start on register "1". You read from "1" but not write. You'll have to look up the modbus register mapping for your PLC.
My knowledge of Python is low, but it appears that you are trying to read up to 5 registers, starting with 1? To write, you probably need to use
reg = client.write_output_registers(?,??)
I usually use a Wago 880. I can write to registers 0-999 and read from 1000-1999. I am assuming Python would take care of the function code for you.
来源:https://stackoverflow.com/questions/31912493/how-to-write-to-plc-input-registers-using-pymodbus