/******************************************************************************* * * * Gros Gin électronique * * 2023 * * * * Project: Otarcik CAN * * * * * * * *******************************************************************************/ /* Description: This class interfaces the PCAN USB CAN puck */ #include "PCANInterface.h" #include "GeneralMessagesLogDispatcher.h" //#include "CANMessage.h" CPCANInterface::CPCANInterface(QObject *parent) : QObject(parent) { } int CPCANInterface::Init() { QString LogMsg; TPCANStatus Result; Result = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K); CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Initializing PCAN USB module. Channel:%1, Baudrate:%2").arg(PCAN_USBBUS1).arg(PCAN_BAUD_500K)); if(Result != PCAN_ERROR_OK) { // An error occurred, get a text describing the error and show it // char strMsg[256]; CAN_GetErrorText(Result, 0, strMsg); qDebug("%s",strMsg); CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Could not initialize PCAN USB module. Error:%1").arg(strMsg),true,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS); return RET_GENERAL_ERROR; } else { CGeneralMessagesLogDispatcher::instance()->AddLogMessage("PCAN USB init SUCCESS"); } return RET_OK; } int CPCANInterface::DeInit(unsigned short Channel) { CAN_Uninitialize(PCAN_USBBUS1); return RET_OK; } QList CPCANInterface::ReadCANFullBuffer(unsigned short Channel) { TPCANMsg CANMsg; TPCANTimestamp CANTimeStamp; TPCANStatus stsResult; int cnt = 0; QList MessagesList; MessagesList.clear(); do { // We execute the "Read" function of the PCANBasic stsResult = CAN_Read(Channel, &CANMsg, &CANTimeStamp); if (stsResult != PCAN_ERROR_QRCVEMPTY) { CCANMessage *NewMsg = new CCANMessage(Channel,CANMsg,CANTimeStamp); MessagesList.append(NewMsg); // We process the received message qDebug("Type: 0x%X ",CANMsg.MSGTYPE); qDebug("ID: 0x%X",CANMsg.ID); qDebug("Length: %d",CANMsg.LEN); qDebug("Time: micros %d - millis %d - overflow %d", CANTimeStamp.micros, CANTimeStamp.millis, CANTimeStamp.millis_overflow); // qDebug("Data: " << GetDataString(CANMsg.DATA, CANMsg.MSGTYPE, GetLengthFromDLC(CANMsg.DLC)) << "\n"; qDebug("Count: %d",cnt++); qDebug("----------------------------------------------------------"); } } while (!(stsResult & PCAN_ERROR_QRCVEMPTY)); return MessagesList; }