Ir al contenido principal

Coerce to Type

I'm surprised the Coerce to Type primitive hasn't found yet its way to the public front. It was revealed some years ago and since then has proved being a reliable way of converting data types, much better than the classic Type Cast, but it's still not visible in the LabVIEW functions API.

Consider the following code example. You're casting an U32 value (2) to U8 (you shouldn't, but shit happens). Type cast just casts it, so the resulting value is 0 (wrong!), but Coerce to Type understands what you are doing and returns 2 (correctly).

To use Coerce to Type in your VIs, the easiest way is downloading the VI below into your drive.

Download (VI for LabVIEW 2016)

Next we're going to place it in the functions palette. So, open any block diagram and click Tools → Advanced → Edit Palette Set. Then select the palette you want to add the VI into (in this case I recommend Programming → Numeric → Data Manipulation) and click Insert → VI(s) and select the path to the VI.

Now, important, right-click the new VI icon and select Edit short name (shorten it to "Coerce to Type") and check the Place VI Contents option. This way when you use it LabVIEW will paste the VI contents (that is, just the primitive) instead the VI itself. And save all the changes, of course 馃榾

Comentarios

Entradas populares de este blog

SNMP library for LabVIEW

SNMP (Simple Network Management Protocol) is a fairly common protocol for communicating with network equipment. It's fast, lightweight and standardized. Unfortunately it's not quite as simple as its name implies and LabVIEW doesn't support it natively.Some years ago I was in the necessity of using SNMP from a LabVIEW system to control some hardware. I started looking for already implemented solutions and found a library developed by Mark Yedinak (great work!). It was useful since it demonstrated it could be done and how, but I needed more control over the operations, so I had to read the protocol down to the low level bytes and implement what I need by myself. Yeah, pretty hard and with time constraints no less. Anyway, here it is.Download (Library in LLB file for LabVIEW 2016)See the examples folder to grasp the basic use. Probably you'll need no more than these sets of VIs to send and retrieve data and to get traps sent by your equipment. To integrate in a program, u…

Required Outputs Wizard

It's an old LabVIEW programmer's dream to be able to make VI outputs required, just as can be done with inputs. Some years ago I discovered a way of achieving it, through a deprecated scripting method. I checked with NI staff and they said it was clever but could cause unexpected problems, so I set it aside.But the time goes by and the functionality is still not available, so here we go, the Required Outputs Wizard is yours. I've been using it on and off for a long time and no errors have arisen, even when applying it to connectors with modern data types, such as classes (OOP). But I guess the risk is still there, so be cautious and test it first with unimportant VIs.Download (LLB for LabVIEW 2016)The bowels of the codeSo, how it works? Launch the top VI in the LLB, you can leave it there running. When a connected front panel indicator is selected, the "REQUIRED" button will be available. Just press it. Yeah, that's all, now your indicator connection is requi…

Variant to XML

Converting to XML format is a good way of exporting data in a standard, human-readable way. I have used it to pass and receive data from other languages, for example, or if you need to dump data to a log file.As you may know, LabVIEW includes its own functions for converting to and from XML, called Flatten To XML and Unflatten From XML. Unfortunately, it's a raw conversion that just gives a node with the data type and two child nodes, name and value. So, to find a field you must recover first all the name nodes, locate the one you need and then find its value sibling. Too crude for a lot of scenarios:So I developed my own library to do this conversion in a more convenient way. Check the differences in the next image:As you can see the field names are now used as tag names, resulting in a shorter and cleaner XML text. The reverse conversion (from XML to LabVIEW data) is shown in the bottom of the image, and works the same.In the block diagram, these functions are used directly, jus…