Using Formatted I/O

VISA COM I/O comes with a basic Formatted I/O Component that provides 488.2-style formatted I/O capabilities. The component implements the IFormattedIO488 Interface.

Instantiating the Formatted I/O Object

Visual Basic

  1. Include the following library in Project > References (as seen below):
    • The "VISA COM 5.2 Type Library", which corresponds to the GlobMgr.dll file  
  2. Use a statement such as

    Dim fmio As VisaComLib.FormattedIO488

    to create the formatted I/O reference .
     

  3. Use

    Set fmio = new VisaComLib.FormattedIO488

    to create the actual object.

true

Figure 1: The Visual Basic Project References Dialog

Visual C++

  1. Use the statement

    #import "GlobMgr.dll" no_namespace

    to import the VISA COM type library.  

  2. Create a variable of type IFormattedIO488Ptr to hold the reference to the object. 

  3. To instantiate the object, use the line

    rm.CreateInstance( __uuidof(FormattedIO488) )

The IFormattedIO488Ptr 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 Formatted I/O Object

Below is sample code for using the Formatted I/O object with various development environments.

Visual Basic

Private Sub CreateResource2()
 On Error GoTo errorhandler
 Dim rm As VisaComLib.ResourceManager
 Dim fmio As VisaComLib.FormattedIO488
 Dim session As VisaComLib.IMessage
 Dim idn As String
 Set rm = New VisaComLib.ResourceManager
 Set fmio = New BASICFORMATTEDIOLib.FmtdIOCls
 Set session = rm.Open("GPIB0::22")
 Set fmio.IO = session
 fmio.WriteString "*IDN?"
 idn = fmio.ReadString()
 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\visa\visacom\GlobMgr.dll" no_namespace 
int main()
{
    IResourceManagerPtr rm;
    IMessagePtr ptr;
    IFormattedIO488Ptr fmioPtr;
    CoInitialize(NULL);
    try{
        // Instantiate the Global Resource Manager and Formatted IO class
        rm.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".*/
        ptr = rm->Open(dev_addr, NO_LOCK, 0, "");

	_bstr_t idnstr;
	long count = 100, written;

	//Using IMessage methods

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


	//Using formattedIO methods
	//First set the IO stream to use for the formatted IO.
	fmioPtr->IO = ptr;
	_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 (ptr != NULL)
	{
		ptr->Close();
		ptr = NULL;
	}

	if (rm != NULL)
	{
	//	rm.Release();
		rm = 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. 
    ptr->Close();
    ptr = NULL;
    // rm.Release();
    rm = NULL;
    // fmioPtr.Release();
    fmioPtr = NULL;
    CoUninitialize();
    return 1;
}