C# code not working for read serial port device

35 views Asked by At

C# code not working for read serial port device. But hyptertemila, putty can get data from that device. Exception is "$exception

{"A device attached to the system is not functioning.\r\n"} System.IO.IOException"

enter image description here

my code is

serialPort = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);

        serialPort.Open();
        serialPort.DataReceived += SerialPort_DataReceived;

        Console.WriteLine($"Reading from {portName}. Press any key to exit.");
        Console.ReadKey();

        serialPort.Close();

    }

    private static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        // This event will be called when data is received on the serial port
        // Read the data from the buffer and process it as needed
        string data = serialPort.ReadExisting();
        Console.WriteLine($"Received data: {data}");
    }
0

There are 0 answers