Use multiple QModbusServer on the same Modbus backend

I'm writing a server application who act as modbus server where multiple modbus clients( currently Siemens LOGO! PLC's) write their inputs status to the app. These inputs are connected to industrial machines and the the goal is to obtain the status of these machines in order to perform some checks( like running status or alarms etc..).

Basing on the modbus slave example found in QtCreator i can succesfully manage one plc at time, by setting the correct server address, listening port and bind address.

i can successfully manage a single plc configured to connect to my computer, using the server '1'.

However, i have no clue on how to expand it to accept connections from others plc, by setting their server address to 2, 3 etc...
If i try to create another QModbusServer with the same configuration but different server address, as expected it cannot bind to the port '502' because it's already used by the previous QModbusServer.

If i change the port to '503' for the second QModbusServer i can manage another PLC but i don't want to use one port for one device when they can be addressed by their server address (also, don't want to open multiple ports on my server)

By reading the description of QModbusServer

Modbus networks can have multiple Modbus servers. Modbus Servers are read/written by a Modbus client represented by QModbusClient. QModbusServer communicates with a Modbus backend, providing users with a convenient API.

i'm asking myself if the tcp socket is managed by the modbus backend and then multiple QModbusServer's can be attached to it where each one filter out their data.

Question: can i use multiple QModbusServer's (obviously with different setServerAddress(X) ) communicating on the same port? If so, how can i reach it?

Question: can i use multiple QModbusServer's (obviously with different setServerAddress(X) ) communicating on the same port? If so, how can i reach it?

I must admit that my Modbus knowledge is limited; however: Isn't the pure base of a server to handle multiple clients? Why does your server need multiple addresses? As all clients only see "their" server, this should not be a problem.

What might be a problem is to differentiate the clients from this single server, but in principle each client should have a separate connection, so even this should be manageable.

Yes, the pure base of the server is to accept multiple clients. But the concept of client and server in the modbus is a bit different from the classical concept everybody knows.

However, the real problem , as you mentioned, is to differentiate the client.
If multiple clients connect to the same server with address '1' (this addres is like an id), when they send (or to be more technical, write) data to the server, a signal is emitted but it does not contain any reference of the client who sent it.

From this, my idea was to create different QModbusServer with different addresses (again , address is like an id) to force one client per server and thus be able to distinguish them.

But being forced to potentially open 1000 ports for 1000 clients is not a big deal.

EDIT:

but in principle each client should have a separate connection, so even this should be manageable.

Yes, but the QModbusServer mask out everything about the phisicals tcp sockets so i'm not able to know where, frompotentially multiple tcp connections, the data comes from.

I think that "serverAddress" is only used when the communication stack is Serial. Think on a RS485 network that a lot of devices are connected, the only way to identify a device is this address as there is not IP or Port.

Why don't you change the way the connection is done? Use a Client instead of a Server in your application and a Server in your Devices(PLC, ...). Then, you need N modbus clients that will poll the servers. Every modbus client will represent a Device.

In this case you will have N modbus clients. Using the other approach you have N modus servers.

However, the real problem , as you mentioned, is to differentiate the client.

Yes, in classical TCP/IP the remote port is used for that. As Modbus/TCP is based on TCP, this information should still be available.

If multiple clients connect to the same server with address '1' (this addres is like an id), when they send (or to be more technical, write) data to the server, a signal is emitted but it does not contain any reference of the client who sent it.

But then you would have to give the client another address, not the server. Am I right?

But being forced to potentially open 1000 ports for 1000 clients is not a big deal.

You want to manage 1000 slaves? :)

I slightly remember, that Linux systemd is able to listen to one port, and if a connection arrives, it starts a new instance of the server process and then connects the server to the existing connection.

@IvanVa
Sorry, I have developed my own modbus classes and I use to ignore this value.

So, there is no other way that have a Server per Client.
You have to think that every Server will be a "map" of the Client.
You cannot have only one Server with multiple Clients because you will mix data. Read the modbus documentation, one Server, one Client connection.
1000 Servers with 1000 Clients connected is not a problem. The problem could be on thefrecuency of the received data. If the Clients send data every second is not a problem but 1000 clients sending data every 1ms could be.

@beecksche
Good to know. I think i will use the multiple ports solution. Sounds like less subject to accidental mistakes on the indexes as your other idea.

Plus i just found that playing with the children() function of all QObjects i can reach the child (or childs) QTcpSocket objects managing the connection between the plc and the server app.
This don't avoid using multiple QModbusServer objects but at least i can check if the connected client have the expected source ip, and even emit an alarm in case multiple plc's are connected to one server.