An Automatic Detection Method for Serial Communication Baud Rate

Publisher:莫愁前路Latest update time:2012-06-06 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Serial communication is the main communication method between the terminal and the host. The communication baud rate is generally 1800, 4800, 9600 and 19200. There are many types of terminals, and there are many choices for their communication rates. How does the host determine the communication rate of the terminal? This article gives a simple and easy method: set the host's receiving baud rate (take 9600 baud as an example), the terminal sends a specific character (take carriage return as an example), and the host can determine the terminal's communication baud rate based on the received character information. This article describes this method in detail.

1 Basic method
The ASCII value of the carriage return character is 0x0D. During serial communication, a start bit and a stop bit are added, and the bit transmission order is generally to transmit the low bit first and then the high bit. At this time, the binary representation of the carriage return character is:

Figure 1 The bit sequence of the carriage return character

The transmission time of a binary bit in serial communication (denoted as T) depends on the baud rate of the communication. The transmission time of a binary bit at 9600 baud is twice that of a binary bit at 19200 baud, that is: 2*T 19200 =T 9600. Therefore, the transmission time of one bit at 9600 baud can transmit two bits at 19200 baud. Similarly, the time of transmitting two bits at 9600 baud can only transmit one bit at 4800 baud. The host sets the receiving baud rate to 9600. The host can only correctly receive the characters sent by the terminal at 9600 baud. Sending baud rates higher or lower than 9600 will cause errors in the characters received by the host. When the receiving baud rate is 9600 and the terminal sends carriage returns at different baud rates, the binary sequence received by the host is shown in Table 1.
It can be seen from Table 1 that except for the two special cases at 19200 and 1800 baud, the binary sequences in other cases are all transformations of the binary sequences at 9600 baud. Take the first ten bits and match them to the bits at 9600 baud. Ignore the data frame error caused by the lack of the stop bit '1' and represent the received characters in byte form (as shown in the rightmost column of Table 1). For example: when the transmission rate is 1200 baud and the receiving rate is 9600 baud, the host receives the byte 0x80 instead of the correct carriage return character 0x0D. Because the bytes received at different transmission rates (9600, 4800, 2400, 1200) are different, the transmission baud rate can be determined by judging the received characters.
When the transmission baud rate is 19200, its transmission speed is exactly twice the receiving speed (9600 baud), so the two bits at the transmitter will be regarded as one by the receiver. Depending on the different serial interface hardware, the two binary bit combinations of '01' and '10' may be regarded as '1' or '0'. Fortunately, only bits 0 to 4 have such ambiguity. The following bits are all '1' because they are all stop bits. Therefore, the upper half byte of the character received at a transmission rate of 19200 baud is 0xF. The lower half byte may be one of several values, but it will not be 0x0 because there are two adjacent '1's in 0x0D, which will produce at least one '1' in the lower half byte. Therefore, the entire byte is of the form 0xF?, and the lower half byte is not 0.

Table 1 Binary sequences at different baud rates

Baud rate

The received binary bit sequence

Byte representation

19200

0 1 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1

0xF?

9600

0 1  0 1  1 0  0  0 0  1

0x0D

4800

0 0 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1

0xE6

2400

0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1

0x78

1800

0 0 0 0 0 x 1 1 1 1 x 0 0 0 0 0 1 1 1 1

0xE0

1800

0 0 0 0 0 x 1 1 1 1 x 0 0 0 0 0 1 1 1 1

0xF0

1200

0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0

0x80

600

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1

0x00

300

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0x00

150

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0x00

110

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0x00

When the transmission rate is 1800 baud, because
T 1800 = T 9600 * 16/3,
and 16/3 is not an integer, the state transition time of the binary bit at the receiving end does not correspond to 9600 baud, causing the state to change during a bit reception cycle at the receiving end. This is the case with the sixth bit (represented as x) given in Table 1. Because x may be seen as '1' or '0', the byte received when the transmission rate is 1800 baud may be 0xE0 or 0xF0. The same problem also exists when the baud rate is 3600 and 7200, and the same method can be used, but the number of uncertain bits will increase, and more types of bytes need to be detected. The transmission rates of 3600 baud and 7200 baud are almost never used, so this problem is not serious. As long as the transmission baud rate is between 1200 and 19200, we can uniquely determine the baud rate by receiving a character. [page]

2 Low baud rate detection
When the transmission rate is lower than 1200 baud, the bytes received by the receiver are all 0x00, so it can only be determined that the rate is lower than 1200 baud, and it is impossible to get more information. To solve this problem, the next byte of information can be received at a rate of 9600 baud. When the transmission rate is 600 baud or lower, the transmission time of one bit is longer than the reception time of the entire byte at 9600 baud. Therefore, every jump from '1' (stop bit) to '0' (start bit) at the transmitter will make the receiver think that a new byte has started. Table 2 shows the binary sequence of the carriage return character at the receiver at a transmission rate of 600 baud or lower (only the first few bits are given).

Table 2: How to receive carriage return characters at low baud rates

Baud rate

9600 baud binary serial

Time difference
(period)

Time difference
(real time)

600

16 0\'s 16 1\'s 16 0\'s

32

3.33ms

300

32 0\'s 32 1\'s 32 0\'s

64

6.66ms

150

64 0\'s 64 1\'s 64 0\'s

128

13.33ms

110

87 0\'s 87 1\'s 87 0\'s

174

18.13ms

75

128 0\'s 128 1\'s 128 0\'s

256

26.66ms

50

192 0\'s 192 1\'s 192 0\'s

384

4 0.00ms

At 600 baud, the first transition from '1' to '0' occurs immediately after initialization. This transition causes the receiver to receive the byte 0x00. The second transition occurs (16+16)*T 9600 seconds after initialization, which causes the receiver to think that another byte has been received. The reception time of a binary bit is T 9600 , so the serial interface circuit will indicate that the first byte has been received 10* T 9600 seconds after the first transition , and the second byte has been received (16+16+10)* T 96 00 seconds after. Therefore, at 600 baud, the time difference between the first byte and the second byte is (16+16+10-10)* T 9600 = 32* T 9600 seconds. The third column of Table 2 shows this time difference expressed as the number of T 9600s . Because T 9600 = 1/9600 seconds = 104.16 milliseconds, the real time difference between the two bytes received can be obtained by multiplication. The time difference for different sending baud rates is shown in the last column of Table 2. With this time difference information, the baud rate at a low transmission rate can be determined: measure the receiving time difference between the first and second bytes, and then find out which baud rate has the closest time difference in the time difference constant table (Table 2), which corresponds to the terminal sending baud rate. Even if the measured time difference has some errors, the baud rate can generally be determined correctly.

3 Implementation method
Through the above analysis, various baud rates can be determined by sending and receiving information of carriage return characters. The pseudo code of the algorithm implementation is given at the end of this article. Application practice proves the effectiveness of this method.
; Pseudo code to determine what baud rate a transmitter is at,

on the b asis of a single

; RETURN (0x0D) character received from it.

Initialise receive baud rate to 9600
Wait for Byte to be received
IF Byte = 0x00 THEN
Start Timer
REPEAT
UNTIL (Timer > 50 ms OR New Byte Received)
CASE Timer IN
1 ms - 4 ms: 600 Baud
5 ms-10 ms: 300 Baud
11 ms-15 ms: 150 Baud
16 ms-22 ms: 110 Baud
23 ms-32 ms: 75 Baud
33 ms-49 ms: 50 Baud
ELSE: Timed out; reset
END CASE;
ELSIF Byte >= 0xF1 THEN
 19200 Baud
ELSE
CASE Byte IN
0x0D: 9600 Baud
0xE6: 4800 Baud
0x78: 2400 Baud
0xE0,0xF0: 1800 Baud
0x80: 1200 Baud
ELSE: Line noise; reset
END CASE
END IF■

References:

[1]Zhao Yijun et al. Single-chip microcomputer interface technology[M]. Beijing: People's Posts and Telecommunications Press, 1989.
[2]Liu Li. Software and hardware technology reference[M]. Beijing: Xueyuan Press, 1993.
[3]Zhang Shiyi. Digital signal processing[M]. Beijing: Beijing Institute of Technology Press, 1987

Reference address:An Automatic Detection Method for Serial Communication Baud Rate

Previous article:Serial port expansion circuit from RS232 to RS422 based on programmable logic array
Next article:Mixed Logic Level Interface Technology

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号