LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing Float Data from Serial Device

Solved!
Go to solution

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.

0 Kudos
Message 1 of 19
(6,099 Views)

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.

0 Kudos
Message 2 of 19
(6,050 Views)
Solution
Accepted by topic author BMoneySpice

Personally, I prefer to use Unflatten From String since it allows me to specify the endianness.  The Type Cast just assumes Big Endian.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 19
(6,023 Views)

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.

0 Kudos
Message 4 of 19
(5,999 Views)
Solution
Accepted by topic author BMoneySpice

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.

 

temp.png

0 Kudos
Message 5 of 19
(5,993 Views)

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?

0 Kudos
Message 6 of 19
(5,986 Views)

Use a Number to Fractional String and not a Number to Decimal String for a float value.

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
0 Kudos
Message 7 of 19
(5,977 Views)

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

0 Kudos
Message 8 of 19
(5,975 Views)

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

0 Kudos
Message 9 of 19
(5,959 Views)

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.

0 Kudos
Message 10 of 19
(5,954 Views)