From cb4445dabc595ee0fcf4468ab1282558ba677e52 Mon Sep 17 00:00:00 2001 From: Georgi Tushev Date: Fri, 4 Jan 2019 15:54:45 +0100 Subject: [PATCH] add serialpackage --- BrainATUMtome/microtome.cpp | 3 +- BrainATUMtome/microtome.h | 1 - BrainATUMtome/serialpackage.cpp | 139 ++++++++++++++------------------ BrainATUMtome/serialpackage.h | 52 +++--------- 4 files changed, 74 insertions(+), 121 deletions(-) diff --git a/BrainATUMtome/microtome.cpp b/BrainATUMtome/microtome.cpp index 2b121ea..5feb039 100644 --- a/BrainATUMtome/microtome.cpp +++ b/BrainATUMtome/microtome.cpp @@ -3,8 +3,7 @@ Microtome::Microtome(QObject *parent) : QObject(parent), m_serialPort(nullptr) { - m_serialPackage = new SerialPackage(this); - m_serialPackage->test(); + } diff --git a/BrainATUMtome/microtome.h b/BrainATUMtome/microtome.h index 7ca8bab..b3375f8 100644 --- a/BrainATUMtome/microtome.h +++ b/BrainATUMtome/microtome.h @@ -19,7 +19,6 @@ class Microtome : public QObject private: SerialPort *m_serialPort; - SerialPackage *m_serialPackage; public slots: void on_uirequest_cuttingMotor(bool state); diff --git a/BrainATUMtome/serialpackage.cpp b/BrainATUMtome/serialpackage.cpp index 571acee..713d701 100644 --- a/BrainATUMtome/serialpackage.cpp +++ b/BrainATUMtome/serialpackage.cpp @@ -1,132 +1,113 @@ #include "serialpackage.h" -SerialPackage::SerialPackage(QObject *parent) : QObject(parent) +SerialPackage::SerialPackage() { } -SerialPackage::~SerialPackage() + +bool SerialPackage::parse(QString &message) { + // clean message + message = message.simplified(); -} + if (message.at(0) == '!') + message.remove(0, 1); + // check for even size + if ((message.size() % 2) != 0) + return false; -void SerialPackage::test() -{ - setAddress(0xCA); - setCommand(0xFC); + // get received checksum + quint8 checksumReceived = static_cast(QString("%1%2").arg(message.mid(message.size() - 2, 1), message.mid(message.size() - 1, 1)).toUInt(nullptr, 16)); + message.remove(message.size() - 2, 2); - qDebug() << m_buffer; - qDebug() << address(); - qDebug() << command(); - qDebug() << checksum(); + return (checksumReceived == checksum(message)); } -quint8 SerialPackage::address() +quint8 SerialPackage::address(const QString &message) { - return unpack(0); + quint8 value; + if (unpack(value, message.mid(0, 2))) { + return value; + } + else { + return 0; + } } -quint8 SerialPackage::command() + +quint8 SerialPackage::command(const QString &message) { - return unpack(2); + quint8 value; + if (unpack(value, message.mid(2, 2))) { + return value; + } + else { + return 0; + } } -quint8 SerialPackage::checksum() + +quint8 SerialPackage::checksum(const QString &message) { - return unpack(m_buffer.size() - 2); -} + quint8 valueChecksum = 0; + for (int i = 0; i < message.size(); i += 2) { + quint8 value = 0; + unpack(value, message.mid(i, 2)); + valueChecksum += value; + } + valueChecksum = ~valueChecksum + 1; -void SerialPackage::setAddress(quint8 value) -{ - pack(value, 0); + return valueChecksum; } -void SerialPackage::setCommand(quint8 value) +QString SerialPackage::pack(quint8 value) { - pack(value, 2); + return QString("%1").arg(value, 2, 16, QChar('0')).toUpper(); } - -void SerialPackage::unpack(quint8 &value, int index) +QString SerialPackage::pack(quint16 value) { - value = static_cast(QString("%1%2").arg(m_buffer.mid(index, 1), m_buffer.mid(index + 1, 1)).toUInt(nullptr, 16)); + return QString("%1").arg(value, 4, 16, QChar('0')).toUpper(); } - - -void SerialPackage::pack(quint8 value, int index) +QString SerialPackage::pack(quint32 value) { - QString textValue = QString("%1").arg(value, 2, 16, QChar('0')).toUpper(); - m_buffer.replace(index, 2, textValue); + return QString("%1").arg(value, 8, 16, QChar('0')).toUpper(); } -void SerialPackage::pack(quint16 value, int index) +bool SerialPackage::unpack(quint8 &value, const QString &textValue) { - QString textValue = QString("%1").arg(value, 4, 16, QChar('0')).toUpper(); - m_buffer.replace(index, 4, textValue); + bool ok; + value = static_cast(textValue.toUInt(&ok, 16)); + return ok; } -void SerialPackage::pack(quint32 value, int index) +bool SerialPackage::unpack(quint16 &value, const QString &textValue) { - QString textValue = QString("%1").arg(value, 8, 16, QChar('0')).toUpper(); - m_buffer.replace(index, 8, textValue); + bool ok; + value = static_cast(textValue.toUInt(&ok, 16)); + return ok; } - - -bool SerialPackage::parse(const QByteArray &package) +bool SerialPackage::unpack(quint32 &value, const QString &textValue) { - // clean message - QString message(package); - - message = message.simplified(); - - if (message.at(0) == '!') - message.remove(0, 1); - - - - // check package size - if ((message.size() < SIZE_PACKAGE_MINIMUM) || (message.size() >= SIZE_PACKAGE_BUFFER)) - { - emit log(EventLogType::Warn, - "SerialPackage:: package size " + - QString::number(message.size()) + - " is outside package range [ " + - QString::number(SIZE_PACKAGE_MINIMUM) + - ", " + - QString::number(SIZE_PACKAGE_BUFFER) + - ") bytes."); - return false; - } - - // check for even size - if ((message.size() % 2) != 0) - { - emit log(EventLogType::Warn, - "SerialPackage:: package size " + - QString::number(message.size()) + - " is not even. Package is truncated."); - return false; - } + bool ok; + value = static_cast(textValue.toUInt(&ok, 16)); + return ok; +} - // get received checksum - quint8 checksumReceived = static_cast(QString("%1%2").arg(message.mid(message.size() - 2, 1), m_buffer.mid(message.size() - 1, 1)).toUInt(nullptr, 16)); - message.remove(message.size() - 2, 2); - m_buffer = message; - return (checksumReceived == checksum()); -} diff --git a/BrainATUMtome/serialpackage.h b/BrainATUMtome/serialpackage.h index 8695338..1966394 100644 --- a/BrainATUMtome/serialpackage.h +++ b/BrainATUMtome/serialpackage.h @@ -1,53 +1,27 @@ #ifndef SERIALPACKAGE_H #define SERIALPACKAGE_H -#include +#include #include -#include "eventlog.h" - -class SerialPackage : public QObject +class SerialPackage { - Q_OBJECT - public: - explicit SerialPackage(QObject *parent = nullptr); - ~SerialPackage(); - - bool parse(const QByteArray &package); - - void test(); - quint8 address(); - quint8 command(); - quint8 checksum(); - - void setAddress(quint8 value); - void setCommand(quint8 value); - - void push(quint8 value); - void push(quint16 value); - void push(quint32 value); - - void pop(quint8 &value); - void pop(quint16 &value); - void pop(quint32 &value); - -private: - QString m_buffer; + explicit SerialPackage(); - void unpack(quint8 &value, int index); - void unpack(quint16 &value, int index); - void unpack(quint32 &value, int index); - void pack(quint8 value, int index); - void pack(quint16 value, int index); - void pack(quint32 value, int index); + static bool parse(QString &message); - static const qint8 SIZE_PACKAGE_MINIMUM = 6; - static const qint8 SIZE_PACKAGE_BUFFER = 64; + static quint8 address(const QString &message); + static quint8 command(const QString &message); + static quint8 checksum(const QString &message); -signals: - void log(EventLogType type, const QString &message); + static QString pack(quint8 value); + static QString pack(quint16 value); + static QString pack(quint32 value); + static bool unpack(quint8 &value, const QString &textValue); + static bool unpack(quint16 &value, const QString &textValue); + static bool unpack(quint32 &value, const QString &textValue); }; #endif // SERIALPACKAGE_H