C# communicates with the ModbusTcp server of Siemens PLC1500 2--ModbusTcp protocol

Modbus TCP is one of the more and more popular industrial control system communication protocols in recent years. Compared with other communication protocols, Modbus TCP has fast communication speed, high reliability, strong compatibility, and is suitable for the transmission of analog or digital signals. Read Before this article, you must be familiar with the Modbus protocol and understand the tcp network.

1. What is Modbus TCP?

Modbus TCP is an Ethernet protocol that encapsulates the Modbus communication protocol in TCP/IP packets for data transmission. It is usually used in industrial control systems, such as data communication between PLCs, sensors and other devices. In the Modbus protocol, data is communicated through a set of registers. These registers can be input registers, output registers, holding registers, and coil registers. They are used for different functions. There are two roles involved in communication: server and client. Usually, the client actively connects to the server.

2. Modbus TCP server and client

1. Modbus TCP server

Modbus TCP server is a device waiting for connection in the network, it listens to port 502, waits for client connection, and provides Modbus protocol services. The server can read and write data. Several important function codes provided by the server, that is, function codes:

1) Read input register: This function code is used to read the Modbus register called input register.

2) Read holding register: This function code is used to read the value of holding register.

3) Write a single holding register: This function code is used to write the value of a single holding register.

4) Write multiple holding registers: This function code is used to write the values ​​of multiple holding registers.

Common Modbus TCP server devices include PLC (Programmable Logic Controller), Raspberry Pi and Raspberry Pi, etc. Suppose we have a PLC device as a Modbus TCP server, it has an input register and a holding register, we can use the Modbus TCP protocol to read and write these registers, this project uses Siemens PLC1500 as a server

2. Modbus TCP client

A Modbus TCP client is a device that actively connects to a Modbus TCP server and provides read-only or read-write services. Once the connection is established, the client can send a data request to the server, read or write the value of the Modbus register, and several important function codes provided by the client:

1) Read input register: This function code is used to read the value of the input register.

2) Read holding register: This function code is used to read the value of holding register.

3) Write a single holding register: This function code is used to write the value of a single holding register to the server.

4) Write multiple holding registers: This function code is used to write the values ​​of multiple holding registers to the server. 

Common Modbus TCP clients can be PLC devices, C#Winform programs, JAVA programs, Python programs, tool software, etc. This project is a C#Winform program

For the Modbus TCP client, it does not generate data by itself, and its data is obtained from the server. In order to obtain data, it must initiate a data request to the server. In the Modbus TCP protocol, the server generally does not actively send data out, and the server needs to decide whether to send data and which data to send according to the data request from the client. This process is shown in the figure below:

 It is not difficult to see from the above figure that first, the client needs to actively initiate a data request, and the data request initiated by the client needs to tell the server what data it requests. After the server receives the data request, the server parses the client's request and returns the data according to the client's request. After receiving the data response, the client parses the data, thus completing a data communication between the client and the server.

Function codes such as 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x0F and 0x10 are implemented in the Modbus TCP protocol stack.

 3. TCP server and client

1. TCP server

A TCP server is a program or device that waits for client connections. Once the client connects, it creates a new socket socket for maintaining the communication connection with the client. Typically, the server will listen on a fixed IP address and port number.

Several important functions provided by the TCP server:

1) Listening (listen): This function is used to listen to the port on the network to wait for the connection request from the client. This listening is an infinite loop unless it is closed manually or terminated abnormally.

2) Receive (accept): This function is used to accept the connection request from the client. All the received data is placed in the buffer, and the data is in the form of a byte array. After receiving the data, it needs to be converted into ascii or utf- 8 formats.

3) Send (send): This function is used to send data to the client, and the sent data must also be sent in the form of byte array byte[], which can be ascii encoding or UTF-8 encoding.

2. TCP client

A TCP client is a device or program that actively establishes a connection and sends data requests. Clients are typically used to send requests to a server to read or write data. Clients can establish connections with multiple servers and can use different port numbers

Several important functions provided by the TCP client:

1) Create socket (socket): This function is used to create a new socket for the client to use.

2) connect (connect): This function is used to connect to a server.

3) Send (send): This function is used to send data to the server.

4) Receive (recv): This function is used to receive data from the server

4. Comparison of the two

Generally speaking, the working principles of Modbus TCP server and client, as well as TCP client and server, are basically the same, and both need to use Socket sockets for communication. The difference is that the Modbus TCP protocol provides a more professional Modbus data reading and writing protocol, which is convenient for data transmission in industrial control systems. It can be seen from this that the Modbustcp communication is based on the tcp communication to realize the transmission of the modbus protocol, so the traditional tcp communication is the basis.

1. Protocol structure

Modbus TCP is an extension based on the Modbus protocol, which uses the TCP/IP protocol for communication.

Modbus protocol is a serial communication protocol, while Modbus TCP is an application layer protocol based on TCP/IP protocol. Its protocol structure is simple and clear, including application layer, transport layer and network layer.

The TCP/IP protocol is a four-layer protocol structure, including the application layer, transport layer, network layer and data link layer.

2. Communication speed

The communication speed of Modbus TCP is relatively slow, mainly because it uses a longer frame structure and data format, which requires more data analysis and processing.

The TCP/IP protocol has a higher communication speed, mainly because its data transmission format is more concise and clear, and it can quickly exchange and transmit data.

3. Network topology

Modbus TCP adopts Ethernet-based star network topology, which can support multiple devices to communicate at the same time.

The TCP/IP protocol supports a variety of network topologies, including star, ring, tree and other structures, which are more flexible and scalable.

4. Data transmission method

Modbus TCP communication adopts half-duplex mode, that is, only one party can transmit data at the same time.

The TCP/IP protocol adopts full-duplex mode, that is, both parties can transmit and receive data at the same time, which improves the communication efficiency.

5. Application scenarios

The Modbus TCP protocol is mainly used in industrial automation control systems and machinery equipment, and supports a wide range of equipment and system integration.

The TCP/IP protocol is widely used in the Internet, local area network and wide area network to support various types of data transmission and communication requirements.

V. Summary

modbus tcp protocol

Modbus TCP is an application layer protocol based on the TCP/IP protocol, which is an extension of the Modbus protocol. The Modbus protocol is a serial communication protocol originally developed by Modicon in 1979 for communication between devices in industrial automation control systems. Modbus TCP converts the Modbus protocol into an Ethernet-based TCP/IP protocol to support a wider range of equipment and system integration. The Modbus TCP protocol uses the transport layer and network layer of the TCP/IP protocol stack to enable data communication between devices.

The Modbus TCP protocol has the advantages of simplicity, reliability and wide application. It can not only support communication between devices, but also allow remote access and control via the Internet. The Modbus TCP protocol has a wide range of application scenarios, including industrial automation control systems, energy management, environmental monitoring, building automation and other fields. At the same time, the Modbus TCP protocol is also an open protocol, which can be easily integrated with other communication protocols.

What is the tcp/ip protocol

The TCP/IP protocol is one of the most commonly used protocols on the Internet, and it is the core protocol of the Internet protocol family. The TCP/IP protocol is a set of Internet communication protocols developed by the US Defense Advanced Research Projects Agency (ARPA) in the early 1970s, aiming to provide standardized communication protocols for global computer networks. The TCP/IP protocol provides a reliable, flexible, and universal data transmission method, enabling different computers and networks on the Internet to communicate and exchange information with each other.

The TCP/IP protocol consists of two main parts: Transmission Control Protocol (TCP) and Internet Protocol (IP). Among them, the IP protocol is mainly responsible for the transmission of data packets in the network, including functions such as routing, fragmentation, and assembling of data packets. The TCP protocol is a transport layer protocol based on the IP protocol. It is mainly responsible for establishing a reliable connection at both ends of the communication, and performing operations such as segmentation, assembly, and inspection of data. The TCP/IP protocol also includes many other protocols, such as User Datagram Protocol (UDP), Network Time Protocol (NTP), and Domain Name System Protocol (DNS).

The TCP/IP protocol is an open standard protocol, which has a wide range of application scenarios, including various network environments such as the Internet, local area network, and wide area network. The TCP/IP protocol provides a common communication standard that enables different types of computers and devices to communicate and exchange information with each other. At the same time, the TCP/IP protocol is also an extensible protocol, which can be easily integrated with other communication protocols to achieve more complex communication and data processing functions.

 

Guess you like

Origin blog.csdn.net/hqwest/article/details/132388470