Introduction to communication protocol programming

Source: Internet
Author: User

The first example is nonsense:

From the company to the present, we are experiencing and growing ...... From the beginning to write 25160 drivers (now, this is actually a task of enhanced version interview nature), to subsequent protocol programming, and then to the current core program upgrade development, i'm all excited! Every time I look back on these times, I will feel inexplicable joy and pride. Although it is 25160 introduced to me, but for me, the contract programming must make me feel the most cordial and emotional. Today, I found a notebook by chance. I suddenly felt that it should not just be in my notebook, so I sent it to my future generations to encourage them.

Note: This article is based on many online articles and BBS comments. Except for the author or source of Nanjing Disa Xiaofan (), I only edited, edited, and modified a few times.

/*********************************** Domineering splitters ************************************* */

Introduction to communication protocol programming: Starting from Anatomy of a sparrow

I found that the fastest way to get started is the so-called "Anatomy of a sparrow ".
Find a typical polling (Q & A) Small protocol, such as a render screen protocol, with the Protocol and source code. Read the Protocol itself first. Then analyze the source code by referring to the Protocol.
Source code analysis should begin with the data structure. Understand the general meaning of these variables from the naming and Annotation of variables. A well-used program should have clear comments and concise descriptions. It is quite good for new users to understand the meaning of these variables by 5%. Even experts familiar with this programmer's habits can only learn about 8% at most. For variables that you do not understand, you can only observe their usage and functions in the program.
After learning about the data structure, you can analyze the program.
The program analysis should first be macro-visual and then microscopic.
The so-called macro is to understand the program structure, process, and call relationship.
For a Protocol, its basic functions include initialization, Packet Assembly and sending, packet receiving, packet parsing, and data receiving.
Microscopic analysis is to analyze each function module. For example:
Packet Assembly first determines which packets are sent. Functions can be divided into: Call telemetry, call remote message, school time, and so on; by mechanism can be divided into: loop query message, timed query message, insert message and so on. After deciding what message to send, the message is assembled according to the Protocol's prescribed frame format. After assembly, you can send the message.
Message sending is generally completed in the interrupt program. Generally, you only need to call a function to fill in the sending buffer.
Message receipt is relatively complicated.
The binary bytes received from the serial port are sequentially filled with a ring receiving buffer, which should be completed in the reception interrupt. It makes no sense before parsing and analyzing packets. I call it a data stream. The function of the protocol is to extract the complete Protocol packet from the data stream and fill it in the processing buffer. Note: Two buffers are mentioned here: the receiving buffer and the processing buffer. A good program should separate the two buffers. It requires high efficiency and no frame loss. There are many techniques and many details to pay attention. The collection process includes searching for synchronization headers, calculating the frame length, copying to the processing buffer, and verifying the frame integrity. I will not elaborate on these processes here. I will introduce them to my friends later. In addition, for all different protocols, the functions are basically the same, except that the synchronization header is different, the frame length and position are different, and the verification method is different. I have made a general module for this purpose. Through configuration, it can adapt to most of the current Protocol packets. This saves a lot of effort when preparing different protocol procedures.
After receiving a complete Protocol packet, You can parse it. Call different processing functions, such as telemetry and remote signals, based on the message type. The functions of these functions are to extract the data of interest from the received message, process the data, and store the data into the database.

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.