C# – Serial port DataReceived event does not fire

cnetserial-port

I working on an application handling serial port communication. It's a WPF, multithreaded application. Everything went fine until I sent only small packages through the COM port with communication.

No I reached a phase, where I have to listen for data sent to my application on COM port. For this big chunk of data I go into trouble. I have only one DataReceived event fired, which reads part of the data I need. But after that there is no DataReceived event fired anymore for the rest of the data.

If I monitor the COM port I saw that the bytes are there, the monitoring software states, that there are 2067 bytes in the in queue, but the event do not fire.

I searched Google for similar problems, but I was unable to find relevant answers for this. Have you got any idea where to start searching the roots of the problem.

Thanks in advance.

Here is the code of the DataReceived handler:

        private void comPort_DataReceived( object sender, SerialDataReceivedEventArgs e )
    {
        do
        {
            new Thread(() =>
            {
                OnReceivingData(new ComPortCommunicationEventArgs(DataTransferDirections.Receiving, DataTransferActions.Start));
            }).Start();
            byte[] packetData = null;
            try
            {
                IsReceiving = true;
                int bytesToReadCount = comPort.BytesToRead;
                if ( bytesToReadCount == 0 )
                {
                    return;
                }

                packetData = new byte[bytesToReadCount];
                comPort.Read(packetData, 0, bytesToReadCount);
            }
            finally
            {
                IsReceiving = false;
                new Thread(() =>
                {
                    OnReceivingData(new ComPortCommunicationEventArgs(DataTransferDirections.Receiving, DataTransferActions.End));
                }).Start();
            }
            OnPacketReceived(new PacketReceivedEventArgs(comPort.PortName, packetData.ToList()));
        } while ( comPort.BytesToRead > 0 );
    }

What I found out meanwhile, that if I comment out the OnPacketReceived call – which just fires event for the UI with the received data – than the data processing works fine. So it's not a COM port communication error I think…

Update:

I found the solution for my problem. It was not related to serial port handling.
In one of my data processing threads I run into a loop, which was supposed to be finished on succesfull processing, but with not enough data it was just run continously. Doing that it was blocked the DataReceived event handler – which is still not fully clear for my why, since the two process should be on diffrerent threads I supposed. But it seems that I'm wrong with this.

Anyway I corrected the processing method, and no I receive the data from the serisl port.

Sorry to take your time, and thanks for your help anyway your ideas helped my localize this problem.

Br,
Csaba

Best Answer

I worked with COM ports 5 years ago, and I know how tricky it can get sometimes. All sorts of tricky problems happened with the Siemens device I used to work with :)

First off, I couldn't follow your code thoroughly, so I can't know for sure if your DataReceived routine is sane. I suggest you try to replace all that code inside comPort_DataReceived to something very simple, only for test purposes.

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

If it still doesn't work, then the problem is not in your code. If you don't know what else to try, I would mess with parameters such as SerialPort.DtrEnable and SerialPort.RtsEnable and see if something happens, because these also caused some not-in-the-manual bugs for me in the past.


Piece of code extracted from MSDN.

Related Topic