BrainChemist
BrainChemist

Reputation: 91

Connect to Arduino with UWP

I am trying to write to Arduino Uno device, but SerialDevice.FromIdAsync always returns null. Here is my code:

    DeviceInformationCollection devices = await DeviceInformation.FindAllAsync();

    foreach (DeviceInformation deviceInfo in devices)
    {
        if (deviceInfo.Name.IndexOf("Arduino") >= 0) {
            Debug.Log("Arduino found with id="+deviceInfo.Id);
            serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
            if (serialDevice == null) Debug.Log("it's null");
            break;
        }
    }

Running this code gives me "Arduino found with id=\\?USB#VID_2341&PID_0001#55...." and then "it's null"

In my manfiest I have the following inside Capabilities:

<DeviceCapability Name="serialcommunication">
  <Device Id="any">
    <Function Type="name:serialPort" />
  </Device>
</DeviceCapability>

Upvotes: 1

Views: 895

Answers (2)

Richard Chambers
Richard Chambers

Reputation: 17593

Microsoft has provided a couple of simple examples for communicating with an Arduino that is connected via a USB cable.

The first is a C# sample at https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/SerialArduino

The second contains both a C# and a C++ UWP sample at https://github.com/microsoft/Windows-iotcore-samples/tree/develop/Samples/SerialUART

I have used the C++ UWP sample from the SerialUART sample and it worked fine with Visual Studio 2017 and an Arduino Uno.

Here is the procedure I used.

I connected the Arduino with the USB cable to a Windows 10 laptop. I checked the Device Manager and I saw that when I inserted the USB Cable that was already plugged into the Arduino USB connecter, a USB Serial port, COM3 in my case, was created.

I opened up the Arduino IDE and cloned the sample Blinky LED project which blinks the built-in LED on the Arduino board. I just happened to have this sketch and it was easy to modify.

I added the SoftwareSerial.h include directive and used the Serial class to initialize a serial connection in setup() (Serial.begin(9600);) and then in loop() I created a global static unsigned long ulCounter; then did a print with (Serial.print("line "); Serial.print(ulCounter); Serial.print("\n"); ulCounter++;) so that I could see that the loop was working and the counter incrementing.

I compiled the sketch, downloaded it, and then opened the Serial Monitor tool in Arduino IDE to make sure that the print statements were working and the counter incrementing. Then I closed the Serial Monitor window in order to free the serial port for another application.

Next I cloned the SerialUART folder, navigated to the C++ folder, opened the solution file, and recompiled. I did have to change the target Windows version in the Properties.

I then ran the compiled application in the Debugger and it worked fine. It appeared that the line of text overwrote whatever was already in the text box which is why having the counter was helpful. The VID of the USB was displayed in the sample application window.

Lessons learned

When the Arduino is connected to the USB port with the USB cable, the USB Serial Port with an assigned COM number will be created under Windows 10.

Only one application can access the USB Serial Port at a time. The Serial Monitor and the compiled sample application can not be used on the same USB Serial Port at the same time.

The Serial Monitor of the Arduino IDE can be used to check that the output from the Arduino is being printed. Having an incrementing counter in the text makes it easier to know that the loop is working.

Upvotes: 0

Nico Zhu
Nico Zhu

Reputation: 32785

Connect to Arduino with UWP

For getting SerialDevice, we suggest use SerialDevice.GetDeviceSelector() to get deviceSelector and use the following to create SerialDevice watcher. And we have provided code sample that you could refer directly. I have tested, it works in my side that the app could list Arduino Uno device with USB\VID_2341&PID_0043\xxxxx device Id.

var deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector);

Upvotes: 2

Related Questions