locked
Best practice for where to start a DeviceWatcher

    Question

  • Hello,

    We are developing an application that uses a USB Composite Device.  I am using a DeviceWatcher to discover the device and on the DeviceAdded event, connect to the device.  I have this code in App.xaml.cs.  For the most part, this works consistently but every now and then, I will get "method called at an unexpected time" when I am trying to connect to the device.

    If I move the code to a UI page, it all works fine.

    My question is, where is the best place to start the DeviceWatcher and react the the DeviceAdded event?  The device is used throughout the app, not on one page which is why I thought App.xaml.cs was a good spot.

    Any feedback is much appreciated.


    Cheri

    Friday, March 7, 2014 5:43 PM

Answers

  • James,

    Yes, that does make sense.  I did as you suggested but still had the same problem.

    It turns out I had to give access to my app to use the USB Composite Device using PC Settings | Privacy | Other Devices.


    Cheri

    • Marked as answer by Cheri Reed Thursday, March 13, 2014 3:44 PM
    Thursday, March 13, 2014 3:44 PM

All replies

  • Hi Cheri,

    Where you put the code? Can you share your App.xaml.cs code with us?

    I thought you could put the code in Navigated

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    Monday, March 10, 2014 7:58 AM
    Moderator
  • Hi James,

    I start the watcher in OnLaunched:

                WinUSB.UsbDeviceList.Singleton.DeviceAdded += this.OnDeviceAdded;
                WinUSB.UsbDeviceList.Singleton.DeviceRemoved += this.OnDeviceRemoved;
                WinUSB.UsbDeviceList.Singleton.StartWatcher();

    And here is the Device added handler and its supporting code:

            private void OnDeviceAdded(object sender, WinUSB.UsbDeviceInfo info)
            {
    
                try
                {
                    // connect to dongle
                    ConnectDongle(info.Id);
                }
                catch (Exception ex)
                {
    
                    throw;
                }
    
    
            }
    
            async void ConnectDongle(string id)
            {
    
                // connect to dongle
                if (SCC.App.tcu == null)
                {
                    SCC.App.tcu = await UsbDevice.FromIdAsync(id);
                    ApplicationData.Current.LocalSettings.Values["tcuID"] = id;
    
                }
                if (SCC.App.tcu != null)
                {
                    SCC.App.port = await UsbSerialPort.CreateAsync(SCC.App.tcu);
                    await SCC.App.port.Open(38400, SDKTemplate.UsbCdcControl.Parity.None, 8, SDKTemplate.UsbCdcControl.StopBits.One);
    
    
                    // reset
                    Packet p = new Packet() { Command = Commands.REQ_MLME_RESET, Data = new byte[] { 0xf0 } };
                    IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(p.ToBytes());
                    await SCC.App.port.Write(buffer, 0, buffer.Length);
                    await Task.Delay(TimeSpan.FromSeconds(5));
    
    
                    // set
                    Packet d = new Packet() { Command = Commands.REQ_MLME_SET, Data = new byte[] { 0x41, 0x01 } };
                    buffer = WindowsRuntimeBufferExtensions.AsBuffer(d.ToBytes());
                    await port.Write(buffer, 0, buffer.Length);
    
    
                    // sometimes we'll get packets before these commands are completed.  Use this boolean to ignore any packets until the set command 
                    // has been issued.
                    SCC.App.SetIssued = true;
    
                }
            }

    I get the error on 

    SCC.App.tcu = await UsbDevice.FromIdAsync(id);


    Cheri

    Monday, March 10, 2014 1:35 PM
  • Thanks for the code, as I told, I don't think OnLaunched is a good place for your code.

    Let's say you have a event OnDeviceAdded fires when the DeviceAdded, but the app is not completed loaded. Plz give a try with following code:

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        ...
        ...
        //fire the event when the page loaded
        rootFrame.Navigated += rootFrame_Navigated;
        ...
        ...
    }
    
            void rootFrame_Navigated(object sender, NavigationEventArgs e)
            {
                WinUSB.UsbDeviceList.Singleton.DeviceAdded += this.OnDeviceAdded;
                WinUSB.UsbDeviceList.Singleton.DeviceRemoved += this.OnDeviceRemoved;
                WinUSB.UsbDeviceList.Singleton.StartWatcher();
            }
    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, March 13, 2014 5:33 AM
    Moderator
  • James,

    Yes, that does make sense.  I did as you suggested but still had the same problem.

    It turns out I had to give access to my app to use the USB Composite Device using PC Settings | Privacy | Other Devices.


    Cheri

    • Marked as answer by Cheri Reed Thursday, March 13, 2014 3:44 PM
    Thursday, March 13, 2014 3:44 PM