Как это происходит? Как я могу это предотвратить?
Вот мой код:
Код: Выделить всё
private SerialPort port;
public void RegisterReceive()
{
...
port.DiscardOutBuffer();
port.DiscardInBuffer();
port.DataReceived += OnReceive;
....
}
public static Mutex mutexReceivingCom = new Mutex();
void OnReceive(object sender, SerialDataReceivedEventArgs args)
{
SerialPort port = sender as SerialPort;
mutexReceivingCom.WaitOne(); // Wait until it is safe to enter.
if (data.AppIsRunning)
{
Byte by;
try
{
while (port.BytesToRead > 0)
{
by = (Byte)port.ReadByte();
mainWindow.Dispatcher.Invoke(() =>
{
General_HandleTelegram.Add_HandleTeleData_ByteToUartInBuffer(by);
});
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("SerialPort OnReceive Exception: " + ex.ToString());
//put other, more interesting error handling here.
}
}
mutexReceivingCom.ReleaseMutex(); // Release the Mutex.
ThreadPool.QueueUserWorkItem(handleReceivedBytes);
}
private void handleReceivedBytes(object state)
{
mutexReceivingCom.WaitOne(); // Wait until it is safe to enter.
if (data.AppIsRunning)
{
try
{
BusAnswerOrCyclic_Handling tempPack = null;
if (port.IsOpen)
{
tempPack = ReceiveTelegram(LastRecTelegramm, this);
while (tempPack != null)
{
mainWindow.Dispatcher.Invoke(() =>
{
if (tempPack != null)
tempPack.ProcessBusTelegram();
});
if (tempPack != null)
{
LastRecTelegramm = tempPack;
}
if (port.IsOpen)
{
tempPack = ReceiveTelegram(LastRecTelegramm, this);
}
else
break;
}
}
CheckIfBufferCanBeReset();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("Error ComPort.cs handleReceivedBytes: " + ex.Message);
}
}
mutexReceivingCom.ReleaseMutex(); // Release the Mutex.
}
Код: Выделить всё
public BusAnswerOrCyclic_Handling ReceiveTelegram(BusAnswerOrCyclic_Handling parLastSendBusTelegram, ComPort comPort)
{
...
pack= new BusAnswerOrCyclic_Handling(recState, CmdToAssignToReceivedTele, LastSendBusTelegram, fetchedByteList.ToArray(), Generic_HandleTelegrams.mainWindow, comPort, -1, ucTelTyp == enTelTyp.SynLine, true);
....
return pack;
}
public BusAnswerOrCyclic_Handling(enRecStates recState, BusTelegram_Command command, BusAnswerOrCyclic_Handling LastBusTelegram, byte[] bytes, MainWindow mainWindow,
ComPort comPort, short iMasterByteCountWithCRC, bool isSynLine, bool isFullyReceived)
{
DateTime dt = DateTime.Now;
...
}
Код: Выделить всё
public void ProcessBusTelegram()
{
// adds it to the datalog
}
посмотрите код, что я пробовал
п>
Подробнее здесь: https://stackoverflow.com/questions/790 ... -over-time