Implement ModBus Server

Hello, dear PLCnext team, I would like to implement a ModBus server on the AXC F 2152 controller to exchange data with a ModBus master.

The Modbus master uses the Modbus functions 0x03 (Read Multi Registers; register 0 (write)) and 0x10 (Write Multi Registers; register 1000 (read)).
The Modbus master has the address 192.168.2.207 and uses port 502.
I tried to establish the ModBus connection using the example. Unfortunately without success. How is the variable arrModbusData described by the ModBus master?
Do I have to consider something else? Thank you very much for your support. I am using the current ModBus-Lib. Source code: udtServer(
arrModbusData := arrModbusData
); CASE iState OF
0: (* Reset state )
udtServer.xActivate := FALSE;
udtServer.xAcknowledge := FALSE;
udtServer.xAutoAck := FALSE;
udtServer.xUDP := FALSE;
udtServer.strBindIp := ‘’;
udtServer.uiBindPort := UINT#502;
udtServer.strDestIp := ‘’;
udtServer.uiDestPort := UINT#502;
udtServer.tTimeout := TIME#10s;
udtServer.uiOffsetInputRegister := UINT#0;
udtServer.uiOffsetHoldingRegister := UINT#1000;
udtServer.uiOffsetInputs := UINT#0;
udtServer.uiOffsetCoils := UINT#0;
FOR iIndex := 0 TO 7167 DO
arrModbusData[iIndex] := INT_TO_WORD(iIndex);
END_FOR;
(
First word has to be <> 0 )
arrModbusData[0] := WORD#16#FEDC;
IF (udtServer.xActive = FALSE) THEN
iState := 100;
END_IF;
100: (
Activation of server*)
udtServer.xActivate := TRUE;
IF (udtServer.xActive = TRUE) THEN
iState := 9000;
END_IF;
9000: (* Error handling )
IF (udtServer.xError = TRUE) THEN
iState := 0; (
Auto restart *)
END_IF;
END_CASE;

Hello,

what happens when you start the Server?
Does it stay in activeState or does it reach the Errorhandling section?

Do you get any errorcodes?

The Master/Client has 192.168.2.207/?? and uses port 502.
What does the Slave/Server have in this case?

udtServer is in your case of type MB_TCP_Server?

arrModbusData | MB_TCP_ARR_W_0_7167 | WORD array
First index: 0
Last index: 7167
This array contains the Modbus registers.

—|—|—
Did you enable the Firewall on yout PLCnext Controll? If so remember to accept Port 502 Traffic. Also keep in mind : client → Server
connect to “dest Port” → listen on “bind Port”
local “bind port” → only accept “dest port”

For starters you can set the “dest port” at your Server to 0 if you are not sure what the client locally binds to.

kind regards,
Oliver

When the server starts, nothing unusual happens.

No error codes are active.
Only the Diag-Code 8000 is displayed. However, the Modbus master cannot read and/or write data. The firewall on the controller is switched off. The master has IP 192.168.2.207 and accesses port 502. The slave (the PLCnext controller) has the IP 192.168.2.208.

Please try setting the destination Port at the Modbus Server FB to “0”.

If you are using 2x AXC F 2152, i will strongly recommend you to use Profinet communication instead of Modbus TCP.

It’s more simple to get working. But data amount is smaller than the Modbus. But if you don’t need more than 512bytes i will recomend Modbus. Normaly for modbus i set (i havn’t tryed it on PLC next, but use it alot in PcWorx 6) udtServer.xActivate := Ture;
udtServer.xAcknowledge := True;
udtServer.xAutoAck := True;
udtServer.xUDP := FALSE;
udtServer.uiBindPort := UINT#502;
udtServer.uiDestPort := UINT#502;
udtServer.tTimeout := TIME#0s;
udtServer.uiOffsetInputRegister := UINT#0;
udtServer.uiOffsetHoldingRegister := UINT#0;
udtServer.uiOffsetInputs := UINT#0;
udtServer.uiOffsetCoils := UINT#0; - Emil

I got it with this Source-Code: CASE iState OF

0: (* Reset state )
udtServer.xActivate := FALSE;
udtServer.xAcknowledge := FALSE;
udtServer.xAutoAck := FALSE;
udtServer.xUDP := FALSE;
udtServer.tTimeout := TIME#10s;
udtServer.uiOffsetHoldingRegister := UINT#0;
FOR iIndex := 0 TO 7167 DO
arrModbusData[iIndex] := INT_TO_WORD(iIndex);
END_FOR;
(
First word has to be <> 0 )
arrModbusData[0] := WORD#16#FEDC;
IF (udtServer.xActive = FALSE) THEN
iState := 100;
END_IF;
100: (
Activation of server*)
udtServer.xActivate := TRUE;
IF (udtServer.xActive = TRUE) THEN
iState := 9000;
END_IF;
9000: (* Error handling )
IF (udtServer.xError = TRUE) THEN
iState := 0; (
Auto restart *)
END_IF;
END_CASE; Sometimes is less more :wink: