Re: Faulty Response from slave

184 views
Skip to first unread message

Luc JEAN

unread,
Jul 20, 2018, 4:01:58 AM7/20/18
to modb...@googlegroups.com
Hi Neeraj

Difficult to answer you question without source code and information about the slave.

- Do you see the same queries when you send with your « C lib » app?
- This « 129 » seems to be a « 128 + 1». The 8th bit is on. Does it has any signification for your slave? I don’t think that this is part of modbus specification.

I hope it helps
Best
luc

Le 19 juil. 2018 à 16:57, Neeraj Nayan <bneera...@gmail.com> a écrit :

Hi 
  •  I am testing the the modbus_tk master and slave communication on 2 linux devices ( raspberry pis) connected through usb to rs485 converters on  both sides. 
  • I use the the rtumaster_example.py and rtuslave_example.py from the examples folder to test the basic write read capabilities on the slave.
  • I modify master code to test 1) Read reigsters and 2) Write single/multiple slaves. I get faulty modbus response from the slave as shown below:
    • Write multiple registers: The slave always is always wrong.  


    • Write single register: The slave response is wrong sometimes and correct the other times:
      • Wrong response: happens sometimes


      • Right resonse : The slave responds correctly most of the times with some inconsistencies.






    • Read register: Behaviour similar to single reg write with incosistent behaviour. Works mostly, but sometimes slave responds faulty.
  • I have tested the same setup with the C library for modbus and that seems to work fine. I also tried both instances (C lib and modbus_tk) as master with a modbus_tk slave. The faulty behavior persists.

What am I missing out on (in terms of some lib spec of the slave) ? My intention is to build a modbus_tk slave that I can read from and write to. Cant wait to hear your ideas and perspectives. Thanks for your time.

Regards,
Neeraj


--
You received this message because you are subscribed to the Google Groups "modbus-tk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modbus-tk+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Neeraj Nayan

unread,
Jul 20, 2018, 6:01:30 AM7/20/18
to modbus-tk
Hi Luc!

Answering your concerns in sequence:

1) Source code is almost exactly same as the RTU master and slave examples you've shared with the library. Only the port has been changed to work with USB. (For USB to RS485 converter) (sharing slave code at end).
2) Slave Info: Slave is a raspberry pi with modbus_tk installed running the rtuslave_example.py from the examples folder in the library. Only port detail has been changed to ttyUSB for the Serial converter.
3) C Lib app:  When I use:
      • C lib master + C lib slave            ----->Always works fine. Slave responds with the right slave ID and function code. Same queries and right response.
      • modbus_tk master + C lib slave  -----> Always Works fine. Slave responds with the right slave ID and function code. Same queries and right response.
      • C lib master + modbus_tk slave -----> Write multiple registers : slave never responds correctly; Read registers : Works fine most of the times. sometimes slave responds exactly like above and communication stops. Same queries but wrong response
      • modbus_tk master + modbus_tk slave -->Write multiple registers: slave never responds correctly, Write single reg and read reg: work mostly. sometimes slave responds wrongly and comm stops. Same queries but wrong respsonse.
4) I anticipate no significance for the slave in the address 129. My slave iD is 1. The following is the slave code I use, it is almost exactly same as the rtuslave_example.py in the examples folder of the lib.

#*****************************************************
import sys

import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import serial


#PORT = 0
#PORT = '/dev/ptyp5'
PORT = '/dev/ttyUSB0'

def main():
    """main"""
    logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s")

    #Create the server
    server = modbus_rtu.RtuServer(serial.Serial(PORT))

    try:
        logger.info("running...")
        logger.info("enter 'quit' for closing the server")

        server.start()

        slave_1 = server.add_slave(1)
        slave_1.add_block('0', cst.HOLDING_REGISTERS, 0, 100)
        while True:
            cmd = sys.stdin.readline()
            args = cmd.split(' ')

            if cmd.find('quit') == 0:
                sys.stdout.write('bye-bye\r\n')
                break

            elif args[0] == 'add_slave':
                slave_id = int(args[1])
                server.add_slave(slave_id)
                sys.stdout.write('done: slave %d added\r\n' % (slave_id))

            elif args[0] == 'add_block':
                slave_id = int(args[1])
                name = args[2]
                block_type = int(args[3])
                starting_address = int(args[4])
                length = int(args[5])
                slave = server.get_slave(slave_id)
                slave.add_block(name, block_type, starting_address, length)
                sys.stdout.write('done: block %s added\r\n' % (name))

            elif args[0] == 'set_values':
                slave_id = int(args[1])
                name = args[2]
                address = int(args[3])
                values = [445]
                for val in args[4:]:
                    values.append(int(val))
                slave = server.get_slave(slave_id)
                slave.set_values(name, address, values)
                values = slave.get_values(name, address, len(values))
                sys.stdout.write('done: values written: %s\r\n' % (str(values)))

            elif args[0] == 'get_values':
                slave_id = int(args[1])
                name = args[2]
                address = int(args[3])
                length = int(args[4])
                slave = server.get_slave(slave_id)
                values = slave.get_values(name, address, length)
                sys.stdout.write('done: values read: %s\r\n' % (str(values)))

            else:
                sys.stdout.write("unknown command %s\r\n" % (args[0]))
    finally:
        server.stop()

if __name__ == "__main__":
    main()
#*********************************************************

My aim is to obtain a python based slave using modbus_tk (to read and write holding registers), but I cant seem to explain why the slave is sending such incorrect responses affecting communication.

Thanks for your time Luc. Looking forward to your opinion.

Regards,
Neeraj
Reply all
Reply to author
Forward
0 new messages