Является ли DateTime.Now непоследовательным с течением времени?C#

Место общения программистов C#
Anonymous
Является ли DateTime.Now непоследовательным с течением времени?

Сообщение Anonymous »

Мое приложение регистрирует телеграммы через UART с шины. Для каждой телеграммы я добавляю временную метку с DateTime.Now при приеме. Теперь у меня такое явление, что иногда временная метка следующей телеграммы оказывается на 100 мс ниже, чем предыдущие. Это так сбивает с толку.
Как это происходит? Как я могу это предотвратить?
Вот мой код:

Код: Выделить всё

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

Вернуться в «C#»