Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions connections/canconmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ void CANConManager::resetTimeBasis()
{
mTimestampBasis = QDateTime::currentMSecsSinceEpoch() * 1000;
mElapsedTimer.restart();
// Flush any frames already queued by connections — they carry timestamps
// computed against the old mElapsedTimer and would appear with incorrect
// (large) values after the reset if not discarded now.
foreach (CANConnection* conn_p, mConns)
conn_p->getQueue().flush();
}

CANConManager::~CANConManager()
Expand Down Expand Up @@ -134,6 +139,11 @@ uint64_t CANConManager::getTimeBasis()
return mTimestampBasis;
}

uint64_t CANConManager::getElapsedUs()
{
return static_cast<uint64_t>(mElapsedTimer.nsecsElapsed()) / 1000ULL;
}

QList<CANConnection*>& CANConManager::getConnections()
{
return mConns;
Expand Down
1 change: 1 addition & 0 deletions connections/canconmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CANConManager : public QObject
CANConnection* getByName(const QString& pName) const;

uint64_t getTimeBasis();
uint64_t getElapsedUs(); // µs elapsed since last resetTimeBasis() — same reference as Tx timestamps
void resetTimeBasis();

int getNumBuses();
Expand Down
33 changes: 32 additions & 1 deletion connections/serialbusconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ void SerialBusConnection::disconnectDevice() {
}
}

/* Immediately update status when the device state changes */
void SerialBusConnection::deviceStateChanged(QCanBusDevice::CanBusDeviceState state)
{
CANConStatus stats;
if (state == QCanBusDevice::ConnectedState) {
setStatus(CANCon::CONNECTED);
stats.conStatus = getStatus();
stats.numHardwareBuses = mNumBuses;
emit status(stats);
} else if (state == QCanBusDevice::UnconnectedState && getStatus() == CANCon::CONNECTED) {
setStatus(CANCon::NOT_CONNECTED);
stats.conStatus = getStatus();
stats.numHardwareBuses = mNumBuses;
emit status(stats);
}
}

void SerialBusConnection::errorReceived(QCanBusDevice::CanBusError error) const
{
Expand Down Expand Up @@ -229,7 +245,22 @@ void SerialBusConnection::framesReceived()
if (useSystemTime) {
frame_p->setTimeStamp(CommFrame::TimeStamp::fromMicroSeconds(QDateTime::currentMSecsSinceEpoch() * 1000ul));
}
else frame_p->setTimeStamp(CommFrame::TimeStamp(0, (recFrame.timeStamp().seconds() * 1000000ul + recFrame.timeStamp().microSeconds()) - timeBasis));
else {
uint64_t hwTimestamp = static_cast<uint64_t>(recFrame.timeStamp().seconds()) * 1000000ULL
+ static_cast<uint64_t>(recFrame.timeStamp().microSeconds());
uint64_t relTimestamp;
if (hwTimestamp >= timeBasis) {
// Epoch-based hw timestamp (e.g. SocketCAN): subtract session basis.
relTimestamp = hwTimestamp - timeBasis;
} else {
// Uptime-based hw timestamp (e.g. PCAN): the hw clock cannot be
// synchronised to the elapsed timer without a fixed anchor bias,
// so use the elapsed timer directly — the same source as Tx timestamps.
// This guarantees Tx and Rx share one consistent clock with no drift.
relTimestamp = CANConManager::getInstance()->getElapsedUs();
}
frame_p->setTimeStamp(CommFrame::TimeStamp(0, relTimestamp));
}

checkTargettedFrame(*frame_p);

Expand Down