英語で読む 編集

次の方法で共有


SerialPort Constructors

Definition

Initializes a new instance of the SerialPort class.

Overloads

SerialPort()

Initializes a new instance of the SerialPort class.

SerialPort(IContainer)

Initializes a new instance of the SerialPort class using the specified IContainer object.

SerialPort(String)

Initializes a new instance of the SerialPort class using the specified port name.

SerialPort(String, Int32)

Initializes a new instance of the SerialPort class using the specified port name and baud rate.

SerialPort(String, Int32, Parity)

Initializes a new instance of the SerialPort class using the specified port name, baud rate, and parity bit.

SerialPort(String, Int32, Parity, Int32)

Initializes a new instance of the SerialPort class using the specified port name, baud rate, parity bit, and data bits.

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

Initializes a new instance of the SerialPort class using the specified port name, baud rate, parity bit, data bits, and stop bit.

SerialPort()

Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs

Initializes a new instance of the SerialPort class.

public SerialPort ();

Examples

The following code example demonstrates the use of the SerialPort class to allow two users to chat from two separate computers connected by a null modem cable. In this example, the users are prompted for the port settings and a username before chatting. This code example is part of a larger code example provided for the SerialPort class.

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

Remarks

This constructor uses default property values when none are specified. For example, the DataBits property defaults to 8, the Parity property defaults to the None enumeration value, the StopBits property defaults to 1, and a default port name of COM1.

Applies to

.NET 9 (package-provided) およびその他のバージョン
製品 バージョン
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SerialPort(IContainer)

Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs

Initializes a new instance of the SerialPort class using the specified IContainer object.

public SerialPort (System.ComponentModel.IContainer container);

Parameters

container
IContainer

An interface to a container.

Exceptions

The specified port could not be found or opened.

Remarks

This constructor uses default property values when none are specified. For example, the DataBits property defaults to 8, the Parity property defaults to the None enumeration value, the StopBits property defaults to 1, and a default port name of COM1.

Applies to

.NET 9 (package-provided) およびその他のバージョン
製品 バージョン
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SerialPort(String)

Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs

Initializes a new instance of the SerialPort class using the specified port name.

public SerialPort (string portName);

Parameters

portName
String

The port to use (for example, COM1).

Exceptions

The specified port could not be found or opened.

Remarks

Use this constructor to create a new instance of the SerialPort class when you want to specify the port name.

Applies to

.NET 9 (package-provided) およびその他のバージョン
製品 バージョン
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SerialPort(String, Int32)

Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs

Initializes a new instance of the SerialPort class using the specified port name and baud rate.

public SerialPort (string portName, int baudRate);

Parameters

portName
String

The port to use (for example, COM1).

baudRate
Int32

The baud rate.

Exceptions

The specified port could not be found or opened.

Remarks

Use this constructor to create a new instance of the SerialPort class when you want to specify the port name and the baud rate.

Applies to

.NET 9 (package-provided) およびその他のバージョン
製品 バージョン
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SerialPort(String, Int32, Parity)

Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs

Initializes a new instance of the SerialPort class using the specified port name, baud rate, and parity bit.

public SerialPort (string portName, int baudRate, System.IO.Ports.Parity parity);

Parameters

portName
String

The port to use (for example, COM1).

baudRate
Int32

The baud rate.

parity
Parity

One of the Parity values.

Exceptions

The specified port could not be found or opened.

Remarks

Use this constructor to create a new instance of the SerialPort class when you want to specify the port name, the baud rate, and the parity bit.

Applies to

.NET 9 (package-provided) およびその他のバージョン
製品 バージョン
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SerialPort(String, Int32, Parity, Int32)

Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs

Initializes a new instance of the SerialPort class using the specified port name, baud rate, parity bit, and data bits.

public SerialPort (string portName, int baudRate, System.IO.Ports.Parity parity, int dataBits);

Parameters

portName
String

The port to use (for example, COM1).

baudRate
Int32

The baud rate.

parity
Parity

One of the Parity values.

dataBits
Int32

The data bits value.

Exceptions

The specified port could not be found or opened.

Remarks

Use this constructor to create a new instance of the SerialPort class when you want to specify the port name, the baud rate, the parity bit, and data bits.

Applies to

.NET 9 (package-provided) およびその他のバージョン
製品 バージョン
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

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

Source:
SerialPort.cs
Source:
SerialPort.cs
Source:
SerialPort.cs

Initializes a new instance of the SerialPort class using the specified port name, baud rate, parity bit, data bits, and stop bit.

public SerialPort (string portName, int baudRate, System.IO.Ports.Parity parity, int dataBits, System.IO.Ports.StopBits stopBits);

Parameters

portName
String

The port to use (for example, COM1).

baudRate
Int32

The baud rate.

parity
Parity

One of the Parity values.

dataBits
Int32

The data bits value.

stopBits
StopBits

One of the StopBits values.

Exceptions

The specified port could not be found or opened.

Remarks

Use this constructor to create a new instance of the SerialPort class when you want to specify the port name, the baud rate, the parity bit, data bits, and stop bit.

Applies to

.NET 9 (package-provided) およびその他のバージョン
製品 バージョン
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)