user2297392
user2297392

Reputation:

How to catch the return of a serial port after sending a command?

How to catch the return of a serial port after sending a command? The return is OK. How do i read? I tried using readLine () and ReadExisting () did not work.

        serialPort.PortName = "COM1";
            serialPort.BaudRate = 115200;
            serialPort.Open();
            if (serialPort.IsOpen)
            {
                // Send command
                serialPort.Write("at");

                // string data = portaSerial.ReadLine();


                // Close serial port
                serialPort.Close();
            }

Upvotes: 2

Views: 6913

Answers (2)

Mark Hall
Mark Hall

Reputation: 54562

You need attach an EventHandler to the DataReceived Event.

From above link:

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

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

which you will either attach in your designer or programically like this.

serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

You are attaching your DataReceivedEvent handler each time you click your button, plus as Greg mentioned you are closing your port as soon as you send your data. see if something like this works. I do not have any serial devices to test this on though. I would usually just open the port at the beginning of your session and leave it open till you were done with all communications.

public partial class Form1 : Form
{
    SerialPort serialPort = new SerialPort();

    public Form1()
    {
        InitializeComponent();
        serialPort.PortName = "Com1";
        serialPort.BaudRate = 115200;
        serialPort.DataReceived += new SerialDataReceivedEventHandler(DataRecievedHandler);
        openSerial();

    }

    void DataRecievedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        MessageBox.Show(indata);
        closeSerial();

    }
    public void openSerial()
    {
        if (!serialPort.IsOpen) serialPort.Open();
    }

    public void closeSerial()
    {
        if (serialPort.IsOpen) serialPort.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        openSerial();
        serialPort.Write("Hello");

    }
}

Upvotes: 5

gregwhitaker
gregwhitaker

Reputation: 13420

You should be reading from, and writing to, the serial port from different threads. You can either spin another thread to block on ReadLine() or you can wire up the SerialDataReceivedEventHandler to receive an event whenever data arrives on the serial port.

Also keep in mind that the DataReceived event is not guaranteed to be raised for every byte received. You need to use the BytesToRead property to check how many bytes are in the buffer.

Upvotes: 1

Related Questions