Sending SCPI/GPIB commands over USB from C#

微笑、不失礼 提交于 2019-12-24 08:57:30

问题


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...


回答1:


If you install the visa driver from either NationalInstruments or Keysight, they do implement classes:

The one from NI:

  1. FormattedIO488Class
  2. ResourceManagerClass
  3. 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 { }
}



回答2:


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.



来源:https://stackoverflow.com/questions/41423094/sending-scpi-gpib-commands-over-usb-from-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!