Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Event handling Visa .Net 2.0

We use:
VisualStudio 2005 - language C#,
NI Visa 4.0,
.NET 2.0 Framework
 
We start a MessageBasedSession:

private MessageBasedSession mbSession;
mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open(sr.ResourceName);

We want to retreive all messages (simple strings), or in other words, we want to listen if messages arrives.
2 questions:

1.) What s the typical way to program this in C# with Visa 4.0, .Net 2.0,
     which methodes, delegations, events are normally used, are methodes used in threads...? 
2) We would prefer to create an event handler which starts every time a callback function,
    when a messages retrieves.

In the documuntation we found:
 stat = viOpen(dfltRM, "VXI0::16::INSTR", VI_NULL, VI_NULL, sesn)
 stat = viEnableEvent(sesn, VI_EVENT_VXI_SIGP, VI_QUEUE, VI_NULL)
 stat = viWaitOnEvent(sesn, VI_EVENT_VXI_SIGP, 5000, eType, eData)

When we use:
mbSession.EnableEvent(MessageBasedSessionEventType.AllEnabledEvents,EventMechanism.Handler);
we get the error:
Specified event type is not supported by the resource.  VISA error code -1073807322 (0xBFFF0026), ErrorInvalidEvent

 

0 Kudos
Message 1 of 10
(6,386 Views)
Hey AxelF,

I see that you are using the AllEnabledEvents function.  This function refers to all events that have been previously enabled on this session.  If I understand your post correctly you haven't enabled any events yet.  If that is the case then you can't use the AllEnabledEvents function.  Please take a look at viEnableEvent in the VISA help file located in Start>>Programs>>National Instruments>>VISA for more infromation.

Also, what bus are you using to communicate with your instrument?  Is it GPIB, Serial, etc...?

Regards,

Jason W
Applications Engineer
National Instruments
0 Kudos
Message 2 of 10
(6,353 Views)

Hey Jason W,
at the moment I have skiing holidays,
so sorry that I am not able to answer detailed
(no access to visa docu).

We are using TCP-socket connection,
and I think I tried to find infos in...
Next week I will proof it again.
Can you please post me an example how to enable an event.

Best regards

Axel
 

0 Kudos
Message 3 of 10
(6,337 Views)
Hey Axel,

Are you talking about how to enable events for any .net application? 

Regards,

Jason W.
Applications Engineer
National Instruments
0 Kudos
Message 4 of 10
(6,314 Views)
Hello Jason W,
 
we want to generate an event, when data receives.
In other words: we want to build a listener, which
calls a mehode, when data receives.
Of course this can be solved with a thread,
but is there a solution with events.
My questions:
Which eventyp do I need?
How do I enable this event?
Exists a standard solution for this problem?
Best regards
 
Axel F
0 Kudos
Message 5 of 10
(6,281 Views)
Hi AxelF,

It seems you're just wanting to create your own events in C# and you yourself handle when those events are created. I'm not sure there are any examples of this using VISA, but you should be able to program your own events based on information you get on the VISA driver. For more information on events in C#, check out the following links. Hope this helps!

http://www.csharphelp.com/archives/archive253.html
http://www.dotnetfun.com/articles/csharp/CreatingEventsCSharp.aspx
http://msdn2.microsoft.com/en-us/library/awbftdfh.aspx

Brandon Vasquez | Software Engineer | Integration Services | National Instruments
0 Kudos
Message 6 of 10
(6,264 Views)

Hi AxelF,

 

When you use the VISA .NET API, you will be working with Session objects and the Session-derived classes (GpibSession, VxiSession, TcpipSession, etc).  The Session-derived classes have .NET event members in which you can register a delegate with a .NET event to receive notification of when the event occurs.  Check out the examples in the <National Instruments>\MeasurementStudioVS2005\DotNET\Examples\Visa directory (I would start with VxiInterruptHandler since it appears that what your working with) to see how to set up the delegate and event handler.

 

So for example, if you had a vxiSession object already created, to register an event handler for VmeInterruptEvent and enable event, you would say


vxiSession.VxiVmeInterrupt += new VxiSessionVxiVmeInterruptEventHandler(OnVxiVmeInterrupt);
vxiSession.EnableEvent(
VxiSessionEventType.VxiVmeInterrupt, EventMechanism.Handler);

 
Continued......
Jonathan N.
National Instruments
0 Kudos
Message 7 of 10
(6,245 Views)

Continued....

 

I would check out the VISA .NET 2.0 API help (Start >> Programs >> National Instruments >> Visa >> Documentation >> NI-VISA .NET 2.0 Framework Help) then navigate to NI Measurement Studio Help >>  NI Measurement Studio .NET Class Library >> Using the Measurement Studio .NET Class Libraries >> Using the Measurement Studio VisaNS .NET Library. This should give you some to start with.

 

Several of the other examples in the Visa directory I mentioned earlier show off using events for other Session-derived classes.

 

One tip to remember (which is stated in the help for AllEnabledEvents) is that all currently enabled events for DisableEvent, DiscardEvent, and WaitOnEvent must be operated on. For EnableEvent, all previously enable events must be operated on.

 

Hope this helps!

 

Best Regards,

Jonathan N.
National Instruments
0 Kudos
Message 8 of 10
(6,243 Views)

Hello Jonathan N , Brandon V,

 

thank you for your answers.

I know and understand event handling and delegations in C#.

Also I read the documentation of NI-VISA

But I am not able to enable events in NI-VISA.

We use a TCP/IP based session,

we use C# and .NET2.0,

and we want to enable 2 events:

 

1.) when message received

2.) when serviceRequest received

 

What I have to do, please dont refer to NI documentation,

I have read it. Maybe you can send me some code lines, I think to enable

this events not more then 10 lines are neccessary.

 

Best regards AxelF

 

 

0 Kudos
Message 9 of 10
(6,231 Views)
Hey AxelF,

Please take a look at the ServiceRequest example (C:\Program Files\National Instruments\MeasurementStudioVS2005\DotNET\Examples\Visa\ServiceRequest).  This example is for a gpibsession but still shows how to establish an event handler for the servicerequest event that occurs. Please note the enableSRQButton_Click button event handler and the OnServiceRequest function.

For your second question, about when a message is received, I am not exactly clear as to what you mean by message.  There are several different events that can occur with different messages, so can you provide some clarification on this?

Best regards,

Jason W.
Applications Engineer
National Instruments
0 Kudos
Message 10 of 10
(6,200 Views)