9

I am very new to Modbus and PyModBus however I have spent a good amount of time trying to read up and experiment with it. If anyone could point me in the right direction I would appreciate it...


I have a drive with distance, velocity, acceleration, and deceleration on registers 40001, 40003, 40005, and 40007 (respectively). I was initially able to write to the distance register, using client.write_register(0000, n). After trying to write to velocity the drive started going haywire and faulting, and spinning 10x as fast as it should've been. However, the real priority is reading registers. I am trying to read the data from these registers and having zero luck. I tried using

request = client.read_holding_registers(0000,4)
response = client.execute(request)
print response


However, all I get back is "ReadRegisterResponse (0)".

So again, my big priority is trying to read values from these registers...any advice? (This is over TCP by the way)

5T4TiC
  • 191
  • 2
  • 2
  • 6

2 Answers2

11

Try to:

 response = client.read_holding_registers(0x00,4,unit=1)

where the unit value is device id of the slave.

To print all:

print response.registers

Also is possible to directly get one value (for example third register):

print response.getRegister(2)

or

print response.registers[2]  
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
Azat
  • 196
  • 1
  • 8
  • Where do you get the parameters `0x00,4` from if the OP wants to read at address 40000? – Philipp Ludwig Sep 18 '21 at 08:08
  • When I use .registers it works correctly in my case but I get a warning from PyCharm and it says "unresolved attribute reference 'registers' for class 'ModbusRespose'. Seems an error but the code works correctly... any idea? – Lorenzo Mar 23 '23 at 10:06
5

you could parse the response by yourself, the following is my code snippet:

    result = client.read_input_registers(0x01,1, unit=0x01)
    #print result
    t = result.registers[0]
    print "current temperature:", t, "  ", float(t/100.0)
Jak
  • 51
  • 1
  • 1