Changing the Modbus TCP Port

changeModbusPort Command

The changeModbusPort command can be used to change the port used for data exchanges with a Modbus TCP master.

The current Modbus Slave Port is displayed on the Modbus TCP configuration window.

The default Modbus port number is 502.

Command

Description

changeModbusPort “portnum”

portnum is the new Modbus port number to use is passed as a string of characters.

Before running the command, refer to Used Ports to ensure that portnum is not being used by any other TCP/UDP protocols or processes.

An error is logged in the /usr/Syslog/FWLog.txt file if the specified port number is already in use.

To limit the number of open sockets, the changeModbusPort command can only be run twice.

A power cycle of the logic controller returns the Modbus port number to the default value (502). The changeModbusPort command must therefore be executed after each power cycle.

NOTE: After changing the port number, the Modbus Server Active checkbox on the Ethernet Configuration window is no longer taken into account, as the Modbus server always uses port 502.

Running the Command from an SD Card Script

Step

Action

1

Create a script file, for example:

; Change Modbus slave port

changeModbusPort "1502";

2

Name the script file Script.cmd.

3

Copy the script file to the SD card.

4

Insert the SD card in the controller.

Running the Command Using ExecuteScript

The changeModbusPort command can be run from within an application using the ExecuteScript function block.

The following sample code changes the Modbus TCP slave port from the default (502) to 1502.

IF (myBExe = FALSE AND (PortNum <> 502)) THEN

   myExecSc( // falling edge for a second change
   xExecute:=FALSE ,
   sCmd:=myCmd ,
   xDone=>myBDone ,
   xBusy=> myBBusy,
   xError=> myBErr,
   eError=> myIerr);
   string1 := 'changeModbusPort "';
   string2 := WORD_TO_STRING(PortNum);
   myCmd := concat(string1,string2);
   myCmd := concat(myCmd,'"');
   myBExe := TRUE;
END_IF

myExecSc(
xExecute:=myBExe ,
sCmd:=myCmd ,
xDone=>myBDone ,
xBusy=> myBBusy,
xError=> myBErr,
eError=> myIerr);