Zigbee, STM32 single-chip microcomputer serial port will send and receive data transmission-analysis of uint8, uint16 and other data-multiple data and analysis of serial port transmission-Internet of Things

I. Overview

        In the common serial port data transmission, what we are familiar with is the array of unsigned char, and some class libraries will define the type of uint8 and uint8_t, but it is still the type of char in essence.

        Our common serial communication is generally divided into two formats, namely ASCII and HEX transmission, which also determine the difference in the analysis method of the data receiver.

        In some cases, we need to use the serial port to send some special text, many of which contain information such as data and format. We need to correctly understand the format of the unsigned char corresponding to the data, and then we can analyze the data.

2. Basic knowledge

        Before learning, we must be familiar with the basic parameters of various types:

        unsigned type

        The unsigned type is also called the unsigned type . Unlike other types, the highest bit is considered to be the sign bit. The unsigned type does not have the highest bit of the sign bit. That is to say, for an eight-bit type, the value range of this type is in -128~127, while the value range of unsigned is 0~255;

        char type

        A char type occupies one byte and 8 bits . For Chinese characters, a Chinese character needs two char characters to store, while for English and numbers, one character occupies the position of one char character.

        unsigned char type

        This is a very common type in single-chip microcomputers. Some class libraries will typedef it as uint8 and uint8_t types, and the value range of unsigned char type is 0~255

        HEX transmission

        HEX is hexadecimal, which means that data is transmitted in hexadecimal. Generally, we often use it to form frames or send protocols to hardware.

        ASCII transmission

        ASCII transmission is a commonly used transmission method of character strings and values. It is simple and reliable. In daily serial communication scenarios, it is often used to communicate with specific readable instructions.

3. Construction of development environment

        In order to be familiar with the development process, the transmission string, data, the length of hexadecimal in the unsigned char type and other situations, it is necessary to develop a program that accepts and outputs each character at any time.

        Next, we use the STM32 chip to develop the program. Since the program development is relatively simple, friends in need can refer to the blog post: TM32 microcontroller development-UART&USART serial port transceiver-STM32CubeMx project generation

        Developers who study theory do not need to follow this article to develop serial port programs, but just follow the ideas of this article.

        First initialize the serial port, this article uses the baud rate: 38400

        The program is also very simple, one input function and one output function.

#include "usart1-board.h"
uint8_t data[10];
uint16_t len;
int main( void )
{
    Init();
		USART1_Init(38400);
    while( 1 )
    {
			len=USART1_ReadRxBuffer(data);
			USART1_SendStr(data,len);
    }
}

        Let's improve the code so that it can output the received character length, that is, output len.

uint8_t data[10];
uint16_t len;
int main( void )
{
    Init();
		USART1_Init(38400);
    while( 1 )
    {
			HAL_Delay(100);
			len=USART1_ReadRxBuffer(data);
			if(len!=0)
			{
				USART1_SendStr(data,10);
				USART1_SendData(len);
				memset(data,'\0',10);
				
			}
    }
}

        Compile and burn the program to the development board, open the serial port debugging assistant, the baud rate is 38400, and we start debugging.

4. Experimental process

        We first send floating-point data of a certain length through the serial port debugging assistant, and observe the data and length returned by the microcontroller.

        This is to accept the returned data in ASCII mode, so we change it to ASCII to send, and HEX accepts it.

        By observing the returned data, it can be concluded that the length is 5, so for the sent ASCII data, each bit represents a Hex character, we continue the experiment and match 105.2 with 31 30 35 2E 32, then '.' should correspond Hex 2E, let's test it.

        Through experiments, it is found that they are indeed in one-to-one correspondence, which also opens up ideas for us to transmit multiple data in the future.

        We continue to experiment and explore the relationship between Chinese characters and unsigned char characters.

        As we expected, when we send three Chinese characters, the actual received length is 6 characters, that is, one Chinese character occupies two characters.

        Next, we change the MCU program and use printf to try to print the data in ASCII format.

float val=102.5;
int main( void )
{
    Init();
	USART1_Init(38400);
    while( 1 )
    {
			sprintf((char *)data,"A%.1f",val);
			USART1_SendStr(data,sizeof(data));
			HAL_Delay(1000);
    }
}

         Next, we burn the program, and the serial port debugging assistant needs to be closed to prevent conflicts.

        Then we continue to open the serial port debugging assistant to debug, the ideal output should be: A102.5, the number of digits should be 6 digits

        According to the data sent by the serial port assistant, we can judge that the speculation is true, so can we specify the length, go back and continue to change the source code.

float val=102.5;
int main( void )
{
    Init();
	USART1_Init(38400);
    while( 1 )
    {
			sprintf((char *)data,"A%4.1f",val);
			USART1_SendStr(data,sizeof(data));
			HAL_Delay(1000);
    }
}

        Open the serial port debugging assistant, and the format can be limited to 4 characters by specifying the format %4.1f, which effectively ensures the reliability of data transmission.

5. Summary of Laws

        When the serial port transmits and receives multiple data, it is necessary to be familiar with the relevant parameters of the uisigned char type. According to the rules summarized in this article, when the single-chip microcomputer is developed, the data transmission of multiple sensors can be carried out by specifying the number of bits, which greatly improves the efficiency and saves time. .

Guess you like

Origin blog.csdn.net/qq_39724355/article/details/127622274