0
votes

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

1
Can you show us the code you're using?Ian Gregory
I added the code of my DataReceived handler. I just notify the UI from here, do nothing else in the handler. I hope it helps to you. I'm new in the COM port communication field, so there can be some trivial mistakes I make...Csaba Benko
What is comPort.ReceivedBytesThreshold set to?Ian Gregory
I already tried to play with that. Now it is set to 1. Nothing changed with the big packet. For the small ones, I've got more event, but for the big one it's the same, it fires only once for the first few bytes. Is it possible that multi threading causes some problem? Or locks on some data structures? But I think that should not affect at least the fireing of the event. Am I wrong in that?Csaba Benko
What I found out, 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...Csaba Benko

1 Answers

3
votes

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.