Main Site Documentation

Using USB host fail


#1

Hi

I’m trying to use USB host with GPS device.

I use the sample code to detect the USB device but when trying to subscribe the USB event -
"USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;"
I get the following exception:
“An unhandled exception of type ‘System.NotSupportedException’ occurred in GHIElectronics.NETMF.USBHost.dll”

What is wrong with my code? I just copied the example in the beginners guide.

Thanks

Ami


#2

You need to know what serial chipset is used inside your GPS. Most of them use prolific but this is not a rule. Look at the documentation on USB serial devices.

You can tell what chipset your GPS uses by connecting it to windows, load the drivers and then go to properties of the device in “device manager”


#3

The exception happens when reaching to the line “USBHostController.DeviceConnectedEvent += DeviceConnectedEvent”.
In that stage it’s just subscribing the event and the GPS isn’t connected to any USB host yet, so I’m not sure this relate to the serial cheapset.


#4

What is the firmware version number you are using?

See this video http://www.youtube.com/watch?v=CEvLqhQrfE4

Are you using FEZ Domino or FEZ Mini? There is no host on FEZ Mini!! :confused:


#5

The firmware is 4.0.2.0 and I’m using FEZ domino


#6

Then it should just work. Use the examples from the book


#7

I run the exact code in the book under emulation and still get the exception.
Moreover, when I write the line “USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;” the Visual C# suggest to add after the “+=” this:
“new USBH_DeviceConnectionEventHandler(USBHostController_DeviceConnectedEvent);” which is quite different from the code example. (For example it use ‘new’).
Does this code suppose to run only on the board or on emulator too?


#8

I asked and you said FEZ domino :slight_smile: The exclusive features from GHI do not work on emulator


#9

Got it
Thanks


#10

I took your advice and loaded the code to the Domino FEZ.
Now I’m get the message “An unhandled exception of type ‘System.Exception’ occurred in GHIElectronics.NETMF.USBHost.dll” when trying to call to :
“serialUSB = new USBH_SerialUSB(device, 9600, System.IO.Ports.Parity.None, 8,
System.IO.Ports.StopBits.One);”

I using the exact code sample as it shows in the tutorial.

Thanks.


#11

Do you have a “serial USB” device connected?
Did you try other USB devices, like USB mouse or joystick?
Where do you see this in the tutorial? You mean the book? At what page number do you see the example so I can try it here?


#12

I connected two serial USB devices.
One is Holux GPS and the other is Fastrax GPS evulation board.
I use the example from the “Begginners Guide to C#” section 21.2 (Serial devices), page 108.
In both cases, the device was recognized as USBH_DeviceType.Serial_FTDI and when try to do the line “serialUSB = new USBH_SerialUSB(device, 9600, System.IO.Ports.Parity.None, 8,
System.IO.Ports.StopBits.One);” an exception is occur.
From debug window:
Device connected

Exception System.Exception - 0xffffffff (3)

GHIElectronics.NETMF.USBHost.USBH_SerialUSB::Init [IP: 0000]

GHIElectronics.NETMF.USBHost.USBH_SerialUSB::.ctor [IP: 0039]

GPSpeak.Program::DeviceConnectedEvent [IP: 0060]

GHIElectronics.NETMF.USBHost.USBH_DeviceConnectionEventHandler::Invoke [IP: 0000]

GHIElectronics.NETMF.USBHost.USBHostController::nativeEventDispatcher_OnInterrupt [IP: 0037]

GHIElectronics.NETMF.System.InternalEvent::nativeEventDispatcher_OnInterrupt [IP: 0054]

A first chance exception of type ‘System.Exception’ occurred in GHIElectronics.NETMF.USBHost.dll
An unhandled exception of type ‘System.Exception’ occurred in GHIElectronics.NETMF.USBHost.dll

I tried to connect a USB mouse and it works fine just like the example in the book.(pages 107-108)


#13

Shrink down your code as much as you can and then copy paste it here please.


#14
using System;
using System.Text;
using System.Threading;
using System.IO.Ports;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.System;
using GHIElectronics.NETMF.USBHost;

namespace MFConsoleApplication1
{ 
    public class Program
    {
        static USBH_SerialUSB serialUSB;
        static Thread serialUSBThread; // Prints data every second
           
        public static void Main()
        {
             USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
            
            // Sleep forever
            Thread.Sleep(Timeout.Infinite);
        }

        static void DeviceConnectedEvent(USBH_Device device)
        {
            Debug.Print("Device connected");
            
            switch (device.TYPE)
            {
                
                case USBH_DeviceType.Serial_FTDI: // FTDI connected                                     

                    serialUSB = new USBH_SerialUSB(device, 9600, System.IO.Ports.Parity.None, 8,
                        System.IO.Ports.StopBits.One);

                    serialUSB.Open();
                    serialUSBThread = new Thread(SerialUSBThread);
                    serialUSBThread.Start();
                    break;
                
                case USBH_DeviceType.Unknown: // SiLabs but not recognized
                    // force SiLabs
              
                    USBH_Device silabs = new USBH_Device(device.ID, device.INTERFACE_INDEX,
                        USBH_DeviceType.Serial_SiLabs, device.VENDOR_ID, device.PRODUCT_ID,
                        device.PORT_NUMBER);
              
                    serialUSB = new USBH_SerialUSB(silabs, 9600, System.IO.Ports.Parity.None, 8,
                        System.IO.Ports.StopBits.One);
                 
                    serialUSB.Open();                
                    serialUSBThread = new Thread(SerialUSBThread);
                    serialUSBThread.Start();
                    break;
            }
        }

        static void SerialUSBThread()
        {
            // Print "Hello World!" every second.
            byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello World!\r\n");
            while (true)
            {
                Thread.Sleep(1000);
                //serialUSB.Write(data, 0, data.Length);
                Debug.Print(data.ToString());
            }
        }
   }
}

#15

Modified your post to make the code more readable.

I am seeing code for silabs too so why is it there? Also, is the GPS really FTDI? When you connect it to the PC and you go to “device manager” then you view details about the driver of the GPS connected, do you see the driver provided by FTDI?

I have looked everywhere I could and I am not seeing any document stating the chip used is FTDI on the GPS devices you mentioned.


#16

I am playing with a Pharos 500 GPS receiver that I got with Microsoft Streets.

Included with the receiver is a converter which makes it look like a serial port.

When connected to FEZ, it connects as a FTDI device.

I have it working using the code below:

private static void OnDeviceConnected(USBH_Device device)
        {
            switch (device.TYPE)
            {
                case USBH_DeviceType.Serial_FTDI:

                    USBH_Device uDevice = new USBH_Device(
                            device.ID, device.INTERFACE_INDEX, USBH_DeviceType.Serial_Prolific,
                            device.VENDOR_ID, device.PRODUCT_ID, device.PORT_NUMBER);

                    serial = new USBH_SerialUSB(uDevice, 4800, Parity.None, 8, StopBits.One);
                 
                    readThread = new Thread(ReadThread);
                    readThread.Start();

                    break;
                default:
                    Debug.Print("Unknown device");
                    break;
            }
        }

#17

Maybe the problem for the other user was not having the right baud rate? This shouldn’t raise an exception but maybe the FTDI configuration locked other baudrates out?

ami, try Matt’s code please

Thanks Matt


#18

I tried Matt’s code and it didn’t work, but I looked at the drivers of Fastrax evulation bourd for UP300 GPS chip that I wrok with and I saw that it uses the following drivers:
silabenm.sys, silabser.sys, WdfCoinstaller01005.dll.
It looks like the driver are Silabs driver and somehow it is recognized by application as FTDI driver.
So, I took the code that handles Silabs device and move it to handle FTDI like that:

 case USBH_DeviceType.Serial_FTDI:
 
  USBH_Device silabs = new USBH_Device(device.ID, device.INTERFACE_INDEX,
                      USBH_DeviceType.Serial_SiLabs, device.VENDOR_ID, device.PRODUCT_ID,
                      device.PORT_NUMBER);

                    serialUSB = new USBH_SerialUSB(silabs, 9600, System.IO.Ports.Parity.None, 8,
                        System.IO.Ports.StopBits.One);

                    serialUSB.Open();
                    serialUSBThread = new Thread(SerialUSBThread);
                    serialUSBThread.Start();
                    break;

And that works, at least the exception disapear.
Is that make sence?


#19

We found the problem.
Some devices’ types are not recognized but in certain cases they are reported as FTDI.
So in your cases, it was SiLabs not FTDI…
This is fixed in next release.


#20

As displayed in the code I posted above, I experienced the same thing with a Prolific GPS chip. I assume the fix in the next release will apply to the Prolific case also?