Main Site Documentation

Panda II - DeviceConnectedEvent throwing System.InvalidOperationException


#1

I keep getting the System.InvalidOperationException error when trying to subscribe to USBHostController.DeviceConnectedEvent

I am using NETMF 4.1 and also have the 4.1.6.0 firmware loaded.

I’m using the following code:


using System.IO;
using System.Text;
using System.Threading;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.USBHost;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;


namespace USB_Host_Test
{
    public class Program
    {
        static SD_Read_Write SD;
        static string SD_Filename = @ "\SD\USB_HOST_TEST_LOG.txt";

        public static void Main()
        {
            SD = new SD_Read_Write();
            SD.Write(SD_Filename, "Starting...");

            // Subscribe to USB events
            try
            {
                SD.Write(SD_Filename, "Subscribing to DeviceConnectedEvent");
                USBHostController.DeviceConnectedEvent += new USBH_DeviceConnectionEventHandler(USBHostController_DeviceConnectedEvent);
            }
            catch (System.Exception ex)
            {
                SD.Write(SD_Filename, ex.Message);
            }
            try
            {
                SD.Write(SD_Filename, "Subscribing to DeviceDisconnectedEvent");
                USBHostController.DeviceDisconnectedEvent += new USBH_DeviceConnectionEventHandler(USBHostController_DeviceDisconnectedEvent);
            }
            catch (System.Exception ex)
            {
                SD.Write(SD_Filename, ex.Message);
            }

            SD.Write(SD_Filename, "events subscribed");

            // Sleep forever
            Thread.Sleep(Timeout.Infinite);

        }

I have made a USB host cable and with or without the cable I get the error. I don’t have a serial port for my Panda yet that’s why I log to the SD card.

Currently this is a bit of a show stopper for me :frowning:


#2

After you get the exception call USBHostController.GetLastError method and see what kind of error happened. That might get you more details.


#3

Thanks.

I tried that now but it only returns a 0…no text or anything else


#4

Of course, the first thing to do is get the latest firmware (4.1.7.0).


#5

Will do thanks…to be honest I didn’t see it was released yet.


#6

No change with the new firmware…still just returns 0


#7

Are you sure the error does not come from the SD.* calls ?

I ask that because you have such calls in the try{} block but also in the catch{} block :o

I would try to remove the calls to the SD card first and see if you’re still getting other things wrong.

Edit: for debugging purposes, why don’t you use Debug.Print() instead of writing to the SD card ?


#8

It’s not coming from the SD.Write calls as the writing to the SD card works 100%

Reason for that is that once running as a USB host I won’t be able to debug via USB but only via serial and I don’t yet have a serial port.

BUT - I have however found that this a false alarm - my apologies guys but thanks for the input :-[

It seems if you want to subscribe to the DeviceConnectedEvent you MUST enable the board for debugging via serial by connecting MOD to GND and by adding a pullup resistor to COM1 IN.

Now, I can subscribe to the event and if I connect a USB device it detects it. Now my next step is to see if the Bluetooth dongle I got will work or if I should get another one to try. That is if I understand correctly that not all USB dongles work with the Panda II