目录
一.数据包格式
(1)HEX数据包
(2)文本数据包
二.代码实现
(1)串口收发HEX数据包
(2)串口收发文本数据包(该程序没有写出来,暂时找不到错误,以后再看)
一.数据包格式
将数据按批进行打包和分割,方便接收方进行识别
(1)HEX数据包
传输最直接,解析数据非常简单(适合陀螺仪,温湿度传感器......)
但灵活性不足,载荷容易与包头包尾重复
数据包格式可以由用户根据需求自己规定,得到的数据形式为字符数组
避免载荷数据与包头包尾重复的方法
1.发送时对数据进行限幅
2.尽量使用固定长度的数据包,先通过包头和包尾对数据进行对齐,在接收载荷数据,并不会对其判断是否为包头包尾,而在接收包头和包尾,则会对其判断是否为包头包尾
3.增加包头和包尾的数量,尽量让其呈现出载荷数据出现不了的状态(也可以不要包尾)
(2)文本数据包
数据直观易理解,非常灵活(适合一些输入指令进行人机交互的场合,蓝牙模块常用的AT指令,CNC和3D打印机常用的G代码),但解析效率低
得到的数据形式为字符串
二.代码实现
(1)串口收发HEX数据包
Serial.c
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
uint8_t SendPacket[4];
uint8_t RePacket[4];
uint8_t RePacketbuffer;
uint8_t ReFlag;
void Serial_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
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_InitStructure.USART_BaudRate=9600;//自动计算写入BRR寄存器
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_ITConfig(USART1,USART_IT_RXNE,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
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_Cmd(USART1,ENABLE);
}
void Serial_SendByte(uint8_t Byte)
{
USART_SendData(USART1,Byte);//将数据写入DR
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}
void Serial_SendArray(uint8_t *Array,uint16_t Size)
{
uint8_t i;
for(i=0;i<Size;i++)
{
Serial_SendByte(Array[i]);
}
}
void Serial_SendString(char *My_String)
{
uint8_t i;
for(i=0;My_String[i] != '\0';i++)
{
Serial_SendByte(My_String[i]);
}
}
uint32_t Serial_Pow(uint8_t Number,uint8_t Num)//次方,返回值定义要足够大
{
uint32_t result=1;
while(Num--)
{
result *= Number;
}
return result;
}
void Serial_SendNumber(uint32_t Number , uint8_t Length)//显示无符号十进制数字并限制位数
{
uint8_t i;
for(i=0;i<Length;i++)
{
Serial_SendByte((Number/Serial_Pow(10,Length-i-1)%10)+'0');//加字符消除偏移
}
}
int fputc(int ch,FILE *f)//fputc是printf的底层
{
Serial_SendByte(ch);//将fputc函数重定向到串口
return ch;
}
/*
封装可变参数的格式
*/
void Serial_Printf(char *format,...)
{
char String[100];
va_list arg;
va_start(arg,format);
vsprintf(String,format,arg);//对于封装格式,要用vsprintf
va_end(arg);//释放参数列表
Serial_SendString(String);
}
uint32_t Get_Flag(void)
{
if(ReFlag == 1)
{
ReFlag=0;
return 1;
}
return 0;
}
void Send_Packet(void)
{
Serial_SendByte(0xFF);
Serial_SendArray(SendPacket,4);
Serial_SendByte(0xFE);
}
void USART1_IRQHandler(void)
{
static uint8_t PacketState;
static uint8_t count;
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == SET)
{
RePacketbuffer = USART_ReceiveData(USART1);//获取接受数据
if(PacketState==0)
{
if(RePacketbuffer == 0xFF)
{
PacketState = 1; /*
这里采用if和else if而不是用并列的if,目的是让每次接收数据进入中断后
只执行一个状态下的指令,也可以使用switch case语句.如果用并列的if,
则会出现在上个if判断成功后,将状态位置新后会立刻执行下一个状态位的指令
*/
count=0;
}
}
else if(PacketState == 1)
{
RePacket[count] = RePacketbuffer;
count++;
if(count == 4)
{
PacketState = 2;
}
}
else if(PacketState == 2)
{
if(RePacketbuffer == 0xFE)
{
PacketState = 0;
ReFlag = 1;
}
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE);//这里使用的是中断清除标志位
}
}
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "Key.h"
uint8_t num;
int main(void)
{
OLED_Init();
Serial_Init();
Key_Init();
OLED_ShowString(1,1,"TxPacket:");
OLED_ShowString(3,1,"RxPacket:");
SendPacket[0]=0x11;
SendPacket[1]=0x22;
SendPacket[2]=0x33;
SendPacket[3]=0x44;
while(1)
{
num=Keynum();
if(num == 1)
{
SendPacket[0]++;
SendPacket[1]++;
SendPacket[2]++;
SendPacket[3]++;
Send_Packet();
OLED_ShowHexNum(2,1,SendPacket[0],2);
OLED_ShowHexNum(2,4,SendPacket[1],2);
OLED_ShowHexNum(2,7,SendPacket[2],2);
OLED_ShowHexNum(2,10,SendPacket[3],2);
}
if(Get_Flag()==1)//发送数据时不要写超过16进制的字母
{
OLED_ShowHexNum(4,1,RePacket[0],2);
OLED_ShowHexNum(4,4,RePacket[1],2);
OLED_ShowHexNum(4,7,RePacket[2],2);
OLED_ShowHexNum(4,10,RePacket[3],2);
}
}
}
(2)串口收发文本数据包(该程序没有写出来,暂时找不到错误,以后再看)
Serial.c
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
char Serial_RxPacket[100]; //"@MSG\r\n"
uint8_t Serial_RxFlag;
void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
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_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_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
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_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 Length)
{
uint16_t i;
for (i = 0; i < Length; 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 Result = 1;
while (Y --)
{
Result *= X;
}
return Result;
}
void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i ++)
{
Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
}
}
int fputc(int ch, FILE *f)
{
Serial_SendByte(ch);
return ch;
}
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 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);
if (RxState == 0)
{
if (RxData == '@' && Serial_RxFlag == 0)
{
RxState = 1;
pRxPacket = 0;
}
}
else if (RxState == 1)
{
if (RxData == '\r')
{
RxState = 2;
}
else
{
Serial_RxPacket[pRxPacket] = RxData;
pRxPacket ++;
}
}
else if (RxState == 2)
{
if (RxData == '\n')
{
RxState = 0;
Serial_RxPacket[pRxPacket] = '\0';
Serial_RxFlag = 1;
}
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
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)
{
if (Serial_RxFlag == 1)
{
OLED_ShowString(4, 1, " ");
OLED_ShowString(4, 1, Serial_RxPacket);
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");
}
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;
}
}
}