YULTek/Otarcik_CAN/Sources/CANAnalyzer.cpp
2023-01-15 12:38:14 -05:00

120 lines
3.3 KiB
C++

#include "CANAnalyzer.h"
#include "PCANInterface.h"
#include "GeneralMessagesLogDispatcher.h"
#include "CANDatabase.h"
CCANAnalyzer::CCANAnalyzer(QObject *parent) : QObject(parent)
{
mCANDriverIF = 0;
mCANDatabase = 0;
mIsCANInitialized = false;
mCANReadTimer = new QTimer;
mCANReadTimer->setInterval(1000);
mCANReadTimer->setSingleShot(true);
connect(mCANReadTimer,SIGNAL(timeout()),this,SLOT(CANTimerExpired()));
}
CCANAnalyzer::~CCANAnalyzer()
{
mCANDriverIF->DeInit(0);
delete mCANReadTimer;
while (!mLastMessagesList.isEmpty())
{
delete mLastMessagesList.takeFirst();
}
}
int CCANAnalyzer::Init(CPCANInterface *CANDriverInterface,CCANDatabase *CANDatabaseHandle)
{
if(CANDriverInterface == 0 || CANDatabaseHandle == 0)
{
return RET_GENERAL_ERROR;
mIsCANInitialized = false;
}
mCANDriverIF = CANDriverInterface;
mCANDatabase = CANDatabaseHandle;
CGeneralMessagesLogDispatcher::instance()->AddLogMessage("OtarcikCan modules starting...");
if(mCANDriverIF->Init() != RET_OK)
{
mIsCANInitialized = false;
}
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;
}
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();
}