Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write serial data to COM ports with Universal Windows Application?

Typically C# applications use System.IO.Ports like so:

SerialPort port = new SerialPort("COM1"); 
port.Open(); 
port.WriteLine("test");`

But Universal Windows Applications don't support System.IO.Ports so this method cannot be used. Does anyone know how to write serial data through COM ports in a UWA?

like image 741
Charles Clayton Avatar asked Apr 03 '16 02:04

Charles Clayton


People also ask

How do I add a serial COM port?

To configure the Serial Port for your device, on your computer go to Control Panel - Device Manager, select “High-Speed USB Serial Port (Com X)”, right click and select Properties. Click the Features tab. This tab is used to change the COM port number and configure the port.

Is a COM port a serial data communication interface?

COM (communication port) is the original, yet still common, name of the serial port interface on PC-compatible computers. It can refer not only to physical ports, but also to emulated ports, such as ports created by Bluetooth or USB adapters.

How do I connect to COM port?

Open a console sessionUsing PuTTY or other terminal emulator, select "Serial" as the connection type and change the "Serial line" to match the COM port noted earlier. The serial console speed is typically 9600. Click "Open" to connect to the console.


2 Answers

You can do this with the Windows.Devices.SerialCommunication and Windows.Storage.Streams.DataWriter classes:

The classes provide functionality to discover such serial device, read and write data, and control serial-specific properties for flow control, such as setting baud rate, signal states.

By adding the following capability to Package.appxmanifest:

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

Then running the following code:

using Windows.Devices.SerialCommunication;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;

//...   

string selector = SerialDevice.GetDeviceSelector("COM3"); 
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
if(devices.Count > 0)
{
    DeviceInformation deviceInfo = devices[0];
    SerialDevice serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
    serialDevice.BaudRate = 9600;
    serialDevice.DataBits = 8;
    serialDevice.StopBits = SerialStopBitCount.Two;
    serialDevice.Parity = SerialParity.None;

    DataWriter dataWriter = new DataWriter(serialDevice.OutputStream);
    dataWriter.WriteString("your message here");
    await dataWriter.StoreAsync();
    dataWriter.DetachStream();
    dataWriter = null;
}
else
{
    MessageDialog popup = new MessageDialog("Sorry, no device found.");
    await popup.ShowAsync();
}
like image 119
Charles Clayton Avatar answered Oct 17 '22 18:10

Charles Clayton


Microsoft has provided an example of accessing and using a com port, using the class SerialDevice, in a Universal Windows Application, named CustomSerialDeviceAccess.

Microsoft has posted it on GitHub. You can find it here:

https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CustomSerialDeviceAccess

Microsoft says this about the sample application:

"This sample allows the user to configure and communicate with a Serial device. You can choose one of four scenarios:

Connect/Disconnect using Device Selection list; Configure the Serial device; Communicate with the Serial device; Register for Events on the Serial device"

References: Microsoft, GitHub

like image 45
Mike Jablonski Avatar answered Oct 17 '22 17:10

Mike Jablonski