Simple Modbus Data Source Tool implementation (1) WinForm and modbuswinform
This is a process of self-review for learning C # And Winform. It is used to find out the shortcomings and to promote continuous learning.
After graduating from college communication, I joined an electric power technology company engaged in software development. I mainly used Delphi to develop the upper computer of the electric power communication protocol. Because the upper computer needs to communicate with the lower computer for testing, but in fact there are not so many ready-made devices for you to borrow for debugging, and the company is slowly starting to implement C #/WPF for some customized software, so I want to learn about C #, but now there is also a demand -- it is difficult to debug the host computer program, so I will start with the winform program that is the easiest to start, gradually deepen your understanding of the C # language. As a result, we plan to create a data source tool with common Modbus protocol, so we have this article.
1. Introduction to Modbus protocol
{From Baidu encyclopedia}
Modbus was invented in 1979 by Modicon (now a brand of Schneider Electric) and is the world's first bus protocol truly used in industrial field. ModBus network is an industrial communication system, which is composed of programmable controllers with smart terminals and computers connected through public lines or local dedicated lines. The system structure includes both hardware and software. It can be applied to various data collection and process monitoring. The ModBus network only has one host, and all communications are sent by him. The network can support as many as 247 remote slave controllers, but the actual number of slaves is determined by the communication device used. With this system, each PC can exchange information with the central host without affecting each PC to execute its own control tasks. Ii. Modbus protocol features {from Baidu Baike} (1) standard and open. Users can use Modbus protocol free of charge and rest assured. No license fee is required or intellectual property rights are infringed. Currently, more than 400 manufacturers support Modbus, and more than 600 Modbus products are supported. (2) Modbus can support a variety of electrical interfaces, such as RS-232, RS-485, etc., can also be transferred in a variety of media, such as twisted pair wire, optical fiber, wireless and so on. (3) the Modbus frame format is simple, compact, and easy to understand. Users are easy to use and vendor development is simple. Iii. Description of Modbus Packet Structure
Each Modbus Packet consists of the following parts:
(1) Address domain
(2) function code domain
(3) data domains
(4) verification domain
Address field: 0 ~ 247,0 is generally used as the broadcast address, 1 ~ 247 is the address number used by the device (lower computer.
Function Code: used to confirm the specific functions of the packet. (Common code 03 and 10)
0x03 |
Read register |
0x10 |
Write register |
0x05 |
Remote Control |
Data field: the length of the Modbus data field varies depending on its specific functions. Verification domain: CRC16 verification method, two bytes in length. Iv. Serial Port and Ethernet Modbus Packet serial port: ModbusRTU Ethernet: ModbusRTU; Message format: [Transaction metadata identifier (2 bytes)] + [Protocol identifier (2 bytes)] + [length (2 bytes)] + ModbusRTU-CRC Check (2 bytes) All in all, ModbusTCP has 6 more packet headers than ModbusRTU, the CRC check with two bytes at the end is missing. The following mainly describes the ModbusRTU: 0x03 code.
Read register data packet format |
Response format |
Slave site address |
1 byte |
Slave site address |
1 byte |
Function Code |
1 byte |
Function Code |
1 byte |
High starting address of register |
1 byte |
Bytes n |
1 byte |
Low starting address of register |
1 byte |
Data1 high |
1 byte |
High number of registers |
1 byte |
Data1 low position |
1 byte |
Low register count |
1 byte |
...... |
|
|
|
Datan/2 high |
1 byte |
|
|
Datan/2 low |
1 byte |
Low CRC verification code |
1 byte |
CRC low |
1 byte |
High CRC verification code |
1 byte |
High CRC |
1 byte |
0x10 code
Write register data packet format |
Response format |
Slave site address |
1 byte |
Slave site address |
1 byte |
Function Code |
1 byte |
Function Code |
1 byte |
High starting address of register |
1 byte |
High starting address of register |
1 byte |
Low starting address of register |
1 byte |
Low starting address of register |
1 byte |
High number of registers |
1 byte |
High number of registers |
1 byte |
Low register count |
1 byte |
Low register count |
1 byte |
Bytes (n) |
1 byte |
Low CRC verification code |
1 byte |
Data1 high |
1 byte |
High CRC verification code |
1 byte |
Data1 low position |
1 byte |
|
|
...... |
|
|
|
Data (n/2) high |
1 byte |
|
|
Data (n/2) low |
1 byte |
|
|
Low CRC verification code |
1 byte |
|
|
High CRC verification code |
1 byte |
|
|
0x05 code
Request format |
Response format |
Slave site address |
0x11 |
Slave site address |
0x11 |
Function Code |
0x05 |
Function Code |
0x05 |
Preset address height |
0x23 |
Preset address height |
0x23 |
Low preset address |
0 x 8C |
Low preset address |
0 x 8C |
Set Data height |
0 x FF |
Set Data height |
0 x FF |
Set low data |
0x00 |
Set low data |
0x00 |
Low CRC verification code |
44 |
Low CRC verification code |
44 |
High CRC verification code |
C5 |
High CRC verification code |
C5 |