LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

C# VISA wait on RQS

Solved!
Go to solution

I'm trying to write a simple C# (.NET 4.0) program to control a Keithley 2400 SMU over VISA GPIB and I'm having trouble with getting the program to wait for the Service Request that the Keithley sends at the end of the sweep.

 

The sweep is a simple linear voltage sweep, controlled internally by the Keithley unit. I've got the unit set up to send a ServiceRequest signal at the end of the sweep or when compliance is reached.

 

I'm able to send the commands to the SMU and read the data buffer, but only if I manually enter a timeout between the sweep start command and the read data command.

 

One issue I'm having is that I'm quite new to C# - I'm using this project (porting parts of my LV code) to learn it. 

 

Here is what I have so far for my C# code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NationalInstruments.VisaNS;

private void OnServiceRequest(object sender, MessageBasedSessionEventArgs e)
{
Console.WriteLine("Service Request Received!");
}

// open the address Console.WriteLine("Sending Commands to Instrument"); instrAddr = "GPIB0::25::INSTR"; mySession = ResourceManager.GetLocalManager().Open(instrAddr);
// Cast to message-based session mbSession = (MessageBasedSession)mySession;
// Here's where things get iffy for me... Enabling the event and whatnot mbSession.ServiceRequest += new MessageBasedSessionEventHandler(OnServiceRequest); MessageBasedSessionEventType srq = MessageBasedSessionEventType.ServiceRequest; mbSession.EnableEvent(srq, EventMechanism.Handler); // Start the sweep (SMU was set up earlier) Console.WriteLine("Starting Sweep"); mbSession.Write(":OUTP ON;:INIT");

int timeout = 10000; // milliseconds // Thread.Sleep(10000); // using this line works fine, but it means the test always takes 10s even if compliance is hit early
// This raises error saying that the event is not enabled.
mbSession.WaitOnEvent(srq, timeout);
// Turn off the SMU.
Console.WriteLine("I hope the sweep is done, cause I'm tired of waiting"); mbSession.Write(":OUTP OFF;:TRAC:FEED:CONT NEV");
// Get the data
string data = mbSession.Query(":TRAC:DATA?"); // Close session mbSession.Dispose();

 

 

All the above is supposed to mimic this LabVIEW code:

simple Keithley Sweep.png

 

 

So, any ideas on where I'm going wrong?

 

Thanks,

0 Kudos
Message 1 of 4
(4,554 Views)

The Error I'm getting is:

-1073807313 Event not Enabled.

 

I believe that I enable the event with the

mbSession.EnableEvent(srq, EventMechanism.Handler);

line.

0 Kudos
Message 2 of 4
(4,498 Views)
Solution
Accepted by topic author dthor

It turns out that I need to enable the event as a Queue rather than a handler:

 

mbSession.EnableEvent(srq, EventMechanism.Handler);

 

Should actually be:

 

mbSession.EnableEvent(srq, EventMechanism.Queue);

 

Source: The documentation under "Remarks". It was a pain to find the docs on it... NI needs to make it easier :-(.

 

With this change, I also don't need to create the MessageBasedSessionEventHandler.

 

The final, working code (with fluff like namespaces and classes removed) looks like:

 

 

rm = ResourceManager.GetLocalManager().Open("GPIB0::25::INSTR");
MessageBasedSession mbSession = (MessageBasedSession)rm;
MessageBasedSessionEventType srq = MessageBasedSessionEventType.ServiceRequest;
mbSession.EnableEvent(srq, EventMechanism.Queue); // Note QUEUE, not HANDLER
int timeout = 10000;
// Start the sweep mbSession.Write(":OUTP ON;:INIT"); // This waits for the Service Request mbSession.WaitOnEvent(srq, timeout);
// After the Service Request, turn off the SMUs and get the data mbSession.Write(":OUTP OFF;:TRAC:FEED:CONT NEV"); string data = mbSession.Query(":TRAC:DATA?"); mbSession.Dispose();

I hope this helps future programmers.

0 Kudos
Message 3 of 4
(4,488 Views)

Your original post had the EnableEvent method.

That is documented at  http://zone.ni.com/reference/en-XX/help/370627F-01/mstudiowebhelp/html/58779685/

 

If you go to that page, and click on the second argument of the method which is EventMechanism

you would be directed to the page for EventMechanism Enumeration which is located at

http://zone.ni.com/reference/en-XX/help/370627F-01/mstudiowebhelp/html/e7327ded/

0 Kudos
Message 4 of 4
(4,480 Views)