122 lines
3.5 KiB
C++
122 lines
3.5 KiB
C++
#include "CANAnalyzer.h"
|
|
|
|
#include "GeneralMessagesLogDispatcher.h"
|
|
#include "CANDatabase.h"
|
|
#include "OtarcikCan.h"
|
|
|
|
|
|
CCANAnalyzer::CCANAnalyzer(QObject *parent) : QObject(parent)
|
|
{
|
|
mCANDriverIF = new CPCANInterface;
|
|
mIsCANInitialized = false;
|
|
mDevicePtr = 0;
|
|
|
|
mCANReadTimer = new QTimer;
|
|
mCANReadTimer->setInterval(200);
|
|
mCANReadTimer->setSingleShot(true);
|
|
connect(mCANReadTimer,SIGNAL(timeout()),this,SLOT(CANTimerExpired()));
|
|
}
|
|
|
|
CCANAnalyzer::~CCANAnalyzer()
|
|
{
|
|
mCANDriverIF->DeInit();
|
|
delete mCANDriverIF;
|
|
delete mCANReadTimer;
|
|
while (!mLastMessagesList.isEmpty())
|
|
{
|
|
delete mLastMessagesList.takeFirst();
|
|
}
|
|
}
|
|
|
|
int CCANAnalyzer::Init(TPCANHandle CANDeviceChannel,TPCANBaudrate CANDeviceBaudrate)
|
|
{
|
|
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage("OtarcikCan modules starting...");
|
|
|
|
if(mCANDriverIF->Init(CANDeviceChannel,CANDeviceBaudrate) != RET_OK)
|
|
{
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage("Could not initialize CAN hardware",true,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
|
|
mIsCANInitialized = false;
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
else
|
|
{
|
|
mCANReadTimer->start();
|
|
mIsCANInitialized = true;
|
|
}
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
int CCANAnalyzer::ReadCAN()
|
|
{
|
|
// TPCANMsg CANMsg;
|
|
// TPCANTimestamp CANTimeStamp;
|
|
|
|
// // We execute the "Read" function of the PCANBasic
|
|
// TPCANStatus stsResult = CAN_Read(PcanHandle, &CANMsg, &CANTimeStamp);
|
|
|
|
// if (stsResult != PCAN_ERROR_QRCVEMPTY)
|
|
// {
|
|
// // We process the received message
|
|
// ProcessMessageCan(CANMsg, CANTimeStamp);
|
|
// }
|
|
|
|
// return stsResult;
|
|
return RET_OK;
|
|
}
|
|
|
|
void CCANAnalyzer::CANTimerExpired()
|
|
{
|
|
QList<CCANMessage *> NewMessagesList = mCANDriverIF->ReadCANFullBuffer(0x51U);
|
|
|
|
unsigned int DeletedPtrs = 0;
|
|
if(NewMessagesList.isEmpty())
|
|
{
|
|
return;
|
|
}
|
|
|
|
for(int i = 0; i < NewMessagesList.size(); i++)
|
|
{
|
|
if(mLastMessagesList.isEmpty())
|
|
{
|
|
mLastMessagesList.append(NewMessagesList.at(i));
|
|
}
|
|
else
|
|
{
|
|
//Check if new message ID for this device already exists in the list.
|
|
bool found = false;
|
|
for(int j = 0; j < mLastMessagesList.size(); j++)
|
|
{
|
|
if(NewMessagesList[i]->mCANChannel == mLastMessagesList[j]->mCANChannel &&
|
|
NewMessagesList[i]->mCANMsgID == mLastMessagesList[j]->mCANMsgID)
|
|
{
|
|
//This message ID is already populated in the list. Update the value and delete the new instance
|
|
mLastMessagesList[j]->mCANMsgData = NewMessagesList[i]->mCANMsgData;
|
|
mLastMessagesList[j]->mCANMsgMicrosecs = NewMessagesList[i]->mCANMsgMicrosecs;
|
|
mLastMessagesList[j]->mCANMsgMillisecs = NewMessagesList[i]->mCANMsgMillisecs;
|
|
mLastMessagesList[j]->mCANMsgMillisecsOverflow = NewMessagesList[i]->mCANMsgMillisecsOverflow;
|
|
|
|
delete NewMessagesList[i];
|
|
DeletedPtrs++;
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!found)
|
|
{
|
|
//This is the first time we receive this message, just add it to the list.
|
|
mLastMessagesList.append(NewMessagesList[i]);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
NewMessagesList.clear();
|
|
|
|
mCANReadTimer->start();
|
|
|
|
// mProgramPtr->UpdateCANViewerDataRequest(mLastMessagesList);
|
|
}
|