Hello all
I have a sketch with a float variable and I want pass it for processing in Visual Basic program.
On arduino side the float needs 4 bytes like in the Visual Basic.In arduino i'm using serial.write for every byte and then seend it to the computer, but on VB side if I already have a float variable declared how can I get it from arduino and put the incomming bytes on the float bytes.I belive i must do some bytewise operation for this, any tip?
I belive i must do some bytewise operation for this, any tip?
Even simpler is a union.
union stuff
{
byte b[4];
float f;
};
stuff.f = your value;
Send all 4 elements of stuff.b.
Use the same union on the other end. Put the 4 bytes in stuff.b. Get the float from stuff.f.
If you're using .Net, it's version of union is a bit different in it's usage (though accomplishes the same thing).
See StructLayoutAttribute Class (System.Runtime.InteropServices) | Microsoft Learn
The safest way to pass a float amongst different architectures is as a string. I don't know if bitwise copies of an ardiuno float will have the same meaning on a PC -- even IEEE 745 has byte order variations -- but "4.3764444E+03" will work fine, both directions. dtostre() will convert float to a specific, portable, string format.
dtostre() will convert float to a specific, portable, string format.
I didn't know this function.What I need to include to be available?
HugoPT:
dtostre() will convert float to a specific, portable, string format.
I didn't know this function.What I need to include to be available?
Shouldn't have to include anything. It's already included in the Arduino core.
OK cool.Just another question where I can find something about it, I goggled for dtostre()but no help
It's part of AVR Libc, which is the underlying C library used by Arduino.
Home page: AVR Libc Home Page
Reference: avr-libc: Modules
dtostre() is in stdlib.h