10-10-2013 10:46 AM
I'm just starting to implement this, so I haven't tried everything out just yet. Someone else at my company developed a USB dll in C#. The C# is compiled (and exposed to COM if that matters). I have utlized the .NET controller which is pretty awesome by the way, and selected the functions/classes that I think I need.
I haven't tried everything yet, however, one question I know I am going to have is about setting up the event callback. I didn't find any information on this. As a packet is received over USB, it is meant to call an event callback. I have a polling method currently, but this is not ideal at all.
In my C# code, I have this defined inside my IDevice Class.
event Device.PacketReceivedDelegate OnPacketReceived;
In Device.cs
#region Public Events
/// <summary>
/// Occurs when a new packet has been received.
/// </summary>
public event PacketReceivedDelegate OnPacketReceived = null;
public delegate void PacketReceivedDelegate( Device aOwner, Packet aPacket );
#endregion
Within the .NET controller, it generated the following in my GUsb.c file.
int CVIFUNC GUsb_IDevice_add_OnPacketReceived( GUsb_IDevice __instance, GUsb_Device_PacketReceivedDelegate value, CDotNetHandle * __exception) { int __error = 0; char * __parameterTypeNames[1]; unsigned int __parameterTypes[1]; void * __parameters[1]; if (__exception) *__exception = 0; // Pre-process parameter: value __parameterTypeNames[0] = "GUsb.Device+PacketReceivedDelegate"; __parameterTypes[0] = (CDOTNET_OBJECT); __parameters[0] = &value; // Call interface member __errChk(CDotNetInvokeGenericInterfaceMember( __assemblyHandle, "GUsb.IDevice", 0, 0, __instance, CDOTNET_CALL_METHOD, "add_OnPacketReceived", 0, 0, 1, __parameterTypeNames, __parameterTypes, __parameters, 0, 0, __exception)); __Error: return __error; }
So my main question is how do i connect the dots to link a callback funtion to the dll function.
Thanks,
Nick
PS, I'm an EE with some embedded SW skill trying to write a test program. I know next to nothing about Windows, delegates, marshalling, USB, OS...
10-11-2013 05:57 PM
Hi Nick,
What protocol are you using to communicate with your USB device? If you are using VISA, you can use viEnableEvent to register an event when data comes in. This document discusses setting up callbacks for VISA events:
http://www.ni.com/support/visa/vevents.pdf
This one talks about using NI-VISA to communicate with a USB device:
http://www.ni.com/white-paper/4478/en
Even if you’re not using VISA, you may be able to set it up to register that event for a callback function.
Kelsey Johnson
Applications Engineer