我正在编写一个 C# 应用程序,它使用 National Instruments GPIB-USB-B 接口和 National Instruments VisaNS NI-488.2 库通过 GPIB 与 Agilent 34970A 数据记录器通信。我成功地将数据记录器设置为以固定间隔扫描其输入,并且我想读取数据以响应来自仪器的 SRQ(因为 GPIB 将用于在扫描之间与其他仪器通信)
到目前为止,我已经成功地处理了第一个 SRQ,但只是第一个。随后的扫描要么不引发 SRQ,要么没有正确处理 SRQ。代码太多,无法在此处完整发布,但关键部分是:
using NationalInstruments.VisaNS;
public class Datalogger
{
private string resourceName = ""; // The VISA resource name used to access the hardware
private ResourceManager resourceManager = null;
private GpibSession session = null; // The VISA session used to communicate with the hardware
public Datalogger(string resourceName)
{
resourceManager = ResourceManager.GetLocalManager();
session = (GpibSession)resourceManager.Open(resourceName);
session.ReaddressingEnabled = true;
// Check the device ID string
session.Write("*IDN?");
string reply = session.ReadString();
// Detail left out
// Add our SRQ event handler and enable events of type ServiceRequest
session.ServiceRequest += OnSRQ;
session.EnableEvent(MessageBasedSessionEventType.ServiceRequest, EventMechanism.Handler);
}
// Other methods & properties omitted
private void OnSRQ(Object sender, MessageBasedSessionEventArgs e)
// Handle an SRQ from the datalogger
{
if (e.EventType == MessageBasedSessionEventType.ServiceRequest)
{
session.Write("*STB?");
string temp = session.ReadString();
int result = Int32.Parse(temp);
if ((result & 128) == 128)
{
session.Write("STAT:OPER:EVEN?");
temp = session.ReadString();
result = Int32.Parse(temp);
if ((result & 512) == 512)
{
session.Write("DATA:POIN?");
string response = session.ReadString();
int count = Int32.Parse(response);
if (count != 0)
{
session.Write(string.Concat("DATA:REM? ", count.ToString()));
response = session.ReadString();
System.Console.WriteLine(response);
}
}
}
}
session.Write("*SRE 192"); // Try re-enabling SRQ
}
}
当我运行此代码时,数据记录器的第一次扫描会导致OnSRQ()
调用处理程序,但后续扫描不会。我可能无法正确编程数据记录器,但在程序运行时使用 NI-488.2 通信器应用程序,我可以看到 STB 寄存器中的 SRQ 位按预期设置。
有任何想法吗?