我想通过UART从PC接收十六进制值并将其保存到STM32-uC。
收到的十六进制值如下:
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个字节中获取整数值:
04D1-> 1233
我创建了多数组
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++;
}
我的想法正确吗?
稍后如何读取存储中的数据? 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中做到这一点?
非常感谢您!
答案 0 :(得分:0)
存在太多语法错误,无法理解您要编写的内容。例如,在第一个代码片段中,您有一个“ receive_done_flag == FALSE”,它是一个条件表达式,产生1(真)或0(假),但是不使用条件的值。你的意思是写一个任务声明吗?还是if陈述?还是...?
在第二个片段中,您具有“ speed_hex_M1 >> 8 = motorspeed_hex [0]”。同样,在C语言中没有意义。左侧是分配给它的“变量(或存储单元)”。所以您的意思是“ motorspeed_hx [0] = speed_hex_M1 >> 8”还是其他意思>
答案 1 :(得分:0)
关于“ receive_done_flag”-我使用UART中断例程。如果接收到数据,则“ receive_done_flag”将设置为TRUE,在保存数据后,我将其设置为FALSE并通过UART等待新数据。
这仅仅是个想法/问题,如何从十六进制中获取整数值?示例:
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);