Handling a GPIB SRQ using the VisaNS & NI-488.2 libraries in C#

2.3k Views Asked by At

I am writing a C# application which talks to an Agilent 34970A datalogger over GPIB, using a National Instruments GPIB-USB-B interface, and the National Instruments VisaNS NI-488.2 libraries. I am successfully setting the datalogger to scan its inputs at fixed intervals, and I want to read the data in response to an SRQ from the instrument (as the GPIB will be in use for talking to other instruments between scans)

So far I have managed to handle the first SRQ correctly, but only the first. Subsequent scans either do not raise an SRQ, or the SRQ is not being handled properly. The code is too much to post here in full, but the key sections are:

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
    }
}

When I run this code, the first scan from the datalogger results in the OnSRQ() handler being called, but subsequent scans do not. I may be failing to program the datalogger correctly, but using the NI-488.2 communicator application while my program is running, I can see the SRQ bits in the STB register being set as expected.

Any ideas?

1

There are 1 best solutions below

0
On

I found the answer! The code fragment

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

should be replaced by

StatusByteFlags status = session.ReadStatusByte();

This returns the same result (converted to an integral type) but in addition seems to reset the callback-calling mechanism within the NI_VISA library.