Stop TCP Server

444 views
Skip to first unread message

Shammya Saha

unread,
Jan 4, 2019, 1:14:16 AM1/4/19
to pymodbus
Hello
Is there any way to create multiple TCP servers? Looks like doing the following did not work

StartTcpServer(context=context, address=('127.0.0.1', 5020))
StartTcpServer(context=context, address=('127.0.0.2', 5020))


Like StartTcpServer, is there any function to stop the TCP server?

Regards
Shammya Saha

sanju dhoomakethu

unread,
Jan 4, 2019, 1:33:43 AM1/4/19
to pymo...@googlegroups.com
StartTcpServer is blocking and hence you will not be able to call the second StartTcpServer . 
To answer the second question, You can use StopServer function to stop an async server, provided it is running on a separate thread. There is no option at present to stop Synchronous server. If you want to control Synchronous server, you can create ModbusTcpServer object and use shutdown method to stop the server.

server = ModbusTcpServer(context, framer, identity, address, **kwargs)
# Start server in a separate thread
# server.serve_forever()
# Shutdown
server.shutdown()





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

Galen Collins

unread,
Jan 4, 2019, 12:13:45 PM1/4/19
to pymo...@googlegroups.com
Also make sure you run each server on a different port. In this case you are trying to run both on the same port of 5020.

Alexander Lutz

unread,
Sep 25, 2020, 12:18:56 PM9/25/20
to pymodbus
Is it possible to run multiple modbus asynchronous servers from different IP addresses on the same machine? I'd like to keep the ports for the different IPs at default of 502.

as mentioned in the response to the above example, if I don't use threading, only 1 server is ran. So i added a second IP alias to my NIC card configuration (such that it looks like it is hosting 2 ip addresses). I then tried to use threading to start multiple instances of the modbus TCP server. When I do this, the "reactor" module is unhappy that I am trying to call it from another thread after it has already been called. 

Is there any way to run multiple asynchronous modbus servers from the same machine? That way I could use port 502 and scale the number of devices with IP address instead of port. If you you limit to one IP and one port then i can only max out at 255 devices, correct?

Here is what i was trying to do:

#set IPs for modbus gateways
port = 502
ip1 = "172.16.1.40"
ip2 = "172.16.1.41"
addy1 = (ip1,port)
addy2 = (ip2,port)

t1 = threading.Thread(target=StartTcpServer, args=[context,identity1,(ip1,port)])
t1.start()

t2 = threading.Thread(target=StartTcpServer, args=[context,identity1,(ip2,port)])
t2.start()

Reply all
Reply to author
Forward
0 new messages