C# Visual Studio GPIB 命令

7

在C#、Visual Studio中,您使用哪些命令与GPIB仪器通信?我需要能够向仪器编写命令并读取输出。

5个回答

4

我使用Agilent IO Library Suite

以下是一个在C#中使用它的教程:C#中I/O编程示例

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

(声明:我在一家密集使用GPIB仪器的公司工作)


2
我正在使用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 的好处在于它可以与以下库之一配合使用:

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 添加到您的项目中。


0

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);

参考资料


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接