# Sending data through serial port

After connect to serial port, you can send data to the target using 2 methods (one of these have 3 overloadings).

# SendLine method

Writes the specified string followed by NewLine Value (opens new window).

# Parameters

The following is the parameter required by this method.

Type Name Description
string data The string data to send
Method parameter details
public void SendLine(string data)
{
    ...
}

# Example code

if (simplePort.IsOpen)
{
    simplePort.SendLine("Hello World");
}
Output

-> Hello World\n

# Send method overloading 1

Writes the specified string directly.

# Parameters

The following is the parameter required by this method.

Type Name Description
string data The string data to send
Method parameter details
public void Send(string data)
{
    ...
}

# Example code

if (simplePort.IsOpen)
{
    simplePort.Send("Hello World");
}
Output

-> Hello World

# Send method overloading 2

Writes a specified number of bytes to the target using data from a buffer.

# Parameters

The following are the parameters required by this method.

Type Name Description
byte[] buffer The byte array that contains the data to send
int buffer The zero-based byte offset in the buffer parameter at which to begin copying bytes to the port.
int buffer The number of bytes to write
Method parameter details
public void Send(byte[] buffer, int offset, int count)
{
    ...
}

# Example code

if (simplePort.IsOpen)
{
    byte[] data = new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64 }; // Hello World
    simplePort.Send(data, 0, data.Length);
}
Output

-> Hello World

# Send method overloading 3

Writes a specified number of characters to the target using data from a buffer.

# Parameters

The following are the parameters required by this method.

Type Name Description
char[] buffer The characters array that contains the data to send
int buffer The zero-based byte offset in the buffer parameter at which to begin copying bytes to the port.
int buffer The number of characters to write
Method parameter details
public void Send(char[] buffer, int offset, int count)
{
    ...
}

# Example code

if (simplePort.IsOpen)
{
    char[] data = new char[] { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };
    simplePort.Send(data, 0, data.Length);
}
Output

-> Hello World

Last Updated: 11/8/2022, 8:03:50 PM

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