Implementation of simple Modbus protocol based on AVR128

Source: Internet
Author: User

Modbus communication protocol was developed by Modicon company in 1979 and applied to the Fieldbus protocol for industrial field control. The Modbus communication system consists of a chip node with programmable control and a common transmission line, which is designed for collecting and monitoring multi-node data. Modbus protocol adopts master-slave mode, the communication system has one host to monitor multiple nodes from the machine, the slave node supports up to 247. Each slave has its own independent slave address, and the address can be identified by the host machine.

The communication system that can support Modbus protocol has rs-232,rs-422,rs-485 and so on. At the same time, Modbus protocol has the characteristics of standard, open, free, simple frame format and so on by the engineers.

Modbus protocol data transmission using ASCII and RTU two modes. The ASCII transmission means the data is easy to understand, convenient and direct communication with the PC, but its disadvantage is to use a single byte to represent a data, the transmission of the same data need more frames and time. The RTU mode uses a compressed hexadecimal representation, a byte can compress two data, so that the same number of frames in the RTU mode can transmit more data.

Modbus Data frame check is divided into two ways: CRC cyclic redundancy check and LRC longitudinal redundancy check. Use the following Proteus Simulation atmega128. two tablets - serial communication between the serial communication, the running is simple Modbus agreement.

The simulation diagram looks like this:


Description: The U1 uses 1602 to display the received data. The data frame is in RTU mode, but for the convenience of 1602, the transmitted data frame message uses the ASCII representation of 0x3x (0,1,2,3 ... etc.), which can be directly fed into 1602 for display, thus facilitating simulation. The three keys in the middle of the picture are the three interrupts of the U2.



After pressing the U2 single-chip opportunity to send a frame of data, the data content is 0x30, 0x31,0x33,0x01,0x34,0x37.

FollowModbusProtocol0X30,0X31is the slave address that is represented,0X33is the character that represents the command,0x01represents the data field length,0x34represents a data field,0X37it's done.LRCchecksum. we simplify it in this way for the convenience of1602is displayed, where0x01is aASCIIspaces in the code"'can also be in1602in the display.

Single Chip microcomputer U1 of the PB Mouth and PE the port states show the LRC Check bytes and data field first byte.


when you press the button1 the time U2 send a data frame, U1 Receive and continue the check on the received data, if the checksum is correct, then the display.

Press the button1 The results are as follows


middle of on because it's a space. ASCII code value so 1602 displays a space. Because the data field I use the array to hold the data,theLCD Display code in the C language , ' " " to end the display, So the check byte is not displayed, I Project it to Port b , and Port b is 0X37. This also verifies the normal transmission of this simple Modbus protocol.

Press Move U2 of the Button2 can proactively produce a LRC checksum error. (I did not use the standard LRC check when I checked the code , but I did a cumulative check). U2 changes the checksum value when the data frame is filled, sends the changed data frame to U1,U1 Checking after receiving generates a checksum error. Press the button2 Results



from the virtual terminal we can see that we have manually changed the checksum value, from the 0x37 changed to 0xff u1 The data received is validated so that a checksum error is generated ... The actual check result is 0x37 0xff 0x37! = 0xFF,

Press the Button3 generates a data frame error. Press button3,U2 deliberately send an incomplete data frame. If the data frame is incomplete,U1 cannot wait for an infinite time. Therefore , the U1 uses timer 0 To determine the timeout, if more than 10ms, the data frame is still incomplete then judge this data frame is an error frame: The results are as follows.


in the event 3 we can make U2 does not send a checksum, causing the frame data to be incomplete. U1 waits for 10ms to determine that the data frame is a frame error.

the simulation program and simulation project are stored to the network address http://yun.baidu.com/share/link?shareid=368414814&uk=840368654&third=0

The project code containing U1 and U2 is in the respective main folder. The emulation file is in the Isis folder. Hope to be of help to you. 88

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.