MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do you configure Modbus TCP communication on an S7-1200?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

How do you configure Modbus TCP communication on an S7-1200?

Modbus TCP on an S7-1200 comes down to letting the CPU act as a client or a server, exchanging frames through instruction blocks and mapping internal data areas onto Modbus register addresses. Configuration has four steps: decide the role, set the connection parameters, call the instruction blocks, and check the address mapping.

How Modbus TCP differs from Modbus RTU

Modbus RTU runs over a serial port, addressing devices by station number and carrying a CRC. Modbus TCP runs over Ethernet, distinguishing devices by IP address and port; the unit identifier survives in the frame but the CRC does not, because Ethernet checks the frame itself. So configuration shifts from baud rate and parity to IP address, port and connection resources. The function codes and the data model are the same, so the way you think about register mapping carries over.

Decide the role first: client or server

The side that initiates requests and reads or writes the other's data is the client; the side that waits to be asked and supplies data is the server. Having the PLC act as client to read meters and instruments is common practice; when the PLC acts as server for a host computer, you only configure it and let it listen on the port, with no client instruction to call. Get the role wrong and both the instruction block and the direction of data are wrong.

Hardware and connection configuration

Start by giving the CPU's Ethernet port an IP address and subnet mask in the device configuration, making sure it is on the same subnet as the partner. Then, in the background data block of the instruction, specify the connection type, the connection ID, the partner IP address and the port (Modbus TCP uses port 502 by default). The connection ID must be unique within one CPU; several connections must not share a single ID.

Calling the instruction blocks

Use MB_CLIENT for the client and MB_SERVER for the server. MB_CLIENT takes the connection ID, request type, remote address, data length and a pointer to the local data area; it works on a polling basis, so it must be driven by the REQ trigger together with the DONE, BUSY and ERROR status bits, and must not fire a fresh request every scan. For several slave devices, give each its own connection or poll them in turn, so that concurrent requests on one port do not collide.

Address mapping and data conversion

Modbus register addresses are normally numbered from 1, whereas a PLC data-area pointer starts at 0, so an offset of one often has to be applied. A register holds 16 bits, so a 32-bit float or integer occupies two registers and the word order matters; on some devices it is the reverse of the PLC, and the value read back looks absurd until the words are swapped. Once the mapping is built, verifying with a small number is the safest check.

Configuration parameters and common values at a glance

ParameterMeaningCommon settingConsequence of a mistake
RoleClient or serverDecided by the data directionInstruction and data direction reversed
IP addressNetwork address of the deviceSame subnet as the partnerConnection cannot be established at all
PortCommunication port numberStandard is 502Connection refused or timed out
Connection IDConnection number inside the CPUUnique per connectionConnection conflict or reuse error
Unit identifierPoints to a device behind a gatewayUsed only in gateway setupsWrong device addressed
Function codeRead or write methodChosen by register typeWrong area read
Start addressFirst register numberWatch the 1-based or 0-based conventionEverything offset by one
Data lengthNumber of registersWithin the device limitFrame rejected
Word orderHigh/low word arrangementPer the device manual32-bit values scrambled

Notes

Modbus TCP is a master/slave protocol, and one connection handles only one request at a time, so the refresh rate depends on the polling cycle; do not expect the synchronous behaviour of a real-time bus. For several devices exchanging data at speed, consider PROFINET instead, or poll in groups. During commissioning, verify the register addresses with dedicated Modbus test software before entering them in the instruction block.

References

The Modbus application-layer protocol and data model follow the GB/T 19582 series (equivalent to the Modbus specification), and its implementation over Ethernet follows the corresponding national standard in that series and the relevant part of IEC 61158. Programming and instruction use follow GB/T 15969.3 (identical to IEC 61131-3). Ports and register addresses of a specific device are as stated in the manufacturer's communication manual, and the electrical connection, protection and earthing of the communicating equipment follow GB 5226.1 and the equivalent international standard IEC 60204-1.

Points to watch

⚠️ Three common traps:
(1) Forgetting the 1-based register convention and skipping the offset of one, so the data read is shifted by a whole row;
(2) Giving several slaves the same connection ID, or re-triggering a request within one scan, which causes a connection conflict;
(3) Ignoring word order, so a 32-bit value comes back off by an enormous factor and the instrument gets blamed.

Stop the machine, isolate, prove dead and apply lock-out/tag-out before changing configuration or wiring. When altering network parameters, avoid clashing with an existing production network, and after commissioning update the network address register and restrict external write access.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL