1. Introduction
What if we need to work with more than one Com Port? Do we need to get another industrial computer to serve as Modbus Gateway? Not necessary. The code we prepared for Modbus Gateway which allows us to create one Tcp Server and use one Com Port can be easily duplicated.
This article is continuation of Modbus Gateway 1ETH 1COM and I encourage You to read it first.
2. Implementation
Tcp Server and ComPort were created as Functon Blocks so we can duplicated them and tight specific Com Port to specific IP address. First let's create another "bridge" variable to hold data being exchanged between Modbus Server and Modbus Master.
VAR_GLOBAL
bridgeCom1 : ModbusBridgeData;
bridgeCom2 : ModbusBridgeData;
END_VAR
We do the same for Com Port task.
PROGRAM COM
VAR
ComPort1 : ComPort;
ComPort2 : ComPort;
END_VAR
ComPort1(com := 1, bridge := ADR(MODBUS.bridgeCom1));
ComPort2(com := 2, bridge := ADR(MODBUS.bridgeCom2));
And Tcp Server task.
PROGRAM Server
VAR
ModbusGatewayServer1 : TcpServer;
ModbusGatewayServer2 : TcpServer;
END_VAR
ModbusGatewayServer1(serverIp := '192.168.0.204', bridge := ADR(MODBUS.bridgeCom1));
ModbusGatewayServer2(serverIp := '192.168.0.203', bridge := ADR(MODBUS.bridgeCom2));
That's it.
If our hardware is equipped with 8 Com Ports, we could create 8 Tcp Servers with 8 different IP addresses and tight them to 8 different Com Port. In result, we would get 8 channel Modbus Gateway.
3. Summary
Maybe to code for single channel Modbus Gateway seemed unusful because there are a lot of devices available in the market that have the same functionality. Here, we start seeing advantage of have our own application.
But that's not all! When You work in huge facility, changes come slowly. To make changes may be also pretty challenging because You have a complex system but You are limited to the functionality offered by Modbus Gateway manufacturers. In this example, we showed that we can easily make multiple channel Modbus Gateway. Assiging one Tcp Server to one Com Port, in my opinion, seems the most effective way. But what if the reality is different? What if You have a system where only one Tcp Server is used for multiple Com Ports? What if Slave addresses are the same on different Com Ports? One way is to adopt control system to the hardware You can get on the market... but You are Automation Engineer. You have already developed application for Modbus Gateway, so maybe You could develop application which uses one Tcp Server with multiple Com Ports? We will try to cover that in next articles.
As ususal, source code for multiple port Modbus Gateway is here (channel 1 IP: 192.168.0.204, channel 2 IP: 192.168.0.203, PORT: 502).