Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Getting the IOSession from an IVISession that was created in a wrapped IVIC driver

Dear all,

 

I have created

  • a C# wrapper
  • for the IVIC driver of the 34420A Micro-Ohm Meter from Keysight (formerly Agilent)
  • using NI Measurement Studio

 

I am able to initialize the instrument using the (wrapped) init function, which returns a valid IVI Session Handle. However, as the wrapped driver does not expose full control over the underlying interface, I want to mix calls to the wrapped IVIC driver methods and "pure" Visa commands. Therefore, I would have to read the IVI_ATTR_IO_SESSION attribute from the IVI session, but I am not sure how to do this. The wrapped IVIC driver does expose the following getAttribute methods:

 

  • public static extern int GetAttributeViInt32(HandleRef instrumentHandle, string channelName, int attributeId, out int attributeValue);
  • public static extern int GetAttributeViReal64(HandleRef instrumentHandle, string channelName, int attributeId, out double attributeValue);
  • public static extern int GetAttributeViString(HandleRef instrumentHandle, string channelName, int attributeId, int arraySize, StringBuilder attributeValue);
  • public static extern int GetAttributeViBoolean(HandleRef instrumentHandle, string channelName, int attributeId, out ushort attributeValue);
  • public static extern int GetAttributeViSession(HandleRef instrumentHandle, string channelName, int attributeId, out HandleRef attributeValue);

However, calling 

GetAttributeViSession(_iviSessionHandle, "", IVI_ATTR_IO_SESSION, out visaHandleRef)

Results in a marshalling error, and calling 

GetAttributeViInt32(iviSessionHandle, "", IVI_ATTR_IO_SESSION, out visaSession);

returns 0.

  

What am I missing here?

 

 

EDIT:

 

I found a way to circumvent the Marshalling Error.

Within the C# wrapper, I added an overload of the GetAttributeViSession method that takes an IntPtr instead of a HandleRef

  • public static extern int GetAttributeViSession(HandleRef instrumentHandle, string channelName, int attributeId, out IntPtr attributeValue);

This method can be called using 

 

GetAttributeViSession(_iviSessionHandle, "", IVI_ATTR_IO_SESSION, out visaSessionPtr)

The pInvokeResult is "0", and visaSessionPtr contains a valid Int32 number. However I have not tested the functionality yet.

 

0 Kudos
Message 1 of 2
(2,986 Views)

The EDIT above seems to solve the problem. I have succesfully tested it by:

  1. Getting the IviSession handle with the driver's init routine.
  2. Getting the IoSession with another, slightly changed GetAttributeViSession overload.
  3. Locking the Resouce using ViLock on the IoSession directly
  4. Trying to create another connection to the isntrument (which fails as the resource is locked).

The problem could be solved as follows:

 

The NI Wrapper Genreator uses a wrong datataype for an IVI Attribute getter in the generated IVIC wrapper:

 

 

public static extern int GetAttributeViSession(HandleRef instrumentHandle, string channelName, int attributeId, out HandleRef attributeValue);

 

 

 

should be (you can add it as another overload):

 

 

public static extern int GetAttributeViSession(HandleRef instrumentHandle, string channelName, int attributeId, out IntPtr attributeValue);

 

 

This overload can be used to get the IoSession after the IviSessionHandle has been created using the wrapped Init method as follows:

 

IntPtr visaSessionPtr;
GetAttributeViSession(_iviSessionHandle, "", IVI_ATTR_IO_SESSION, out visaSessionPtr);

 Afterwards you can of course create a VisaSessionHandle in a HandleRef.

0 Kudos
Message 2 of 2
(2,967 Views)