This is a study of C #, WinForm's self-review process, to find the shortcomings of the existence, but also to promote their continued learning.
University communication major After graduation, entered a power technology company engaged in software development work, mainly used in Delphi language for power Communication protocol host computer development. Because the host computer needs to communicate with the next computer to test, and in fact there are not so many ready-made devices for you to borrow debugging, and the company slowly began to implement C#/WPF to do some custom software, so want to learn C #, just now there is a demand for themselves-the upper machine program debugging difficulties, Therefore, from the most easy to start the WinForm program into, slowly deepen the understanding of C # language. So I intend to do a common protocol Modbus data source tools, so there is this article.
I. Introduction of Modbus protocol
{from Baidu Encyclopedia}
Modbus was invented in 1979 by Modicon (now a brand of Schneider Electric), the First Bus protocol in the world to be truly used for industrial sites. Modbus Network is an industrial communication system, which is composed of a programmable controller and a computer with intelligent terminal connected by a public line or a local dedicated line. The system structure includes both hardware and software. It can be applied to a variety of data acquisition and process monitoring. The Modbus network has only one host and all communications are sent by him. The network can support up to 247 remote slave controllers, but the number of slaves actually supported depends on the communication device used. With this system, each PC can exchange information with the central host without affecting the PC to perform its own control tasks. Second, the characteristics of Modbus agreement {from Baidu Encyclopedia} (1) Standard, open, users can free, rest assured that the use of Modbus protocol, do not need to pay license fees, will not infringe intellectual property rights. At present, the support of the Modbus manufacturers more than 400, supporting the Modbus products more than 600 kinds. (2) Modbus can support a variety of electrical interfaces, such as RS-232, RS-485, etc., can also be transmitted on a variety of media, such as twisted pair, optical fiber, wireless and so on. (3) The frame format of Modbus is simple, compact and easy to understand. User easy to use, vendor development simple. Three, Modbus packet structure description
Each Modbus packet consists of the following parts:
(1) Address field
(2) Function Code field
(3) Data domain
(4) Check domain
Address field: 0~247,0 is generally used as a broadcast address, 1~247 as the address number of the device (lower machine).
Function code: Used to confirm the specific function of the packet message. (Common 03 yards, 10 yards)
0x03 |
Read Register |
0x10 |
Write register |
0x05 |
Remote operation |
Data field: Modbus data field is variable in length, depending on its specific function. Check domain: CRC16 check method, 2 byte length. Four, the serial port, the Ethernet port Modbus message serial port: Modbusrtu Ethernet: Modbusrtu, the message format is: [Transaction meta identifier (2 bytes)]+[Protocol ID (2 bytes)]+[Length (2 bytes)]+MODBUSRTU-CRC checksum (2 bytes) in a word, MODBUSTCP than the MODBUSRTU message, more than 6 bytes of the message header, less the end of the 2-byte CRC check the following main to Modbusrtu description: 0x03 code
Read Register packet format |
Response format |
Slave address |
1 bytes |
Slave address |
1 bytes |
function code |
1 bytes |
function code |
1 bytes |
Register start Address High |
1 bytes |
Number of bytes N |
1 bytes |
Register Start Address Low |
1 bytes |
Data1 High |
1 bytes |
High number of registers |
1 bytes |
Data1 Low |
1 bytes |
Number of registers low |
1 bytes |
...... |
|
|
|
DATAN/2 High |
1 bytes |
|
|
DATAN/2 Low |
1 bytes |
CRC Check Code Low |
1 bytes |
CRC Low |
1 bytes |
CRC Check Code High |
1 bytes |
CRC High |
1 bytes |
0x10 yards
Write Register Packet format |
Response format |
Slave address |
1 bytes |
Slave address |
1 bytes |
function code |
1 bytes |
function code |
1 bytes |
Register start Address High |
1 bytes |
Register start Address High |
1 bytes |
Register Start Address Low |
1 bytes |
Register Start Address Low |
1 bytes |
High number of registers |
1 bytes |
High number of registers |
1 bytes |
Number of registers low |
1 bytes |
Number of registers low |
1 bytes |
Number of bytes (n) |
1 bytes |
CRC Check Code Low |
1 bytes |
Data1 High |
1 bytes |
CRC Check Code High |
1 bytes |
Data1 Low |
1 bytes |
|
|
...... |
|
|
|
Data (N/2) high |
1 bytes |
|
|
Data (N/2) Low |
1 bytes |
|
|
CRC Check Code Low |
1 bytes |
|
|
CRC Check Code High |
1 bytes |
|
|
0x05 yards
Request format |
Response format |
Slave address |
0x11 |
Slave address |
0x11 |
function code |
0x05 |
function code |
0x05 |
Preset Address high |
0 x 23 |
Preset Address high |
0 x 23 |
Preset address Low |
0 x 8C |
Preset address Low |
0 x 8C |
Set the data high |
0 x FF |
Set the data high |
0 x FF |
Low Set data |
0x00 |
Low Set data |
0x00 |
Low CRC Check Code |
44 |
Low CRC Check Code |
44 |
CRC Check Code High |
C5 |
CRC Check Code High |
C5 |
Simple Modbus Protocol Data Source Tool implementation (i) WinForm