SerialPort Constructors
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Initializes a new instance of the SerialPort class.
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. |
- Source:
- SerialPort.cs
- Source:
- SerialPort.cs
- Source:
- SerialPort.cs
Initializes a new instance of the SerialPort class.
public:
SerialPort();
public SerialPort ();
Public Sub New ()
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 = 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
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) |
- 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);
public SerialPort (System.ComponentModel.IContainer container);
new System.IO.Ports.SerialPort : System.ComponentModel.IContainer -> System.IO.Ports.SerialPort
Public Sub New (container As IContainer)
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) |
- Source:
- SerialPort.cs
- Source:
- SerialPort.cs
- Source:
- SerialPort.cs
Initializes a new instance of the SerialPort class using the specified port name.
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)
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) |
- 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(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)
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) |
- 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(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)
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, 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) |
- 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(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)
Parameters
- portName
- String
The port to use (for example, COM1).
- baudRate
- Int32
The baud rate.
- 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) |
- 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(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)
Parameters
- portName
- String
The port to use (for example, COM1).
- baudRate
- Int32
The baud rate.
- 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, 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) |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。