The Programmator

Autodiscover an Arduino from a Windows IoT application

In a previous post, I elaborated on how you can connect your Arduino to your development machine and write a simple Windows Universal application to control a relay. Now, because we don’t want to check our device manager for the correct USB address every time we want to develop with our Arduino, it’s time we try and autodiscover it.

Arduino Uno

How can I auto-detect my Arduino?

There are a few approaches I found on the internet while looking for a solution to autodiscover the Arduino. One was to write a handshaking mechanism which then needed to be deployed on the Arduino via the Arduino IDE. Others used the Windows.Devices.Usb namespace to discover devices in a more generic way. I’ll stick with the Windows.Remote libary and use its ready available UsbSerial.ListAvailableDevicesAsync() method.

The Code

The source code for this post is available on GitHub. It should be pretty straightforward, but I’ll highlight some snippets here.

Discovering

The function below is what “discovers” the Arduino. I don’t know if it’s perfect and works in all cases, but I’ve run some tests with this, and it does work for me 100% of the time. If it can be optimised, please feel free to use the comment section below to share your knowledge.

First, I manually went through the list of usbDevices to see which information could separate the Arduino from other peripherals. I found that there was a value “USB-SERIAL” in one of the DeviceInformation.Properties collection items which didn’t appear on other devices, and ta-daa! Our Arduino was connected! 🙂

Discover Arduino in windows 10 app

Multi-threading gotcha

Because we’re running the connect code in a separate thread (async), we need a dispatcher to update the UI on the main thread.

Update UI from other thread in WPF

This is how the UiDispatcher is initialised:

Use Dispatcher to update UI

Wrap up

So, this is pretty much it. Once a possible Arduino candidate is found, we try to connect to it.

Once the device is ready, we can switch the relay by using the buttons in the application.

Arduino Windows IOT auto discover

Enjoy playing with this example and leave your thoughts in the comment section below! – GitHub Source code

Like what you read? Maybe your friends do too! Share this article:

Leave a Reply

Tags: , ,