5YrsLaterDBA
5YrsLaterDBA

Reputation: 34730

SerialPort communication questions

I know SerialPort communication in .NET is designed to send the DataReceived event to the receiver when data is available and reach the threshhold.

Can we not use that DataReceived event and start a thread in the receiver side to freqenutly call one of those ReadXXX methods to get data?

What will happen if receiver is much slower than the sender? The SerialPort buffer will overflow (data lost)?

Upvotes: 2

Views: 1752

Answers (3)

Juan Carlos Velez
Juan Carlos Velez

Reputation: 2940

The function of the thread that read serial port can be as this:

        private void ThreadRx()
        {
                while (true)
                {
                    try
                    {
                        if (this._serialPort.IsOpen == true)
                        {
                            int count = this._serialPort.BytesToRead;
                            if (count > 0)
                            {
                                byte[] Buffer = new Byte[count];
                                this._serialPort.Read(Buffer, 0, count);

                            //To do: Call your reception event (sending the buffer)
                            }
                            else
                            {
                                Thread.Sleep(50);
                            }
                        }
                        else
                        {
                            Thread.Sleep(200);
                        }
                    }
                    catch (ThreadAbortException ex)
                    {
                        //this exception is invoked calling the Abort method of the thread to finish the thread
                        break;//exit from while
                    }
                    catch (Exception ex)
                    {
                        //To do:call your error event
                    }
                }
        }

Do not worry about the input buffer, because the thread can be read much faster than the baud rate of serial port communication, you can even use this same code to read a tcp/ip socket.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941218

There's little point in doing this, just start the reader thread yourself after you open the port and don't bother with DataReceived. Doing it your way is difficult, tough to cleanly unsubscribe from the DataReceived event after you started the thread, especially at the very moment data is being received. You can't afford to have them both.

Upvotes: 1

Aphex
Aphex

Reputation: 7490

That works, in fact it's one of the ways I used in my question Constantly reading from a serial port with a background thread.

For your scenario you could listen to the DataReceived event, then start a thread that calls ReadExisting on the port to get all currently available bytes. You can also check how many bytes are waiting in the receive buffer by looking at the SerialPort.BytesToRead property.

As for your receive buffer overflowing, a) it's big enough (you can check with the SerialPort.ReadBufferSize property) and b) this isn't 1982, so CPUs are fast enough to process data from the port so that it doesn't have time to fill up (certainly much faster than the serial data rate).

Upvotes: 0

Related Questions