When using the serial port to send data, you can choose to send the string or hex send , usually we used to choose string to send data. About the difference between the two, you need to store data from the computer format.
In a computer, data is stored in binary form, such as decimal 1 (10), which is represented by 0000 0001 (2) in the computer. When we send data using the serial port, we first convert the data to the corresponding ASCII code, and then send the ASCII code in a binary way.
For example, we want to send a string of data "a852010100000000a91a", sent in both strings and hexadecimal:
(1) string send
The serial port sends the data as a string, first converting the string to binary, in the following format:
0000 1010 0000 1000 0000 0110 0000 0010 0000 0000& nbsp; 0000 0001 0000 0000 0000 0001
A 8 5 2 0 1 0 1
0000 0000 0000 0000 0000 0000 0000 0000 0000 1010 0000 1001 0000 0001 0000 1010
0 0 0 0 A 9 1 A
The data is then sent in the form of a 8-bit (serial port setting data bit 8-bit).
The data format received by the serial port is as follows
Number |
Data content |
Number |
Data content |
0 |
A |
8 |
0 |
1 |
8 |
9 |
0 |
2 |
5 |
10 |
0 |
3 |
2 |
11 |
0 |
4 |
0 |
12 |
A |
5 |
1 |
13 |
9 |
6 |
0 |
14 |
1 |
7 |
1 |
15 |
A |
(2) Hex Send data
The serial port sends the data in hexadecimal, first turning the data into:
1010 1000 0110 0010 0000 0001 0000 0001 0000 0000 0000 0000 1010 1001 0001 1010
0xa8 0X52 0x01 0x01 0x00 0x00 0xa9 0 X1a
The data is then sent in the form of a 8-bit (serial port setting data bit 8-bit).
The data format received by the serial port is as follows:
Number |
Data content |
Number |
Data content |
0 |
A8 |
4 |
00 |
1 |
52 |
5 |
00 |
2 |
01 |
6 |
A9 |
3 |
01 |
7 |
1 A |
It is easy to understand the difference between string sending and hexadecimal sending by carefully comparing the received results.
Serial string Send data