Quote:

Connecting the serial port is the bridge between the application and MCU to receive and send instructions. The hexadecimal data stream is an important information display, but at the beginning, I was confused when I got a string of data packets from the electronic control. They are given byte by byte, so this article is to document how to read bytes.

Why does the MCU send data byte by byte?

MCU is generally 8-bit or 16-bit, and its cache is generally (1~3) 8-bit. Therefore, IN order to prevent data from being overwritten, MCU basically operates data in real time, so byte streams are also accepted on the tablet.

Begin with “0 xaa”

“0x” in Java is lowercase hexadecimal, “0xAA” in binary is” 10 10 10 10 10 “, and “170” in unsigned decimal is “-86”. What about when the tablet receives a “0xAA” byte?

It’s -86. Why not 170?

The binary code in the computer is stored as complement. The byte type is 8 bits, and the int type is 32 bits. A byte AA to int would first complete “10, 10, 10, 10” into signed bits

11 11 11 11 11 11 11 11 11 11 11 10 10 10 (complement) Complement to reverse code: -1

11 11 11 11 11 11 11 11 11 11 11 11 11 11 10 10 01 (inverse) inverse code to source code: remove the sign bit other bits take inverse

10 00 00 00 00 00 00 00 00 00 00 01 01 01 10

How do I get 170?

11 11 11 11 11 11 11 11 11 11 11 11 10 10 10 10 & 0xFF

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 10 10 10 (complement) The complement and inverse of a positive number are both themselves

00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 10 10 10

Prepare data on the plate

Int a= (byte)

conclusion

The problem with serial communication is that if the number is up to 170 and there is no sign bit, then all the data up to 255 needs &0xFF.