Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Creating a CIN to Work with Clusters

LabVIEW 8.5 Help
August 2007

NI Part Number:
371361D-01

»View Product Info

In this example, the CIN takes an array of clusters and a single cluster as inputs. The clusters contain a 16-bit signed integer and a string. The top terminal of the CIN is an input-output terminal. The top terminal takes the array of clusters as an input and returns the new array of clusters as an output.

In addition to the new array of clusters, the CIN returns a Boolean parameter and a 32-bit signed integer. If the single cluster is already present in the array of clusters, the CIN sets the Boolean parameter to TRUE. If the Boolean parameter is TRUE, the CIN returns in the 32-bit integer output the position the single cluster occupies in the array of clusters.

If the single cluster is not present in the array of clusters, the CIN adds it to the array, sets the Boolean output to FALSE, and returns through the 32-bit integer output the position the single cluster now occupies in the new array of clusters.

This example shows only the front panel, block diagram, and source code. Complete the following steps to create the CIN.

Note  LabVIEW must be installed on the computer you use to create the CIN.
  1. Create the CIN. The following illustration shows the front panel for this VI.

    The following illustration shows the block diagram for this VI.

  2. Save the VI as tblsrch.vi.
  3. Create the .c file and save it as tblsrch.c. The following code is the source code for tblsrch.c with the CINRun routine added.

/*
 * CIN source file
 */
#include "extcode.h"
#define ParamNumber 0
    /* The array parameter is parameter 0 */
/*
 * typedefs
 */
typedef struct {
    int16 number;
    LStrHandle string;
    } TD2;
typedef struct {
    int32 dimSize;
    TD2 arg1[1];
    } TD1;
typedef TD1 **TD1Hdl;
CIN MgErr CINRun(
                TD1Hdl clusterTableh,
                TD2 *elementp,
                LVBoolean *presentp,
                int32 *positionp);
CIN MgErr CINRun(
                TD1Hdl clusterTableh,
                TD2 *elementp,
                LVBoolean *presentp,
                int32 *positionp) {
    int32 size,i;
    MgErr err=noErr;
    TD2 *tmpp;
    LStrHandle newStringh;
    TD2 *newElementp;
    int32 newNumElements;

    size = (*clusterTableh)–>dimSize;
    tmpp = (*clusterTableh)–>arg1;
    *positionp = –1;
    *presentp = LVFALSE;
      for(i=0; i<size; i++) {
        if(tmpp–>number == elementp–>number)
            if(LStrCmp(*(tmpp–>string-->,
                  *(elementp–>string-->) == 0)
              break;
        tmpp++;
        }
      if(i<size) {
        *positionp = i;
        *presentp = LVTRUE;
        goto out;
        }
        /* DSCopyHandle will allocate a new handle since this is NULL */
    newStringh = NULL;
    if(err = DSCopyHandle(&newStringh,elementp->string-->)
      goto out;
    newNumElements = size+1;
    if(err = SetCINArraySize((UHandle)clusterTableh,
        ParamNumber,newNumElements)) {
      DSDisposeHandle(newStringh);
      goto out;
      }
    (*clusterTableh)–>dimSize = size+1;
    newElementp = &((*clusterTableh)–>arg1[size]);
    newElementp–>number = elementp–>number;
    newElementp–>string = newStringh;
    *positionp = size;
out:
    return err;
    }

In this example, CINRun is the only routine performing substantial operations. CINRun first searches through the table to see if the single cluster is present. CINRun then compares string components using the LabVIEW routine LStrCmp. If CINRun finds the single cluster, the routine returns the position the single cluster occupies in the array of clusters.

If the routine does not find the single cluster in the array of clusters, the CIN adds a new element to the array of clusters. The memory manager routine DSCopyHandle creates a new handle containing the same string as the one in the single cluster you passed to the CIN. CINRun increases the size of the array of clusters using SetCINArraySize and fills the last position in the new array of clusters with a copy of the single cluster you passed to the CIN. If the SetCINArraySize call fails, the CIN returns the error code returned by the manager. If the CIN is unable to resize the array, LabVIEW disposes of the duplicate string handle.

Refer to the Search an array of clusters VI in the labview\examples\cins\tblsrch directory for an example of using the CIN with clusters.


Resources


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit