串口发送Hex数据包和文本数据包

一.基本信息

HEX数据包数据直出,解析简单
文本数据包需要先解析成编码,解析复杂
流程就是包头-数据-包尾

文本数据包可以与开关灯实验结合

二.代码实现

1.串口收发Hex数据包

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "Key.h"

uint8_t KeyNum;

int main(void)
{
	OLED_Init ();
	Key_Init();
	Serial_Init();
	
	OLED_ShowString(1,1,"TxPacket");
	OLED_ShowString(3,1,"RxPacket");
	
	Serial_TxPacket[0] = 0x01;
	Serial_TxPacket[1] = 0x02;
	Serial_TxPacket[2] = 0x03;
	Serial_TxPacket[3] = 0x04;
	
	
	while(1)
	{
		KeyNum = Key_GetNum ();
		if(KeyNum == 1)
		{
			//按键按一次数据自增
			Serial_TxPacket[0] ++;
	        Serial_TxPacket[1] ++;
	        Serial_TxPacket[2] ++;
	        Serial_TxPacket[3] ++;
			
			//发送数据包
			Serial_SendPacket();
			
			//自增后数据
			OLED_ShowHexNum (2,1,Serial_TxPacket[0],2);
		    OLED_ShowHexNum (2,4,Serial_TxPacket[1],2);
		    OLED_ShowHexNum (2,7,Serial_TxPacket[2],2);
		    OLED_ShowHexNum (2,10,Serial_TxPacket[3],2);
		}
		//获取标志位状态
      if(Serial_GetRxFlag() == 1)
	  {
		  //接收数据包
		  OLED_ShowHexNum (4,1,Serial_RxPacket[0],2);
		  OLED_ShowHexNum (4,4,Serial_RxPacket[1],2);
		  OLED_ShowHexNum (4,7,Serial_RxPacket[2],2);
		  OLED_ShowHexNum (4,10,Serial_RxPacket[3],2);
	  }
    }
}

Serial.c

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

uint8_t Serial_TxPacket[4];//定义发送数据包
uint8_t Serial_RxPacket[4];//定义接收数据包
uint8_t Serial_RxFlag;//定义接收数据包标志位

void Serial_Init(void)
{
	//开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA ,ENABLE);
	
	//初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
	GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
	GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//初始化USART
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;//波特率
	USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;//硬件流控制
	USART_InitStructure.USART_Mode = USART_Mode_Tx |USART_Mode_Rx;//串口发送与接受模式
    USART_InitStructure.USART_Parity = USART_Parity_No;//奇偶校验
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长
	USART_Init(USART1,&USART_InitStructure);
	
	//USART中断
	USART_ITConfig (USART1,USART_IT_RXNE,ENABLE);
	
	//NVIC优先级通道
	NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);
	
	//初始化NVIC
	NVIC_InitTypeDef NVIC_Initstructure;
	NVIC_Initstructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_Initstructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority =1;
	NVIC_Initstructure.NVIC_IRQChannelSubPriority =1;
	NVIC_Init(&NVIC_Initstructure);
	
	//USART使能
	USART_Cmd(USART1,ENABLE);
}

//串口发送数据函数
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1,Byte);
	while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET) ;
}

//串口发送数组函数
void Serial_SendArray(uint8_t *Array,uint16_t len)
{
	uint16_t i;
	for(i = 0;i < len;i++)
	{
		Serial_SendByte(Array[i]);
	}
}

//串口发送字符串函数
void Serial_SendString(char *String)
{
	uint8_t i;
	for(i = 0 ; String[i] != '\0'; i++)
	{
		Serial_SendByte(String[i]);
	}
}

//定义次方
uint32_t Serial_Pow(uint32_t x,uint32_t y)
{
	uint32_t Reuslt =1;
	while(y--)
	{
    Reuslt *= x;
	}
	return Reuslt;
}

//串口发送数字函数
void Serial_SendNumber(uint32_t Number,uint8_t len)
{
	uint8_t i;
	for(i = 0; i < len; i++)
	{
	Serial_SendByte(Number / Serial_Pow(10,len - i - 1) % 10 + '0');
    }
}

//重定向
int fputc(int ch,FILE *f)//fputc函数原型
{
	Serial_SendByte(ch);//重定向到串口,默认是输出屏幕单片机没有屏幕;
	return ch;
}

//sprintf函数
void Serial_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg,format);
	vsprintf(String,format,arg);
	va_end(arg);
	Serial_SendString(String);
}

//发送数据包函数
void Serial_SendPacket(void)
{
	Serial_SendByte(0xFF);
	Serial_SendArray(Serial_TxPacket,4);
	Serial_SendByte(0xFE);
}

//获取接收数据标志位
uint8_t Serial_GetRxFlag(void)
{
	if (Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}

//定义USART1中断函数
void USART1_IRQHandler(void)
{
	static uint8_t RxState = 0;//目前状态:包头-数据发送\接收-包尾
	static uint8_t pRxPacket = 0; //接收次数
	
	if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET)//中断标志位
	{
		uint8_t RxData = USART_ReceiveData (USART1);//RxData用来接收数据
		
		//当处于包头状态时
		if(RxState == 0)//状态为0
		{
			if(RxData == 0xFF)//串口接收0xFF到包头
			{
				RxState = 1;//状态置1
				pRxPacket = 0;//初始化串口接收次数
			}
		}
		//处于串口接收状态
		else if(RxState ==1)
		{
			Serial_RxPacket[pRxPacket] = RxData;//接收数据
			pRxPacket ++;//接受次数自增
			if(pRxPacket >= 4)//接收4次
			{
				RxState = 2;//接受结束置2
			}
		}
		//处于包尾状态
		else if(RxState == 2)
		{
			if(RxData == 0xFE)//包尾数据接收为0xFE
			{
				RxState =0;//状态置0
				Serial_RxFlag = 1;//标志位置1完成一次数据包接收
			}
		}
		
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除中断标志位
	}
}

Serial.h

#ifndef _SERIAL_H
#define _SERIAL_H

#include <stdio.h>

extern uint8_t Serial_TxPacket[];
extern uint8_t Serial_RxPacket[];

void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t len);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t len);
void Serial_Printf(char *format,...);
void USART1_IRQHandler(void);
void Serial_SendPacket(void);


uint8_t Serial_GetRxFlag(void);

	
#endif

2.串口收发文本数据包

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "LED.h"
#include "String.h"

int main(void)
{
	OLED_Init ();
	LED_Init();
	Serial_Init();
	
	OLED_ShowString(1,1,"TxPacket");
	OLED_ShowString(3,1,"RxPacket");
		
	
	while(1)
	{
		//标志位置1时
		if(Serial_RxFlag == 1)
		{
			OLED_ShowString(4,1,"                ");
			OLED_ShowString(4,1,Serial_RxPacket );
			
			//对比接收Hex数据包与LED_ON
			if(strcmp(Serial_RxPacket, "LED_ON") == 0)
			{
				//对比一样开灯
				LED1_ON();
				Serial_SendString("LED_ON_OK\r\n");
				OLED_ShowString(2,1,"                ");
				OLED_ShowString(2,1,"LED_ON_OK");
			}
			//对比接收Hex数据包与LED_OFF
			else if(strcmp(Serial_RxPacket, "LED_OFF") == 0)
			{
				//对比一样关灯
				LED1_OFF();
				Serial_SendString("LED_OFF_OK\r\n");
				OLED_ShowString(2,1,"                ");
				OLED_ShowString(2,1,"LED_OFF_OK");
			}
			//两次对比都不一样
			else
			{
				//发送以下字符串
				Serial_SendString("ERROR_COMMAND\r\n");
				OLED_ShowString(2,1,"                ");
				OLED_ShowString(2,1,"ERROR_COMMAND");
			}
			Serial_RxFlag = 0;//接收标志位置0
		}
    }
}

Serial.c

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>

char Serial_RxPacket[100];//定义接收数据包
uint8_t Serial_RxFlag;//定义接收数据包标志位

void Serial_Init(void)
{
	//开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA ,ENABLE);
	
	//初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
	GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
	GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//初始化USART
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;//波特率
	USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;//硬件流控制
	USART_InitStructure.USART_Mode = USART_Mode_Tx |USART_Mode_Rx;//串口发送与接受模式
    USART_InitStructure.USART_Parity = USART_Parity_No;//奇偶校验
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长
	USART_Init(USART1,&USART_InitStructure);
	
	//USART中断配置
	USART_ITConfig (USART1,USART_IT_RXNE,ENABLE);
	
	//NVIC优先级通道
	NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);
	
	//初始化NVIC
	NVIC_InitTypeDef NVIC_Initstructure;
	NVIC_Initstructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_Initstructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority =1;
	NVIC_Initstructure.NVIC_IRQChannelSubPriority =1;
	NVIC_Init(&NVIC_Initstructure);
	
	//USART使能
	USART_Cmd(USART1,ENABLE);
}

//串口发送函数
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1,Byte);
	while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET) ;
}

//数组
void Serial_SendArray(uint8_t *Array,uint16_t len)
{
	uint16_t i;
	for(i = 0;i < len;i++)
	{
		Serial_SendByte(Array[i]);
	}
}

//字符串
void Serial_SendString(char *String)
{
	uint8_t i;
	for(i = 0 ; String[i] != '\0'; i++)
	{
		Serial_SendByte(String[i]);
	}
}

//次方
uint32_t Serial_Pow(uint32_t x,uint32_t y)
{
	uint32_t Reuslt =1;
	while(y--)
	{
    Reuslt *= x;
	}
	return Reuslt;
}

//数
void Serial_SendNumber(uint32_t Number,uint8_t len)
{
	uint8_t i;
	for(i = 0; i < len; i++)
	{
	Serial_SendByte(Number / Serial_Pow(10,len - i - 1) % 10 + '0');
    }
}

//重定义
int fputc(int ch,FILE *f)//fputc函数原型
{
	Serial_SendByte(ch);//重定向到串口,默认是输出屏幕单片机没有屏幕;
	return ch;
}

//sprintf函数
void Serial_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg,format);
	vsprintf(String,format,arg);
	va_end(arg);
	Serial_SendString(String);
}

//USART1中断函数
void USART1_IRQHandler(void)
{
	static uint8_t RxState = 0;//状态初始化
	static uint8_t pRxPacket = 0; //次数初始化
	
	if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET)//获取接收标志位
	{
		uint8_t RxData = USART_ReceiveData (USART1);//串口接收
		
		//状态0
		if(RxState == 0)
		{
			//包头@并且标志位0
			if(RxData == '@' && Serial_RxFlag == 0)
			{
				RxState = 1;//状态置1
				pRxPacket = 0;//次数初始化
			}
		}
		//状态1
		else if(RxState ==1)
		{
			//\r进入状态2
			if(RxData == '\r')
			{
				RxState = 2;
			}
			//接收数据
			else
			{
			Serial_RxPacket[pRxPacket] = RxData;
			pRxPacket ++;
			}
		}
		//状态2
		else if(RxState == 2)
		{
			//\n进入下一次轮回
			if(RxData == '\n')
			{
				//状态置0
				RxState =0;
				Serial_RxPacket[pRxPacket] = '\0';
				Serial_RxFlag = 1;
			}
		}
		
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除接收标志位
	}
}

Serial.h

#ifndef _SERIAL_H
#define _SERIAL_H

#include <stdio.h>

extern char Serial_RxPacket[];
extern uint8_t Serial_RxFlag;

void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t len);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t len);
void Serial_Printf(char *format,...);

	
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值