How to Configure Modbus TCP Communication on Siemens S7-1500 in TIA Portal

To establish Modbus TCP communication on a Siemens S7-1500 CPU in TIA Portal, execute the MB_CLIENT or MB_SERVER instruction, supply a data buffer pointer to MB_DATA_PTR, and pass a connection configuration data block typed as TCON_IP_v4 to the CONNECT parameter. For client operations, set ActiveEstablished to TRUE and specify the remote server IP and port; for server operations, set ActiveEstablished to FALSE and assign a unique local port (such as 502) for each concurrent connection instance.

Table of Contents

Technical Cause: Why Programmed Connections Fail

Modbus TCP on SIMATIC S7-1500 controllers uses open user communication over TCP/IP (TCON). When using programmed connections rather than hardware-configured connections, connection parameters are supplied dynamically through a data block typed as TCON_IP_v4.

Communication faults typically occur due to three core misconfigurations:

  1. Hardware Identifier Mismatches: Placing an invalid hardware ID (e.g., CPU system ID instead of the specific Ethernet interface/port Hardware Identifier) into TCON_IP_v4.InterfaceId results in TCON status errors such as 809B.
  2. Duplicate Connection IDs: Reusing the internal id parameter across multiple CONNECT structures causes resource conflicts within the CPU's internal communication stack.
  3. Invalid Data Pointers: Passing structured data types to MB_DATA_PTR when pointing to optimized block memory generates error 818B. Optimized blocks strictly require simple arrays (such as Array of WORD or Array of INT), whereas structured data requires standard (non-optimized) data blocks with absolute addressing.

Plaintext

+-----------------------------------------------------------------------+
|                         SIMATIC S7-1500 CPU                           |
|                                                                       |
|  +---------------------+                       +-------------------+  |
|  |     MB_CLIENT       |                       |   TCON_IP_v4 DB   |  |
|  |  (Instruction FB)   |                       | (Connection Parameters)|
|  |                     |                       |                   |  |
|  |  REQ -------------> | (Pulse Trigger)       | InterfaceId = 64  |  |
|  |  CONNECT ---------->|---------------------> | ID          = 1   |  |
|  |  MB_DATA_PTR -----> | (Array of WORD/INT)   | ConnectionType=11 |  |
|  +---------------------+                       | ActiveEstablished |  |
|                                                |   = TRUE (Client) |  |
|                                                | RemoteAddress     |  |
|                                                | RemotePort        |  |
|                                                +-------------------+  |
+-----------------------------------------------------------------------+

Prerequisites

Before implementing Modbus TCP communication on an S7-1500, verify the following configuration requirements:

  • Engineering Environment: Siemens TIA Portal (STEP 7 Professional V12 SP1 or higher).
  • Target Hardware: S7-1500 CPU (or S7-1200 CPU) with integrated PROFINET interface or a Communications Processor (e.g., CP 1543-1).
  • IP Addressing: PLC and remote partner configured on reachable IP subnets (or routed via a configured gateway).
  • Hardware Identifiers: Hardware ID of the Ethernet interface identified from system constants in TIA Portal.

Method 1: Configuring S7-1500 as a Modbus TCP Client

Use this method when the S7-1500 initiates requests to read or write data from a remote Modbus TCP server (slave device).

Procedure

  1. Create the Connection Data Block:

    • Add a new Data Block (e.g., Client_Conn_DB).
    • Add a variable named Connection and manually type TCON_IP_v4 in the Data type field.
      2. Configure TCON_IP_v4 Connection Parameters:

    • InterfaceId: Set to the Hardware Identifier of the specific PROFINET port or CP card (e.g., 64 or 260).

    • ID: Assign a unique CPU connection ID (e.g., 1).
    • ConnectionType: Set to 16#0B (or 11 decimal for TCP/IP).
    • ActiveEstablished: Set to TRUE (Client actively establishes the socket).
    • RemoteAddress: Enter the IP address of the remote Modbus server (e.g., 192.168.0.10).
    • RemotePort: Set to the target server's Modbus port (default is 502).
    • LocalPort: Leave set to 0 to allow the CPU to allocate an ephemeral local port dynamically.
      3. Create the Data Buffer:

    • Create a Data Block (e.g., Modbus_Read_Buffer).

    • Declare a variable as an array of words (e.g., Data : Array[0..9] of Word).
    • If using optimized block access, ensure the buffer is typed as an Array of WORD or Array of INT.
      4. Instantiate the MB_CLIENT Instruction:

    • Place MB_CLIENT in your program (e.g., in OB1 or a cyclic FB).

    • Map CONNECT to Client_Conn_DB.Connection.
    • Map MB_DATA_PTR to Modbus_Read_Buffer.Data.
    • Set MB_MODE, MB_DATA_ADDR, and MB_DATA_LEN according to the required Modbus operation.
    • Trigger REQ using a rising edge signal (e.g., a pulse from a timer or clock bit).

Operational Caution: Do not hold REQ continuously high. A permanent high signal will re-trigger requests as fast as possible, which can saturate local PLC communications and overload the remote server. Always use a rising edge pulse (|P|).

Method 2: Configuring S7-1500 as a Modbus TCP Server

Use this method when the S7-1500 acts as a passive server processing requests from remote Modbus TCP clients (master devices).

Procedure

  1. Create the Connection Data Block:

    • Create a Data Block (e.g., Server_Conn_DB) containing a variable typed as TCON_IP_v4.
      2. Configure TCON_IP_v4 Parameters for Passive Listening:

    • InterfaceId: Set to the Hardware Identifier of the communication port.

    • ID: Assign a unique CPU connection ID (e.g., 2).
    • ConnectionType: Set to 16#0B (TCP/IP).
    • ActiveEstablished: Set to FALSE (Server listens passively).
    • RemoteAddress: Set to 0.0.0.0 (accepts connections from any IP) or specify a dedicated client IP.
    • RemotePort: Set to 0.
    • LocalPort: Set to 502 (standard Modbus TCP port).
      3. Configure the Data Holding Area:

    • Create a Data Block holding the registers exposed to external clients.

    • Pass the buffer tag or absolute pointer to MB_DATA_PTR on the MB_SERVER block.
      4. Instantiate MB_SERVER:

    • Call MB_SERVER in a periodic program cycle.

    • Assign CONNECT to Server_Conn_DB.Connection.
    • Assign MB_DATA_PTR to your server data buffer.

Handling Multiple Concurrent Connections

A single MB_SERVER or MB_CLIENT call processes only one connection at a time. To communicate with multiple clients or servers simultaneously, adhere to these structural requirements:

Multiple Server Connections (Multiple Clients Accessing One PLC)

  • Call a separate instance of MB_SERVER for each simultaneous client connection.
  • Each MB_SERVER instance requires its own unique instance Data Block.
  • Each TCON_IP_v4 structure must use a unique ID and listen on a distinct LocalPort (e.g., Client 1 on Port 502, Client 2 on Port 503).

Multiple Client Connections (PLC Connecting to Multiple Remote Servers)

  • Call a separate instance of MB_CLIENT for each remote server destination.
  • Assign a unique instance DB and a unique TCON_IP_v4 structure with a unique ID parameter to each client instance.
  • Specify the respective remote IP address and remote port in each corresponding TCON_IP_v4 block.

Common Error Codes and Troubleshooting

Error Code (STATUS)Fault ContextRoot CauseCorrective Action818BMB_CLIENT / MB_SERVERInvalid MB_DATA_PTR structure or data type.Ensure MB_DATA_PTR references an Array of WORD or Array of INT when using optimized DBs. For complex structs, use non-optimized DBs with absolute addressing (P#...).809BTCON / ConnectionInvalid InterfaceId inside TCON_IP_v4.Set InterfaceId to the correct Hardware Identifier of the active CPU port or CP module (e.g., check System Constants in PLC tags).8086TCON / ConnectionThe connection ID is out of valid range or invalid.Ensure ID is a unique non-zero integer distinct from other active CPU connections.8383MB_SERVER / Read-WriteAccess outside the address area of MB_DATA_PTR.Verify that client read/write address ranges (MB_DATA_ADDR + MB_DATA_LEN) do not exceed the array bounds declared at MB_DATA_PTR.Unit ID IssuesRemote Device CommunicationNon-matching Modbus Unit ID (Slave ID).Modify the internal static parameter MB_Unit_ID inside the MB_CLIENT instance DB. Third-party gateways or devices (such as Schneider NOC cards) often require MB_Unit_ID to be set explicitly to 0 or 1 instead of the default 16#FF.

Verification

The procedure is successful when:

  1. The STATUS output on MB_CLIENT or MB_SERVER displays 16#0000 or 16#7002 (indicating an active, processing connection).
  2. The DONE pulse toggles TRUE following rising edge requests on MB_CLIENT.
  3. Data written to the client transmission buffer appears accurately in the destination server's memory block without generating communication fault flags.

Limitations and Operational Cautions

  • Optimized Access Restrictions: Passing structured variables to MB_DATA_PTR in optimized data blocks causes runtime pointer errors. Use standard arrays for optimized memory, or uncheck Optimized block access in the block properties if absolute pointers (P#...) are required.
  • Online Program Changes: Modifying TCON_IP_v4 parameters online while a connection is established can leave open sockets in an inconsistent state. Re-initialize the connection block or cycle the PLC program state to apply modified connection parameters cleanly.
  • Subnet Routing: When communicating with Modbus devices on a different IP subnet, ensure Use router is enabled in the S7-1500 CPU PROFINET interface configuration and populated with a valid gateway IP address.

Engineer FAQ

How do I locate the correct InterfaceId value for TCON_IP_v4?

Open the PLC tags table, select the System constants tab, and filter for the hardware identifier corresponding to your CPU's integrated PROFINET interface port (e.g., Local~PN_Interface_1 or Port_1). Use the integer value displayed in this table (such as 64, 65, or 260).

Can I create the TCON_IP_v4 data type manually without generating a TSEND_C instruction?

Yes. Open or create a global Data Block, declare a variable name, and manually type TCON_IP_v4 into the Data type field. TIA Portal automatically expands the full connection structure.

Why does my Modbus server reject incoming requests when testing with third-party software?

Check the MB_Unit_ID variable within the MB_CLIENT or MB_SERVER instance data block. Some third-party devices ignore requests if MB_Unit_ID is set to default 16#FF, requiring it to be set to 1 or 0.

Related Articles

Related Tags

Code: C277

6 respuestas