当前位置:网站首页>STM32之串口传输结构体

STM32之串口传输结构体

2022-08-11 08:46:00 梅山剑客

传输数据还可以,上位机解析数据耽误了一个下午的时间特此来记录一下!这里将16位数据转成8位数据转发到上位机。

STM32端口

这次协议帧头定义的是 0xAA0xBB0xFF0xEE0xBB确保数据的准确性!!!

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]);
        }

结果:

结果如图所示:
发送和接收的数据是一致的!!!!
请添加图片描述

原网站

版权声明
本文为[梅山剑客]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_46152793/article/details/126274699