C# Visual Studio GPIB Commands

◇◆丶佛笑我妖孽 提交于 2019-12-01 05:15:07
Benoit Blanchon

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)

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:

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.

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

Send commands out the serial port.

See Microsoft's COM Port Example.

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