Modbus TCP is connected to the network port, and multiple slave stations can access the master station at the same time, and different Modbus transactions at the same time can be distinguished through the Modbus transaction identification, which is different from Modbus ASCII and Modbus RTU.
1. Access mode:
The Modbus client usually enters the IP address and port number of the Modbus server to establish a TCP connection, and then determines which slave device to access according to the slave address.
2. Address allocation rules
There are 255 types of Modbus slave addresses, but 0 is used for broadcast addresses, 1 to 247 are used for slave addresses, and the remaining addresses are reserved.
3. Modbus TCP format
3.1 Meaning of MBAP packet header
The MBAP message header is the message that loads the beginning of the Modbus frame. It contains 7 bytes in total, as follows:
1) The two bytes of the transaction identifier
are used to distinguish multiple Mobus requests sent at the same time, so you can use In the self-increment method, every time a Modbus request is sent, the value is increased by 1, and then reaches the maximum value, and then the value is changed to 1 again, and the cycle repeats.
2) Protocol identifier
These two byte values are 0, 0 represents the Modbus protocol
3) Length
These two bytes represent the unit identifier (slave station address) and the total number of bytes occupied by all subsequent data
4) The unit identifier
is the address of the slave station
3.4 Function code
The function of the function code is to specify the specific purpose of the master station to access the slave station, whether to read data or write data or others. There are three types of function codes, public function codes, used to define function codes, and reserved function codes. Common function codes are generally encountered in the development process of the computer, and the value range of the public function codes is as follows:
Regarding the application of each function code in Modbus TCP, the details are as follows:
1. C# Modbus communication from beginner to proficient ( 22)——Modbus TCP (0x01 function code)
2. C# Modbus communication from entry to proficiency (23)——Modbus TCP (0x02 function code)
3. C# Modbus communication from entry to proficiency (24)——Modbus TCP (0x03 Function code)
4. C# Modbus communication from entry to proficiency (25) - Modbus TCP (0x04 function code)
5. C# Modbus communication from entry to proficiency (26) - Modbus TCP (0x05 function code)
6. C# Modbus communication From entry to proficiency (27) - Modbus TCP (0x06 function code)
7. C# Modbus communication from entry to proficiency (28) - Modbus TCP (0x0F function code)
8. C# Modbus communication from entry to proficiency (29) - —Modbus TCP (0x10 function code)
3.5 Data
The data will change according to different function codes. The details are as follows:
1. C# Modbus communication from entry to master (22)——Modbus TCP (0x01 function code)
2. C# Modbus communication from entry to master (23)—— Modbus TCP (0x02 function code)
3. C# Modbus communication from entry to proficiency (24) - Modbus TCP (0x03 function code)
4. C# Modbus communication from entry to proficiency (25) - Modbus TCP (0x04 function code)
5 , C# Modbus communication from entry to proficiency (26) - Modbus TCP (0x05 function code)
6. C# Modbus communication from entry to proficiency (27) - Modbus TCP (0x06 function code)
7. C# Modbus communication from entry to proficiency (28)——Modbus TCP (0x0F function code)
8. C# Modbus communication from entry to proficiency (29)——Modbus TCP (0x10 function code)