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.
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”
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.
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?
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.
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?
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
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)
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());
}
}
}
}
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.
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?
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?
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.
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?