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:
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