Missing UsbSerialDisconnected event in GHI.Usb.Host.Controller in 4.3

I am using the latest version 4.3 (R3) in a raptor board. I connected a USB printer to the raptor board. 4.2 used to have a USBHostController.DeviceDisconnectedEvent. But it is missing UsbSerialDisconnected event in GHI.Usb.Host.Controller in 4.3. The printer works but when I power it off or unplug the USB port I got an exception in Visual Studio. I cannot catch this exception:

Exception System.Exception - 0xffffffff (7)

Message:

GHI.Usb.Host.UsbSerial::NativeRead [IP: 0000]

GHI.Usb.Host.UsbSerial::CheckEvents [IP: 0017]

An unhandled exception of type ‘System.Exception’ occurred in GHI.Usb.dll

This is part of my code:

        // Subscribe to USBH event.
        Controller.UsbSerialConnected += Controller_UsbSerialConnected;

private UsbSerial serialUSB;
private void Controller_UsbSerialConnected(object sender, UsbSerial _usbSerial)
{
try
{
serialUSB = _usbSerial;
serialUSB.DataReceived += usbSerial_DataReceived;
serialUSB.Disconnected += usbSerial_Disconnected;
.
.

I don’t get serialUSB.Disconnected event when I power off the printer or unplug the USB port . If I don’t run it in Visual Studio, just start the raptor board normally. When I unplug the USB port from the raptor board, the raptor board would reboot. It would be nice if I can get a disconnected event instead of an exception which I cannot catch so that the raptor board won’t reboot itself. Any help would be appreciated.

@ nsmith - That issue should be resolved in the next SDK.

Was this supposed to be fixed with 2014 R5 or next release?

@ iamin - It should have been fixed in 2014 R5, and R4 for that matter.

I see. That means I have an issue. Here is what I do:

  1. Create an empty Gadgeteer project.
  2. Deploy it.
  3. Plug USB to UART converter based on FT232 (or any other serial device) to Cobra II.
  4. Unplug it.

And that causes many:

[quote]A first chance exception of type ‘System.Exception’ occurred in GHI.Usb.dll

Exception System.Exception - 0xffffffff (8)

Message:

GHI.Usb.Host.UsbSerial::NativeRead [IP: 0000]

GHI.Usb.Host.UsbSerial::CheckEvents [IP: 0019] ####[/quote]

I have tried the same but with mouse and webcam - no exceptions.

@ iamin - Just to be sure, you weren’t doing any reading or subscribing to events? A completely empty ProgramStarted?

@ John - Yes.

@ iamin - Which Mainboard, FEZ Cobra II Eco in R5?

@ John - Yes.

@ iamin - I was able to reproduce it, but it didn’t interrupt normal flow of the program. There’s a race condition between the internal workers that raise the events and the disconnect event. The workers fail because there is no device connected but the disconnect event has not yet arrived. The R4 SDK catches those exceptions internally, but you’re still notified of them like other caught exceptions in VS.

That said, the behavior is not ideal and it is something on our list to improve.

I am not sure, but I think when I have tried disconnecting it multiple times rapidly it cause my mainboard to hang. I could test this later.

I would prefer it to be consistent - if I don’t get any exceptions when, for example, mouse is disconnected, I would like to get no exceptions when serial device is disconnected, if possible.

@ iamin - You can actually get exceptions with the mouse as well. It’s just not predictable.

I have tried it multiple times but I couldn’t make it happen. Maybe I was not lucky :smiley:

Couldn’t reproduce it.

I don’t know the internals, but isn’t it possible to add a couple of milliseconds delay to solve the arising issues of racing?

@ iamin - It is possible, we will take a look to see what we can improve.