0

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.

Emma
  • 27,428
  • 11
  • 44
  • 69
driv3r
  • 1
  • 1
  • Did you try `rr = client.read_holding_registers(10904, 2).registers` ? – Gigioz Dec 07 '20 at 16:23
  • That worked! Thank you so much! I swear I couldn't piece this together from the documentation I could find anywhere! How do I thank you with flair? – driv3r Dec 07 '20 at 19:37
  • You're welcome! I will turn the comment in an answer then, just for sake of completeness – Gigioz Dec 07 '20 at 21:02

1 Answers1

0

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.

Gigioz
  • 367
  • 5
  • 19