Index: sources/canbus/caninterface.cpp =================================================================== diff -u -rfeb3423b373dc2a2c4267ef9fcb4d924d738423d -re1605219ac2baf49ef21d0889f845ac53d59c3c1 --- sources/canbus/caninterface.cpp (.../caninterface.cpp) (revision feb3423b373dc2a2c4267ef9fcb4d924d738423d) +++ sources/canbus/caninterface.cpp (.../caninterface.cpp) (revision e1605219ac2baf49ef21d0889f845ac53d59c3c1) @@ -15,17 +15,16 @@ // Qt #include +#include // Project #include "logger.h" #include "frameinterface.h" +#include "messageglobals.h" // namespace using namespace Can; -// Singleton -SINGLETON_INIT(CanInterface) - /*! * \brief Caninterface Constructor * \param parent object @@ -39,32 +38,47 @@ */ bool CanInterface::init() { - QString mError; - _canDevice = QCanBus::instance()->createDevice(_canType, _canInterface, &mError); - if (!_canDevice) { - status(tr("Device Creation"), mError); - LOG_ERROR(status()); - return false; - } - _numberFramesWritten = 0; + // This is required for Signal/Slots in threading. + qRegisterMetaType("QCanBusFrame"); + // runs in main thread + Q_ASSERT_X(QThread::currentThread() == qApp->thread() , "_CanInterface::init", "The Class initialization must be done in Main Thread" ); + _CanFrame_Thread.setObjectName("CAN Frame Thread"); + connect(qApp, &QApplication::aboutToQuit, this, &CanInterface::quit); + _CanFrame_Thread.start(); + moveToThread(&_CanFrame_Thread); + + if ( ! createDevice() ) return false; + if ( ! checkDevice () ) return false; + + _numberFramesWritten = 0; initConnections(); - if (!_canDevice->connectDevice()) { - status(tr("Connection")); - LOG_ERROR(status()); - delete _canDevice; - _canDevice = nullptr; - return false; - } status(tr("Connected")); LOG_EVENT(QObject::tr("CanInterface Initialized")); LOG_EVENT(status()); return true; } /*! + * \brief CanInterface quit + * \details Quit the CANBUS Interface by disconnecting the bus and device. + */ +void CanInterface::quit() +{ + deleteDevice(); + + // runs in Logger thread + moveToThread(qApp->thread()); + + // runs in main thread + _CanFrame_Thread.quit(); + _CanFrame_Thread.wait(); + +} + +/*! * \brief CanInterface status * \details CanInterface status description * \return The current stores status @@ -92,11 +106,59 @@ this , SLOT (onFrameWrittern(qint64))); } - connect(_FrameInterface, SIGNAL(didFrameTransmit(QCanBusFrame)), - this , SLOT( onFrameTransmit(QCanBusFrame))); + connect(&_FrameInterface, SIGNAL(didFrameTransmit(QCanBusFrame)), + this , SLOT( onFrameTransmit(QCanBusFrame))); } /*! + * \brief CanInterface::createDevice + * \details Creates the CANBus device + * \return false if can't create the device + */ +bool CanInterface::createDevice() +{ + QString mError; + _canDevice = QCanBus::instance()->createDevice(_canType, _canInterface, &mError); + if (!_canDevice) { + status(tr("Device Creation"), mError); + LOG_ERROR(status()); + return false; + } + return true; +} + +/*! + * \brief CanInterface::checkDevice + * \details Checks if the device has been connected. + * \return + */ +bool CanInterface::checkDevice() +{ + if (!_canDevice->connectDevice()) { + status(tr("Connection")); + LOG_ERROR(status()); + delete _canDevice; + _canDevice = nullptr; + return false; + } + return true; +} + +/*! + * \brief CanInterface::deleteDevice + * \details Disconnect the CANBus device and deletes the pointer + */ +void CanInterface::deleteDevice() +{ + if (!_canDevice) + return; + _canDevice->disconnectDevice(); + delete _canDevice; + _canDevice = nullptr; + status(tr("Disconnected")); +} + +/*! * \brief CanInterface status * \details Sets the Can interface status description * \param vDescription - Description about the CANBUS Interface errors @@ -159,22 +221,6 @@ } /*! - * \brief CanInterface quit - * \details Quit the CANBUS Interface by disconnecting the bus and device. - */ -void CanInterface::quit() -{ - if (!_canDevice) - return; - - _canDevice->disconnectDevice(); - delete _canDevice; - _canDevice = nullptr; - - status(tr("Disconnected")); -} - -/*! * \brief frameFlags * \details CANBUS message frame type as flags * \param vFrame - CANBUS message frame @@ -237,10 +283,10 @@ { if (!_canDevice) return; - while (_canDevice->framesAvailable()) { - // qDebug() << "framesAvailable : " << _canDevice->framesAvailable(); + //int i = _canDevice->framesAvailable(); const QCanBusFrame frame = _canDevice->readFrame(); + //qDebug() << "frame: #" << i << frame.toString(); consoleOut(frame); emit didFrameReceive(frame); }