Share via


SerialDevice Class

Definition

Represents a serial port. The object provides methods and properties that an app can use to find and interact with serials ports on the system.

public ref class SerialDevice sealed : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class SerialDevice final : IClosable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class SerialDevice : System.IDisposable
Public NotInheritable Class SerialDevice
Implements IDisposable
Inheritance
Object Platform::Object IInspectable SerialDevice
Attributes
Implements

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

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

...

DeviceInformationCollection serialDeviceInfos = await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelector());

foreach (DeviceInformation serialDeviceInfo in serialDeviceInfos)
{
    try
    {
        SerialDevice serialDevice = await SerialDevice.FromIdAsync(serialDeviceInfo.Id);

        if (serialDevice != null)
        {
            // Found a valid serial device.

            // Reading a byte from the serial device.
            DataReader dr = new DataReader(serialDevice.InputStream);
            int readByte = dr.ReadByte();

            // Writing a byte to the serial device.
            DataWriter dw = new DataWriter(serialDevice.OutputStream);
            dw.WriteByte(0x42);
        }
    }
    catch (Exception)
    {
        // Couldn't instantiate the device
    }
}

Remarks

The serial device capability, serialCommunication, is required to use the SerialDevice class. For more information, see Windows.Devices.SerialCommunication.

Create a SerialDevice object

  1. Generate an Advanced Query Syntax (AQS) string that contains search criteria for finding the device in the enumerated device collection. If you have the vendor and product identifiers, call GetDeviceSelectorFromUsbVidPid.
  2. Pass the retrieved string to FindAllAsync. The call retrieves a DeviceInformationCollection object.
  3. Loop through the collection. Each iteration gets a DeviceInformation object.
  4. Get the DeviceInformation.Id property value. The string value is the device interface path. (e.g \\?\usb#vid_03eb&pid_2157&mi_01#7&1435ec7f&0&0001#{86e0d1e0-8089-11d0-9ce4-08003e301f73}).
  5. Call FromIdAsync by passing the device interface string and get the SerialDevice object. If this throws an exception or returns null, it is likely:
    • The device interface path is invalid
    • The device interface path does not respresent a serial device
    • Application is missing the serialCommunication capability
    • The serial device is inaccessible. (see Windows.Devices.SerialCommunication)

Read and write data

You can then use the SerialDevice object to read from or write to the serial port by using the Windows.Storage.Streams namespace.

  1. Obtain a reference to the input stream by getting the SerialDevice.InputStream property.
  2. Create a DataReader object by specifying the input stream in the DataReader constructor.
  3. Obtain a reference to the output stream by getting the SerialDevice.OutputStream property.
  4. Create a DataWriter object by specifying the output stream in the DataWriter constructor.

Properties

BaudRate

Gets or sets the baud rate.

BreakSignalState

Gets or sets the break signal state.

BytesReceived

Represents the number of bytes received by the last read operation of the input stream.

CarrierDetectState

Gets the state of the Carrier Detect (CD) line.

ClearToSendState

Gets the state of the Clear-to-Send (CTS) line.

DataBits

The number of data bits in each character value that is transmitted or received, and does not include parity bits or stop bits.

DataSetReadyState

Gets the state of the Data Set Ready (DSR) signal.

Handshake

Gets or sets the handshaking protocol for flow control.

InputStream

Input stream that contains the data received on the serial port.

IsDataTerminalReadyEnabled

Gets or sets a value that enables the Data Terminal Ready (DTR) signal.

IsRequestToSendEnabled

Gets or sets a value that enables the Request to Send (RTS) signal.

OutputStream

Gets an output stream to which the app can write data to transmit through the serial port.

Parity

Gets or sets the parity bit for error-checking.

PortName

Gets the port name for serial communications.

ReadTimeout

Gets or sets the time-out value for a read operation.

StopBits

Gets or sets the standard number of stop bits per byte.

UsbProductId

Gets the idProduct field of the USB device descriptor. This value indicates the device-specific product identifier and is assigned by the manufacturer.

UsbVendorId

Gets the idVendor field of the USB device descriptor. The value indicates the vendor identifier for the device as assigned by the USB specification committee.

WriteTimeout

Gets or sets the time-out value for a write operation.

Methods

Close()

Releases the reference to the SerialDevice object that was previously obtained by calling FromIdAsync.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

FromIdAsync(String)

Starts an asynchronous operation that creates a SerialDevice object.

GetDeviceSelector()

Gets an Advanced Query Syntax (AQS) string that the app can pass to DeviceInformation.FindAllAsync in order to find all serial devices on the system.

GetDeviceSelector(String)

Gets an Advanced Query Syntax (AQS) string that the app can pass to DeviceInformation.FindAllAsync in order to find a serial device by specifying its port name.

GetDeviceSelectorFromUsbVidPid(UInt16, UInt16)

Gets an Advanced Query Syntax (AQS) string that the app can pass to DeviceInformation.FindAllAsync in order to find a specific Serial-to-USB device by specifying its VID and PID.

Events

ErrorReceived

Event handler that is invoked when error occurs on the serial port.

PinChanged

Event handler that is invoked when the state of a signal or line changes on the serial port.

Applies to

See also