diff --git a/src/core/seriallayer.cpp b/src/core/seriallayer.cpp index 6e39fc3..ba4b6c0 100644 --- a/src/core/seriallayer.cpp +++ b/src/core/seriallayer.cpp @@ -1,164 +1,167 @@ /* AtCore Copyright (C) <2016 - 2018> Authors: Patrick José Pereira Chris Rizzitello Tomaz Canabrava This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include #include "seriallayer.h" Q_LOGGING_CATEGORY(SERIAL_LAYER, "org.kde.atelier.core.serialLayer") -namespace -{ -QByteArray _return = QByteArray("\r"); -QByteArray _newLine = QByteArray("\n"); -QByteArray _newLineReturn = QByteArray("\n\r"); -QStringList _validBaudRates = { +/** + * @brief The SerialLayerPrivate class + */ +struct SerialLayer::SerialLayerPrivate { + /** _lastError: the last reported error */ + QSerialPort::SerialPortError _lastError = QSerialPort::NoError; + /** New Line String */ + static const QByteArray _newLine; + /** New Line Return String */ + static const QByteArray _newLineReturn; + /** _rawData: the raw serial data */ + QByteArray _rawData; + /** _rByteCommand: received Messages */ + QVector _rByteCommands; + /** Return String */ + static const QByteArray _return; + /** _sByteCommand: sent Messages */ + QVector _sByteCommands; + /** _serialOpened: is Serial port opened */ + bool _serialOpened = false; + /** List of valid Baud Rates */ + static const QStringList _validBaudRates; +}; + +const QByteArray SerialLayer::SerialLayerPrivate::_newLine = QByteArray("\n"); +const QByteArray SerialLayer::SerialLayerPrivate::_newLineReturn = QByteArray("\n\r"); +const QByteArray SerialLayer::SerialLayerPrivate::_return = QByteArray("\r"); +const QStringList SerialLayer::SerialLayerPrivate::_validBaudRates = { QStringLiteral("9600"), QStringLiteral("14400"), QStringLiteral("19200"), QStringLiteral("28800"), QStringLiteral("38400"), QStringLiteral("57600"), QStringLiteral("76800"), QStringLiteral("115200"), QStringLiteral("230400"), QStringLiteral("250000"), QStringLiteral("500000"), QStringLiteral("1000000") }; -} - -/** - * @brief The SerialLayerPrivate class - */ -class SerialLayer::SerialLayerPrivate -{ -public: - /** _serialOpened: is Serial port opened */ - bool _serialOpened = false; - /** _lastError: the last reported error */ - QSerialPort::SerialPortError _lastError = QSerialPort::NoError; - /** _rawData: the raw serial data */ - QByteArray _rawData; - /** _rByteCommand: received Messages */ - QVector _rByteCommands; - /** _sByteCommand: sent Messages */ - QVector _sByteCommands; -}; SerialLayer::SerialLayer(const QString &port, int32_t baud, QObject *parent) : QSerialPort(parent), d(new SerialLayerPrivate()) { setPortName(port); setBaudRate(baud); if (open(QIODevice::ReadWrite)) { d->_serialOpened = true; connect(this, &QSerialPort::readyRead, this, &SerialLayer::readAllData); connect(this, &QSerialPort::errorOccurred, this, &SerialLayer::handleError); } }; void SerialLayer::readAllData() { d->_rawData.append(readAll()); //Remove any \r in the string, then split by \n. //This removes any trailing \r or \n from the commands // Proper line endings are added when the command is pushed. - d->_rawData = d->_rawData.replace(_return, QByteArray()); + d->_rawData = d->_rawData.replace(d->_return, QByteArray()); - QList tempList = d->_rawData.split(_newLine.at(0)); + QList tempList = d->_rawData.split(d->_newLine.at(0)); for (auto i = tempList.begin(); i != tempList.end(); ++i) { // Get finished line to _byteCommands if (i < tempList.end() - 1) { d->_rByteCommands.append(*i); emit receivedCommand(*i); } else { d->_rawData.clear(); d->_rawData.append(*i); } } } void SerialLayer::pushCommand(const QByteArray &comm, const QByteArray &term) { if (!isOpen()) { qCDebug(SERIAL_LAYER) << "Serial not connected !"; return; } QByteArray tmp = comm + term; write(tmp); emit pushedCommand(tmp); } void SerialLayer::pushCommand(const QByteArray &comm) { - pushCommand(comm, _newLineReturn); + pushCommand(comm, d->_newLineReturn); } void SerialLayer::add(const QByteArray &comm, const QByteArray &term) { QByteArray tmp = comm + term; d->_sByteCommands.append(tmp); } void SerialLayer::add(const QByteArray &comm) { - add(comm, _newLineReturn); + add(comm, d->_newLineReturn); } void SerialLayer::push() { if (!isOpen()) { qCDebug(SERIAL_LAYER) << "Serial not connected !"; return; } for (const auto &comm : qAsConst(d->_sByteCommands)) { write(comm); emit pushedCommand(comm); } d->_sByteCommands.clear(); } bool SerialLayer::commandAvailable() const { return !d->_rByteCommands.isEmpty(); } QStringList SerialLayer::validBaudRates() const { - return _validBaudRates; + return d->_validBaudRates; } void SerialLayer::handleError(QSerialPort::SerialPortError error) { if (d->_lastError == error) { return; } d->_lastError = error; emit serialError(error); } diff --git a/src/core/seriallayer.h b/src/core/seriallayer.h index 0c0609c..3fdacbf 100644 --- a/src/core/seriallayer.h +++ b/src/core/seriallayer.h @@ -1,136 +1,136 @@ /* AtCore Copyright (C) <2016 - 2018> Authors: Patrick José Pereira Chris Rizzitello Tomaz Canabrava This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #pragma once #include #include #include "atcore_export.h" /** * @brief The SerialLayer class. * Provide the low level serial operations */ class ATCORE_EXPORT SerialLayer : public QSerialPort { Q_OBJECT private: - class SerialLayerPrivate; + struct SerialLayerPrivate; SerialLayerPrivate *d; /** * @brief Read all available serial data * */ void readAllData(); signals: /** * @brief Emit signal when command is pushed * * @param comm : Command */ void pushedCommand(const QByteArray &comm); /** * @brief Emit signal when command is received * * @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: /** * @brief SerialLayer Class to realize communication * * @param port : Port (/dev/ttyUSB ACM) * @param baud : Baud rate (115200) * @param parent : Parent */ SerialLayer(const QString &port, int32_t baud, QObject *parent = nullptr); /** * @brief Add command to be pushed * * @param comm : Command * @param term : Terminator */ void add(const QByteArray &comm, const QByteArray &term); /** * @brief Add command to be pushed * * @param comm : Command, default terminator will be used */ 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 * * @param comm : Command * @param term : Terminator */ void pushCommand(const QByteArray &comm, const QByteArray &term); /** * @brief Push command directly * * @param comm : Command, default terminator will be used */ void pushCommand(const QByteArray &comm); /** * @brief Push all commands used in add to serial write * */ void push(); /** * @brief Check if is a command available * * @return bool */ bool commandAvailable() const; /** * @brief Return a QStringList of valids serial baud rates * * @return QStringList */ QStringList validBaudRates() const; };