Devices. Serial Communication Namespace
The Windows.Devices.SerialCommunication namespace defines Windows Runtime classes that a UWP app can use to communicate with a device that exposes a serial port or some abstraction of a serial port. 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.
The namespace also supports devices that belong to the USB CDC device class. This includes ports exposed by Serial-to-USB adapters and internal USB to serial bridge chips like those used in Arduino Uno R3s. See note below on compatible IDs.
System-internal or on-chassis serial ports may be enumerated by DeviceInformation.FindAllAsync(), but cannot be opened by SerialDevice.FromIdAsync() because they currently are not supported. However, serial ports connected over USB, such as on USB-to-Serial cables are supported.
It is possible that the DeviceInformation collection returned by DeviceInformation.FindAllAsync() may have a serial device whose DeviceInformation.Name property is set to the machine name. This is by design and may occur when enumerating an on-board serial port. Apps should handle this case by either:
- Filtering such serial ports from the user's view, so that the user will not be able to interact with such an unsupported serial port at all.
- If the app decides to let the user interact with such a serial port, then note that when the app calls the DeviceAccessInformation.CreateFromId() function, an exception with the message: "The system cannot find the file specified. (Exception from HRESULT: 0x80070002)" will be thrown. The app should handle such an exception and let the user know that the port is unsupported.
Serial devices that use the following compatible IDs can be supported by the in-box Windows 10 usbser.sys driver; these are automatically supported by Windows.Devices.SerialCommunication. In addition, USB serial devices that use other compatible IDs are supported through a vendor-supplied driver.
Serial device capability usage
Your UWP app must include certain device capabilities in its App package manifest. The capabilities identify the device and its purpose. Here are the required elements in hierarchical order:
<DeviceCapability>: The Name attribute must be "serialcommunication".
<Device>: The Id attribute must specify the device identifier. If you are using a Serial-to-USB adapter, Id must specify vendor/product identifiers or can be "any" to allow access to any device that matches the function type.
- <Function>: The Type attribute specifies the device function. For serial devices this must be "serialPort".
<DeviceCapability Name="serialcommunication"> <Device Id="vidpid:xxxx xxxx"> <Function Type="name:serialPort"/> </Device> </DeviceCapability>
Serial device capability example
Here is an example for defining serial device capabilities. It allows the app to access the serial port through a Serial-to-USB adapter.
<DeviceCapability Name="serialcommunication"> <Device Id="vidpid:045E 0610"> <Function Type="name:serialPort"/> </Device> </DeviceCapability>
Represents the object that is passed as a parameter to the event handler that is invoked when error occurs on the serial port.
Represents the object that is passed as a parameter to the event handler that is invoked when the state of a signal line changes on the serial port.
Represents a serial port. The object provides methods and properties that an app can use to find the port (in the system).
Defines values for error conditions that can occur on the serial port.
Defines values for types of signal state changes on the serial port.