LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Typecasting from (char *)floatArray to floatArray in LabVIEW

Solved!
Go to solution

in c++ I do this: 

 

float arr[1000];
CustomDataType dt;
int size = 4 * sizeof(float);  //bytes to read: 16
int n = read(channelID, (char *)arr, size, &dt)
//n = 16

This will read 4 floats into arr. In LabVIEW however, when I call the read function which is inside a dll, I get back the correct &dt but buffer out (second param) from the dll is:

CDA0 6D46 (hex presentation)

I labview I'm using SGL which is 4 bytes I think. 

 

Now I have tried to typecast it into a float array but it seems that either the data i'm receiving is incorrect or I'm typecasting in the wrong way. What am I doing wrong? The read function is correctly configured in LV because I also use it in the handshake procedure with expected bahaviour. 

 

labview 2016

 

 

0 Kudos
Message 1 of 6
(2,580 Views)

What value do you expect?

0 Kudos
Message 2 of 6
(2,568 Views)

Show us the code that makes the call to the DLL, since that's where the problem is. As far as LabVIEW is concerned there should be no need for a type cast to a string.

0 Kudos
Message 3 of 6
(2,557 Views)
Solution
Accepted by topic author aan928

Without the exact function prototype some of the things are left to guessing but something along these lines should work. Error handling is only template like and needs improving.Read float array.png

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 6
(2,519 Views)

I think that I have a mistake in the LabVIEW code. I have to test more tomorrow morning and report back. Thank you!

 

I can't pass a float array to buf. Buf is configured as CString pointer. 

I read the correct amount of bytes (foo). I get the correct type which is 11. I get a buf with correct length of (foo); I just need to typecast it. I used the typecast function which you can see in my original question, but its not correct.

 

 

 It was a mistake to communicate with c dlls directly from LabVIEW. I should have listened to another user in here who suggested to make a LabVIEW friendly dll which communicates with other c dlls. It would have saved me hours.

 

Download All
0 Kudos
Message 5 of 6
(2,505 Views)

wrote:

I can't pass a float array to buf. Buf is configured as CString pointer. 

I read the correct amount of bytes (foo). I get the correct type which is 11. I get a buf with correct length of (foo); I just need to typecast it. I used the typecast function which you can see in my original question, but its not correct.

 


Yes you can! The DLL function may be prototyped to accept a pointer to a char array but when you have the variable declaration

static float data[1024];

this basically creates an array of 32 bit floating point numbers. Then you typecast this with

int n = read(channelID, (char*)data, size, dt);

This is because the function is declared to have a char pointer buffer and the C compiler would bark if you tried to directly pass data to the function parameter.

 

However data is still an array of floats. Nothing changes in memory when you typecast a data pointer to a different type of pointer. The typecast is simply telling the compiler "yes I know this is a pointer to floats and this function rather would want to see a pointer to char and I know that it is safe to pass this float buffer to this function eventhough the function claims to want to see a char buffer. Go ahead and compile this anyhow, I know what I'm doing!"

 

No such thing is needed with the Call Library Node since this node does not know if the second parameter of the function in the DLL is to a pointer to a char (array), a float (array) or a bunch of oranges. It has to rely on you as programmer to configure the Call Library Node correctly. And if you tell it that it is a pointer to a float array it will pass the pointer to the float array to the function and if that function expects something else you crash but the typecasting in C is simply replaced by you defininig that it is this array. If this is not true the C code you show would also not be correct.

 

And since you only post pictures it is not possible to see if you have done this right but unless you configured the second parameter of the read function to have a minimum size equal to the size parameter, you are making the by far single most common error in trying to call a C function with the Call Library Node:

The read function wants to write into the buffer that it is passed in the second parameter and well up to the number of bytes passed in the third parameter. If you don't preallocate a large enough buffer explicitly either by configuring the minimum size for the second parameter in the Call Library Node or by explicitly allocating a large enough buffer through Initialize Array as done in my example LabVIEW will believe you what you told it and pass the pointer to a zero length buffer to the DLL and crash-bum you have a hard crash if you are lucky or a buffer overflow error otherwise, which would corrupt something in LabVIEWs memory.

Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 6
(2,496 Views)