Represents the method that will handle the data received event of a SerialPort object. : SerialPort « Development Class « C# / C Sharp






Represents the method that will handle the data received event of a SerialPort object.

  

using System;
using System.IO.Ports;

class PortDataReceived
{
    public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM1");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);

        mySerialPort.Open();

        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    private static void DataReceviedHandler(
                        object sender,
                        SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);
    }
}

   
    
  








Related examples in the same category

1.Set BaudRate, Parity, ReadTimeout, StopBits for COM1 SerialPort
2.SerialPort Class represents a serial port resource.
3.Gets an array of serial port names for the current computer.
4.Write data to COM port