Single chip microcomputer Modbus communication source code

Source: Internet
Author: User
Tags time interval

好久没有写些什么了,最近在出差也没做关于Linux的东西。由于是做自动化的因此最近做了一块单片机的板子要作为MODBUS从站来与上面的触摸屏进行显示功能还不是很完善但是MODBUS功能的模块程序已经写好经过测试基本可以用。具体要求是下面有2个AI和4个DO量需要检测和控制,我使用的是90C52单片机模拟量采集部分用的OP07的放大器电路,这里就不详细说明了(有需要的可以留言,有图纸),最麻烦的就是MODBUS通讯部分。一开始我根据MODBUS标准协议规定来做了程序但是不能通讯经过总结主要问题出在二个地方第一:crc校验计算不正确第二:发送的程序太大导致发送时间过长主站端认为超时(我自己认为的原因)

After careful study found that the CRC verification procedures I can not understand the network has a lot of ready-made to use it is very good, after the streamlining process can be communicated but there is a very important problem is that the problem of time-out judgment has not been able to deal with the following time issues summarized
Modbus character and data frame interval time problem
1, the provisions of the Modbs agreement are as follows
In RTU mode, the message has a space interval of at least 3.5 characters in time to differentiate between

The entire message must be sent in a continuous character stream, if the idle between two characters is greater than 1.5 character time, the message frame is considered incomplete and should be discarded by the receiving point.

Note that the implementation of the RTU receive driver, due to 1.5T and 3.5T timing, implies a large number of interrupt management. At high communication rates, the CPU burden is increased, so the two timings must be strictly observed at <=19200PBS, and a fixed value of 2 timing should be used for the >19200PBS case. The recommended time-out between characters is t1.5 to 750us; inter-frame time-out is 1.75ms
The above is the Modbus protocol provisions, but in the actual use of 1.5T there is no need to pay attention to. The 3.5T between frames and frames needs to be handled by the program. Since the RTU mode does not have a start character and Terminator, two packets can only be distinguished by time interval. In the actual use of the instrument in the factory is generally spaced 40ms, 50ms or even longer time to read the data, this interval is completely more than T3.5;
Let's say that the baud rate is 9600bps to send the request to fetch data:
D5 CA (formatted as 8 data bits 1 stop bit)
Then each byte contains a starting bit, a stop bit, which is 10 bits.
Then sending this sequence of commands takes time: 8x10/9600x1000=8.3ms
That is, the first frame is sent at 8.3ms and the 3.5T time is 3.5x10/9600x1000=3.65ms
So the second frame of data starts sending at least 12ms (8.3+3.65=11.95MS)
After modifying the program can finally achieve the desired effect but I use three software debugging can read the data (Serial debugging assistant, Modbussim, Modscan) which only modscan receive data when the flash red but can read the data do not know why the adjustment of his poll time or not the other two software normal can also write data. Thought for a moment that these two days have done all these things. The program is written in Keil with the need to contact me, can be used directly.

Single chip microcomputer Modbus communication source code

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.