Pages: [1]   Go Down
Author Topic: Pass 4 bytes of a float variable to Visual Basic process  (Read 699 times)
0 Members and 1 Guest are viewing this topic.
Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1544
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?





Logged

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


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Ontario
Offline Offline
God Member
*****
Karma: 24
Posts: 860
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1544
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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


New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1544
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK cool.Just another question where I can find something about it, I goggled for dtostre()but no help
Logged

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


New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Pages: [1]   Go Up
Jump to: