Main Site Documentation

USBHost - device connected before power up


#1

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!");


#2

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?


#3

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…


#4

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);

#5

Or

Thread.Sleep(Timeout.Infinite);

But it’s the same as skewworks mentioned.

I never had any bootup problems either?


#6

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…


#7

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/


#8

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

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


#9

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


#10

Gus,

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


#11

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


#12

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:


#13

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:


#14

Gus,

I have replied in this thread:

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


#15

Speaking of Kinect.

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

http://singularityhub.com/2010/11/17/hacked-irobot-uses-xbox-kinect-to-see-the-world-obey-your-commands-video/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+SingularityHub+(Singularity+Hub)


#16

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


#17

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