如何等待服务请求(RQS) [英] How to Wait on Service Request (RQS)

查看:330
本文介绍了如何等待服务请求(RQS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**注:交叉张贴在LabVIEW论坛上: http://forums.ni.com/t5/LabVIEW/C-VISA-wait-on-RQS/td-p/3122939

**Note: Cross-posted at LabVIEW forums: http://forums.ni.com/t5/LabVIEW/C-VISA-wait-on-RQS/td-p/3122939

我正在尝试编写一个简单的C#(.NET 4.0)程序来通过VISA GPIB控制Keithley 2400 SMU,但我无法让程序等待Keithley最后发送的服务请求扫一扫.

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.

扫描是一种简单的线性电压扫描,由吉时利单元内部控制.我已经设置好单元,可以在扫描结束时或达到合规性时发送ServiceRequest信号.

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.

我能够将命令发送到SMU并读取数据缓冲区,但前提是必须在扫描开始命令和读取数据命令之间手动输入超时.

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.

我遇到的一个问题是,我对C#还是很陌生-我正在使用这个项目(移植我的LV代码的一部分)来学习它.

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.

这是到目前为止我的C#代码:

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!");
}

// The following code is in a class method, but
public double[,] RunSweep()
{
    // Create the session and message-based session
    MessageBasedSession mbSession = null;
    Session mySession = null;
    string responseString = null;

    // 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();

    // For now, create a dummy array, 3x3, to return. The array after is the starting value.
    double[,] dummyArray = new double[3, 3] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    return dummyArray;
}

以上所有内容均应模仿以下LabVIEW代码:

All the above is supposed to mimic this LabVIEW code:

那么,关于我要去哪里的任何想法?

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

谢谢

经过一番摆弄后,我发现实际上在正确的时间触发了服务请求功能OnServiceRequest(在控制台上打印了已收到服务请求!").

After a little more fiddling, I've found that the Service Request function OnServiceRequest is actually fired at the right time ("Service Request Received!" is printed to the console).

推荐答案

事实证明,我需要将事件作为 Queue 而不是处理程序来启用.这行:

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

mbSession.EnableEvent(srq, EventMechanism.Handler);

实际上应该是:

mbSession.EnableEvent(srq, EventMechanism.Queue);

来源:文档在备注"下.找到文档很痛苦... NI需要简化它:-(.

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

通过此更改,我也不需要创建MessageBasedSessionEventHandler.

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

最终的有效代码如下:

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();

这篇关于如何等待服务请求(RQS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆