SerialPort 建構函式

定義

初始化 SerialPort 類別的新執行個體。

多載

SerialPort()

初始化 SerialPort 類別的新執行個體。

SerialPort(IContainer)

使用指定的 IContainer 物件,初始化 SerialPort 類別的新執行個體。

SerialPort(String)

使用指定的連接埠名稱,初始化 SerialPort 類別的新執行個體。

SerialPort(String, Int32)

使用指定的連接埠名稱和傳輸速率,初始化 SerialPort 類別的新執行個體。

SerialPort(String, Int32, Parity)

使用指定的連接埠名稱、傳輸速率和同位位元,初始化 SerialPort 類別的新執行個體。

SerialPort(String, Int32, Parity, Int32)

使用指定的連接埠名稱、傳輸速率、同位位元和資料位元,初始化 SerialPort 類別的新執行個體。

SerialPort(String, Int32, Parity, Int32, StopBits)

使用指定的連接埠名稱、傳輸速率、同位位元、資料位元和停止位元,初始化 SerialPort 類別的新執行個體。

SerialPort()

來源:
SerialPort.cs
來源:
SerialPort.cs
來源:
SerialPort.cs

初始化 SerialPort 類別的新執行個體。

public:
 SerialPort();
public SerialPort ();
Public Sub New ()

範例

下列程式碼範例示範 如何使用 SerialPort 類別,讓兩位使用者從以 Null 數據機纜線連線的兩部不同電腦聊天。 在此範例中,系統會在聊天之前提示使用者輸入埠設定和使用者名稱。 此程式碼範例是提供給 類別之較大程式碼範例的 SerialPort 一部分。

public:
    static void Main()
    {
        String^ name;
        String^ message;
        StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;
        Thread^ readThread = gcnew Thread(gcnew ThreadStart(PortChat::Read));

        // Create a new SerialPort object with default settings.
        _serialPort = gcnew SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort->PortName = SetPortName(_serialPort->PortName);
        _serialPort->BaudRate = SetPortBaudRate(_serialPort->BaudRate);
        _serialPort->Parity = SetPortParity(_serialPort->Parity);
        _serialPort->DataBits = SetPortDataBits(_serialPort->DataBits);
        _serialPort->StopBits = SetPortStopBits(_serialPort->StopBits);
        _serialPort->Handshake = SetPortHandshake(_serialPort->Handshake);

        // Set the read/write timeouts
        _serialPort->ReadTimeout = 500;
        _serialPort->WriteTimeout = 500;

        _serialPort->Open();
        _continue = true;
        readThread->Start();

        Console::Write("Name: ");
        name = Console::ReadLine();

        Console::WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console::ReadLine();

            if (stringComparer->Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort->WriteLine(
                    String::Format("<{0}>: {1}", name, message) );
            }
        }

        readThread->Join();
        _serialPort->Close();
    }

    static void Read()
    {
        while (_continue)
        {
            try
            {
                String^ message = _serialPort->ReadLine();
                Console::WriteLine(message);
            }
            catch (TimeoutException ^) { }
        }
    }
public static void Main()
{
    string name;
    string message;
    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
    Thread readThread = new Thread(Read);

    // Create a new SerialPort object with default settings.
    _serialPort = new SerialPort();

    // Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName);
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
    _serialPort.Parity = SetPortParity(_serialPort.Parity);
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

    // Set the read/write timeouts
    _serialPort.ReadTimeout = 500;
    _serialPort.WriteTimeout = 500;

    _serialPort.Open();
    _continue = true;
    readThread.Start();

    Console.Write("Name: ");
    name = Console.ReadLine();

    Console.WriteLine("Type QUIT to exit");

    while (_continue)
    {
        message = Console.ReadLine();

        if (stringComparer.Equals("quit", message))
        {
            _continue = false;
        }
        else
        {
            _serialPort.WriteLine(
                String.Format("<{0}>: {1}", name, message));
        }
    }

    readThread.Join();
    _serialPort.Close();
}

public static void Read()
{
    while (_continue)
    {
        try
        {
            string message = _serialPort.ReadLine();
            Console.WriteLine(message);
        }
        catch (TimeoutException) { }
    }
}
Public Shared Sub Main()
    Dim name As String
    Dim message As String
    Dim stringComparer__1 As StringComparer = StringComparer.OrdinalIgnoreCase
    Dim readThread As New Thread(AddressOf Read)

    ' Create a new SerialPort object with default settings.
    _serialPort = New SerialPort()

    ' Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName)
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate)
    _serialPort.Parity = SetPortParity(_serialPort.Parity)
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits)
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits)
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake)

    ' Set the read/write timeouts
    _serialPort.ReadTimeout = 500
    _serialPort.WriteTimeout = 500

    _serialPort.Open()
    _continue = True
    readThread.Start()

    Console.Write("Name: ")
    name = Console.ReadLine()

    Console.WriteLine("Type QUIT to exit")

    While _continue
        message = Console.ReadLine()

        If stringComparer__1.Equals("quit", message) Then
            _continue = False
        Else
            _serialPort.WriteLine([String].Format("<{0}>: {1}", name, message))
        End If
    End While

    readThread.Join()
    _serialPort.Close()
End Sub

Public Shared Sub Read()
    While _continue
        Try
            Dim message As String = _serialPort.ReadLine()
            Console.WriteLine(message)
        Catch generatedExceptionName As TimeoutException
        End Try
    End While
End Sub

備註

當未指定任何值時,這個建構函式會使用預設屬性值。 例如, DataBits 屬性預設為 8、 Parity 屬性預設 None 為列舉值、 StopBits 屬性預設為 1,以及 COM1 的預設埠名稱。

適用於

SerialPort(IContainer)

來源:
SerialPort.cs
來源:
SerialPort.cs
來源:
SerialPort.cs

使用指定的 IContainer 物件,初始化 SerialPort 類別的新執行個體。

public:
 SerialPort(System::ComponentModel::IContainer ^ container);
public SerialPort (System.ComponentModel.IContainer container);
new System.IO.Ports.SerialPort : System.ComponentModel.IContainer -> System.IO.Ports.SerialPort
Public Sub New (container As IContainer)

參數

container
IContainer

容器 (Container) 的介面。

例外狀況

找不到或無法開啟指定的連接埠。

備註

當未指定任何值時,這個建構函式會使用預設屬性值。 例如, DataBits 屬性預設為 8、 Parity 屬性預設 None 為列舉值、 StopBits 屬性預設為 1,以及 COM1 的預設埠名稱。

適用於

SerialPort(String)

來源:
SerialPort.cs
來源:
SerialPort.cs
來源:
SerialPort.cs

使用指定的連接埠名稱,初始化 SerialPort 類別的新執行個體。

public:
 SerialPort(System::String ^ portName);
public SerialPort (string portName);
new System.IO.Ports.SerialPort : string -> System.IO.Ports.SerialPort
Public Sub New (portName As String)

參數

portName
String

要使用的連接埠 (例如 COM1)。

例外狀況

找不到或無法開啟指定的連接埠。

備註

當您想要指定埠名稱時, SerialPort 請使用這個建構函式來建立 類別的新實例。

適用於

SerialPort(String, Int32)

來源:
SerialPort.cs
來源:
SerialPort.cs
來源:
SerialPort.cs

使用指定的連接埠名稱和傳輸速率,初始化 SerialPort 類別的新執行個體。

public:
 SerialPort(System::String ^ portName, int baudRate);
public SerialPort (string portName, int baudRate);
new System.IO.Ports.SerialPort : string * int -> System.IO.Ports.SerialPort
Public Sub New (portName As String, baudRate As Integer)

參數

portName
String

要使用的連接埠 (例如 COM1)。

baudRate
Int32

傳輸速率。

例外狀況

找不到或無法開啟指定的連接埠。

備註

當您想要指定埠名稱和傳輸速率時, SerialPort 請使用這個建構函式來建立 類別的新實例。

適用於

SerialPort(String, Int32, Parity)

來源:
SerialPort.cs
來源:
SerialPort.cs
來源:
SerialPort.cs

使用指定的連接埠名稱、傳輸速率和同位位元,初始化 SerialPort 類別的新執行個體。

public:
 SerialPort(System::String ^ portName, int baudRate, System::IO::Ports::Parity parity);
public SerialPort (string portName, int baudRate, System.IO.Ports.Parity parity);
new System.IO.Ports.SerialPort : string * int * System.IO.Ports.Parity -> System.IO.Ports.SerialPort
Public Sub New (portName As String, baudRate As Integer, parity As Parity)

參數

portName
String

要使用的連接埠 (例如 COM1)。

baudRate
Int32

傳輸速率。

parity
Parity

其中一個 Parity 值。

例外狀況

找不到或無法開啟指定的連接埠。

備註

當您想要指定埠名稱、傳輸速率和同位位時,請使用這個建構函式來建立 類別的新實例 SerialPort

適用於

SerialPort(String, Int32, Parity, Int32)

來源:
SerialPort.cs
來源:
SerialPort.cs
來源:
SerialPort.cs

使用指定的連接埠名稱、傳輸速率、同位位元和資料位元,初始化 SerialPort 類別的新執行個體。

public:
 SerialPort(System::String ^ portName, int baudRate, System::IO::Ports::Parity parity, int dataBits);
public SerialPort (string portName, int baudRate, System.IO.Ports.Parity parity, int dataBits);
new System.IO.Ports.SerialPort : string * int * System.IO.Ports.Parity * int -> System.IO.Ports.SerialPort
Public Sub New (portName As String, baudRate As Integer, parity As Parity, dataBits As Integer)

參數

portName
String

要使用的連接埠 (例如 COM1)。

baudRate
Int32

傳輸速率。

parity
Parity

其中一個 Parity 值。

dataBits
Int32

資料位元值。

例外狀況

找不到或無法開啟指定的連接埠。

備註

當您想要指定埠名稱、傳輸速率、同位位和資料位時,請使用這個建構函式來建立 類別的新實例 SerialPort

適用於

SerialPort(String, Int32, Parity, Int32, StopBits)

來源:
SerialPort.cs
來源:
SerialPort.cs
來源:
SerialPort.cs

使用指定的連接埠名稱、傳輸速率、同位位元、資料位元和停止位元,初始化 SerialPort 類別的新執行個體。

public:
 SerialPort(System::String ^ portName, int baudRate, System::IO::Ports::Parity parity, int dataBits, System::IO::Ports::StopBits stopBits);
public SerialPort (string portName, int baudRate, System.IO.Ports.Parity parity, int dataBits, System.IO.Ports.StopBits stopBits);
new System.IO.Ports.SerialPort : string * int * System.IO.Ports.Parity * int * System.IO.Ports.StopBits -> System.IO.Ports.SerialPort
Public Sub New (portName As String, baudRate As Integer, parity As Parity, dataBits As Integer, stopBits As StopBits)

參數

portName
String

要使用的連接埠 (例如 COM1)。

baudRate
Int32

傳輸速率。

parity
Parity

其中一個 Parity 值。

dataBits
Int32

資料位元值。

stopBits
StopBits

其中一個 StopBits 值。

例外狀況

找不到或無法開啟指定的連接埠。

備註

當您想要指定埠名稱、傳輸速率、同位位、資料位和停止位時,請使用這個建構函式來建立 類別的新實例 SerialPort

適用於