C#Visual Studio GPIB命令

时间:2012-06-27 18:05:42

标签: c# gpib

您使用什么命令与C#,visual studio中的GPIB仪器通信? 我需要能够将命令写入仪器并读取输出。

5 个答案:

答案 0 :(得分:4)

我使用Agilent IO Library Suite

以下是在C#上使用它的教程:I/O programming examples in C#

然而,在我的公司,我们在VISA-COM实现方面存在稳定性问题,因此我们使用P / Invoke在visa32.dll(也是IO库套件的一部分)周围编写了自己的包装器。

(披露:我在一家大量使用GPIB工具的公司工作)

答案 1 :(得分:0)

您应首先使用LangInt类创建一个对象。然后使用GPIB方法使用该对象。 最常见和最常用的是(假设您创建了一个名为“dev”的对象);

dev.ibwrt(deviceHandle, "*IDN?", "*IDN?".Length);

dev.ibrd(deviceHandle, out Value, Arraysize);

这两个可以查询设备。或者您可以连续使用它们,例如设置发生器的频率,然后设置振幅。

重要的部分是在发送SCPI命令之前;你必须先初始化设备。要做到这一点,请使用;

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

必须在代码中首先声明这些参数。初始化后,您可以使用该设备处理的每个GPIB命令。

当然,您应该将NationalInstruments.NI4882和LangInt.dll添加到您的项目中。

答案 2 :(得分:0)

我正在使用National Instruments VISANI 488.2

首先确保您检查了NI-VISA设置中的VisaNS.NET API,见下图:

enter image description here

向您的项目添加对NationalInstruments.VisaNSNationalInstruments.Common的引用。

创建MessageBasedSession,请参阅以下代码:

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

可以使用MessageBasedSession通过GPIB,以太网或USB与您的仪器进行通信。

<强>更新

Ivi.Visa已取代NationalInstruments.VisaNS。因此,您应该只向项目添加Ivi.Visa的引用。

示例如下:

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

使用Ivi.Visa的好处是它适用于以下库之一:

答案 3 :(得分:0)

您可以使用NI Visa。 如果您使用的是Vb或C#,请使用示例程序磁盘中的Visa32.bas或Visa32.cs

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

答案 4 :(得分:-1)

从串口发送命令。

请参阅Microsoft的COM Port Example