Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 2 months ago.
Sending floating point number over UART from NUCLEO-F401RE to a Raspberry Pi
I am trying to send temperature sensor data(float) from NUCLEO-F401RE board to a Raspberry Pi 3 through UART. could someone suggest me the best way to do it. Initiallyfor checking purpose I connected the TX and RX in the nucleo board and tried sending and receiving a character, which works. But not able to figure out how to send floating point numbers.
1 Answer
8 years, 2 months ago.
Here are some code snips of how I do it. The float is a 4-byte variable so I use a union. Then I address the bytes via the union when sending over serial. You can do this other ways for example if you want to printf on the Tx side and scanf on the receive side. This is a binary format technique:
... Serial _mySerial(Tx_pin, Rx_pin); ... union{ float celsius; //Digital Board temperature char tempbytes[4]; }dig; ... _mySerial.putc(dig.tempbytes[0]); _mySerial.putc(dig.tempbytes[1]); _mySerial.putc(dig.tempbytes[2]); _mySerial.putc(dig.tempbytes[3]); ....
Then be sure to reassemble it in the same manner. Be aware that this technique assumes the same endianess on both sides.