USBHost - device connected before power up

I have a USB RFID reader. It’s a Prolific chip based device that sends data via serial. When i plug it into Cobra an DeviceConnected event is fired and reader is recognized as Prolific. The problem is when the reader is already pluged in before i power up my Cobra. No DeviceConnected event is fired and GetDevices() method returns empty collection. If i unplug and plug reader back in, everything goes back to normal. How can i fix this ?


Debug.Print("Already connected: " + USBHostController.GetDevices().Length);
USBHostController.DeviceConnectedEvent += (s,e) => Debug.Print("Device connected!");

Steps to recreate this effect:

  1. Plug any USB device to your Cobra
  2. Plug Cobra to PC (power it up)
  3. Deploy this program

using System.Threading;
using GHIElectronics.NETMF.USBHost;
using Microsoft.SPOT;

namespace FEZ_Cobra_Console_Application
{
    public class Program
    {
        public static void Main()
        {
            Debug.Print("Already connected: " + USBHostController.GetDevices().Length);
            USBHostController.DeviceConnectedEvent += d => Debug.Print("Device connected!");

            while (true)
            {
                Thread.Sleep(100);
            }
        }
    }
}

I suppose i can manualy create the USBH_Device object if i know what should be connected to my board. But what if i don’t have that privilage?

Try the example from ebook, it should work fine.

using System;
using System.Threading;
using Microsoft.SPOT;

using GHIElectronics.NETMF.USBHost;

namespace Test
{
    class Program
    {
        public static void Main()
        {
            // Subscribe to USBH events.
            USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
            USBHostController.DeviceDisconnectedEvent += DeviceDisconnectedEvent;

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

        static void DeviceConnectedEvent(USBH_Device device)
        {
            Debug.Print("Device connected...");
            Debug.Print("ID: " + device.ID + ", Interface: " + device.INTERFACE_INDEX + ", Type: " + device.TYPE);
        }

        static void DeviceDisconnectedEvent(USBH_Device device)
        {
            Debug.Print("Device disconnected...");
            Debug.Print("ID: " + device.ID + ", Interface: " + device.INTERFACE_INDEX + ", Type: " + device.TYPE);
        }
    }
}

Also, if you try this it should work fine too.

while(true)
{
  Debug.Print("Already connected: " + USBHostController.GetDevices().Length);
}

The reason is, it doesn’t get you the count the first time you call it. It has to do some processing and initializing and then return the count…

Gralin,

Changing your code to Mike’s example should work just fine for you. I start up my Cobra w/ my USB HDD attached all the time and it’s picked up straight away.

Also I see your infinite sleep loop there; if you just want to sleep forever you can call:

Thread.Sleep(-1);

Or

Thread.Sleep(Timeout.Infinite);

But it’s the same as skewworks mentioned.

I never had any bootup problems either?

Subscribing to DeviceConnected event or pooling GetDevices().Length works in most cases. I started plugging in different USB devices (pen drive, mouse, keyboard, and my problematic RFID reader). I repeated the test described earlier a few times for each device. Only in case of my RFID device (recognized as Serial_FTDI not as Prolific as I previously mentioned) my device was not signaling its presence. The funny thing is - the behavior is random! Doing the same test over and over sometimes the reader IS detected and sometimes it’s not.

I don’t know if I can find any pattern for this behavior to occur…

As a side note

There is an excellent article that explains some of the USB internals. It is about reverse engineering USB devices and Kinect in particular. Very good read

http://ladyada.net/learn/diykinect/

Wonder if kinect would work with FEZ Cobra? :smiley:

do you know what is funny? It looks like it is programmed in C#!

Even “Windows Next” is planned to be programmed in C# for the most parts :wink:

Gus,

It is actually on my list to try it with ChipworkX. Planning on ordering one soon.

Architect, the first thing Joe said was, “let us run this on ChipworkX”

I’ve been looking at ChipworkX for some time now, comparing it to other similar solutions and I like it the most.
-Any planned promotions in near future for that product? :wink:

You are a FEZ senior :wink: Send an email to GHI and tell them you need one but can’t afford it, you never know :smiley:

Gus,

I have replied in this thread:

http://www.tinyclr.com/forum/18/1680/

Speaking of Kinect.

Here is a great implementation of the hack on a robot.

Holly crap! This is a very new device :o when did users get chance to build robots out of it?

The beauty of internet and open source. It spreads fast and you have all the pieces available for you just need to put it all together.

Kinect is an amazing device and there will be more custom build projects. One million units sold in 10 days.

I am excited. ;D

P.S. Smileys are broken