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