Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   UDP from C++ to Labview (http://www.chiefdelphi.com/forums/showthread.php?t=109372)

moler 02-11-2012 16:09

UDP from C++ to Labview
 
Hey everyone,

I've been trying to send a structure from C to labview.
My structure is composed of 3 variables, two integers and one float.

I can get labview to receive the two integer values however, it cannot interpret the float properly and gives me a number like 7.5264 e-41.

I feel like the problem is how labview interprets a float compared to a c++ on my linux machine.

just thought ill post this and see if anyone knows what my problem is?

Thanks

Greg McKaskle 03-11-2012 07:41

Re: UDP from C++ to Labview
 
LV and most languages support more than one size of floating point number. In particular, the single precision (4 byte), double precision (8 byte), and extended precision (10 or more) are pretty common. In C/C++ they will usually be called single, double, and long double. Sometimes you will see double doubles and other odd numeric types as well.

Check that the size of all three elements are correct, as misalignment of one of the earlier elements will affect the later elements.

The other factor causing this could be the byte order. Multibyte numbers can have their bytes transmitted either left-to-right, or right-to-left. This is often called little-endian and big-endian. It comes into play with memory architectures as well as transmission. By default, LabVIEW always assumes that data coming in from network and file are big-endian and does the work on little-endian machines like Intel to convert back and forth. If your data is in little endian format, then LV will load the bytes in the wrong order. If you are using the unflatten from string node, try wiring up the other byte-order parameter and see if that fixes it.

Greg McKaskle

moler 03-11-2012 10:58

Re: UDP from C++ to Labview
 
Hi Greg,

I've verified that the size of all the elements are correct, and I have tried the different byte orders with no luck. Any other ideas?

Thanks again for helping me out,

RyanCahoon 03-11-2012 21:37

Re: UDP from C++ to Labview
 
Quote:

Originally Posted by Greg McKaskle (Post 1192626)
In C/C++ they will usually be called single, double, and long double.

single-precision is usually called float in C/C++

kwojcik 03-11-2012 22:22

Re: UDP from C++ to Labview
 
To test try this just to make sure you don't run into any surprises. A long is sometimes 4 bytes just like an int.
Code:

printf("%d %d %d\n", sizeof(float), sizeof(double), sizeof(long double));
If you are still having trouble, try multiplying your double by 1000000, casting it to a long long and sending that over the socket. Then divide by 1000000 on the other side. But beware, if you want to keep decent precision doing this, you need to use long long because a long is only 4 bytes on the robot in c++! I did exactly this for a custom telemetry client I wrote
Code:

void writeDouble(std::vector<char>* packet, double value)
{
#ifndef ENABLE_TELEMETRY
        return;
#endif
        long long adjustedValue = ((long long)(value*1000000)); //send floating point as integer * 1000000
        unsigned char *dblPtr = (unsigned char*)&adjustedValue;
        for(int i = 0; i < sizeof(long long); i++)
        {
                packet->push_back(*dblPtr);
                dblPtr++;
        }
}

If all else fails you can send it as a string. Usually fool-proof.

moler 04-11-2012 01:09

Re: UDP from C++ to Labview
 
thanks everyone for your help!


All times are GMT -5. The time now is 11:06.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi