Share via


SerialDevice 類別

定義

表示序列埠。 物件提供方法與屬性,讓應用程式可用來尋找和與系統上的序列埠互動。

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
繼承
Object Platform::Object IInspectable SerialDevice
屬性
實作

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

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
    }
}

備註

需要序列裝置功能 serialCommunication ,才能使用 SerialDevice 類別。 如需詳細資訊,請參閱 Windows.Devices.SerialCommunication

建立 SerialDevice 物件

  1. 產生進階查詢語法 (AQS) 字串,其中包含搜尋準則,以尋找列舉裝置集合中的裝置。 如果您有廠商和產品識別碼,請呼叫 GetDeviceSelectorFromUsbVidPid
  2. 將擷取的字串傳遞至 FindAllAsync。 呼叫會擷取 DeviceInformationCollection 物件。
  3. 迴圈查看集合。 每個反復專案都會取得 DeviceInformation 物件。
  4. 取得 DeviceInformation.Id 屬性值。 字串值是裝置介面路徑。 (例如 \\?\usb#vid_03eb&pid_2157&mi_01#7&1435ec7f&0&0001#{86e0d1e0-8089-11d0-9ce4-08003e301f73}) 。
  5. 傳遞裝置介面字串並取得 SerialDevice 物件,以呼叫FromIdAsync。 如果這會擲回例外狀況或傳回 Null,則可能是:
    • 裝置介面路徑無效
    • 裝置介面路徑不會重新呈現序列裝置
    • 應用程式缺少 serialCommunication 功能
    • 無法存取序列裝置。 (請參閱 Windows.Devices.SerialCommunication)

讀取和寫入資料

然後,您可以使用 SerialDevice 物件,透過 Windows.Storage.Streams 命名空間讀取或寫入序列埠。

  1. 取得 SerialDevice.InputStream 屬性,以取得輸入資料流程的參考。
  2. DataReader 建構函式中指定輸入資料流程,以建立 DataReader 物件。
  3. 取得 SerialDevice.OutputStream 屬性,以取得輸出資料流程的參考。
  4. DataWriter 建構函式中指定輸出資料流程,以建立 DataWriter 物件。

屬性

BaudRate

取得或設定傳輸速率。

BreakSignalState

取得或設定中斷信號狀態。

BytesReceived

表示輸入資料流程的最後一個讀取作業所接收的位元組數目。

CarrierDetectState

取得「貨運公司偵測」 (CD) 行的狀態。

ClearToSendState

取得 Clear-to-Send (CTS) 行的狀態。

DataBits

傳輸或接收之每個字元值中的資料位數目,不包含同位位或停止位。

DataSetReadyState

取得 Data Set Ready (DSR) 信號的狀態。

Handshake

取得或設定流程式控制制的交握通訊協定。

InputStream

輸入資料流程,其中包含序列埠上收到的資料。

IsDataTerminalReadyEnabled

取得或設定值,這個值會啟用資料終端機就緒 (DTR) 訊號。

IsRequestToSendEnabled

取得或設定值,這個值可讓要求傳送 (RTS) 訊號。

OutputStream

取得輸出資料流程,應用程式可以寫入資料以透過序列埠傳輸。

Parity

取得或設定錯誤檢查的同位。

PortName

取得序列通訊的埠名稱。

ReadTimeout

取得或設定讀取作業的逾時值。

StopBits

取得或設定每個位元組的標準停止位數目。

UsbProductId

取得USB 裝置描述項idProduct欄位。 此值表示裝置特定的產品識別碼,並由製造商指派。

UsbVendorId

取得USB 裝置描述項idVendor欄位。 值表示裝置的廠商識別碼,由 USB 規格委員會指派。

WriteTimeout

取得或設定寫入作業的逾時值。

方法

Close()

釋放先前透過呼叫FromIdAsync取得之SerialDevice物件的參考。

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

FromIdAsync(String)

啟動建立 SerialDevice 物件的非同步作業。

GetDeviceSelector()

取得進階查詢語法 (AQS) 字串,應用程式可以傳遞至 DeviceInformation.FindAllAsync ,以尋找系統上的所有序列裝置。

GetDeviceSelector(String)

取得進階查詢語法 (AQS) 字串,應用程式可以傳遞至 DeviceInformation.FindAllAsync ,以指定其埠名稱來尋找序列裝置。

GetDeviceSelectorFromUsbVidPid(UInt16, UInt16)

取得進階查詢語法 (AQS) 字串,應用程式可以傳遞至 DeviceInformation.FindAllAsync ,藉由指定其 VID 和 PID 來尋找特定的串列到 USB 裝置。

事件

ErrorReceived

在序列埠發生錯誤時叫用的事件處理常式。

PinChanged

當訊號或行的狀態在序列埠上變更時所叫用的事件處理常式。

適用於

另請參閱