我正在编写一个C#应用程序,它通过GPIB与安捷伦34970A数据记录器通信,使用美国国家仪器公司的GPIB-USB-B接口和美国国家仪器公司的VisaNS NI-488.2库。我正在成功地将数据记录器设置为以固定的时间间隔扫描其输入,并且我希望根据仪器的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位已按预期设置。
有什么想法吗?
发布于 2014-01-21 22:45:48
我找到答案了!代码片段
session.Write("*STB?");
string temp = session.ReadString();
应替换为
StatusByteFlags status = session.ReadStatusByte();
这将返回相同的结果(转换为整数类型),但似乎还重置了NI_VISA库中的回调调用机制。
https://stackoverflow.com/questions/21240011
复制相似问题