简体   繁体   English

如何为 UART 传输设置消息开始的字节

[英]How to set a byte of start of message for a UART transmission

I'm trying to transmit with UART, from an stm32 Nucleo to Matlab Simulink, a certain amount of data.我正在尝试使用 UART,从 stm32 Nucleo 到 Matlab Simulink,传输一定量的数据。 More precisely I want to transmit two uint16 variables.更准确地说,我想传输两个 uint16 变量。 I splitted them in two packeges of 1 byte each.我将它们分成两个包,每个包 1 个字节。 The problem is that when I receive them on Simulink, it's not garanteed that SImulink take the packages in the correct order, so I have to implement something that allows me to understand where is the start of the message.问题是,当我在 Simulink 上收到它们时,并不能保证 SIMulink 以正确的顺序获取包,所以我必须实现一些让我了解消息的开始位置的东西。 I'm trying to do that, but at the moment I'm not succeeding.我正在努力做到这一点,但目前我没有成功。 I want to specify that in Simulink I can write the header of the message, so I think that I just have to set it in my stm32 board.我想指定在 Simulink 中我可以写消息的标题,所以我认为我只需要在我的 stm32 板中设置它。

Here it is my code:这是我的代码:

//Serial Stream Routine
         if ((streamActive != 0) && (dataReady == 1))
         {
             do{
                     //Sending the first byte of counter
                     firstbyte = counter & 0xFF;
                     memcpy(str, &firstbyte, 1);
                     HAL_UART_Transmit(&huart2, str, 1, HAL_MAX_DELAY);
                     //Sending the second byte of counter
                     secondbyte =(counter >> 8) & 0xFF ;
                     memcpy(str, &secondbyte, 1);
                     HAL_UART_Transmit(&huart2, str, 1, HAL_MAX_DELAY);

                     if(streamActive == 2){ //We transmit the counter with an offset

                         offset = counter + OFFSET_VALUE;

                         //Sending the first byte of offset
                         firstbyte = offset & 0xFF;
                         memcpy(str, &firstbyte, 1);
                         HAL_UART_Transmit(&huart2, str, 1, HAL_MAX_DELAY);
                         //Sending the second byte of offset
                         secondbyte =(offset >> 8) & 0xFF ;
                         memcpy(str, &secondbyte, 1);
                         HAL_UART_Transmit(&huart2, str, 1, HAL_MAX_DELAY);
                     }

                     counter++;
                     dataReady = 0;
                 }while(counter < MAX_VALUE);

So I want to trasmit the variables counter and offset.所以我想传输变量计数器和偏移量。 Help me, thanks.帮帮我,谢谢。

Now I want to receive them in the correct order现在我想以正确的顺序接收它们

Add header & PC & ARM Corex are both little-endian so it is enough to:添加标头和 PC 和 ARM Corex 都是小端,因此足以:

         do{
                 HAL_UART_Transmit(&huart2, (void *)"START", 5, HAL_MAX_DELAY);
                 HAL_UART_Transmit(&huart2, (void *)&counter, 2, HAL_MAX_DELAY);
                 if(streamActive == 2){ //We transmit the counter with an offset
                     offset = counter + OFFSET_VALUE;
                     HAL_UART_Transmit(&huart2, (void *)"START", 5, HAL_MAX_DELAY);
                     HAL_UART_Transmit(&huart2, (void *)&offset, 2, HAL_MAX_DELAY);
                 }
                 counter++;
                 dataReady = 0;
            }while(counter < MAX_VALUE);


And in simulink (matlab command): uint16('START') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM