Creating and Using the Resource Manager

Overview

The IVI VISA Shared components (installed with the Keysight IO Libraries Suite) include a Global Resource Manager (GRM), which can instantiate (create) any VISA COM resource installed on the system. The GRM can find and use Vendor-Specific Resource Managers (SRMs) to find and create sessions to all the resources on a system. The GRM and the SRMs export the IResourceManager interface, which you will use to find and instantiate resources.

(Some early versions of the Keysight IO Libraries did not include the Global Resource Manager, but only the Keysight specific resource manager (SRM). If your program was written to use the Keysight SRM, modify it to use the GRM, by modifying the resource manager instantiation code (see below) and recompiling it to use the GlobalRM.dll type library file rather than the visacom.tlb type library file. If you do not make these changes, your program will still run, but will be less flexible and less portable. Whereas the GRM is able to locate resources that are supported by any implementation of VISA installed on your PC, the Keysight SRM communicates only with Keysight VISA. So if you need to use non-Keysight resources – for example, if you need to communicate with National Instruments PXI devices using VISA – then you need to use the GRM.)

Instantiating the Global Resource Manager

Visual Basic

  1. Include the "VISA COM 5.2 Type Library" (located in the GlobMgr.dll file) in Project >References (as shown in Figure 1 below).

  2. Create an object reference variable with the line

    Dim rm As VisaComLib.ResourceManager

  3. To instantiate the GRM object, use the line

    Set rm = New VisaComLib.ResourceManager

true

Figure 1: The Visual Basic Project References Dialog Box

Visual C++

  1. Use the statement

    #import "c:\program files\IVI Foundation\visa\visacom64\GlobMgr.dll" no_namespace

    or

    #import "c:\program files (x86)\IVI Foundation\visa\visacom\GlobMgr.dll" no_namespace

    to import the VISA COM type library.

  2. Create a variable of type IResourceManagerPtr with a statement such as

    IResourceManagerPtr rmPtr;

  3. To instantiate the GRM, use the line

    rmPtr.CreateInstance(__uuidof(ResourceManager));

The IResourceManagerPtr type is a specialization of the _com_ptr_t template class defined in comdef.h. See MSDN help on #import for more information.

Using the Global Resource Manager

Both GRMs and SRMs use the same interface, IResourceManager, to create resources. This interface provides resource creation and location services. The two methods you will find most useful will be the Open and FindRsrc methods. Use Open to create resources and use FindResourceAddress to retrieve valid resource strings based on a search string. Below is sample code for Open:

Visual Basic

Private Sub CreateResource()
On Error GoTo errorhandler
 Dim rm As VisaComLib.ResourceManager
 Dim session As VisaComLib.IMessage
 Dim status As Long
 Dim idn As String
 ' Instantiate the Global Resource Manager
 Set rm = New VisaComLib.ResourceManager
 ' Open the session with the default values for the Lock (None), Timeout(N/A), 
 ' and Option String (""). The return value is an IVisaSession, but the
 ' session variable is an IMessage interface reference, causing an implicit
 ' IUnknown::QueryInterface() to occur. VB handles the details.
 Set session = rm.Open("GPIB0::22")
 session.WriteString "*IDN?" & vbLf
 idn = session.ReadString(1000)
 MsgBox "The IDN String is: " & idn, vbOKOnly, "IDN? Result"
 Exit Sub
errorhandler:
 MsgBox Err.Description, vbExclamation, "Error Occurred", Err.HelpFile, Err.HelpContext

End Sub

Visual C++

#import "c:\program files\IVI Foundation\visa\visacom64\GlobMgr.dll" no_namespace 
int main()
{
    IResourceManagerPtr rmPtr;
    IMessagePtr msgPtr;
    IFormattedIO488Ptr fmioPtr;
    CoInitialize(NULL);
    try{
        // Instantiate the Global Resource Manager and Formatted IO class
        rmPtr.CreateInstance(__uuidof(ResourceManager));
	fmioPtr.CreateInstance(__uuidof(FormattedIO488));

        /* Open the session with the default values for the Lock (None), Timeout(N/A), 
           and OptionString (""). The dev_addr can be something like "GPIB0::1::INSTR".*/
        msgPtr = rmPtr->Open(dev_addr, NO_LOCK, 0, "");

	_bstr_t idnstr;
	long count = 100, written;

	//Using IMessage methods

	msgPtr->Clear();
	written = msgPtr->WriteString("*IDN?\n");
	idnstr = msgPtr->ReadString(count);


	//Using formattedIO methods
	//First set the IO stream to use for the formatted IO.
	fmioPtr->IO = msgPtr;
	_bstr_t str_result;
	fmioPtr->FlushRead();
	fmioPtr->FlushWrite(VARIANT_FALSE);
	fmioPtr->WriteString("*IDN?\n", VARIANT_FALSE);
	str_result = fmioPtr->ReadString();
    }
    catch(_com_error &err)
    {
	if (msgPtr != NULL)
	{
		msgPtr->Close();
		msgPtr = NULL;
	}

	if (rmPtr != NULL)
	{
	//	rmPtr.Release();
		rmPtr = NULL;
	}

	if (fmioPtr != NULL)
	{
	//	fmioPtr.Release();
		fmioPtr = NULL;
	}
        MessageBox(NULL, err.Description(), "Error Occurred", MB_OK);
        return 0;
    }
    // Note that it is very important to close the opened session before
    // calling CoUninitialize(). Without doing this, there will be a memory 
    // reference problem. 
    msgPtr->Close();
    msgPtr = NULL;
    // rmPtr.Release();
    rmPtr = NULL;
    // fmioPtr.Release();
    fmioPtr = NULL;
    CoUninitialize();
    return 1;
} 

See VISA Resource Strings for more information on the ResourceString parameter of Open and The Option String for more information on the OptionString parameter of Open.

See Also

Supported VISA COM I/O Resource Classes