当前位置:网站首页>STM32之串口传输结构体
STM32之串口传输结构体
2022-08-11 08:46:00 【梅山剑客】
传输数据还可以,上位机解析数据耽误了一个下午的时间特此来记录一下!这里将16位数据转成8位数据转发到上位机。
STM32端口
这次协议帧头定义的是 0xAA、0xBB和0xFF、0xEE、0xBB确保数据的准确性!!!
uint16_t VLpress = 1000;
uint16_t AC1press = 999;
uint16_t AC2press = 998;
uint16_t ASpress = 997;
uint16_t SRpress = 996;
uint16_t BCpress = 995;
typedef struct My_Test
{
uint8_t head; //帧头
uint8_t head1; //帧头
uint8_t VLpressH; //VL压力高八位
uint8_t VLpressL; //VL压力低八位
uint8_t AC1pressH;//AC1压力高八位
uint8_t AC1pressL;//AC21力低八位
uint8_t AC2pressH;//AC2压力高八位
uint8_t AC2pressL;//AC2压力低八位
uint8_t ASpressH; //AS高力低八位
uint8_t ASpressL; //AS压力低八位
uint8_t SRpressH; //SR压力高八位
uint8_t SRpressL; //SR压力低八位
uint8_t BCpressH; //BC压力高八位
uint8_t BCpressL; //BC压力低八位
uint8_t end; //帧尾
uint8_t end1; //帧尾
uint8_t end2; //帧尾
}My_Test;
//实际使用
My_Test mytest;
void My_Struct_Test(My_Test *mytest)
{
mytest->head = 0xAA; //帧头数据
mytest->head1 = 0xBB; //帧头数据
mytest->VLpressH = ((VLpress&0xff00)>>8);
mytest->VLpressL = (VLpress&0x00ff);
mytest->AC1pressH = ((AC1press>>8)&0xff);
mytest->AC1pressL = (AC1press&0xff);
mytest->AC2pressH = ((AC2press>>8)&0xff);
mytest->AC2pressL = (AC2press&0xff);
mytest->ASpressH = ((ASpress>>8)&0xff);
mytest->ASpressL = (ASpress&0xff);
mytest->SRpressH = ((SRpress>>8)&0xff);
mytest->SRpressL = (SRpress&0xff);
mytest->BCpressH = ((BCpress>>8)&0xff);
mytest->BCpressL = (BCpress&0xff);
mytest->end = 0xFF; //帧尾数据
mytest->end1 = 0xEE; //帧尾数据
mytest->end2 = 0xBB; //帧尾数据
}
发送函数定义,通过指针的方式发送结构体数据,一位一位的发送数据,直到发送完成!!!!
void send_data(My_Test*mytest,uint8_t len)
{
static uint8_t* date=0,i=0;
for(i=0;i<len;i++) //使用sizeof计算结构体
{
date = (((uint8_t *)&mytest->head)+i); //从帧头开始 然后依次向下指向
HAL_UART_Transmit(&UART1_Handler,date,len,1000); //发送接收到的数据
while(__HAL_UART_GET_FLAG(&UART1_Handler,UART_FLAG_TC)!=SET); //等待发送结束
}
}
在主函数中调用即可:
My_Struct_Test(&mytest); //赋值
send_data(&mytest,sizeof(mytest)); //发送一个结构体
QT上位机解析数据!!!
因为没有把接收的数据进行类型转换,耽误了不少时间!!!
这里最关键的是要把接收数据的字节进行类型转换 VLpress = (((uchar)bytes.at(2)<<8)|(uchar)bytes.at(3)),必须要这样,否则会出错,显示负数!!!
/* * 串口接收数据并且解析数据 */
void Widget::serialPortReadyRead()
{
bytes = serialPort->readAll();
bytes.resize(17);
if((bytes.at(0) == '\xAA')&&(bytes.at(1) == '\xBB')&&(bytes.at(14) == '\xFF')&&(bytes.at(15) == '\xEE')&&(bytes.at(16) == '\xBB')){
VLpress = (((uchar)bytes.at(2)<<8)|(uchar)bytes.at(3)),
AC1press = (((uchar)bytes.at(4)<<8)|(uchar)bytes.at(5)),
AC2press = (((uchar)bytes.at(6)<<8)|(uchar)bytes.at(7)),
ASPress = (((uchar)bytes.at(8)<<8)|(uchar)bytes.at(9)),
SRpress = (((uchar)bytes.at(10)<<8)|(uchar)bytes.at(11)),
BCpress = (((uchar)bytes.at(12)<<8)|(uchar)bytes.at(13));
StrI1 = QString::number(VLpress);
StrI2 = QString::number(AC1press);
StrI3 = QString::number(AC2press);
StrI4 = QString::number(ASPress);
StrI5 = QString::number(SRpress);
StrI6 = QString::number(BCpress);
QList <QString>list2;
list2<<StrI1<<StrI2<<StrI3<<StrI3<<StrI5<<StrI6;
for(int i = 0;i<6;i++){
pressEdit[i]->setText(list2[i]);
}
结果:
结果如图所示:
发送和接收的数据是一致的!!!!
边栏推荐
猜你喜欢

Initial use of IDEA

如何通过开源数据库管理工具 DBeaver 连接 TDengine

基于 VIVADO 的 AM 调制解调(1)方案设计

音视频+AI,中关村科金助力某银行探索发展新路径 | 案例研究

excel将数据按某一列值分组并绘制分组折线图

Audio and video + AI, Zhongguancun Kejin helps a bank explore a new development path | Case study

用 Antlr 重构脚本解释器

进阶-指针

关于架构的认知

Detailed Explanation of the Level 5 Test Center of the Chinese Institute of Electronics (1)-string type string
随机推荐
pycharm中绘图,显示不了figure窗口的问题
C语言操作符详解
Unity3D - modification of the Inspector panel of the custom class
excel将数据按某一列值分组并绘制分组折线图
Machine Learning Summary (2)
基于hydra库实现yaml配置文件的读取(支持命令行参数)
《价值》读书与投资
快速幂,逆元的求解
UNITY gameobject代码中setacvtive(false)与面板中直接去掉勾 效果不一样
抽象类和接口
For the first time, I suspect that there is a bug in selenium4 because the iframe element is not found?
对比学习系列(三)-----SimCLR
观察表情和面部,会发现他有焦虑和失眠的痕迹
MySql事务
持续集成/持续部署(2)Jenkins & SonarQube
为什么会没有内存了呢
如何通过 IDEA 数据库管理工具连接 TDengine?
机器学习(一)数据的预处理
go sqlx 包
设置Vagrant创建的虚拟机名称和内存