How to use USBClientController.State to detect device being unplugged

Hello,

I have a question about how to detect when a usb client device like a joystick is unplugged from the pc

I am looking at the state as the controller is running.

USBClientController.State usbState = USBClientController.GetState();

Debug.Print(usbState.ToString());

// Start Joystick
USBC_Joystick joystick = USBClientController.StandardDevices.StartJoystick();

usbState = USBClientController.GetState();

Debug.Print(usbState.ToString());

while (true)
{

            usbState = USBClientController.GetState();
            Debug.Print(usbState.ToString());  

             Thread.Sleep(500);
       }

THE OUTPUT LOOKS LIKE THIS

255 (before startJoyStick)
1 (after start joystick line)
6
6
6
… cable not yet plugged in

1 (plug in device to pc)
5
5
5
5
5 (unplug device from pc - NOTE the state remains “RUNNING”)
5
5
5

Now if you plug the cable back into the pc, there will be a message saying USB Device not recognized, because the joystick never found out it was unplugged)

Does anybody know how to detect unplugging the device and how to handle it with the usbclient methods?

thanks,
Peter

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

Hello,

I have a question about how to detect when a usb client device like a joystick is unplugged from the pc

I am looking at the state as the controller is running.


using System;
using System.Threading;

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

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.Hardware;


namespace USBClientcontrollerGetStateTest
{
    public class Program
    {
        public static void Main()
        {
            USBClientController.State clientState;

            // Check debug interface
            if (Configuration.DebugInterface.GetCurrent() ==
                Configuration.DebugInterface.Port.USB1)
                throw new InvalidOperationException("Current debug interface is USB. It must be changed to something else before proceeding.Refer to your platform user manual to change the debug interface.");

            clientState = USBClientController.GetState();

            Debug.Print(clientState.ToString());

            // Start keyboard
            USBC_Keyboard kb = USBClientController.StandardDevices.StartKeyboard();

            clientState = USBClientController.GetState();

            Debug.Print(clientState.ToString());

            while (true)
            {
                //keep monitoring usb client state
                clientState = USBClientController.GetState();

                Debug.Print(clientState.ToString());

                Thread.Sleep(1000);
            }
        }
    }
}

THE OUTPUT LOOKS LIKE THIS

255 (before startJoyStick)
1 (after start joystick line)
6
6
6
… cable not yet plugged in

1 (plug in device to pc)
5
5
5
5
5 (unplug device from pc - NOTE the state remains “RUNNING”)
5
5
5

Now if you plug the cable back into the pc, there will be a message saying USB Device not recognized, because the joystick never found out it was unplugged)

Does anybody know how to detect unplugging the device and how to handle it with the usbclient methods?

thanks,
Peter

You have to track following events:

USBHostController.DeviceConnectedEvent
USBHostController.DeviceDisconnectedEvent

Check following demo application:

http://www.tinyclr.com/codeshare/entry/499

Code inside GHIGraphicalDemo\Presentation\DesktopWindow.cs shows how to do that.

What GHI board are you using?

not sure how to use this.

bought the compass module ghi-ss-compass-gm-323 for a simple project before i knew of the ghi world…

there was no information for the 10 pin plug… where would i find what the pins are?

thanks,
Mark

@ mdwilliams - Welcome to the community!

This totally a different question unrelated to this thread. Please start a new thread under Gadgeteer section.

ok thx

Hello,

Thanks for the response.

I am not sure how to hook in to the DeviceConnectedEvent in my application which is a usb client running on a USBizi100 chipset.

Do you have an example of that?

thanks,
Peter

Please tell more about what you are trying to do.

Hello,

I have several applications where a USBizi is used as a USB client, either joystick, keyboard or mass storage.

The programs work fine and are stable, but when the client is unplugged from the host and then plugged back in, the host pc will not recognize the device.

I believe this is because the device does not detach and reattach itself from the USB bus.

Is there a way to make this work?

thanks,
Peter

I see. I thought you are using the host so scratch my previous suggestion.

The joystick has Disconnected event.

http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation/html/9e638214-ebe2-de41-e144-99885ea46298.htm

I would try to handle that event.

Hello,

thanks for your response.

The device I have is a special type of joystick that connects to a pc - so the USBizi is the client.

From what I see in the samples, the “Disconnected” event is only available in host mode.

Is there a similar way to detect this from the client side?

-Peter

Hello,

I guess it just dawned on me that my application is a bit unusual in another way.

The USBizi is not powered by the USB port, so when you disconnect the cable, it keeps running.

I know that the USB power is detected on P2.8 of the USBizi chip - should I read the pin through direct register access to detect if the cable is connected and stop
the usbclientdevice when I see that the power went off on the usb connector?

-Peter

Check this thread. Looks like there is an issue what end the cable is unplugged from:

http://www.tinyclr.com/forum/topic?id=540