diff --git a/BrainATUMtome/driverepos.cpp b/BrainATUMtome/driverepos.cpp index a7b1198..268b17a 100644 --- a/BrainATUMtome/driverepos.cpp +++ b/BrainATUMtome/driverepos.cpp @@ -1,14 +1,16 @@ #include "driverepos.h" DriverEpos::DriverEpos(QObject *parent) : QObject(parent), - m_keyHandle_lower(0), - m_keyHandle_upper(0), + m_keyHandle_lower(nullptr), + m_keyHandle_upper(nullptr), m_nodeId(1), m_errorCode(0) { // open motor interfaces - m_keyHandle_lower = open("USB0"); - m_keyHandle_upper = open("USB1"); + char portLower[] = "USB0"; + char portUpper[] = "USB1"; + m_keyHandle_lower = open(portLower); + m_keyHandle_upper = open(portUpper); // set velocity mode setVelocityMode(m_keyHandle_lower); @@ -18,13 +20,10 @@ DriverEpos::DriverEpos(QObject *parent) : QObject(parent), DriverEpos::~DriverEpos() { close(m_keyHandle_lower); - delete m_keyHandle_lower; - close(m_keyHandle_upper); - delete m_keyHandle_upper; } -HANDLE DriverEpos::open(const char *portName) +HANDLE DriverEpos::open(char *portName) { char deviceName[] = "EPOS2"; @@ -34,32 +33,32 @@ HANDLE DriverEpos::open(const char *portName) HANDLE m_keyHandle = VCS_OpenDevice(deviceName, protocolStackName, interfaceName, - (char *)portName, + portName, &m_errorCode); if (!m_keyHandle) { - throwError("VCS_OpenDevice"); - return 0; + throwError(QString("%1 %2").arg("failed to open device on port", portName)); + return nullptr; } - qDebug() << "DriverMaxonMotors:: device is open on port " << portName; + emit error("DriverEpos", QString("%1 %2").arg("device is open on port", portName)); return m_keyHandle; } void DriverEpos::close(HANDLE m_keyHandle) { - if (m_keyHandle == 0) + if (m_keyHandle == nullptr) return; if (!VCS_CloseDevice(m_keyHandle, &m_errorCode)) { - throwError("VCS_CloseDevice"); + throwError("failed to close device"); return; } - m_keyHandle = 0; + m_keyHandle = nullptr; } void DriverEpos::clear(HANDLE m_keyHandle) @@ -233,20 +232,17 @@ void DriverEpos::setLedRed(BOOL state) } -void DriverEpos::throwError(QString message) +void DriverEpos::throwError(const QString &errorMessage) { const WORD errorInfoSize = 256; char errorInfo[errorInfoSize]; - if (!VCS_GetErrorInfo(m_errorCode, errorInfo, errorInfoSize)) { - strncpy(errorInfo, "failed to read error information", errorInfoSize); + strncpy_s(errorInfo, "failed to read error information", errorInfoSize); } - qDebug() << "DriverMaxonMotors::Error, " << message; - qDebug() << "\terror code = " << QString::number(m_errorCode, 16); - qDebug() << "\terror info = " << errorInfo; + emit error("DriverEpos", QString("%1 %2 %3").arg(errorMessage, QString::number(m_errorCode, 16), errorInfo)); // led indicator setLedGreen(FALSE); diff --git a/BrainATUMtome/driverepos.h b/BrainATUMtome/driverepos.h index a346c06..a78608d 100644 --- a/BrainATUMtome/driverepos.h +++ b/BrainATUMtome/driverepos.h @@ -2,7 +2,6 @@ #define DRIVEREPOS_H #include -#include #include "Definitions.h" @@ -20,7 +19,7 @@ class DriverEpos : public QObject WORD m_nodeId; DWORD m_errorCode; - HANDLE open(const char *portName); + HANDLE open(char *portName); void close(HANDLE m_keyHandle); void clear(HANDLE m_keyHandle); void enable(HANDLE m_keyHandle); @@ -30,7 +29,6 @@ class DriverEpos : public QObject void stop(HANDLE m_keyHandle); void setDOPin(HANDLE m_keyHandle, WORD digitalOutputNb, WORD configuration, BOOL state); - void throwError(QString messages); void setLedRed(BOOL state); void setLedGreen(BOOL state); void setSound(BOOL state); @@ -43,9 +41,14 @@ class DriverEpos : public QObject MOTORS_BOTH = 0x11 }; + void throwError(const QString &errorMessage); + public slots: void on_uirequest_run(quint8 motor, qint16 velocity, bool direction, bool tension); +signals: + void error(const QString &errorSource, const QString &errorMessage); + }; #endif // DRIVEREPOS_H diff --git a/BrainATUMtome/drivermicrotome.cpp b/BrainATUMtome/drivermicrotome.cpp index 37427e1..5db98d7 100644 --- a/BrainATUMtome/drivermicrotome.cpp +++ b/BrainATUMtome/drivermicrotome.cpp @@ -5,10 +5,15 @@ DriverMicrotome::DriverMicrotome(QObject *parent) : QObject(parent), m_sections(0) { // configure serial communication - m_dsp = new DriverSerialPort("DriverMicrotome", "COM2", QSerialPort::Baud19200, this); - connect(this, &DriverMicrotome::error, m_dsp, &DriverSerialPort::error); - connect(this, &DriverMicrotome::send, m_dsp, &DriverSerialPort::on_bufferSchedule); - connect(m_dsp, &DriverSerialPort::received, this, &DriverMicrotome::on_respond); + m_driverSerialPort = new DriverSerialPort("DriverMicrotome", "COM2", QSerialPort::Baud19200, this); + connect(this, &DriverMicrotome::send, m_driverSerialPort, &DriverSerialPort::on_bufferSchedule); + connect(m_driverSerialPort, &DriverSerialPort::received, this, &DriverMicrotome::on_respond); + connect(m_driverSerialPort, &DriverSerialPort::error, + [this](const QString &errorSource, const QString &errorMessage) + { + QString errorParent = QString("%1::%2").arg("DriverSyringe", errorSource); + emit error(errorParent, errorMessage); + }); // configure command set m_parser = new QHash(); @@ -61,9 +66,9 @@ QString DriverMicrotome::parserCallback_GetPartId(QString respond) QString DriverMicrotome::parserCallback_GetVersion(QString respond) { - quint8 revisionHardware = respond.mid(5, 2).toUInt(NULL, 16); - quint8 revisionMajor = respond.mid(7, 2).toUInt(NULL, 16); - quint8 revisionMinor = respond.mid(9, 2).toUInt(NULL, 16); + quint8 revisionHardware = static_cast(respond.mid(5, 2).toUInt(nullptr, 16)); + quint8 revisionMajor = static_cast(respond.mid(7, 2).toUInt(nullptr, 16)); + quint8 revisionMinor = static_cast(respond.mid(9, 2).toUInt(nullptr, 16)); return QString("%1\n\t%2 %3\n\t%4 %5\n\t%6 %7").arg("Controller Version", "Hardware Revision", QString::number(revisionHardware), "Major Revision", QString::number(revisionMajor), @@ -74,7 +79,7 @@ QString DriverMicrotome::parserCallback_GetVersion(QString respond) QString DriverMicrotome::parserCallback_CommandTransmissionError(QString respond) { - quint8 code = respond.mid(5, 2).toUInt(NULL, 16); + quint8 code = static_cast(respond.mid(5, 2).toUInt(nullptr, 16)); QString log = "Command Transmission Error"; switch (code) { @@ -105,9 +110,9 @@ QString DriverMicrotome::parserCallback_CommandTransmissionError(QString respond QString DriverMicrotome::parserCallback_FeedrateMotorControl(QString respond) { static const quint32 positionMax = 200000; - quint32 position = respond.mid(7, respond.size() - 9).toUInt(NULL, 16); + quint32 position = respond.mid(7, respond.size() - 9).toUInt(nullptr, 16); quint32 positionLeft = (positionMax > position) ? (positionMax - position) : 0; - float ratioLeft = (positionMax > position) ? (100.0 * (positionMax - position)) / positionMax : 0.0; + float ratioLeft = (positionMax > position) ? static_cast((100.0 * (positionMax - position)) / positionMax) : 0.0; emit advance(positionLeft, ratioLeft); return QString("%1 %2").arg("Feedrate Position", QString::number(position)); } @@ -116,8 +121,8 @@ QString DriverMicrotome::parserCallback_FeedrateMotorControl(QString respond) QString DriverMicrotome::parserCallback_Feed(QString respond) { - quint8 code = respond.mid(5, 2).toUInt(NULL, 16); - quint16 value = respond.mid(7, 4).toUInt(NULL, 16); + quint8 code = static_cast(respond.mid(5, 2).toUInt(nullptr, 16)); + quint16 value = static_cast(respond.mid(7, 4).toUInt(nullptr, 16)); QString status = (code == 0x01) ? "Set" : "Get"; emit feedStep(value); @@ -129,8 +134,8 @@ QString DriverMicrotome::parserCallback_Feed(QString respond) QString DriverMicrotome::parserCallback_CuttingMotorControl(QString respond) { - quint8 code = respond.mid(5, 2).toUInt(NULL, 16); - quint8 value = respond.mid(7, 2).toUInt(NULL, 16); + quint8 code = static_cast(respond.mid(5, 2).toUInt(nullptr, 16)); + quint8 value = static_cast(respond.mid(7, 2).toUInt(nullptr, 16)); QString log = "Cutting Motor Control"; QString status = (code == 0xFF) ? "Get" : "Set"; QString state = "invalid status"; @@ -159,8 +164,8 @@ QString DriverMicrotome::parserCallback_CuttingMotorControl(QString respond) QString DriverMicrotome::parserCallback_CuttingSpeed(QString respond) { - quint8 code = respond.mid(5, 2).toUInt(NULL, 16); - quint16 value = respond.mid(7, 4).toUInt(NULL, 16); + quint8 code = static_cast(respond.mid(5, 2).toUInt(nullptr, 16)); + quint16 value = static_cast(respond.mid(7, 4).toUInt(nullptr, 16)); QString status = (code == 0x01) ? "Set" : "Get"; emit cuttingSpeed(static_cast(value * 10)); @@ -172,8 +177,8 @@ QString DriverMicrotome::parserCallback_CuttingSpeed(QString respond) QString DriverMicrotome::parserCallback_ReturnSpeed(QString respond) { - quint8 code = respond.mid(5, 2).toUInt(NULL, 16); - quint16 value = respond.mid(7, 4).toUInt(NULL, 16); + quint8 code = static_cast(respond.mid(5, 2).toUInt(nullptr, 16)); + quint16 value = static_cast(respond.mid(7, 4).toUInt(nullptr, 16)); QString status = (code == 0x01) ? "Set" : "Get"; emit returnSpeed(static_cast(value * 10)); @@ -184,7 +189,7 @@ QString DriverMicrotome::parserCallback_ReturnSpeed(QString respond) QString DriverMicrotome::parserCallback_HandwheelPosition(QString respond) { - quint8 code = respond.mid(7, 2).toUInt(NULL, 16); + quint8 code = static_cast(respond.mid(7, 2).toUInt(nullptr, 16)); QString log = "Handwheel Position"; switch (code) { @@ -237,14 +242,14 @@ QByteArray DriverMicrotome::packCommand(quint8 EA, quint8 CC, quint8 status, qui void DriverMicrotome::verifyCheckSum(QString respond) { QString checksum_received = respond.mid(respond.size() - 2, 2); - quint8 checksum_value_received = checksum_received.toUInt(NULL, 16); + quint8 checksum_value_received = static_cast(checksum_received.toUInt(nullptr, 16)); QString checksum_expected = calculateCheckSum(respond.mid(1, respond.size() - 3)); - quint8 checksum_value_expected = checksum_expected.toUInt(NULL, 16); + quint8 checksum_value_expected = static_cast(checksum_expected.toUInt(nullptr, 16)); if (checksum_value_received != checksum_value_expected) { - emit error("VerifyCheckSum::wrong check sum of the respond"); + emit error("DriverMicrotome","VerifyCheckSum::wrong check sum of the respond"); } } @@ -254,14 +259,14 @@ void DriverMicrotome::verifyCheckSum(QString respond) QString DriverMicrotome::calculateCheckSum(QString package) { // command components to 16 bit values - quint16 EA = package.mid(0, 2).toUInt(NULL, 16); - quint16 CC = package.mid(2, 2).toUInt(NULL, 16); + quint16 EA = static_cast(package.mid(0, 2).toUInt(nullptr, 16)); + quint16 CC = static_cast(package.mid(2, 2).toUInt(nullptr, 16)); quint16 DD = 0; if (package.size() > 4) { for (int i = 4; i < package.size(); i += 2) { - DD += package.mid(i, 2).toUInt(NULL, 16); + DD += static_cast(package.mid(i, 2).toUInt(nullptr, 16)); } } @@ -290,8 +295,8 @@ void DriverMicrotome::on_respond(const QByteArray &package) verifyCheckSum(respond); // parse based on current address and command - quint8 EA = QString("%2%1").arg(respond.mid(1, 1), respond.mid(2, 1)).toUInt(NULL, 16); - quint8 CC = respond.mid(3, 2).toUInt(NULL, 16); + quint8 EA = static_cast(QString("%2%1").arg(respond.mid(1, 1), respond.mid(2, 1)).toUInt(nullptr, 16)); + quint8 CC = static_cast(respond.mid(3, 2).toUInt(nullptr, 16)); quint16 parserKey = calculateParserKey(EA, CC); if (m_parser->contains(parserKey)) @@ -299,7 +304,7 @@ void DriverMicrotome::on_respond(const QByteArray &package) QString log = ""; ParserCallback callback = m_parser->value(parserKey); - if (callback != NULL) + if (callback != nullptr) log = (this->*callback)(respond); if (!log.isEmpty()) @@ -310,7 +315,7 @@ void DriverMicrotome::on_respond(const QByteArray &package) QString errorMessage = QString("%1\n\t%2 %3\n\t%4 %5").arg("unknown parser", "address", QString::number(EA, 16), "command", QString::number(CC, 16)); - emit error(errorMessage); + emit error("DriverMicrotome", errorMessage); } } diff --git a/BrainATUMtome/drivermicrotome.h b/BrainATUMtome/drivermicrotome.h index 116c4e0..5af38f2 100644 --- a/BrainATUMtome/drivermicrotome.h +++ b/BrainATUMtome/drivermicrotome.h @@ -17,7 +17,7 @@ class DriverMicrotome : public QObject private: bool m_cuttingMotor_requestStop; quint32 m_sections; - DriverSerialPort *m_dsp; + DriverSerialPort *m_driverSerialPort; enum CommandSet : quint8 { @@ -66,7 +66,7 @@ public slots: void on_uirequest_setFeed(quint16 value); signals: - void error(const QString &errorMessage); + void error(const QString &errorSource, const QString &errorMessage); void send(const QByteArray &package); void sections(quint32 sectionsCount); diff --git a/BrainATUMtome/driverserialport.cpp b/BrainATUMtome/driverserialport.cpp index be85d33..b6401a6 100644 --- a/BrainATUMtome/driverserialport.cpp +++ b/BrainATUMtome/driverserialport.cpp @@ -48,14 +48,13 @@ DriverSerialPort::DriverSerialPort(const QString &deviceName, const QString &por // configure callbacks connect(m_serialPort, &QSerialPort::readyRead, this, &DriverSerialPort::on_bufferReading); connect(state_sender, &QState::entered, this, &DriverSerialPort::on_bufferWriting); - connect(this, &DriverSerialPort::error, this, &DriverSerialPort::on_raiseError); - connect(m_respondTimer, &QTimer::timeout, [this](){emit error("respond timeout");}); + connect(m_respondTimer, &QTimer::timeout, [this](){emit error("DriverSerialPort", "respond timeout");}); connect(m_handshakeTimer, &QTimer::timeout, [this](){emit command();}); // check if port is open if (!m_serialPort->isOpen()) { - emit error("open Serial Port failed"); + emit error("DriverSerialPort","open Serial Port failed"); } else { @@ -128,15 +127,15 @@ void DriverSerialPort::on_bufferWriting() if (bytesWritten == -1) { - emit error("SerialPort::Write, failed to write data to serial port"); + emit error("DriverSerialPort","failed to write data to serial port"); } else if (bytesWritten != bufferWrite.size()) { - emit error("SerialPort::Write, failed to write full buffer to serial port"); + emit error("DriverSerialPort","failed to write full buffer to serial port"); } else { - qDebug() << "DriverSerialPort::Command:: " << QString(bufferWrite); + emit error("DriverSerialPort", QString("%1::%2").arg("command", QString(bufferWrite))); m_respondTimer->start(); emit sent(); } @@ -144,15 +143,6 @@ void DriverSerialPort::on_bufferWriting() } -void DriverSerialPort::on_raiseError(const QString &errorMessage) -{ - QString logMessage = QString("%1::%2, %3").arg(m_deviceName, "Error", errorMessage); - qDebug("%s", logMessage.toStdString().c_str()); - //QCoreApplication::exit(1); -} - - - void DriverSerialPort::on_bufferSchedule(const QByteArray &package) { m_commandQueue->append(package); diff --git a/BrainATUMtome/driverserialport.h b/BrainATUMtome/driverserialport.h index 8365db3..d7f953f 100644 --- a/BrainATUMtome/driverserialport.h +++ b/BrainATUMtome/driverserialport.h @@ -5,7 +5,6 @@ #include #include #include -#include class DriverSerialPort : public QObject { @@ -30,10 +29,9 @@ public slots: private slots: void on_bufferReading(); void on_bufferWriting(); - void on_raiseError(const QString &errorMessage); signals: - void error(const QString &errorMessage); + void error(const QString &errorSource, const QString &errorMessage); void listen(); void command(); void sent(); diff --git a/BrainATUMtome/driversyringe.cpp b/BrainATUMtome/driversyringe.cpp index b0581ce..5798b12 100644 --- a/BrainATUMtome/driversyringe.cpp +++ b/BrainATUMtome/driversyringe.cpp @@ -3,10 +3,15 @@ DriverSyringe::DriverSyringe(QObject *parent) : QObject(parent) { // configure serial communication - m_dsp = new DriverSerialPort("DriverSyringe", "COM6", QSerialPort::Baud9600, this); - connect(this, &DriverSyringe::error, m_dsp, &DriverSerialPort::error); - connect(this, &DriverSyringe::send, m_dsp, &DriverSerialPort::on_bufferSchedule); - connect(m_dsp, &DriverSerialPort::received, this, &DriverSyringe::on_respond); + m_driverSerialPort = new DriverSerialPort("DriverSyringe", "COM6", QSerialPort::Baud9600, this); + connect(this, &DriverSyringe::send, m_driverSerialPort, &DriverSerialPort::on_bufferSchedule); + connect(m_driverSerialPort, &DriverSerialPort::received, this, &DriverSyringe::on_respond); + connect(m_driverSerialPort, &DriverSerialPort::error, + [this](const QString &errorSource, const QString &errorMessage) + { + QString errorParent = QString("%1::%2").arg("DriverSyringe", errorSource); + emit error(errorParent, errorMessage); + }); // set command set m_commandSet = new QVector(); diff --git a/BrainATUMtome/driversyringe.h b/BrainATUMtome/driversyringe.h index b1bbce8..552a4d4 100644 --- a/BrainATUMtome/driversyringe.h +++ b/BrainATUMtome/driversyringe.h @@ -2,6 +2,7 @@ #define DRIVERSYRINGE_H #include +#include #include "driverserialport.h" @@ -14,7 +15,7 @@ class DriverSyringe : public QObject ~DriverSyringe(); private: - DriverSerialPort *m_dsp; + DriverSerialPort *m_driverSerialPort; enum CommandIndex : quint8 { @@ -32,7 +33,7 @@ public slots: void on_respond(const QByteArray &package); signals: - void error(const QString &errorMessage); + void error(const QString &errorSource, const QString &errorMessage); void send(const QByteArray &package); }; diff --git a/BrainATUMtome/mainwindow.cpp b/BrainATUMtome/mainwindow.cpp index d0a563c..1a039d4 100644 --- a/BrainATUMtome/mainwindow.cpp +++ b/BrainATUMtome/mainwindow.cpp @@ -13,6 +13,8 @@ MainWindow::MainWindow(QWidget *parent) : m_driverEpos->moveToThread(m_thread_driverEpos); connect(m_thread_driverEpos, &QThread::finished, m_driverEpos, &DriverEpos::deleteLater); m_thread_driverEpos->start(); + + connect(m_driverEpos, &DriverEpos::error, this, &MainWindow::on_error_throw); connect(this, &MainWindow::uirequest_dep_run, m_driverEpos, &DriverEpos::on_uirequest_run); QIntValidator *validator_velocity = new QIntValidator(0, 15000, this); ui->lineEdit_dep_velocity->setValidator(validator_velocity); @@ -45,6 +47,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_thread_driverMicrotome, &QThread::finished, m_driverMicrotome, &DriverMicrotome::deleteLater); m_thread_driverMicrotome->start(); + connect(m_driverMicrotome, &DriverMicrotome::error, this, &MainWindow::on_error_throw); connect(this, &MainWindow::uirequest_dmt_cuttingMotroOff, m_driverMicrotome, &DriverMicrotome::on_uirequest_cuttingMotorOff); connect(this, &MainWindow::uirequest_dmt_cuttingMotorOn, m_driverMicrotome, &DriverMicrotome::on_uirequest_cuttingMotorOn); connect(this, &MainWindow::uirequest_dmt_setCuttingSpeed, m_driverMicrotome, &DriverMicrotome::on_uirequest_setCuttingSpeed); @@ -105,6 +108,8 @@ MainWindow::MainWindow(QWidget *parent) : m_driverSyringe->moveToThread(m_thread_driverSyringe); connect(m_thread_driverSyringe, &QThread::finished, m_driverSyringe, &DriverSyringe::deleteLater); m_thread_driverSyringe->start(); + + connect(m_driverSyringe, &DriverSyringe::error, this, &MainWindow::on_error_throw); connect(ui->pushButton_dsy_pump, &QPushButton::clicked, m_driverSyringe, &DriverSyringe::on_uirequest_pump); @@ -115,7 +120,6 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_thread_videoProducer, &QThread::finished, m_videoProducer, &VideoProducer::deleteLater); m_thread_videoProducer->start(); - /* --- configure VideoConverter --- */ m_videoConverter = new VideoConverter(nullptr); m_thread_videoConverter = new QThread(this); @@ -135,9 +139,8 @@ MainWindow::MainWindow(QWidget *parent) : connect(ui->pushButton_dvs_snap, &QPushButton::clicked, m_videoProducer, &VideoProducer::stop); connect(m_videoProducer, &VideoProducer::frameReady, m_videoConverter, &VideoConverter::on_convertFrame); connect(m_videoConverter, &VideoConverter::pixmapReady, ui->widget_videoViewer, &VideoViewer::on_pixmapReady); - - //connect(m_videoProducer, &VideoProducer::error, this, &MainWindow::on_error_throw); - //connect(m_videoWriter, &VideoWriter::error, this, &MainWindow::on_error_throw); + connect(m_videoProducer, &VideoProducer::error, this, &MainWindow::on_error_throw); + connect(m_videoWriter, &VideoWriter::error, this, &MainWindow::on_error_throw); } diff --git a/BrainATUMtome/videoproducer.h b/BrainATUMtome/videoproducer.h index dd24bf3..a473ca8 100644 --- a/BrainATUMtome/videoproducer.h +++ b/BrainATUMtome/videoproducer.h @@ -5,7 +5,6 @@ #include #include #include -#include class VideoProducer : public QObject { diff --git a/BrainATUMtome/videowriter.h b/BrainATUMtome/videowriter.h index 79d3b13..d6e9dcb 100644 --- a/BrainATUMtome/videowriter.h +++ b/BrainATUMtome/videowriter.h @@ -4,8 +4,8 @@ #include #include #include -#include #include +#include class VideoWriter : public QObject {