Skip to content

Use with AI

Use with AI

PLC sample programs (Modbus) ​

The following chapters describe various sample programs for Modbus communication via PLC (Simatic S7-1200, TIA Portal v16).

1. Reading horstOS Bool-Register ​

Learn how to read a Bool value from a Modbus-Coil (Address 1000) in Siemens TIA Portal (v16) with this guide.

1.1 Function Block ​

Using the MB_CLIENT function block, you can read a Bool (1-bit) from the Modbus Coils.

In this function block, the Modbus-Coil-Register starts at MV_DATA_ADDR 1, which corresponds to Coil Entry Number 0.

Since the Bool registers start at Address 1000, you need to pass MV_DATA_ADDR 1001 to this function block (1 + 1000) to query the value of Bool Register 0.

Upon successful reading, the value is written to the MB_DATA_PTR variable. This variable must be of type real.

Illustration: 1.1 Function Block

1.2 ​

Data Structure

For the sake of completeness, the data structure used in this example is provided below.

The parameters for establishing the connection can be found under the variable MB_Connection.

The value of Bool Register 0 is written to the variable horsFX_BoolRegister_0.

Illustration: 1.2

The associated TIA project can be downloaded here:

Read Modbus Bool Register

2. Reading horstOS Float-Register ​

Discover how to read a 32-bit floating-point value from two Modbus-Holding-Registers (Address 2000 and 2001) in Siemens TIA Portal (v16) with this helpful guide.

2.1 Function Block ​

Using the MB_CLIENT function block, you can read two 4-byte words from the Modbus Registers.

In this function block, the Modbus Holding Register starts at MV_DATA_ADDR 40001, which corresponds to Holding Register Entry Number 0.

Since the Float Registers start at Address 2000, you need to pass MV_DATA_ADDR 42001 to this function block (40001 + 2000) to query the value of Float Register 0.

Since two registers need to be read, the MB_DATA_LEN is set to 2.

Upon successful reading, the value is written to the MB_DATA_PTR variable. This variable must be of type real.

Illustration: 2.1 Function Block

2.2 Data Structure ​

For the sake of completeness, the data block used for this example is shown below. The parameters for establishing the connection can be found under the variable MB_Connection.

The value of the bool register 0 is written to the variable horsFX_BoolRegister_0.

Illustration: 2.2 Data Structure

The associated TIA project can be downloaded here:

Read Modbus Float-Register

3. Reading horstOS Integer Registers ​

Learn how to read a 32-bit integer value from two Modbus-Holding-Registers (Address 1000 and 1001) in Siemens TIA Portal (v16) with this step-by-step guide.

3.1 Function Block ​

The MB_CLIENT function block is used to read two words (4 bytes) from the Modbus registers.

In this function block, the Modbus holding register starts at MV_DATA_ADDR 40001, which corresponds to holding register entry number 0. As the integer registers start at address 1000, MV_DATA_ADDR 41001 must be transferred to this function block (40001 + 1000) in order to query the value of integer register 0.

As two registers have to be read, the MB_DATA_LEN is 2.

The value is written to the variable MB_DATA_PTR if the read-out is successful. This must be of type DInt.

Illustration: 3.1 Function Block

3.2 Data Structure ​

For the sake of completeness, the data block used for this example is shown below. The parameters for establishing the connection can be found under the variable MB_Connection.

The value of the integer register 0 is written to the variable horsFX_IntRegister_0.

Illustration: 3.2 Data Structure

The associated TIA project can be downloaded here:

Read Modbus Integer Register