From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem typecasting byte array to float array

Solved!
Go to solution

Can someone help me find the part of this process that I am doing wrong?

 

  • I am reading a string of 6 floats over serial.
  • I take the string and convert it into a byte array.
  • I then reverse the byte array because my microcontroller is sending the LSB first and Labview is Big-endian
  • I typecast this array to a float array
  • I reverse the array again to get the data back into the original order

I can see the bytes coming into the byte array as 'byte, 0, 0, 0' when it is a small number but when i typecast I get really large or really small floats. Can someone help me figure this out?

 

Thanks!

 

Labview 2011

  • byte_to_float_error.JPG
0 Kudos
Message 1 of 8
(4,173 Views)
Solution
Accepted by topic author ipatka

You should probably use unflatten from string, select little endian byte order, and use a type of 1D DBL array. Everything should fall in place without all these bit gynastics you are currently doing.

 

Can you show us your code? I don't quite understand your use of "decimate array".

 

Please attach a small VI containingh a typical received string as a diagram constant.

0 Kudos
Message 2 of 8
(4,164 Views)

Thanks for the suggestion. I probably implemented it wrong because I am getting similarly weird data.

 

My 1D array contains 6 different types of data which I decimate into their own 1D arrays to graph them.

 

I'm not quite sure how to represent a typical string as a diagram constant but write now my test C code is just transmitting 6 floats incrementing with each transmission. 

0 Kudos
Message 3 of 8
(4,151 Views)

ipatka wrote:

I'm not quite sure how to represent a typical string as a diagram constant...


  1. Create a string indicator on the raw string wire (right-click ...create indicator)
  2. Run the VI so the indicator contains typical data
  3. Stop the VI
  4. Right-click the terminal on the string indicator and "create constant"
  5. Place constant in a new VI, save it, and attach it here.
  6. revert the original VI.
0 Kudos
Message 4 of 8
(4,129 Views)

Here is the constant.

 

I now kind of have an idea of what is going on:

One float displayed in my code is 7.42688E-44

which is 00000000 00000000 00000000 00110101 in IEEE 754 format


The last byte 0010101 is 53 in decimal, which is the number I tried to send in. 

So it appears that my microcontroller is sending the wrong format.

 

0 Kudos
Message 5 of 8
(4,125 Views)

You're reading what's at the serial port and try to handle that, but you dont know or check what you've got. If you know you're recieving an array of singles, why not read 4 bytes at a time? 

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 6 of 8
(4,104 Views)

Are you sure it is sending 4-byte floats and not U32 or I32 32-bit integers?

 

Because 53 as a float would look like 4254 0000 as a string of hex bytes.

0 Kudos
Message 7 of 8
(4,090 Views)

I was transmitting 4 floats (3 of them empty) typecasted as bytes instead of the 4 bytes of a float. 


@altenbach wrote:

You should probably use unflatten from string, select little endian byte order, and use a type of 1D DBL array. Everything should fall in place without all these bit gynastics you are currently doing.


This solved the issue on the labview side

and 

 


bobgardner (avrfreaks) wrote:

float temperature; 
unsigned char *p; 



  p = (unsigned char *)&temperature; 
  for(i=0; i<4; i++){ 
    send(*p++); 
  }


this solved the issue on the avr side

Message 8 of 8
(4,084 Views)