Go Down

Topic: Pass 4 bytes of a float variable to Visual Basic process (Read 813 times) previous topic - next topic

HugoPT

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?





Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

PaulS

Quote
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.

jraskell

If you're using .Net, it's version of union is a bit different in it's usage (though accomplishes the same thing).

See http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute%28v=vs.71%29.aspx

gardner

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.

HugoPT

Quote
dtostre() will convert float to a specific, portable, string format.

I didn't know this function.What I need to include to be available?
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

jraskell


Quote
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.

HugoPT

OK cool.Just another question where I can find something about it, I goggled for dtostre()but no help
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

jraskell

It's part of AVR Libc, which is the underlying C library used by Arduino.

Home page: http://www.nongnu.org/avr-libc/
Reference: http://www.nongnu.org/avr-libc/user-manual/modules.html

dtostre() is in stdlib.h

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy