LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

eventhandler and event call back from .net dll doesn't work.

Hello,

 

I have a .net dll that was written in C#.  This .dll called nalusb.dll is also used in a usb tester program that is also written in C# and I am trying to do something similar in labview which will connect to the device, write data to the device and read the results.  In the usb tester program that was written in C#, nalusb.dll calls an event to read the data.  There is a public event in nalusb.dll that is called PacketReadCompleted and there is also a public delegate called PacketReadCompletedEventHandler.  I am able to connect to the device and write data to the device but after I register the callback event to PacketReadCompleted, the read event doesn't get handled after data is written to the device.  Do I need to do something with the PacketReadCompletedEventHandler?  This is a delegate which requires two inputs of type object and method but I have no idea what to wire into those........ I've included a snippit of the C# code of the usb tester.  I think I am stuck on the line I highlighted in red.  I've also included what I have so far in labview.  The section in purple is what I would like to do.   Any suggestions or help will be greatly appreciated!

 

public CentralForm()
{
this.InitializeComponent();

this.nalUsb = new NalUsb();
this.nalUsb.PacketReadCompleted += new PacketReadCompletedEventHandler(this.OnNalUsbPacketReadCompleted);
this.nalUsb.ConnectionLost += new EventHandler(this.OnNalUsbConnectionLost);

this.UpdateDevicePresentStatus(NalUsb.DevicePresent());
}

private void OnNalUsbPacketReadCompleted(object sender, PacketReadCompletedEventArgs e)

{
string eventDescription = "Packet Read - ";

if (e.Type == PacketType.Data)
{
eventDescription += e.Data.Length + " Bytes";
this.receivedTextBox.Text += Encoding.GetEncoding(1252).GetString(e.Data);
}
else if (e.Type == PacketType.FlowControlValues)
{
eventDescription += "Flow Control Values";
this.dsrCheckBox.Checked = e.Dsr;
this.ctsCheckBox.Checked = e.Cts;
this.dcdCheckBox.Checked = e.Dcd;
this.riCheckBox.Checked = e.Ring;
}

this.eventsTextBox.AppendText(eventDescription + "\r\n");
}

Download All
0 Kudos
Message 1 of 4
(3,588 Views)

I didn't try to get it to actually run but I'm going to go ahead and say I'm pretty sure this is where the problem is:

wrong.png

You're registering the event, and then immediately unregistering it, so unless the event fires in the microsecond between those two nodes it will never execute the callback VI.  You need to have a delay between those two steps until all of the events you want to deal with have occurred.  Or just don't unregister it at all until LV takes care of it when the main VI ends in its general memory cleanup.

0 Kudos
Message 2 of 4
(3,564 Views)

I tried what you suggested by removing the unregister and also adding a delay but the event is still not handled.  I also moved the writedata after registering the event thinking that the event needs to be registered first before you can trigger it and still no luck.  I am thinking that the write data isn't what calls the event.

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

I am starting to believe that the .net dll that I have which was compiled in C# doesn't work in LabVIEW.  I have tried on both LabVIEW 2011 and LabVIEW 2015 without any success.  The problem is in the Register Event Callback.  The event is never handled.  I tried it with a simple event handler in the .net dll that handles a lost connection on the usb device.  Below is a simple vi that should handle the event the moment I unplug the usb cable to my pc.  In the USB tester program that is written in C# that also uses the same dll, the event gets handled without any problems.  In LabVIEW 2011 and 2015, the moment I unplug the usb cable, LabVIEW crashes out.  Is there a possibility that this .net dll cannot work in LabVIEW?  I do not have access to the source code for this dll.  What are my options?  Or is this a LabVIEW problem?  Any suggestions or ideas is greatly appreciated!

 

Untitled.png

 

Here is the vi for the callback.vi for the Register Event Callback:

 

 

 

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