1
votes

I'm fairly new to pymodbus and I'm trying to read holding registers of a collaborative robot with pymodbus to get the value of the current z coordinate. Such information is at the 7053 address. I looked at the older questions but I couldn't get my code to work:

from pymodbus.client.sync import ModbusTcpClient

host = '192.168.250.201' # Robot IP address
port = 502 # port

client = ModbusTcpClient(host, port)
client.connect()
request = client.read_holding_registers(
    address=0x03, # The starting address to read from 
    count=4, # The number of registers to read
    unit=1) # The slave unit this request is targeting
response = client.execute(request)
print(response.bits[0]) 
client.close()

I keep getting this errore message:

ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusTcpClient(192.168.250.201:502)]

I guess there must be something wrong in my code or maybe something else is preventing me to establish the connection. Any suggestions? Thanks

2
Is the IP address you listed in the code actually on your network? Can you ping it and get a return? Does that machine (I guess robot) actually have port 502 open?KDecker
I just found out that i was confusing the robot ip address with my own in the network. I modified my code with the right ip address and the connection is working now, but i don't understand what I am getting back from my request: ReadRegisterResponse (0)Federico

2 Answers

0
votes

You have a couple of minor issues:

1) The address of the register you are querying does not seem correct, double-check the manual of your device to see if you are reading the correct address, most likely you will need to query address=7053.

2) You are reading holding registers but you then try to print the value in coils (bits). Check if they are really holding registers and use print(response.registers[0])

0
votes

I fixed my code like suggested above and I also added 2 lines to try and decode what I get back (Z coordinate of my robot):

from pymodbus.client.sync import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder

host = '192.168.250.2' # Robot IP address
port = 502 # Modbus port on the robot

client = ModbusTcpClient(host, port)
client.connect()
request = client.read_holding_registers(
    address=7053, # The starting address to read from 
    count=4, # The number of registers to read
    unit=1) # The slave unit this request is targeting (slave ID)
#response = client.execute(request)
print(request.registers)
decoder = BinaryPayloadDecoder.fromRegisters(request.registers, Endian.Big, Endian.Little)
print(decoder.decode_32bit_float())
client.close()

Output:

[0, 0, 0, 0]
0.0

What does this output mean? I know the robot Z coordinate is 400mm, but It looks like that I get 0 no matter what address i use in the request. This is the debug output:

DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x3 0x1b 0x8d 0x0 0x4
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x0 0x1 0x0 0x0 0x0 0xb 0x1 0x3 0x8 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x1 0x0 0x0 0x0 0xb 0x1 0x3 0x8 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
DEBUG:pymodbus.factory:Factory Response[ReadHoldingRegistersResponse: 3]
DEBUG:pymodbus.transaction:Adding transaction 1
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
DEBUG:pymodbus.payload:[0, 0, 0, 0]
DEBUG:pymodbus.payload:[b'\x00\x00', b'\x00\x00']

I get the stuff in SEND but not quite the one in RECV, it's not what i am expecting.