# Opening serial port

After get and identify a serial port to work with, you can open communication using 4 method overloadings available through the created SimplePort instance. If serial port is successfully opened, the method returns true.

WARNING

This method checks if the target serial port is available to connect before attempting to open communication. If the target port does not exists in available ports list, the intent to open serial port will be aborted.

# Open method overloading 1

# Parameters

The following is the parameter required by this method.

Type Name Description
string portName The name of the target port, this should contains "COM"
Method parameter details
public bool Open(string portName)
{
    ...
}

# Default values

The following are the default values setted by the library for some variables.

Type Name value
int BaudRate 9600
int DataBits 8
SimpleStopBits StopBits SimpleStopBits.One
int ReadTimeout 500
int WriteTimeout 500

# Example code

simplePort.Open("COM3");
if (simplePort.IsOpen)
{
    Console.WriteLine("Open Port");
    simplePort.OnDataReceived += OnReceiveData; // This event is to receive data in real time.
}
else
    Console.WriteLine("Puerto no se abrió");

# Open method overloading 2

# Parameters

The following are the parameters required by this method.

Type Name Description
string portName The name of the target port, this should contains "COM"
int bauds The baud rate, is the data transmition speed
Method parameters details
public bool Open(string portName, int bauds)
{
    ...
}

# Default values

The following are the default values setted by the library for some variables.

Type Name value
int DataBits 8
SimpleStopBits StopBits SimpleStopBits.One
int ReadTimeout 500
int WriteTimeout 500

# Example code

simplePort.Open("COM3", 115200);
if (simplePort.IsOpen)
{
    Console.WriteLine("Open Port");
    simplePort.OnDataReceived += OnReceiveData; // This event is to receive data in real time.
}
else
    Console.WriteLine("Cannot Open Port");

# Open method overloading 3

# Parameters

The following are the parameters required by this method.

Type Name Description
string portName The name of the target port, this should contains "COM"
int bauds The baud rate, is the data transmition speed
int dataBits Standard length of data bits per byte
Method parameters details
public bool Open(string portName, int bauds, int dataBits)
{
    ...
}

# Default values

The following are the default values setted by the library for some variables.

Type Name value
SimpleStopBits StopBits SimpleStopBits.One
int ReadTimeout 500
int WriteTimeout 500

# Example code

simplePort.Open("COM3", 115200, 8);
if (simplePort.IsOpen)
{
    Console.WriteLine("Open Port");
    simplePort.OnDataReceived += OnReceiveData; // This event is to receive data in real time.
}
else
    Console.WriteLine("Puerto no se abrió");

# Open method overloading 4

# Parameters

The following are the parameters required by this method.

Type Name Description
string portName The name of the target port, this should contains "COM"
int bauds The baud rate, is the data transmition speed
int dataBits Standard length of data bits per byte
SimpleStopBits stopBits Enum that specifies the number of stop bits used on the SimplePort object
Method parameters details
public bool Open(string portName, int bauds, int dataBits, SimpleStopBits stopBits)
{
    ...
}

# Default values

The following are the default values setted by the library for some variables.

Type Name value
int ReadTimeout 500
int WriteTimeout 500

# Example code

simplePort.Open("COM3", 115200, 8, SimpleStopBits.One);
if (simplePort.IsOpen)
{
    Console.WriteLine("Open Port");
    simplePort.OnDataReceived += OnReceiveData; // This event is to receive data in real time.
}
else
    Console.WriteLine("Puerto no se abrió");

# Checking if serial port is open

The library allows to check if the selected serial port still opened anytime during the application runtime. If the port is open, the property IsOpen returns true.

# Example

if (simplePort.IsOpen)
{
    // Do something if port is open
}
else{
    // Do something if port is closed
}
Last Updated: 11/8/2022, 8:03:50 PM

Made by Andres Ruiz with ❤️
All rights reserved © 2024