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.
ping
it and get a return? Does that machine (I guess robot) actually have port502
open? – KDecker