diff --git a/src/core/atcore.h b/src/core/atcore.h --- a/src/core/atcore.h +++ b/src/core/atcore.h @@ -1,5 +1,5 @@ /* AtCore - Copyright (C) <2016> + Copyright (C) <2016 - 2018> Authors: Tomaz Canabrava @@ -26,6 +26,7 @@ #pragma once #include +#include #include #include "ifirmware.h" @@ -249,6 +250,7 @@ * - Waiting for firmware detect. * - No Plugin found for (detected FW) * - Failed to open device in Read / Write mode. + * - Device Errors. * @param msg: the message. */ void atcoreMessage(const QString &msg); @@ -504,6 +506,11 @@ */ void getSDFileList(); + /** + * @brief Handle serial Errors. + */ + void handleSerialError(QSerialPort::SerialPortError error); + private: /** * @brief True if a firmware plugin is loaded diff --git a/src/core/atcore.cpp b/src/core/atcore.cpp --- a/src/core/atcore.cpp +++ b/src/core/atcore.cpp @@ -1,5 +1,5 @@ /* AtCore - Copyright (C) <2016> + Copyright (C) <2016 - 2018> Authors: Tomaz Canabrava @@ -218,6 +218,7 @@ bool AtCore::initSerial(const QString &port, int baud) { d->serial = new SerialLayer(port, baud); + connect(serial(), &SerialLayer::serialError, this, &AtCore::handleSerialError); if (serialInitialized() && d->serial->isWritable()) { setState(AtCore::CONNECTING); connect(serial(), &SerialLayer::receivedCommand, this, &AtCore::findFirmware); @@ -395,6 +396,7 @@ QString msg = d->pluginLoader.unload() ? QStringLiteral("closed.") : QStringLiteral("Failed to close."); qCDebug(ATCORE_CORE) << QStringLiteral("Firmware plugin %1 %2").arg(name, msg); } + disconnect(serial(), &SerialLayer::serialError, this, &AtCore::handleSerialError); serial()->close(); //Clear our copy of the sdcard filelist clearSdCardFileList(); @@ -770,3 +772,43 @@ } pushCommand(GCode::toCommand(GCode::M27)); } + +void AtCore::handleSerialError(QSerialPort::SerialPortError error) +{ + QString errorString; + + switch (error) { + case (QSerialPort::DeviceNotFoundError): + errorString = tr("Device not found"); + break; + case (QSerialPort::WriteError): + errorString = tr("Unable to write to device"); + break; + case (QSerialPort::ReadError): + errorString = tr("Unable to read from device"); + break; + case (QSerialPort::ResourceError): + case (QSerialPort::TimeoutError): + errorString = tr("The device no longer available"); + closeConnection(); + break; + case (QSerialPort::UnsupportedOperationError): + errorString = tr("Device does not support opperation"); + break; + case (QSerialPort::UnknownError): + errorString = tr("Unknown Error"); + break; + default: + //Not Directly processed errors + //QSerialPort::NoError, No error has happened + //QSerialPort::PermissionError), Already handled. + //QSerialPort::OpenError), Already handled. + //QSerialPort::NotOpenError, SerialLayer destroyed if not connected. + //QSerialPort::ParityError, Obsolete. Qt Docs "We strongly advise against using it in new code." + //QSerialPort::FramingError, Obsolete. Qt Docs "We strongly advise against using it in new code." + //QSerialPort::BreakConditionError, Obsolete. Qt Docs "We strongly advise against using it in new code." + return; + };//End of Switch + qCDebug(ATCORE_CORE) << "SerialError:" << errorString; + emit atcoreMessage(QStringLiteral("SerialError: %1").arg(errorString)); +} diff --git a/src/core/seriallayer.h b/src/core/seriallayer.h --- a/src/core/seriallayer.h +++ b/src/core/seriallayer.h @@ -1,5 +1,5 @@ /* AtCore - Copyright (C) <2016> + Copyright (C) <2016 - 2018> Authors: Patrick José Pereira @@ -61,6 +61,12 @@ * @param comm : Command */ void receivedCommand(const QByteArray &comm); + + /** + * @brief Emit a signal if an error has happened. + * @param error: the Error + */ + void serialError(QSerialPort::SerialPortError error); public: /** @@ -87,6 +93,12 @@ */ void add(const QByteArray &comm); + /** + * @brief handleError Handle Errors from the serial port + * @param error: The reported error + */ + void handleError(QSerialPort::SerialPortError error); + /** * @brief Push command directly * diff --git a/src/core/seriallayer.cpp b/src/core/seriallayer.cpp --- a/src/core/seriallayer.cpp +++ b/src/core/seriallayer.cpp @@ -1,5 +1,5 @@ /* AtCore - Copyright (C) <2016> + Copyright (C) <2016 - 2018> Authors: Patrick José Pereira @@ -57,6 +57,7 @@ { public: bool _serialOpened; //!< @param _serialOpened: is serial port opened + QSerialPort::SerialPortError _lastError; //!< @param _lastError: the last reported error QByteArray _rawData; //!< @param _rawData: the raw serial data QVector _rByteCommands; //!< @param _rByteCommand: received Messages QVector _sByteCommands; //!< @param _sByteCommand: sent Messages @@ -70,6 +71,7 @@ if (open(QIODevice::ReadWrite)) { d->_serialOpened = true; connect(this, &QSerialPort::readyRead, this, &SerialLayer::readAllData); + connect(this, &QSerialPort::errorOccurred, this, &SerialLayer::handleError); } }; @@ -145,3 +147,13 @@ { return _validBaudRates; } + +void SerialLayer::handleError(QSerialPort::SerialPortError error) +{ + if (d->_lastError == error) { + return; + } + + d->_lastError = error; + emit serialError(error); +} diff --git a/testclient/mainwindow.cpp b/testclient/mainwindow.cpp --- a/testclient/mainwindow.cpp +++ b/testclient/mainwindow.cpp @@ -1,5 +1,5 @@ /* AtCore Test Client - Copyright (C) <2016> + Copyright (C) <2016 - 2018> Authors: Patrick José Pereira @@ -416,7 +416,6 @@ if (core->initSerial(comboPort->currentText(), comboBAUD->currentText().toInt())) { connect(core, &AtCore::receivedMessage, logWidget, &LogWidget::appendRLog); connect(core->serial(), &SerialLayer::pushedCommand, logWidget, &LogWidget::appendSLog); - buttonConnect->setText(tr("Disconnect")); logWidget->appendLog(tr("Serial connected")); if (!comboPlugin->currentText().contains(tr("Autodetect"))) { core->loadFirmwarePlugin(comboPlugin->currentText()); @@ -428,7 +427,6 @@ core->closeConnection(); core->setState(AtCore::DISCONNECTED); logWidget->appendLog(tr("Disconnected")); - buttonConnect->setText(tr("Connect")); } } @@ -484,6 +482,7 @@ QString stateString; switch (state) { case AtCore::IDLE: + buttonConnect->setText(tr("Disconnect")); printWidget->setPrintText(tr("Print File")); stateString = tr("Connected to ") + core->connectedPort(); sdDock->setVisible(core->firmwarePlugin()->isSdSupported()); @@ -513,6 +512,7 @@ case AtCore::DISCONNECTED: stateString = QStringLiteral("Not Connected"); + buttonConnect->setText(tr("Connect")); setDangeriousDocksDisabled(true); break; @@ -566,5 +566,8 @@ if (!disabled) { temperatureWidget->updateExtruderCount(core->extruderCount()); printWidget->updateFanCount(fanCount); + } else { + printWidget->setPrintText(tr("Print File")); + statusWidget->showPrintArea(false); } }