LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

floating-point values over serial port?

Hi, NG!

Can anyone give me a hint, how to send/receive a motorola type
floating-point (float, real) value over the rs232 interface with
visa serial read/write?
After that I want to write these values as a string into a multicolumn list
box.

Any idea?

Thanks Radek
0 Kudos
Message 1 of 6
(5,285 Views)
Radek:

You mention the format of the actual data but you don't mention the type of the header or trailer. For example:

If the data is by itself (no header/trailer) you might do this:
ViReal64 value = 0.00000051;
viPrintf (io, "%.9le", value);

If the data has no header but is an array of floating point numbers terminated by a \n, you might do this:
ViInt32 totalPoints = MAX_DATA_PTS;
ViReal64 rdBuffer[MAX_DATA_PTS];
viScanf (io, "%,#le", &totalPoints, rdBuffer);
And on output, totalPoints will contain the actual number of data values read.

Both of the above methods transmit the data in ASCII format. If you need to transmit binary data, consider %b for IEEE-488.2 compliant binary blocks or %y for raw binary blocks. You can use %z for 32-bit f
loats or %Z for 64-bit floats. Note that these 2 work with %e or %b but not %y.

If your data is not terminated with \n or some special character, then you will need to do manual/raw viWrite and viRead functions. You can format the data in a seperate step with viSPrintf or viSScanf. Note that this separate step is not necessary with GPIB or any other bus in which END is a native concept.

See the NI-VISA 2.6 Programmer Reference Manual or online help for a complete list of valid format combinations. See chapter 5 of the NI-VISA 2.6 User Manual for many more examples than what I showed above.

Dan Mondrik
Senior Software Engineer, NI-VISA
National Instruments
0 Kudos
Message 2 of 6
(5,285 Views)
Sorry, maybe I'll explain my problem more detailed.

Here comes an example:

On a microcontroller I have a float number: -12.5 (intel little-endian)
The float is coded in that way:

0000 0000 0000 0000 1100 0001 0100 1000

After a conversion into motorola big-endian byte order:

1100 0001 0100 1000 0000 0000 0000 0000

this byte stream is sent to the pc via rs232 and read by visa serial read
vi.
There's no ascii transmission, no termination character, only pure binary
data.

First I'd like to show this float number in an indicator and later in a
multicolumn listbox.
How to convert this incoming string back into a float number on a pc?
Have I to swap the bytes again? I read about motorola-like storing binary
data under labview...

By the way: I'm pretty new t
o labview.
Where can I write code like:
viScanf (io, "%,#le", &totalPoints, rdBuffer); ? Is this a CIN vi?

Thanks

Radek
0 Kudos
Message 4 of 6
(5,285 Views)
What you can do is perform an unflatten (see my previous post w/attachment) using the first 4 bytes (you can throw away the last 4 bytes of zeros). This will convert the string to a single precision float which you can then use as any other float in LabVIEW.
0 Kudos
Message 6 of 6
(5,285 Views)
Serial data is 5,6,7,8 and sometimes 9 bits per character. This lends itself nicely to sending data in the form of signed or unsigned bytes. In order to send other forms of data, you will have to convert them to a byte-form first. For example, if I wanted to send a series of floats, I could transmit the following string:

"3.1415E0,2.7784E1,1.2345E-6,"...

Then on the other side I can decode the string and create an array of floats. LV gives you float-to-string conversion tools that will help do this. However, this "plain text" format is somewhat wasteful.

Another option is to transmit floats in 4 byte segments. The IEEE single precision floating point structure is as follows:

(32 bits) S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF

Where S = Si
gn, E = Exponent, and F is the fraction. (For a detailed description on decoding this see http://www.psc.edu/general/software/packages/ieee/ieee.html).

Once you have the 4 byte form for your floats, you can send this as a 4 byte string and then decode the value on the 'other side'. In LV, you can flatten the floats to a string, then transmit the string over serial, and unflatten the string on the other side. I've attached a vi that takes a float, flattens it, then unflattens it as an example.
0 Kudos
Message 3 of 6
(5,285 Views)
If I understand what you are trying to do, you want to send a floating-point value over a serial link and on the other end, take the received data values and display them in a multicolumn list box.

There are a number of ways of doing this quite simply. Basically, you just convert the float into an ascii string and then send it as you would any other string, but to give more guidance I need a little more information:

First, is the device sending the data doing on a timed basis like every 30 seconds or when ever a new measurement is ready? or does the remote device have to request a new value? Are the values coming as individual values or as a block of 10 or 20 values at once?

Second, what is the required format for the data in the multicolumn list box? D
oes it have an absolute maximum fixed size? How many decimal places must it have?

Third, the multicolumn list box is in essence a 2D display in that it displays both columns and rows. What will the columns and rows represent?

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 5 of 6
(5,285 Views)