2410_LSS100100/LSS100200_SW_03.0

Modbus Settings in the Controller Using Scripts

When working with Modbus in the controller, it is essential to understand the available function codes. These codes determine the type of memory (such as holding registers, input coils, etc.) to access and the corresponding action (read or write). All the functions described below can be used both for Modbus TCP and Modbus RTU.

  1. Function Codes:

    • Function codes define specific operations:

      • Read: Retrieve data from extension devices.

      • Write: Update coils or registers.

    • Common function codes include:

      • Read Discrete Inputs (Function Code 2)

        Name

        "Read discrete input"

        Command

        value = mb:readdiscreteinputs(address)

        Arguments

        [address]: address of the input

        Returned values

        1: ON, 0: OFF

        Exception codes

        01 or 02 or 03 or 04.

        Name

        "Read discrete inputs"

        Command

        value = mb:readdiscreteinputs(start,count)

        Arguments

        [address]: address of first input to read

        [count]: number of inputs to read (max 2000)

        Returned values

        1: ON, 0: OFF

        Exception codes:

        01 or 02 or 03 or 04

        Example

        bool1, bool2= mb: readdiscreteinputs(10,2)

        Value read from discrete input address 11 is returned into variable bool1.

        Value read from discrete input address 12 is returned into variable bool2.

      • Read Coils (Function Code 1)

        Name

        "Read single coil"

        Command

        coil = mb:readcoils(address)

        Command

        coil= mb:readcoils(start, count)

        Arguments

        [address]: address of the coils

        Returned values

        1: ON, 0: OFF

        Exception codes

        01 or 02 or 03 or 04

        Name

        "Read Multiple coil"

        Arguments

        [start]: address of first coil to read

        [count]: number of coils to read (max 2000)

        Returned values

        1: ON, 0: OFF

        Exception codes

        01 or 02 or 03 or 04

        Example

        coil1,coil2,coil3= mb:readcoils(1000, 3)

        Value read from coil address 1000 is returned into variable coil1.

        Value read from coil address 1001 is returned into variable coil2.

        Value read from coil address 1002 is returned into variable coil3.

      • Read Input Registers (Function Code 4)

        Name

        "Read input registers"

        Command

        value = mb:readinputregisters(address,count)

        Arguments

        [address]: address of first input register to read

        [count]: number of input registers to read (max 125)

        Returned values

        2byte values

        Exception codes

        01 or 02 or 03 or 04.

        Example

        value1, value2, value3, value4 = mb:readinputregisters(1015,4)

        Value read from input register address 1015 is returned into variable value1.

        Value read from input register address 1016 is returned into variable value2.

        Value read from input register address 1017 is returned into variable value3.

        Value read from input register address 1018 is returned into variable value4.

      • Read Holding Registers (Function Code 3)

        Name

        "Read registers"

        Command

        value = mb:readregisters(address,count)

        Arguments

        [address]: address of first register to read

        [count]: number of registers to read (max 125)

        Returned values

        2byte values

        Exception codes

        01 or 02 or 03 or 04.

        Example

        int1, int2= mb: readregisters(1100,3)

        Value read from register address 1100 is returned into variable int1.

        Value read from register address 1101 is returned into variable int2.

        Value read from register address 1102 is returned into variable int3.

      • Write Single Coil (Function Code 5)

        Name

        "Write single bit"

        Command

        value = mb:writebits (1000, value)

        [starting address, value "true" or "false"/"0"]

      • Write Single Register (Function Code 6)

        Name

        "Write single register"

        Command

        value = mb:writeregisters(1000, 123)

        [address, value]

      • Write Multiple Coils (Function code 0F)

        Name

        "Write multiple bits"

        Command

        value = mb:writebits(1000, true, false, true, ...)

        [address, bit value1, bit value2,..{max 1968 bits}]

      • Write Multiple Registers (Function code 10)

        Name

        "Write multiple registers"

        Command

        value = mb:writeregisters(1000, 123, 321, 222, ...)

        [address, value1, value2, ..{max 123 registers}]

        Exception codes

        mb:readcoils(start, count)

        mb:readdiscreteinputs(start, count)

        mb:readregisters(start, count)

        mb:readinputregisters(start, count)These commands read one or more registers/coils from the start address and return all values in case of success. In case of error, three variables are sent back:

        Nil

        Exception code description

        Exception code

Modbus exception codes: (see here).

QR Code is a registered trademark of DENSO WAVE INCORPORATED in Japan and other countries.

Was this helpful?