c# Pause listening to COM port

Gerald Oakham 101 Reputation points
2020-12-16T09:17:44.857+00:00

Hello,
I am trying to write a Windows form program to listen to a COM port and intercept the data stream.
I have managed this, but I would now like to add the option to pause the listening.

I am using the CLOSE() option, but his then also makes my program terminate also.

Come someone please advise on what I am doing wrong, and what I can to pause the listening?

Thank you in advance.

Code snippet


    private void Form1_Load(object sender, EventArgs e)
        {
                --- do some other stuff, then create com port

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

        ///# Open (above) port
        mySerialPort.Open();
        }

    private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
        SerialPort sp = (SerialPort)sender;
        string output = sp.ReadTo("\r");    
         ---- do some stuff with the data
          }

    private void stopListeningToolStripMenuItem_Click(object sender, EventArgs e)
        {
        try
            {
            mySerialPort.Close(); // <----------------------- Closes program. need it not to. Just Pause listening to port
            }
        catch (Exception ex)
            {
            Form1.instance.richTextBox1.AppendText(ex.Message.ToString() + Environment.NewLine);    // If port already closed, then say so.
            }
        }


    private void startListeningToolStripMenuItem_Click(object sender, EventArgs e)
        {
        try
            {
            mySerialPort.Open();
            }
        catch (Exception ex)
            {
            Form1.instance.richTextBox1.AppendText(ex.Message.ToString() + Environment.NewLine);    // If port already open, then say so.
            }
        }

--- End Of Code snippet

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 117.9K Reputation points
    2020-12-16T09:36:31.717+00:00

    It looks strange that the program is closed by closing a port.

    If you want to ignore the data during pausing, then consider this approach too:

    bool pause = false;
    
    private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
       SerialPort sp = (SerialPort)sender;
       if( pause ) { sp.ReadExisting(); return; }
       . . .
    }
    
    private void stopListeningToolStripMenuItem_Click(object sender, EventArgs e)
    {
       pause = true;
    }
    
    0 comments No comments

  2. John-Ross Medeiros Costa 1 Reputation point
    2021-01-04T15:05:39.867+00:00

    Hi @Gerald Oakham ,

    If the serial port is closed after you have called

    SerialPort.ReadTo("\r");

    and the specified value has not yet been received, An InvalidOperationException will be thrown. If you wanted to pause receiving data from the serial port you can unsubscribe from the SerialPort.DataReceived Event

    mySerialPort.DataReceived -= port_DataReceived;

    This will not close the serial port or stop data from being received from the port but you will no longer receive the event that data is received.

    0 comments No comments

  3. Timon Yang-MSFT 9,591 Reputation points
    2021-01-05T05:54:16.127+00:00

    According to the solution in this post, the rule of SerialPort.Close() is that the serial port can only be closed if no SerialPort event handler is activated.
    Is your DataReceived event handler always active?
    Before closing the serialport, you may need to stop this event first, you may refer to this link about this:
    SerialPort fires DataReceived event after close


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.