【STM32】STM32学习笔记-USART串口收发HEX和文本数据包(29)

00. 目录

文章目录

    • 00. 目录
    • 01. 串口简介
    • 02. 串口收发HEX数据包接线图
    • 03. 串口收发HEX数据包示例1
    • 04. 串口收发HEX数据包示例2
    • 05. 串口收发文本数据包接线图
    • 06. 串口收发文本数据包示例
    • 07. 程序示例下载
    • 08. 附录

01. 串口简介

串口通讯(Serial Communication)是一种设备间非常常用的串行通讯方式,因为它简单便捷,因此大部分电子设备都支持该通讯方式, 电子工程师在调试设备时也经常使用该通讯方式输出调试信息。

在计算机科学里,大部分复杂的问题都可以通过分层来简化。如芯片被分为内核层和片上外设;STM32标准库则是在寄存器与用户代码之间的软件层。 对于通讯协议,我们也以分层的方式来理解,最基本的是把它分为物理层和协议层。物理层规定通讯系统中具有机械、电子功能部分的特性, 确保原始数据在物理媒体的传输。协议层主要规定通讯逻辑,统一收发双方的数据打包、解包标准。 简单来说物理层规定我们用嘴巴还是用肢体来交流,协议层则规定我们用中文还是英文来交流。

02. 串口收发HEX数据包接线图

在这里插入图片描述

03. 串口收发HEX数据包示例1

uart.h

#ifndef __UART_H__
#define __UART_H__#include "stm32f10x.h"    extern uint8_t recvPacket[];void uart_init(void);void uart_send_byte(uint8_t byte);void uart_send_array(uint8_t *arr, uint16_t len);void uart_send_string(char *str);void uart_send_number(uint32_t num, uint8_t len);void uart_printf(char *format, ...);uint8_t uart_getRxFlag(void);uint8_t uart_getRxData(void);void uart_send_packet(uint8_t arr[], int len);#endif /**/

uart.c

#include "uart.h"#include <stdio.h>
#include <stdarg.h>uint8_t recvData;
uint8_t recvFlag;uint8_t recvPacket[32];void uart_init(void)
{GPIO_InitTypeDef GPIO_InitStruct;USART_InitTypeDef USART_InitStruct;NVIC_InitTypeDef NVIC_InitStruct;RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//GPIO初始化  PA9 TXGPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStruct);//GPIO初始化  PA10 RXGPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStruct);USART_InitStruct.USART_BaudRate = 9600;USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;USART_InitStruct.USART_Parity = USART_Parity_No;USART_InitStruct.USART_StopBits = USART_StopBits_1;USART_InitStruct.USART_WordLength = USART_WordLength_8b;USART_Init(USART1, &USART_InitStruct);//设置串口中断USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//设置中断分组NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;NVIC_Init(&NVIC_InitStruct);USART_Cmd(USART1, ENABLE);
}void uart_send_byte(uint8_t byte)
{USART_SendData(USART1, byte);while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}void uart_send_array(uint8_t *arr, uint16_t len)
{uint16_t i;for (i = 0; i < len; i++){uart_send_byte(arr[i]);}
}void uart_send_string(char *str)
{uint16_t i = 0;while(*(str + i) != '\0'){uart_send_byte(str[i]);i++;}
}//x的y次方
uint32_t uart_pow(uint32_t x, uint32_t y)
{uint32_t result = 1;while(y){result *= x;y--;}return result;
}void uart_send_number(uint32_t num, uint8_t len)
{uint8_t i;for (i = 0; i < len; i++){uart_send_byte(num / uart_pow(10, len - i - 1) % 10 + '0');}}int fputc(int ch, FILE *fp)
{uart_send_byte(ch);return ch;
}void uart_printf(char *format, ...)
{char str[128];va_list arg;va_start(arg, format);vsprintf(str, format, arg);va_end(arg);uart_send_string(str);
}void USART1_IRQHandler(void)
{static uint8_t recvState = 0;static uint8_t i = 0;if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET){recvData = USART_ReceiveData(USART1);//状态机if (0 == recvState){if (recvData == 0xFF){recvState = 1;i = 0;}}else if (1 == recvState){recvPacket[i] = recvData;i++;if (i >= 4){recvState = 2;}} else if (2 == recvState){if (recvData == 0xFE){i = 0;recvState = 0;recvFlag = 1;}}USART_ClearITPendingBit(USART1, USART_IT_RXNE);}}uint8_t uart_getRxFlag(void)
{if (1 == recvFlag){recvFlag = 0;return 1;}return 0;
}uint8_t uart_getRxData(void)
{return recvData;
}//发送HEX报文数据
void uart_send_packet(uint8_t arr[], int len)
{//发送报文 FFuart_send_byte(0xFF);uart_send_array(arr, len);//发送报尾uart_send_byte(0xFE);	
}

main.c

#include "stm32f10x.h"
#include <stdio.h>
#include "delay.h"
#include "oled.h"
#include "uart.h"int main(void){	 uint8_t arr[] = {0x1, 0x2, 0x3, 0x4};OLED_Init();uart_init();//中断分组NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);OLED_ShowChar(1, 1, 'A');uart_send_packet(arr, 4);while(1){if (1 == uart_getRxData()){OLED_ShowHexNum(1, 1, recvPacket[0], 2);OLED_ShowHexNum(1, 4, recvPacket[1], 2);OLED_ShowHexNum(1, 7, recvPacket[2], 2);OLED_ShowHexNum(1, 10, recvPacket[3], 2);			 }	 }return 0;}

04. 串口收发HEX数据包示例2

uart.h

#ifndef __UART_H__
#define __UART_H__#include "stm32f10x.h"    extern uint8_t recvPacket[];void uart_init(void);void uart_send_byte(uint8_t byte);void uart_send_array(uint8_t *arr, uint16_t len);void uart_send_string(char *str);void uart_send_number(uint32_t num, uint8_t len);void uart_printf(char *format, ...);uint8_t uart_getRxFlag(void);uint8_t uart_getRxData(void);void uart_send_packet(uint8_t arr[], int len);#endif /**/

uart.c

#include "uart.h"#include <stdio.h>
#include <stdarg.h>uint8_t recvData;
uint8_t recvFlag;uint8_t recvPacket[32];void uart_init(void)
{GPIO_InitTypeDef GPIO_InitStruct;USART_InitTypeDef USART_InitStruct;NVIC_InitTypeDef NVIC_InitStruct;RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//GPIO初始化  PA9 TXGPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStruct);//GPIO初始化  PA10 RXGPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStruct);USART_InitStruct.USART_BaudRate = 9600;USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;USART_InitStruct.USART_Parity = USART_Parity_No;USART_InitStruct.USART_StopBits = USART_StopBits_1;USART_InitStruct.USART_WordLength = USART_WordLength_8b;USART_Init(USART1, &USART_InitStruct);//设置串口中断USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//设置中断分组NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;NVIC_Init(&NVIC_InitStruct);USART_Cmd(USART1, ENABLE);
}void uart_send_byte(uint8_t byte)
{USART_SendData(USART1, byte);while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}void uart_send_array(uint8_t *arr, uint16_t len)
{uint16_t i;for (i = 0; i < len; i++){uart_send_byte(arr[i]);}
}void uart_send_string(char *str)
{uint16_t i = 0;while(*(str + i) != '\0'){uart_send_byte(str[i]);i++;}
}//x的y次方
uint32_t uart_pow(uint32_t x, uint32_t y)
{uint32_t result = 1;while(y){result *= x;y--;}return result;
}void uart_send_number(uint32_t num, uint8_t len)
{uint8_t i;for (i = 0; i < len; i++){uart_send_byte(num / uart_pow(10, len - i - 1) % 10 + '0');}}int fputc(int ch, FILE *fp)
{uart_send_byte(ch);return ch;
}void uart_printf(char *format, ...)
{char str[128];va_list arg;va_start(arg, format);vsprintf(str, format, arg);va_end(arg);uart_send_string(str);
}void USART1_IRQHandler(void)
{static uint8_t recvState = 0;static uint8_t i = 0;if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET){uint8_t recvData1 = USART_ReceiveData(USART1);//状态机if (0 == recvState){if (recvData1 == 0xFF){recvState = 1;i = 0;}}else if (1 == recvState){recvPacket[i] = recvData1;i++;if (i >= 4){recvState = 2;}} else if (2 == recvState){if (recvData1 == 0xFE){recvState = 0;recvFlag = 1;}}USART_ClearITPendingBit(USART1, USART_IT_RXNE);}
}uint8_t uart_getRxFlag(void)
{if (1 == recvFlag){recvFlag = 0;return 1;}return 0;
}uint8_t uart_getRxData(void)
{return recvData;
}//发送HEX报文数据
void uart_send_packet(uint8_t arr[], int len)
{//发送报文 FFuart_send_byte(0xFF);uart_send_array(arr, len);//发送报尾uart_send_byte(0xFE);	
}

main.c

#include "stm32f10x.h"
#include <stdio.h>
#include "delay.h"
#include "oled.h"
#include "uart.h"
#include "key.h"int main(void){	 uint8_t arr[] = {0x1, 0x2, 0x3, 0x4};//中断分组NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);key_init();uart_init();OLED_Init();OLED_ShowString(1, 1, "TxPacket");OLED_ShowString(3, 1, "RxPacket");while(1){if (1 == key_scan()){arr[0]++;arr[1]++; arr[2]++;arr[3]++;uart_send_packet(arr, 4); OLED_ShowHexNum(2, 1, arr[0], 2);OLED_ShowHexNum(2, 4, arr[1], 2);OLED_ShowHexNum(2, 7, arr[2], 2);OLED_ShowHexNum(2, 10, arr[3], 2);			 } if (1 == uart_getRxData()){OLED_ShowHexNum(4, 1, recvPacket[0], 2);OLED_ShowHexNum(4, 4, recvPacket[1], 2);OLED_ShowHexNum(4, 7, recvPacket[2], 2);OLED_ShowHexNum(4, 10, recvPacket[3], 2);			 }	 }return 0;}

05. 串口收发文本数据包接线图

在这里插入图片描述

06. 串口收发文本数据包示例

uart.h

#ifndef __SERIAL_H
#define __SERIAL_H#include <stdio.h>extern uint8_t Serial_TxPacket[];
extern uint8_t 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 Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number, uint8_t Length);
void Serial_Printf(char *format, ...);void Serial_SendPacket(void);#endif

uart.c

#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>uint8_t Serial_TxPacket[4];				//FF 01 02 03 04 FE
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_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 Serial_SendPacket(void)
{Serial_SendByte(0xFF);Serial_SendArray(Serial_TxPacket, 4);Serial_SendByte(0xFE);
}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 == '@' && 0 == Serial_RxFlag){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();OLED_ShowString(2, 1, "                     ");OLED_ShowString(2, 1, "LED_ON_OK");Serial_SendString("LED_ON_OK\r\n");}else if (strcmp(Serial_RxPacket, "LED_OFF") == 0){LED1_OFF();OLED_ShowString(2, 1, "                     ");OLED_ShowString(2, 1, "LED_OFF_OK");	Serial_SendString("LED_OFF_OK\r\n");				}else{OLED_ShowString(2, 1, "                     ");OLED_ShowString(2, 1, "ERROR_COMMAND");	Serial_SendString("ERROR_COMMAND\r\n");				}Serial_RxFlag = 0;}}
}

07. 程序示例下载

24-UART收发HEX数据包

25-UART收发HEX数据包2.rar

26-UART收发文本数据包.rar

08. 附录

参考: 【STM32】江科大STM32学习笔记汇总

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/410435.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Oracle-java下载、开源/商业许可证(收费、免费说明)、版本发布日志

Oracle-java下载、开源/商业许可证&#xff08;收费、免费说明&#xff09;、版本发布日志 下载开源/商业许可证&#xff08;收费、免费说明&#xff09;java8版本发布日志以上是一般情况&#xff0c;具体的以官网发布信息为准例如&#xff1a; JDK17某些特定版本是免费的&…

OceanBase集群部署

我认为学习一个中间件比较好的方式是&#xff0c;先了解它的架构和运行原理&#xff0c;然后动手部署一遍&#xff0c;加深对它的了解&#xff0c;再使用它&#xff0c;最后进行总结和分享 本篇介绍OceanBase部署前提配置和集群部署 1.使用开源免费的社区版&#xff0c;企业版…

评论转换输出 - 华为OD统一考试

OD统一考试 分值: 200分 题解: Java / Python / C++ 题目描述 在一个博客网站上,每篇博客都有评论。每一条评论都是一个非空英文字母字符串。 评论具有树状结构,除了根评论外,每个评论都有一个父评论。当评论保存时,使用以下格式: 首先是评论的内容; 然后是回复当前…

【计算机网络】内容整理

概述 分组交换 分组交换则采用存储转发&#xff08;整个包必须到达路由器&#xff0c;然后才能在下一个链路上传输)技术。 在发送端&#xff0c;先把较长的报文划分成较短的、固定长度的数据段。 电路交换 在端系统间通信会话期间&#xff0c;预留了端系统间沿路径通信所需…

8.临床预测模型验证——交叉验证/Bootstrap法

基本概念 交叉验证&#xff1a; 将一定比例的数据挑选出来作为训练集&#xff0c;将其余未选中的样本作为测试集&#xff0c;先在训练集中构建模型&#xff0c;再在测试集中做预测。 内部验证&#xff1a;手动将样本随机分为训练集和测试集&#xff0c;先在训练集中构建模型…

子类的构造函数和析构函数调用顺序

看代码&#xff1a; class A { public:A() { cout << __FUNCTION__ << endl; }~A() { cout << __FUNCTION__ << endl; } };class B { public:B() { cout << __FUNCTION__ << endl; }~B() { cout << __FUNCTION__ << endl; …

8类CNN-Transformer混合架构魔改方案盘点,附23个配套模型&代码

为进一步提高模型的性能&#xff0c;我们将CNN在局部特征提取方面的优势与Transformer在全局信息建模方面的优势两相结合&#xff0c;提出了CNN-Transformer混合架构。目前&#xff0c;它已经成为我们研究视觉任务、发文章离不开的模型。针对CNNtransformer组合方向的研究也成为…

Clickhouse表引擎之CollapsingMergeTree引擎的原理与使用

前言 继续上次关于clickhouse的一些踩坑点&#xff0c;今天讲讲另外一个表引擎——CollapsingMergeTree。这个对于引擎对于数据量较大的场景是个不错的选择。注意&#xff0c;选择clickhouse的一般原因都是为了高效率查询&#xff0c;提高用户体验感&#xff0c;说白了就是以空…

跨站点请求伪造攻击 - Cross Site Request Forgery (CSRF)

什么是CSRF 最好理解CSRF攻击的方式是看一个具体的例子。 假设你的银行网站提供一个表单,允许当前登录用户将钱转账到另一个银行账户。例如,转账表单可能如下所示: <form method="post"action="/transfer"> <

Invalid bound statement (not found)(xml文件创建问题)

目录 解决方法&#xff1a; 这边大致讲一下我的经历&#xff0c;不想看的直接点目录去解决方法 今天照着老师视频学习&#xff0c;中间老师在使用动态SQL时&#xff0c;直接复制了一份&#xff0c;我想这么简单的一个&#xff0c;我直接从网上找内容创建一个好了&#xff0c;…

前端学习路径

菜鸟感觉很多人不太知道菜鸟写的博客是一个可以跟着学习、一起深入理解的过程&#xff0c;其中包括了菜鸟从刚开始学习到后面重新学习&#xff0c;再到后面进入学框架等一系列学习过程、知识和感悟&#xff0c;所以菜鸟把自己的博客整理成一个目录提取出来&#xff0c;好让读者…

彻底解决charles抓包https乱码的问题

最近做js逆向&#xff0c;听说charles比浏览器抓包更好用&#xff0c;结果发现全是乱码&#xff0c;根本没法用。 然后查询网上水文&#xff1a;全部都是装证书&#xff0c;根本没用&#xff01; 最后终于找到解决办法&#xff0c;在这里记录一下&#xff1a; 乱码的根本原因…