C# Visual Studio GPIB Commands

后端 未结 5 727
醉梦人生
醉梦人生 2021-01-05 14:10

What commands do you use to talk to a GPIB instrument in C#, visual studio? I need to be able to write commands to the instrument and read the output.

相关标签:
5条回答
  • 2021-01-05 14:44

    Send commands out the serial port.

    See Microsoft's COM Port Example.

    0 讨论(0)
  • 2021-01-05 14:45

    I'm using National Instruments VISA and NI 488.2.

    First make sure that you checked the VisaNS.NET API in the NI-VISA Setup, see the following figure:

    Add a reference to NationalInstruments.VisaNS and NationalInstruments.Common to your project.

    Create a MessageBasedSession, see the following code:

    string resourceName = "GPIB0::20::INSTR"; // GPIB adapter 0, Instrument address 20
    var visa = new NationalInstruments.VisaNS.MessageBasedSession(resourceName);
    visa.Write("*IDN?"); // write to instrument
    string res = visa.ReadString(); // read from instrument
    

    A MessageBasedSession can be used to communicate with your instrument over GPIB, Ethernet or USB.

    Update

    Ivi.Visa superseded NationalInstruments.VisaNS. So you should add a reference only to Ivi.Visa to your project.

    The example would look like that:

    string resourceName = "GPIB0::20::INSTR"; // GPIB adapter 0, Instrument address 20
    var visa = GlobalResourceManager.Open(resourceName) as IMessageBasedSession;
    visa.RawIO.Write("*IDN?\n"); // write to instrument
    string res = visa.RawIO.ReadString(); // read from instrument
    

    The benefit of using Ivi.Visa is that it works with one of the following libraries:

    • National Instruments VISA
    • Keysight IO Libraries Suite
    • Rohde & Schwarz VISA
    0 讨论(0)
  • 2021-01-05 14:51

    You can use NI Visa. If you are using Vb or C# use Visa32.bas or Visa32.cs from the sample programs disk

    int DefaultSessionId= 0;
    int SessionId= 0;
    int LastStatus = 0;
    string Address = "GPIB0::6" ; //any address
    
    //Session Open
    LastStatus = visa32.viOpenDefaultRM(out DefaultSessionId);
    
    //Connection Open
    LastStatus = visa32.viOpen(DefaultSessionId, Address + "::INSTR", 0, 0, out sessionId);
    LastStatus = visa32.viSetAttribute(SessionId, visa32.VI_ATTR_TERMCHAR, 13);// Set the termination character to carriage return (i.e., 13);
    LastStatus = visa32.viSetAttribute(SessionId, visa32.VI_ATTR_TERMCHAR_EN, 1);// Set the flag to terminate when receiving a termination character
    LastStatus = visa32.viSetAttribute(SessionId, visa32.VI_ATTR_TMO_VALUE, 2000);// Set timeout in milliseconds; set the timeout for your requirements
    
    //Communication
    LastStatus = visa32.viPrintf(SessionId, command + "\n");//device specific commands to write
    StringBuilder message = new StringBuilder(2048);
    LastStatus = visa32.viScanf(SessionId, "%2048t", message);//Readback
    
    //Session and Connection Close
    visa32.viClose(SessionId);
    visa32.viClose(DefaultSessionId);
    

    Reference

    0 讨论(0)
  • 2021-01-05 14:57

    I use Agilent IO Library Suite.

    Here is a tutorial to use it on C#: I/O programming examples in C#

    Nevertheless, in my company we had stability issues with the VISA-COM implementation, so we wrote our own wrapper around the visa32.dll (also part of the IO Library suite) using P/Invoke.

    (Disclosure: I work in a company that make intense use of GPIB instruments)

    0 讨论(0)
  • 2021-01-05 14:57

    You should create an object with LangInt class first. Then use that object with GPIB methods. Most common and used ones are(assuming you created an object named "dev");

    dev.ibwrt(deviceHandle, "*IDN?", "*IDN?".Length);
    
    dev.ibrd(deviceHandle, out Value, Arraysize);
    

    These two can query the device. Or you can use them consecutively for example setting a generator's frequency and then it's amplitude.

    Important part is before sending SCPI commands; you MUST initialize devices first. To do this use;

    deviceHandle = ibdev(GPIBINDEX, GPIBADDRESS, SECONDARYADDRESS, TIMEOUT, EOTMODE, EOSMODE);
    

    These parameters must declared first within the code. After initialization you can use every GPIB command with that device handles.

    And of course you should add NationalInstruments.NI4882 and LangInt.dll to your project.

    0 讨论(0)
提交回复
热议问题