Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
add serialpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
MPIBR-tushevg committed Jan 4, 2019
1 parent defcc7c commit cb4445d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 121 deletions.
3 changes: 1 addition & 2 deletions BrainATUMtome/microtome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
Microtome::Microtome(QObject *parent) : QObject(parent),
m_serialPort(nullptr)
{
m_serialPackage = new SerialPackage(this);
m_serialPackage->test();

}


Expand Down
1 change: 0 additions & 1 deletion BrainATUMtome/microtome.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Microtome : public QObject

private:
SerialPort *m_serialPort;
SerialPackage *m_serialPackage;

public slots:
void on_uirequest_cuttingMotor(bool state);
Expand Down
139 changes: 60 additions & 79 deletions BrainATUMtome/serialpackage.cpp
Original file line number Diff line number Diff line change
@@ -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<quint8>(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<quint8>(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<quint8>(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<quint16>(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<quint32>(textValue.toUInt(&ok, 16));
return ok;
}

// get received checksum
quint8 checksumReceived = static_cast<quint8>(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());
}



Expand Down
52 changes: 13 additions & 39 deletions BrainATUMtome/serialpackage.h
Original file line number Diff line number Diff line change
@@ -1,53 +1,27 @@
#ifndef SERIALPACKAGE_H
#define SERIALPACKAGE_H

#include <QObject>
#include <QString>
#include <QDebug>

#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

0 comments on commit cb4445d

Please sign in to comment.