简体   繁体   English

需要帮助来理解指针(在C中)以通过UART接收浮点数

[英]Need help understanding pointers (in c) to receive a float over UART

I need to send floats from Simulink to C (embedded in a MCU) via UART. 我需要通过UART将浮点数从Simulink发送到C(嵌入在MCU中)。

I have found code that works for sending floats in the opposite direction, but I need to fully understand it to write code for receiving a float. 我发现了可以反向发送浮动消息的代码,但是我需要完全理解它才能编写用于接收浮动消息的代码。

This is the original code: 这是原始代码:

 unsigned char *chptr;
 chptr = (unsigned char *) &floatvalue;
 Tx(*chptr++);Tx(*chptr++);Tx(*chptr++);Tx(*chptr);

This is the my altered code (that works): 这是我修改过的代码(有效):

float testFloat = 3.1416;
unsigned char *chptr;                           
chptr = (unsigned char *) &testFloat;           
ROM_UARTCharPut(UART0_BASE,*chptr++);           
ROM_UARTCharPut(UART0_BASE,*chptr++);
ROM_UARTCharPut(UART0_BASE,*chptr++);
ROM_UARTCharPut(UART0_BASE,*chptr);

I think I understand the gist of what is going on but some things I am not sure about: 我想我了解发生了什么事,但有些事情我不确定:

Float is declared which is 4 bytes long. 浮点数声明为4个字节长。

float testFloat = 3.1416;

A pointer is declared which is 1 byte long 声明了一个1字节长的指针

unsigned char *chptr;    

The address of the float is cast into the pointer. 浮点数的地址被强制转换为指针。 Because of the difference in bit length of the pointer and the char I am assuming that only the address of bits 0 to 7 of the float are cast into the pointer (little endianness) 由于指针和char的位长不同,我假设仅将float的位0到7的地址强制转换为指针(小字节序)。

chptr = (unsigned char *) &testFloat;    

The next four lines is where my understanding breaks down. 接下来的四行是我理解的地方。

ROM_UARTCharPut(UART0_BASE,*chptr++);           
ROM_UARTCharPut(UART0_BASE,*chptr++);
ROM_UARTCharPut(UART0_BASE,*chptr++);
ROM_UARTCharPut(UART0_BASE,*chptr);

I understand that "*chptr" is the value of the variable the pointer is pointing to. 我知道“ * chptr”是指针所指向的变量的值。 I also understand that "*chptr++" increments the address of the pointer to the next byte. 我也了解“ * chptr ++”将指针的地址递增到下一个字节。 However the order does not make sense to me. 但是,该命令对我而言没有意义。

If I was to label a 4 byte float as: 如果我将一个4字节的float标记为:

byte4 byte3 byte2 byte1 字节4字节3字节2字节1

It seems to me like the first send line sending *chptr++ would send byte2 not byte1 在我看来,第一个发送* chptr ++的发送行将发送byte2而不是byte1

next line would send byte3, 下一行将发送byte3,

next line byte4 下一行byte4

and the last line a byte of a neighbouring variable or byte1. 最后一行是相邻变量或字节1的字节。

However it does work properly on the receiving end (Simulink set to receive in little-endiann) so my understanding must be wrong. 但是,它在接收端(Simulink设置为在little-endiann中接收)可以正常工作,因此我的理解一定是错误的。

Thank you for any clarification. 谢谢您的澄清。

PS: once I understand this method, would it work for receiving floats? PS:一旦我了解了这种方法,该方法是否可以接收浮动消息? Or am I barking up the wrong tree? 还是我吠错了树?

Thank you 谢谢

As the comments on your question state, you're using post-increment. 作为对问题状态的评论,您正在使用后递增。 So your code is accessing the value at the given memory location, doing what it needs to do with it and THEN moving the pointer to the next memory location, ready for the next line of code. 因此,您的代码正在访问给定存储位置处的值,执行该操作所需的操作,然后将指针移至下一个存储位置,为下一行代码做好准备。

If you'd been using pre-increment, then you're statement "*It seems to me like the first send line sending chptr++ would send byte2 not byte1 " would be true. 如果您一直在使用pre-increment,那么您会说“ *在我看来,发送chptr ++的第一个发送行将发送byte2而不是byte1 ”将为真。 If you want to test this, run the code in debug mode and change *chptr++ to *++chptr. 如果要对此进行测试,请在调试模式下运行代码,并将* chptr ++更改为* ++ chptr。

This post has a good explanation of incrementing operations. 这篇文章很好地解释了增量操作。

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

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