I'm trying to communicate with some test equipment from C# over SCPI. I managed to communicate with one device that is connected through TCP/IP by using this code example.
However, my other devices are connected through USB and I haven't find how to communicate with them over USB.
BTW, I found this question, and the link from the answer to the IVI-COM programming examples in C# document, but I couldn't apply the code samples (e.g. in section 5.4) because all of the IVI and VISA COM libraries I found (e.g. VisaComLib 5.5) has only interfaces and enums in it, and no concrete class that I can use...
If you install the visa driver from either NationalInstruments or Keysight, they do implement classes:
The one from NI:
- FormattedIO488Class
- ResourceManagerClass
- VisaConflictTableManagerClass
To get a connection, you only need 1 and 2
As soon as you try to embed the interoptypes, you need to remove the 'Class' suffix, as described here
Here comes a sample snippet from Keysight (Application Note: 5989-6338EN)
Ivi.Visa.Interop.ResourceManager rm = new Ivi.Visa.Interop.ResourceManager();
Ivi.Visa.Interop.FormattedIO488 ioobj = new Ivi.Visa.Interop.FormattedIO488();
try
{
object[] idnItems;
ioobj.IO = (Ivi.Visa.Interop.IMessage)rm.Open("GPIB2::10::INSTR",
Ivi.Visa.Interop.AccessMode.NO_LOCK, 0, "");
ioobj.WriteString("*IDN ?", true);
idnItems = (object[])ioobj.ReadList(Ivi.Visa.Interop.IEEEASCIIType.ASCIIType_Any, ",");
foreach(object idnItem in idnItems)
{
System.Console.Out.WriteLine("IDN Item of type " + idnItem.GetType().ToString());
System.Console.Out.WriteLine("\tValue of item is " + idnItem.ToString());
}
}
catch(Exception e)
{
System.Console.Out.WriteLine("An error occurred: " + e.Message);
}
finally
{
try { ioobj.IO.Close(); }
catch { }
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(ioobj);
}
catch { }
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(rm);
}
catch { }
}
I'm using National Instruments VISA.
Add a reference to NationalInstruments.VisaNS
and NationalInstruments.Common
to your project.
Create a MessageBasedSession
, see the following code:
string resourceName = "USB0::0x0957::0x0118::US56070667::INSTR"; // See NI MAX for resource name
var visa = new NationalInstruments.VisaNS.MessageBasedSession(resourceName);
visa.Write("*IDN?"); // write to instrument
string res = visa.ReadString(); // read from instrument
See as well https://stackoverflow.com/a/49388678/7556646.