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

Required Outputs Wizard

Base64 Encoding