# Receiving incoming data

Simple Serial Manager implements a custom event to manage data received from target. The library receive packets internally throug SerialPort.DataReceived Event (opens new window).

While Serial Port is open, you can check the incoming data in real-time using an exposed event called OnDataReceived.

# ReceivedDataEventArgs

This is a class that contains the OnDataReceived event, provides a value to use arguments for events that do not include event data.

# Argument values

The following are the values that you can get using this ReceivedDataEventArgs.

Type Name Description
int ReceivedBytes The count of bytes received in the incoming packet
string ReceivedData The received data represented in string
byte[] ReceivedDataInBytes The received data represented in byte array

# Example code

In the following example, you can know how to add a listener to OnReceivedData event, remove the same listener and receive data when needed.

private void OpenPort(string Port)
{
    simplePort.Open(Port, 115200, 8, SimpleStopBits.One);
    if (simplePort.IsOpen)
    {
        Console.WriteLine("Open port");
        // It's recommended to add listener after open serial port.
        simplePort.OnDataReceived += OnReceiveData; 
    }
    else
        Console.WriteLine("Cannot open the port");

}

private void OnReceiveData(object sender, SimpleSerialManager.ReceivedDataEventArgs e)
{
    // Method to use received data
    Console.WriteLine($"Received Bytes: {e.Packet.ReceivedBytes}\nData: {e.Packet.ReceivedData}");
}

private void ClosePort()
{
    if (simplePort.IsOpen)
    {
        simplePort.Close();
        // It's recommended to remove listener after close serial port.
        simplePort.OnDataReceived -= OnReceiveData;
    }
}
Last Updated: 11/8/2022, 8:03:50 PM

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