IoT Lora module from entry to proficiency (7) serial communication

I. Introduction

        In the program design of the Lora module, serial port communication must be an extremely important and commonly used communication method. With the help of serial port communication, we can not only transmit the data we obtain, but also respond to external commands.

        Similarly, in the routine, a library named usart1-board.c is provided for us to help us complete the development of serial communication tasks better and faster.

2. Code implementation

        The task of this article is to send the fetched data out in ASCII format through the serial port, and accept the instructions from the host computer through the serial port debugging assistant to make corresponding operations.

        The specific tasks are as follows:

        The single-chip microcomputer sends light data to the host computer at intervals of one second, and records the sending times. When receiving the reset command FA 00 FB from the host computer, clear the sending times.

        By analyzing the usart1-board.c library, we often use the following methods:

void USART1_Init(uint32_t bound);
//初始化串口 bound为波特率
void USART1_SendStr(uint8_t *Data, uint16_t length);
//发送uint8_t类型数据数组 length为发送的长度
uint16_t USART1_ReadRxBuffer( uint8_t *payload);
//接受来自串口的内容并存入传入的数据数组内 返回接受到的数据长度

        Code example:

#include <string.h>
#include "board.h"
#include "hal_key.h"
#include "tim-board.h"
#include "timer_handles.h"
#include "led_light.h"
#include "adc_reader.h"
#include <math.h>          //library 
#include <stdio.h>
#include "stm32l1xx_hal.h" // controller register definitions 
#include "sht1x.h"         // controller register definitions 
#include "sht3x.h"
#include "usart1-board.h"

void Init() {
    BoardInitMcu();
    BoardInitPeriph();
    keys_init();//按键初始化
    setTimer2Callback(Time2Handler);
    Tim2McuInit(1);//定时器初始化,设置定时中断1ms中断一次
};

int count=0;//用于记录发送次数
float temp=0,hum=0,light=0;

uint8_t data[30];
int main( void )
{
    Init();
		ADCS_Init();//初始化ADC
		hal_temHumInit();//初始化温湿度模块
		USART1_Init(115200);
    while( 1 )
    {
				HAL_Delay(1000);//延时1000ms
				memset(data,'\0',30);//清空数组内容
				if(USART1_ReadRxBuffer(data)!=0)//如果接收到内容
				{
					if(data[0]==0xfa)//如果指令正确
						if(data[1]==0x00)
							if(data[2]==0xfb)
								count=0;//计数器清零
				}
				AdcScanChannel();//更新通道值
				light=((5.0/2.0)*AdcReadCh0())*100.0;//获取光照并通过公式计算
				call_sht11(&temp,&hum);//获取温湿度
				memset(data,'\0',30);//清空数组内容
				sprintf((char *)data,"Count=%d,Light=%.2f Lx",count+1,light);//将内容存入数组
				USART1_SendStr(data,30);//发送数据
				count++;//发送完一次数据计数器增加
    }
}

        Show results:

        

        At this point, we have completed the receiving and sending of serial port data, but this is only the basis for us to learn serial port communication.

        There are generally two ways to analyze the data of serial port communication: ASCII and Hex. But they are essentially transmitted in binary, but the data is processed differently.

3. About data processing

        Regarding data processing, it is generally divided into basic knowledge and data analysis. Learning can help us improve our programming thinking and write the programs we need better and faster.

        If you want to master, the following two articles are what you must be familiar with:

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

Common methods of data conversion and processing in data transmission-Internet of Things Development-MCU Communication

Guess you like

Origin blog.csdn.net/qq_39724355/article/details/131146839
Recommended