Skip to content
Permalink
Browse files

v0.2.3: improve PCAN basic message handling

probably fixes problem when more than one message avail with one receive event
  • Loading branch information...
Hubert Denkmair
Hubert Denkmair committed Apr 25, 2017
1 parent 1f898ee commit 8a03e50b344de7b052e577874509c933c09bfa3c
Showing with 52 additions and 36 deletions.
  1. +49 −34 src/driver/PeakCanDriver/PeakCanInterface.cpp
  2. +1 −0 src/driver/PeakCanDriver/PeakCanInterface.h
  3. +2 −2 src/mainwindow.cpp
@@ -230,57 +230,72 @@ uint32_t PeakCanInterface::getState()
return state_unknown;
}

bool PeakCanInterface::readMessage(CanMessage &msg, unsigned int timeout_ms)
bool PeakCanInterface::tryReadMessage(CanMessage &msg)
{
TPCANMsg buf;
TPCANTimestamp timestamp;

if (WaitForSingleObject(_autoResetEvent, timeout_ms) != WAIT_OBJECT_0) {
TPCANStatus result = CAN_Read(_handle, &buf, &timestamp);
if (result != PCAN_ERROR_OK) {
return false;
}

TPCANStatus result = CAN_Read(_handle, &buf, &timestamp);
if (result == PCAN_ERROR_OK) {
if ((buf.MSGTYPE & PCAN_MESSAGE_STATUS) != 0) {
// TODO handle status message?
return false;
} else {
msg.setErrorFrame(false);
}
if ((buf.MSGTYPE & PCAN_MESSAGE_STATUS) != 0) {
// TODO handle status message?
return false;
}
msg.setErrorFrame(false);

msg.setInterfaceId(getId());
msg.setId(buf.ID);
msg.setExtended((buf.MSGTYPE & PCAN_MESSAGE_EXTENDED)!=0);
msg.setRTR((buf.MSGTYPE & PCAN_MESSAGE_RTR)!=0);
msg.setInterfaceId(getId());
msg.setId(buf.ID);
msg.setExtended((buf.MSGTYPE & PCAN_MESSAGE_EXTENDED)!=0);
msg.setRTR((buf.MSGTYPE & PCAN_MESSAGE_RTR)!=0);

uint8_t len = (buf.LEN > 8) ? 8 : buf.LEN;
msg.setLength(len);
for (int i=0; i<len; i++) {
msg.setByte(i, buf.DATA[i]);
}
uint8_t len = (buf.LEN > 8) ? 8 : buf.LEN;
msg.setLength(len);
for (int i=0; i<len; i++) {
msg.setByte(i, buf.DATA[i]);
}

// Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
uint64_t ts = timestamp.millis;
ts += 0x100000000 * (uint64_t)timestamp.millis_overflow;
ts *= 1000;
ts += timestamp.micros;
// Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
uint64_t ts = timestamp.millis;
ts += 0x100000000ul * (uint64_t)timestamp.millis_overflow;
ts *= 1000ul;
ts += timestamp.micros;

if ( (_peakOffsetFirstFrame==0) || (_hostOffsetFirstFrame==0) ) {
_hostOffsetFirstFrame = getDriver()->backend().getUsecsAtMeasurementStart()
+ getDriver()->backend().getUsecsSinceMeasurementStart();
_peakOffsetFirstFrame = ts;
}
if ( (_peakOffsetFirstFrame==0) || (_hostOffsetFirstFrame==0) ) {
_hostOffsetFirstFrame = getDriver()->backend().getUsecsAtMeasurementStart()
+ getDriver()->backend().getUsecsSinceMeasurementStart();
_peakOffsetFirstFrame = ts;
}

ts -=_peakOffsetFirstFrame;
ts += _hostOffsetFirstFrame;
ts += _hostOffsetFirstFrame;
ts -= _peakOffsetFirstFrame;

msg.setTimestamp(ts/1000000, ts % 1000000);
msg.setTimestamp(ts/1000000, ts % 1000000);

return true;
}

bool PeakCanInterface::readMessage(CanMessage &msg, unsigned int timeout_ms)
{

if (tryReadMessage(msg))
{
return true;
} else {
QThread::msleep(10);
}

if (WaitForSingleObject(_autoResetEvent, timeout_ms) != WAIT_OBJECT_0)
{
return false;
}

bool retval = tryReadMessage(msg);
if (!retval)
{
QThread::msleep(1);
}
return retval;
}

void PeakCanInterface::sendMessage(const CanMessage &msg)
@@ -54,5 +54,6 @@ class PeakCanInterface : public CanInterface

uint16_t calcBitrateMode(unsigned bitrate, unsigned samplePoint);
QString getErrorText(uint32_t status_code);
bool tryReadMessage(CanMessage &msg);

};
@@ -408,9 +408,9 @@ void MainWindow::showAboutDialog()
"cangaroo\n"
"open source can bus analyzer\n"
"\n"
"version 0.2.2\n"
"version 0.2.3\n"
"\n"
"(c)2015-2016 Hubert Denkmair"
"(c)2015-2017 Hubert Denkmair"
);
}

0 comments on commit 8a03e50

Please sign in to comment.
You can’t perform that action at this time.