【问题标题】:Receive and saving hex-values to arrays [STM32]接收十六进制值并将其保存到数组 [STM32]
【发布时间】:2019-10-04 21:38:56
【问题描述】:

我想通过 UART 从 PC 接收十六进制值并将其保存到 STM32-uC。

收到的 HEX 值如下所示:

FF00 4a85 04b5 08aa 6b7r 00FF

(FF00 和 00FF 只是检查字节)

我想保存很多(不同的)数据行,如下所示: 4a85 04b5 08aa 6b7r 4a85 04b5 08aa 6b7r 4a85 04b5 08aa 6b7r 4a85 04b5 08aa 6b7r 4a85 04b5 08aa 6b7r 4a85 04b5 08aa 6b7r ...................

稍后我必须读取内存并从每 2 个字节获取 int 值:

04D1 -> 1233

  1. 问题:

我创建多数组

volatile uint8_t testarrays[8][5000]; //data storage

并初始化:

uint8_t received_str[12] //4 Bytes: `FF 00 and 00 FF` checksum
uint32_t h=0;
while(h<5000){
  if (receive_done_flag == TRUE) { //UART receive data
  testarrays[0][h]=received_str[2];
  testarrays[1][h]=received_str[3];
  testarrays[2][h]=received_str[4];
  testarrays[3][h]=received_str[5];
  testarrays[4][h]=received_str[6];
  testarrays[5][h]=received_str[7];
  testarrays[6][h]=received_str[8];
  testarrays[7][h]=received_str[9];
  }
  receive_done_flag == FALSE
  h++;
}

我的想法对吗?

  1. 问题:

我以后如何从存储中读取数据? uint16_t x=0; uint16_t y=0; uint8_t motorspeed_hex[8];

uint16_t speed_hex_M1;
uint16_t speed_hex_M2;
uint16_t speed_hex_M3;
uint16_t speed_hex_M4;

while(y<vert_length){
   x=0;
   while(x<hor_length){
   motorspeed_hex[x] = testarrays[x][y];
   x++;
   }
uint16_t speed_hex_M1 >> 8 = motorspeed_hex[0]
uint16_t speed_hex_M1 = motorspeed_hex[1]
uint16_t speed_hex_M2 >> 8  = motorspeed_hex[2]
uint16_t speed_hex_M2 = motorspeed_hex[3]
uint16_t speed_hex_M3 >> 8  = motorspeed_hex[4]
uint16_t speed_hex_M3 = motorspeed_hex[5]
uint16_t speed_hex_M4 >> 8  = motorspeed_hex[6]
uint16_t speed_hex_M4 = motorspeed_hex[7]
y++;
}

现在,我必须将 speed_hex_M1.. 转换为 int 值,我该如何在 c 中做到这一点?

非常感谢!

【问题讨论】:

  • 怕你不懂基本的东西。你需要从 c 书开始,学习语言和数据类型,然后开始编程微控制器。现在对你来说太早了
  • 谢谢,但我尝试通过做来学习它)数据类型有什么问题?

标签: c hex qbytearray


【解决方案1】:

关于“receive_done_flag” - 我正在使用 UART 的中断例程。如果接收到数据,“receive_done_flag”将设置为 TRUE,数据保存后我将其设置为 FALSE 并通过 UART 等待新数据。

这只是想法/问题,如何从十六进制获取 int 值?示例:

2 字节

uint16_t speed_hex_M1 = 04D1; 
uint16_t speed_hex_M2 = 04D1;
uint16_t speed_hex_M3 = 04D1;
uint16_t speed_hex_M4 = 04D1;

这是错误的,我知道(下面是正确的方法?):

uint16_t speed_hex_M1 >> 8 = motorspeed_hex[0]
uint16_t speed_hex_M1 = motorspeed_hex[1]
uint16_t speed_hex_M2 >> 8  = motorspeed_hex[2]
uint16_t speed_hex_M2 = motorspeed_hex[3]
uint16_t speed_hex_M3 >> 8  = motorspeed_hex[4]
uint16_t speed_hex_M3 = motorspeed_hex[5]
uint16_t speed_hex_M4 >> 8  = motorspeed_hex[6]
uint16_t speed_hex_M4 = motorspeed_hex[7]

所以,1 个字节

uint8_t motorspeed_hex[0] = 0x04;
uint8_t motorspeed_hex[1] = 0xD1;
....................

然后我可以将它保存到多个 uint8_t 数组中,然后读取它并将十六进制值转换为十进制 04D1 -> 1233。

编辑:

motorspeed_hex[0] = motorspeed_hex[0] | ((speed_hex_M1 & 0xFF00) >> 8); motorspeed_hex[1] = motorspeed_hex[1] | (speed_hex_M1 & 0x00FF);

【讨论】:

    【解决方案2】:

    语法错误太多,无法理解您尝试编写的内容。例如,在第一个代码片段中,您有“receive_done_flag == FALSE”,这是一个产生 1(真)或 0(假)的条件表达式,但不使用条件的值。你的意思是写一个赋值语句?还是 if 语句?还是……?

    在第二个片段中,您有“speed_hex_M1 >> 8 = motorspeed_hex[0]”。同样,在 C 中是无意义的。左侧是分配给的“变量(或存储单元)”。所以你的意思是“motorspeed_hx[0] = speed_hex_M1 >> 8”还是别的意思>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2012-10-07
      • 1970-01-01
      • 2018-07-26
      • 2016-07-25
      相关资源
      最近更新 更多