Data sent and received when the serial port debugging assistant is used is in bytes. You can select three formats: character, hexadecimal, and binary, how can we differentiate the three formats?
First, let's clarify a concept: the Unit for sending and receiving data through the serial port is ''byte '', 1 byte = 8 bits, and the format for sending and receiving data through the serial port is generally 1 bit (usually 0) + 8 bits data bit (one byte) + 1 bit verification bit (optional) + 1 bit verification bit (generally 1)
The following three sending and receiving formats are divided one by one:
(1) hexadecimal: Because the width of a 1-bit hexadecimal digit is 4 bits, the two-bit hexadecimal digits occupy the Bit Width of one byte, therefore, when sending and receiving data in hexadecimal format, each byte sends or receives two hexadecimal numbers. For example, when a group of data ''0f3c781a ''is sent in hexadecimal format, the data of each byte is as follows:
| Send data |
0x0f |
0x3c |
0x78 |
0x1a |
| Bytes |
1 |
2 |
3 |
4 |
Note: similar to ''0xff'', it indicates the standard writing method of two hexadecimal numbers. ''0x ''is omitted directly in the serial modulation assistant.
(2) binary: refer to the principle of the hexadecimal format for sending and receiving. The width of each binary digit is 1 bit, so each byte in the serial port can transmit 8 bits. Similarly, when data ''0f3c781a ''is transmitted, the data corresponding to each byte is the binary number corresponding to the hexadecimal number in the table above.
| Send data |
2017_1111 |
0011_1100 |
01100001000 |
000100001010 |
| Bytes |
1 |
2 |
3 |
4 |
(3) character: When the serial port sends and receives data in the character format, because each character must be an 8-bit binary in the ASCII code table, exactly one byte, therefore, by default, this character is first converted to the corresponding binary number and then sent, which is equivalent to sending one character per byte. If the serial port receiving end is in binary format, it will be directly displayed. If it is in hexadecimal format, it will display the two-digit hexadecimal numbers corresponding to this character in the ASCII code table; if the serial port receiving end is displayed in character format, the binary numbers to be received are converted to the corresponding characters according to the ASCII code table (this character is the same as the sent character) and then displayed.
Similarly, when sending data ''0f3c781a ''in character format, first find the binary and hexadecimal numbers corresponding to each character according to the ASCII code table (URL: http://www.asciima.com/) as follows:
| Character |
0 |
F |
3 |
C |
7 |
8 |
1 |
A |
| Hexadecimal number |
0x30 |
0x46 |
0x33 |
0x43 |
0x37 |
0x38 |
0x31 |
0x41 |
| 2-digit number |
0011_0000 |
0100_0110 |
0011_0011 |
0100_0011 |
0011_0111 |
0011_1000 |
0011_0001 |
0100_0001 |
After sending the data segment in character format, the data received in character format, hexadecimal format, and binary format are:
| Number of received bytes |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
| Receiving characters |
0 |
F |
3 |
C |
7 |
8 |
1 |
A |
| Receive hexadecimal numbers |
0x30 |
0x46 |
0x33 |
0x43 |
0x37 |
0x38 |
0x31 |
0x41 |
| Receive binary data |
0011_0000 |
0100_0110 |
0011_0011 |
0100_0011 |
0011_0111 |
0011_1000 |
0011_0001 |
0100_0001 |
At this point, the readers must be clear about the differences between the three formats for sending and receiving data through the serial port.