Serial Communication Protocol _ serial communication

Source: Internet
Author: User
Tags crc32

Serial communication refers to the serial port bitwise (BIT) Send and receive bytes. Although it is slower than parallel traffic by byte (byte), the serial port can receive data with another line while sending data using one line.

Serial Communication Protocol (serial communication Protocol) for acquiring data from remote acquisition devices, sending and receiving bytes.

In serial communication, the commonly used protocols include RS-232, RS-422 and RS-485.


RS-232 protocol

The RS-232 (ansi/eia-232 standard) is a serial connection standard on IBM-PC and its compatible machines. Can be used for many purposes, such as connecting the mouse, printer or modem, but also can connect industrial instrumentation. For the improvement of drive and wiring, the transmission length or speed of RS-232 in practical application often exceeds the standard value. RS-232 is limited to point-to-point communication between PC serial and device. The RS-232 serial port communication is 50 feet farthest away.



Data: TXD (PIN 3): Serial output (transmit data) Rxd (pin 2): Serial data input (Receive data)
Handshake: RTS (pin 7): Send data requests (request to send) CTS (pin 8): Purge Send (clear to send) DSR (PIN 6): Data Send Ready (Ready)
DCD (pin 1): Carrier detection (data Carrier detect)
DTR (pin 4): Data Terminal Ready (Terminal Ready) Ground:
GND (pin 5): Ground other RI (pin 9): Ring indicator

Note: Generally we need is the 2,3,5 interface, connection is TXD connect rxd,rxd txd,gnd connect gnd (same ground, unified low level standard). Can own txd mouth rxd mouth, spontaneous self collection, test serial port is normal.

The basic tasks of serial communication interface:



CRC CHECKSUM algorithm

About the error check commonly used three kinds is parity check, accumulate and check and CRC check; The first two methods of verification believe that we have some understanding, so here is the emphasis on the CRC algorithm;

CRC (Cyclic redundancy check code): The basic idea of CRC algorithm is to use the transmitted data as a very long number of digits. Divide this number by another number. The resulting remainder is appended to the original data as a validation data. Transmission of the data and verify the data sent this, in the receiver we use the data received by using "mode two division" divided by the use of polynomial, if the remainder of 0 indicates that there is no error in the transmission process, if not 0 indicates that there are errors in the transmission.

Step1: Confirm the use of polynomials, usually we will adopt a fixed polynomial, common several kinds of generating polynomials such as:

Crc8=x8+x5+x4+x0

Crc-ccitt=x16+x12+x5+x0

Crc16=x16+x15+x2+x0

Crc12=x12+x11+x3+x2+x0

After the crc32=x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x1+x0 generation polynomial is determined, we can do algorithmic programming: From the previous introduction we know that CRC verification core is to achieve borrow division operations (that is, modulo II division). Here's an example of how to implement a CRC checksum.

Suppose our generation polynomial is: 100110001 (denoted is 0x31), i.e. CRC-8

The calculation steps are as follows:

(1) The CRC register (8-bits, than the generation of less than 1bit) to assign an initial value of 0

(2) Add 8 0 after the information stream to be transmitted

(3) while (data is not processed)

(4) Begin

(5) If (CRC Register First is 1)

(6) Reg = Reg XOR 0x31

(7) CRC registers the left one bit, reads a new data in the CRC registers the 0 bit position.

(8) End

(9) The CRC Register is the remainder we require.

In fact, real CRC calculations are usually somewhat different from what is described above. This is because the most basic CRC division has a very obvious flaw, that is, the beginning of the data flow to add some 0 does not affect the final check word results. The problem is very annoying ah, so the real application of CRC algorithm is basically in the original CRC algorithm based on a small change.

The so-called change, that is, added two concepts, the first is "remainder initial value", the second is "result of different or value."

The so-called "remainder of the initial value" is to calculate the CRC value at the beginning of the CRC register to an initial value. Result XOR or value is the final checksum of the value of the CRC register after the rest of the calculation is done.

The common three CRC standards are used in the following table for each parameter.

CCITT

CRC16

CRC32

Checksum bit width W

16

16

32

To generate the polynomial

X16+x12+x5+1

X16+x15+x2+1

x32+x26+x23+x22+x16+

x12+x11+x10+x8+x7+x5+

X4+x2+x1+1

Divisor (polynomial)

0x1021

0x8005

0x04c11db7

Remainder initial value

0xFFFF

0x0000

0xFFFFFFFF

Result XOR or value

0x0000

0x0000

0xFFFFFFFF

By adding these variants, the common algorithm description form is like this:

(1) Set the CRC register and assign it a value of "remainder initial value".

(2) The first 8-bit character of the data is different from the CRC register, and the result is stored in the CRC register.

(3) CRC registers one position to the right, MSB 0, remove and check the LSB.

(4) If the LSB is 0, repeat the third step, if the LSB is a 1,CRC register that is different from the 0x31.

(5) Repeat steps 3rd and 4th until all 8 shifts are completed. At this point a 8-bit data processing is completed.

(6) Repeat steps 2nd through 5th until all data are processed.

(7) The contents of the final CRC register and the "result XOR or value" after or after operation is the CRC value.

Sample Code implementation:
U16 crc16_check (u8 *data_ptr, u8 data_length)    {        /*polynomial*/       U16 crc_gen = 0xa001;        U16 crc;       U8 i, j;             /*init value of crc*/        crc = 0xffff;       if  (data_length != 0)        {           for  (i = 0 ;  i < data_length; i++)            {                crc ^=  (U16) ( Data_ptr[i]);               for  (j  = 0; j < 8; j++)                 {                    if  ((crc & 0x01)   == 0x01)                       {                        crc >>= 1;                            crc ^= crc_gen;                    }                     else                  {                        crc >>= 1;                    }                }/*end for*/           }/*end for*/        }/*end if*/       return crc;   } 




Features and formats: Start-and-end asynchronous protocol is characterized by a character transmission, and the transmission of a character always begins with the starting bit, with stop bit end, there is no fixed time interval between characters. The format is as shown in the figure. Each character is preceded by a starting bit (low level, logical value 0, the character itself has a 5~7 bit data bits, followed by a check bit (also can not check bit), the last is a, or a half, or two-bit stop bit, after the stop bit is an indefinite length of idle bits. Both the stop bit and the idle bit are specified as high levels (logical values) so that there must be a down-hop along at the beginning of the start bit. As can be seen from the diagram, this format is based on the start bit and stop bit to achieve the definition or synchronization of characters, it is called the starting protocol. At the time of transmission, the low of the data is before the high, and the following figure represents the waveform 1010001 of the ASCAII code that transmits a character E. When the least significant bit is written to the right, it is the ASCII code 1000101=45h of E.


The role of the start/stop position: The starting bit is actually attached as a contact signal, and when it becomes low-power, tell Deceptions to start the transmission. Its arrival, means that the next data bit is coming, to be ready to receive. The stop bit flags the end of a character, and it appears, indicating that a character has been transmitted. This will provide a sign of when to start sending and receiving, and when to end the communication between the two parties. Before the transmission begins, both sides of the sending and ending format (including character data bit length, stop bit number, check bit and parity check or even parity) and data transmission rate are uniformly stipulated. After the transmission begins, the receiving device continuously detects the transmission line to see if there is a starting bit coming. When a series of "1" (stop or idle bits) is received, a down-hop is detected, indicating that the starting bit appears, and the starting bit is confirmed, it begins to receive the specified data bit and parity bit and stop bit. After processing, the stop bit is removed, the data bits are assembled into a parallel byte, and after checking, no parity error is taken to receive a character correctly. After one character is received, the receiving device continues to test the transmission line, monitoring the arrival of the "0" level and the beginning of the next character until all data is transmitted.

From the above work process can be seen, asynchronous communication is transmitted by character, each transmission of a character, with the start bit to notify the deceptions, so as to recheck the synchronization between the transceiver. If there is a slight deviation between the clock frequency of the receiving device and the sending device, this will not result in dislocation due to the accumulation of the deviation, and the idle bits between the characters also provide a buffer for this deviation, so the reliability of the asynchronous serial communication is high. However, due to the addition of the starting bit and stop bit before and after each character, the transmission efficiency is reduced, only about 80%. Therefore, the start-start protocol is generally used in the case of slower data rates (less than 19.2kbit/s). In the high-speed transmission, the general need to adopt synchronization protocol.


The so-called transmission rate refers to the number of transmissions per second, transmission rate is often called baud rate. A standard baud rate series has been established internationally, and the standard baud rate is also the most commonly used baud rate. Symbol bps (bite pre second)


Extracurricular knowledge:

Institute of Electrical and Electronic Engineers (IEEE, full name is Institute of Electrical and Electronics Engineers)

International Organization for Standardization (International Organization for standardization,iso) abbreviation ISO




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.