Modbus TCP Server on RaspberryPi

1,556 views
Skip to first unread message

esch...@gmail.com

unread,
Feb 25, 2015, 5:46:35 AM2/25/15
to modb...@googlegroups.com
Hello,

i tried some simple examples for both Modbus TCP client and server on my RaspberryPi with arch-linux.
The client function works quite well but the server does not work.
I used following code (from the source -> examples folder -> tcpslave_example.py):

#!/usr/bin/env python
# -*- coding: utf_8 -*-
"""
 Modbus TestKit: Implementation of Modbus protocol in python

 (C)2009 - Luc Jean - luc....@gmail.com
 (C)2009 - Apidev - http://www.apidev.fr

 This is distributed under GNU LGPL license, see license.txt
"""


import sys

#add logging capability
import logging
import threading

import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus as modbus
import modbus_tk.modbus_tcp as modbus_tcp

logger
= modbus_tk.utils.create_logger(name="console", record_format="%(message)s")
   
if __name__ == "__main__":
       
   
try:
       
#Create the server
        server
= modbus_tcp.TcpServer()
        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, 100, 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
= []
               
for v in args[4:]:
                    values
.append(int(v))
                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()


I start the server with the command "python2 tcpslave_example.py" and then i try to connect with the Modbus testtool "Ananas" but it says "Connection creation failed".


I have also tried connecting to a PLC with Modbus TCP server functionality and it works, but it does not work on the RaspberryPi.
Any suggestions? Thanks in advance!!!

Luc JEAN

unread,
Feb 25, 2015, 5:50:26 AM2/25/15
to modb...@googlegroups.com
Hello,

You may have to bind the IP address when creating the server

modbus_tcp.TcpServer(address="10.0.0.15")

I hope it helps
Best
luc

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

esch...@gmail.com

unread,
Feb 25, 2015, 6:26:59 AM2/25/15
to modb...@googlegroups.com
Thanks, great hint!
BR
Reply all
Reply to author
Forward
0 new messages