08-14-2017 04:13 PM
Hello LabVIEW community,
I tried getting help earlier on my problem but wasn't able to come to the conclusion that I needed. Here is the situation. I have a micro controller that transmits 8 bits at a time over UART. I need to send a float value that is 32 bits. I decided to send the number in four 8 bit segments. I was wondering if there was a way in labview to take my four 8 bit values, construct it back into a 32 bit float value and then write that value into a file? Currently I have the following VI built. I have my firmware programmed to write the integer values 0 to 255 when it received the ASCII letter 'g' in it's received buffer (hence the VISA write block in my labVIEW program). I have tested and confirmed in my firmware debugger that I can receive characters being transmitted by the LabVIEW program. I was wondering if someone could help point me into the right direction.
Solved! Go to Solution.
08-14-2017 10:14 PM
You want the Typecast function. Wire a single precision floating point constant into the top of it so it interprets the 4 bytes as a 32-bit float.
You DO NOT want to use bytes at port. And certainly not with no wait function between the Write and the Read. Get rid of bytes at port and wire a 4 into the VISA Read. Otherwise you'll get incomplete messages and quickly get bytes that that won't line up to fit a 4-byte float if you do happen to reassemble them.
08-15-2017 04:09 AM
Personally, I prefer to use Unflatten From String since it allows me to specify the endianness. The Type Cast just assumes Big Endian.
08-15-2017 11:23 AM
Hello Crosrulz,
How exactly do I use the unflatten from string function. There is an input for type, I guess in my case I would like the to cast my data to float 32 (as this is what I am sending through the serial port). What exactly do I wire to the type as I can seem to create a constant or control to satisfy this code.
08-15-2017 11:35 AM
You can use a constant or a control for the type, it shouldn't care. Just make sure the type is single precision float, which is stored in 32 bits.
08-15-2017 11:48 AM
Hello gregoryj,
This is currently what my VI looks like I seem to be getting an error at the unflatten string function.
So if wire a numeric constant that is in the range of float 32 number, it will understand I want a float 32 number?
08-15-2017 12:34 PM
Use a Number to Fractional String and not a Number to Decimal String for a float value.
08-15-2017 12:43 PM - edited 08-15-2017 12:45 PM
@BMoneySpice wrote:
Hello gregoryj,
This is currently what my VI looks like I seem to be getting an error at the unflatten string function.
So if wire a numeric constant that is in the range of float 32 number, it will understand I want a float 32 number?
Yes.
Right now you have a Double precision float wired in which is 8 bytes or 64 bits. Right click on that constant and change the representation to Single.
08-15-2017 01:13 PM - edited 08-15-2017 01:21 PM
Hello RavensFan,
I changed the representation to a single for the constant I wired. I also changed the function to fractional string. However I am getting the error 74 at the unflatten to string function still. Currently I am getting the first value 0.000 before getting the error 74. I think this might be because after sending a number, I want to send the string "\nstring\n."
some context for my firmware. Initially, it will send the string "Hello World\n" through the UART. Previously I have been successful with writing Hello World to the text file. Afterwards my firmware for my microcontroller will sit in a while loop waiting for it's receive buffer to have a value. When microcontroller detects the character 'g', it is suppose to go into a for loop transmitting the whole numbers 0 to 255 in float32 format. This is just my test program eventually I will be sending decimal values (hence the necessity of float32).
08-15-2017 01:25 PM
Put an indicator on the string wire that comes out of the VISA Read. Run your VI and hit stop. Go to Edit menu >> Make Current Values Default. Save VI and attach. Without seeing the data you are receiving, it is impossible to know why you are getting error 74.