使用VisaNS&处理GPIB SRQ C#中的NI-488.2库

时间:2014-01-20 17:07:04

标签: c# gpib

我正在编写一个C#应用程序,它通过GPIB与Agilent 34970A数据记录器进行通信,使用National Instruments GPIB-USB-B接口和National Instruments 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位按预期设置。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我找到了答案!代码片段

session.Write("*STB?");
string temp = session.ReadString();

应替换为

StatusByteFlags status = session.ReadStatusByte();

返回相同的结果(转换为整数类型),但另外似乎重置了NI_VISA库中的回调调用机制。