1

I have a modbus simulator with 4 slaves [1,2,3,4] defined in it [using modbus_tk] , I also have a pymodbus client to talk to this modbus slaves over TCP . Everything is working fine as long as we are using the correct slave device id's (unit id) but as soon an invalid id is used , all the transactions start returning None even for valid device address.

A sample snippet of client code and debug log is provided below for reference , as could be seen "5" is a invalid slave id and it fails as expected (debug log shows 0x83 0x4) , in the second iteration even for the valid device ("1") the result is returned as None , from the the debug log it appears that data is available . Any hint on what could be going wrong is highly appreciated

Code

from pymodbus.client.sync import ModbusTcpClient
import logging
logging.basicConfig()
log = logging.getLogger('pymodbus')
log.setLevel(logging.DEBUG)

client = ModbusTcpClient('127.0.0.1', 5440)
for x in range(2):
log.debug( "iteration %s " % x)

for slave in [1, 5]:
    try:
        log.debug(  "Slave ID %s" % slave)
        result = client.read_holding_registers(0,1, unit=slave)
        print result.registers[0]
    except Exception as e :
        # Attribute error due to result as None
        log.debug( "In Exception for slave ID %s" % slave)
client.close()

LOGS

DEBUG:pymodbus:iteration 0 
DEBUG:pymodbus:Slave ID 1
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x0 0x52
82 comment-----> This is the actual Value for holding registers from print statement(0x52)
DEBUG:pymodbus.factory:Factory Response[3]
DEBUG:pymodbus.transaction:adding transaction 1
DEBUG:pymodbus.transaction:getting transaction 1
DEBUG:pymodbus:Slave ID 5
DEBUG:pymodbus.transaction:Running transaction 2
DEBUG:pymodbus.transaction:0x83 0x4
DEBUG:pymodbus.transaction:getting transaction 2
DEBUG:pymodbus:In Exception for slave ID 5
DEBUG:pymodbus:iteration 1 
DEBUG:pymodbus:Slave ID 1
DEBUG:pymodbus.transaction:Running transaction 3
DEBUG:pymodbus.transaction:0x0 0x3 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x0 0x52
comment ---> Value (0x52) appears to be recieved but somehow not reached
DEBUG:pymodbus.transaction:getting transaction 3
DEBUG:pymodbus:In Exception for slave ID 1
DEBUG:pymodbus:Slave ID 5
DEBUG:pymodbus.transaction:Running transaction 4
DEBUG:pymodbus.transaction:0x83 0x4
DEBUG:pymodbus.transaction:getting transaction 4
DEBUG:pymodbus:In Exception for slave ID 5
Sanju
  • 1,974
  • 1
  • 18
  • 33
  • Well, I figured out the problem , the issue was with the buffer not cleared on error ,on a new transaction , the buffer would also contain the previous error message and hence would again result in an exception. I have posted the sample workaround at https://github.com/dhoomakethu/pymodbus/commit/11f739260967578a8889b62cd49a2530b64a7c50 – Sanju Oct 16 '15 at 06:22

0 Answers0