YULTek/Otarcik_CAN/Sources/PCANInterface.cpp

148 lines
5.4 KiB
C++

/*******************************************************************************
* *
* 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)
{
//Init only... values not used.
mInterfaceBaudrate = PCAN_BAUD_500K;
mInterfaceCANHandle = PCAN_USBBUS1;
CANInterfaceOpened = false;
}
int CPCANInterface::GetDeviceHandle(quint8 DeviceID, TPCANHandle &Handle)
{
TPCANHandle TempHandle;
QString Parameters = QString("devicetype=pcan_usb, deviceid=%1").arg(DeviceID);
if(CAN_LookUpChannel(Parameters.toLocal8Bit().data(),&TempHandle) != PCAN_ERROR_OK || TempHandle == PCAN_NONEBUS)
{
Handle = 0;
return RET_GENERAL_ERROR;
}
Handle = TempHandle;
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Module PCAN trouvé à l'adresse %1 avec le handle 0x%2").arg(DeviceID).arg(Handle,0,16),"CPCANInterface");
return RET_OK;
}
int CPCANInterface::Init(TPCANHandle CANDeviceHandle, TPCANBaudrate CANBaudRate)
{
mInterfaceBaudrate = CANBaudRate;
mInterfaceCANHandle = CANDeviceHandle;
TPCANStatus Result;
Result = CAN_Initialize(CANDeviceHandle, CANBaudRate);
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Initialisation d'un module PCAN. Handle:0x%1, Baudrate:%2").arg(CANDeviceHandle,0,16).arg(CANBaudRate),"CPCANInterface");
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("Impossible d'initialiser un module USB PCAN. Erreur:%1").arg(strMsg),"CPCANInterface",true,1,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
CANInterfaceOpened = false;
return RET_GENERAL_ERROR;
}
else
{
CANInterfaceOpened = true;
CGeneralMessagesLogDispatcher::instance()->AddLogMessage("Module PCAN USB initialisé avec succès","CPCANInterface",true,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_SUCCESS_STATUS);
}
return RET_OK;
}
int CPCANInterface::DeInit()
{
TPCANStatus Result;
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Déinitialisation du module PCAN USB ID %1").arg(mInterfaceCANHandle),"CPCANInterface");
Result = CAN_Uninitialize(mInterfaceCANHandle) != PCAN_ERROR_OK;
if(Result)
{
char strMsg[256];
CAN_GetErrorText(Result, 0, strMsg);
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Impossible de déinitialiser un module USB PCAN.. Error:%1").arg(strMsg),"CPCANInterface",true,1,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
}
else
{
CGeneralMessagesLogDispatcher::instance()->AddLogMessage("Module PCAN USB déinitialisé avec succès...","CPCANInterface",true,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_SUCCESS_STATUS);
}
return RET_OK;
}
QList<CCANMessage *> CPCANInterface::ReadCANFullBuffer(unsigned short Channel)
{
TPCANMsg CANMsg;
TPCANTimestamp CANTimeStamp;
TPCANStatus stsResult;
//int cnt = 0;
QList<CCANMessage *> 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;
}
int CPCANInterface::SendCANMessage(unsigned short Channel,CCANMessage Msg)
{
TPCANMsg CANMsg;
TPCANStatus stsResult;
CANMsg.ID = Msg.mCANMsgID;
CANMsg.LEN = Msg.mCANMsgLength;
CANMsg.MSGTYPE = PCAN_MESSAGE_STANDARD;
for(int byte = 0; byte < 8; byte++)
{
CANMsg.DATA[byte] = Msg.mCANMsgData[byte];
}
stsResult = CAN_Write(Channel,&CANMsg);
return stsResult;
}