driv3r
driv3r

Reputation: 1

How to get pymodbus to correctly communicate with PLC?

Before I ask, I just want to mention that I have spent a few days researching this and can't seem to find my way out of this basic issue. I have read the docs and spent time here. Otherwise I wouldn't have asked.

I have inherited a massive monolithic python program that successfully reads holding registers with pymodbus + read_holding_registers. I have spent time editing python before but have never really learned it. To try and understand this on the python side, I have tried writing my own basic program from scratch. I started big and eventually broke the code down to as simple as I can.

from pymodbus.client.sync import ModbusTcpClient as ModbusClient

client = ModbusClient('192.168.1.98', port=502)
client.connect()

rr = client.read_holding_registers(10904, 2)

print rr

client.close()

I know that this is the register I want. It's a holding register on a Horner PLC. It's well-documented in both the python program and in Horner CSCAPE. But whenever I run the program, it just prints whatever value I put in the second item of the tuple. So here, it will just print 2. If I supply a tuple of (10905, 1) it just prints 1. The true value of the register bit is supposed to be 0.

I would post the massive program, but it is private unfortunately. This is python2.7. I know it's bad but I just want to catch up on understanding the program before I worry about porting it to 3.9.

Upvotes: 0

Views: 1236

Answers (1)

Gigioz
Gigioz

Reputation: 387

You have to call it with .registers like this

rr = client.read_holding_registers(10904, 2).registers

Also, if you check the documentation, the author claims that the code is compatible with both Python 2.7 and Python 3.x so that you can port it.

Upvotes: 0

Related Questions