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 类的新实例。

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、属性默认为None枚举值、ParityStopBits属性默认值为 1 和 COM1 的默认端口名称。

适用于

SerialPort(IContainer)

使用指定的 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

容器的接口。

例外

未能找到或打开指定的端口。

注解

如果未指定任何值,此构造函数将使用默认属性值。 例如,属性DataBits默认为 8、属性默认为None枚举值、ParityStopBits属性默认值为 1 和 COM1 的默认端口名称。

适用于

SerialPort(String)

使用指定的端口名初始化 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 类的新实例。

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 类的新实例。

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 类的新实例。

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 类的新实例。

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

适用于