Attribute-Based Resource Matching

NI-VISA

Attribute-Based Resource Matching

VISA can also search for a resource based on the values of the resource's attributes. The viFindRsrc() search expression is handled in two parts: the regular expression for the resource string and the (optional) logical expression for the attributes. Assuming that a given resource matches the given regular expression, VISA checks the attribute expression for a match. The resource matches the overall string if it matches both parts.

Attribute matching works by using familiar constructs of logical operations such as AND (&&), OR (||), and NOT (!). Equal (==) and unequal (!=) apply to all types of attributes, and you can additionally compare numerical attributes using other common comparators (>, <, >=, and <=).

You are free to make attribute matching expressions as complex as you like, using multiple ANDs, ORs, and NOTs. Precedence applies as follows:

  • The grouping operator () in an attribute matching expression has the highest precedence.
  • The NOT ! operator has the next highest precedence.
  • The AND && operator has the next highest precedence.
  • The OR operator || has the lowest precedence.

The following table shows three examples of matching based on attributes.

Expression Meaning
GPIB[0-9]*::?*::?*::INSTR {VI_ATTR_GPIB_SECONDARY_ADDR > 0 && VI_ATTR_GPIB_SECONDARY_ADDR < 10} Find all GPIB devices that have secondary addresses from 1 to 9.
ASRL?*INSTR{VI_ATTR_ASRL_BAUD == 9600} Find all serial ports configured at 9600 baud.
?*VXI?INSTR{VI_ATTR_MANF_ID == 0xFF6 && !(VI_ATTR_VXI_LA ==0 || VI_ATTR_SLOT <= 0)} Find all VXI instrument resources with manufacturer ID of FF6 and which are not logical address 0, slot 0, or external controllers.

Notice that only global VISA attributes are permitted in the attribute matching expression.

The following example is similar to the finding resources example, except that it uses a regular expression with attribute matching. Notice that because only the first match is needed, VI_NULL is passed for both the retCount and findList parameters. This tells VISA to automatically close the find list rather than return it to the application.

Example

Note  This example shows C source code. There is also an example in Visual Basic syntax.

#include <stdio.h>
#include "visa.h"

#define MANF_ID
#define MODEL_CODE
0xFF6
0x0FE
/* 12-bit VXI manufacturer ID of device */
/* 12-bit or 16-bit model code of device */

/* Find the first matching device and return a session to it */
ViStatus autoConnect2(ViPSession instrSesn)
{

ViStatus
ViSession
ViChar
status;
defaultRM, instr;
desc[VI_FIND_BUFLEN], regExToUse[VI_FIND_BUFLEN];

status = viOpenDefaultRM(&defaultRM);
if (status < VI_SUCCESS) {

/* Error initializing VISA ... exiting */
return status;

}

/* Find the first matching VXI instrument */
sprintf(regExToUse,
"?*VXI?*INSTR{VI_ATTR_MANF_ID==0x%x && VI_ATTR_MODEL_CODE==0x%x}", MANF_ID, MODEL_CODE);
status = viFindRsrc(defaultRM, regExToUse, VI_NULL, VI_NULL, desc);
if (status < VI_SUCCESS) {

/* Error finding resources ... exiting */
viClose(defaultRM);
return status;

}

status = viOpen(defaultRM, desc, VI_NULL, VI_NULL, &instr);
if (status < VI_SUCCESS) {

viClose(defaultRM);
return status;

}

*instrSesn = instr;
/* Do not close defaultRM, as that would close instr too */
return VI_SUCCESS;

}