StopBits 列舉

定義

指定在 SerialPort 物件上使用的停止位元數目。

public enum class StopBits
public enum StopBits
type StopBits = 
Public Enum StopBits
繼承
StopBits

欄位

None 0

未使用停止位元。 StopBits 屬性不支援這個值。

One 1

使用一個停止位元。

OnePointFive 3

使用 1.5 個停止位元。

Two 2

使用兩個停止位元。

範例

下列範例示範如何將 屬性設定 StopBitsOne

SerialPort^ mySerialPort = gcnew SerialPort("COM1");

mySerialPort->BaudRate = 9600;
mySerialPort->Parity = Parity::None;
mySerialPort->StopBits = StopBits::One;
mySerialPort->DataBits = 8;
mySerialPort->Handshake = Handshake::None;
mySerialPort->RtsEnable = true;
SerialPort mySerialPort = new SerialPort("COM1");

mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
Dim mySerialPort As New SerialPort("COM1")

mySerialPort.BaudRate = 9600
mySerialPort.Parity = Parity.None
mySerialPort.StopBits = StopBits.One
mySerialPort.DataBits = 8
mySerialPort.Handshake = Handshake.None
mySerialPort.RtsEnable = True

下列程式碼範例會顯示主控台列舉的 StopBits 可能值,然後提示使用者選擇一個。 此程式碼範例是提供給 類別之較大程式碼範例的 SerialPort 一部分。

static StopBits SetPortStopBits(StopBits defaultPortStopBits)
{
    String^ stopBits;

    Console::WriteLine("Available Stop Bits options:");
    for each (String^ s in Enum::GetNames(StopBits::typeid))
    {
        Console::WriteLine("   {0}", s);
    }

    Console::Write("Enter StopBits value (None is not supported and \n" +
        "raises an ArgumentOutOfRangeException. \n (Default: {0}):", defaultPortStopBits.ToString());
    stopBits = Console::ReadLine();

    if (stopBits == "")
    {
        stopBits = defaultPortStopBits.ToString();
    }

    return (StopBits)Enum::Parse(StopBits::typeid, stopBits);
}
public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
{
    string stopBits;

    Console.WriteLine("Available StopBits options:");
    foreach (string s in Enum.GetNames(typeof(StopBits)))
    {
        Console.WriteLine("   {0}", s);
    }

    Console.Write("Enter StopBits value (None is not supported and \n" +
     "raises an ArgumentOutOfRangeException. \n (Default: {0}):", defaultPortStopBits.ToString());
    stopBits = Console.ReadLine();

    if (stopBits == "" )
    {
        stopBits = defaultPortStopBits.ToString();
    }

    return (StopBits)Enum.Parse(typeof(StopBits), stopBits, true);
}
' Display StopBits values and prompt user to enter a value.

Public Shared Function SetPortStopBits(defaultPortStopBits As StopBits) As StopBits
    Dim stopBits As String

    Console.WriteLine("Available StopBits options:")
    For Each s As String In [Enum].GetNames(GetType(StopBits))
        Console.WriteLine("   {0}", s)
    Next

    Console.Write("Enter StopBits value (None is not supported and " &
                  vbLf & "raises an ArgumentOutOfRangeException. " &
                  vbLf & " (Default: {0}):", defaultPortStopBits.ToString())
    stopBits = Console.ReadLine()

    If stopBits = "" Then
        stopBits = defaultPortStopBits.ToString()
    End If

    Return CType([Enum].Parse(GetType(StopBits), stopBits, True), StopBits)
End Function

備註

在類別上 SerialPort 設定 屬性的值 StopBits 時,您可以使用這個列舉。 停止位會分隔非同步序列連接上每個資料單位。 當沒有資料可供傳輸時,它們也會持續傳送。

當您將 StopBits 屬性設定為 None 時,類別 SerialPortArgumentOutOfRangeException 擲回例外狀況。

適用於