Changeset View
Changeset View
Standalone View
Standalone View
src/core/seriallayer.cpp
Show All 23 Lines | |||||
24 | */ | 24 | */ | ||
25 | 25 | | |||
26 | #include <QLoggingCategory> | 26 | #include <QLoggingCategory> | ||
27 | 27 | | |||
28 | #include "seriallayer.h" | 28 | #include "seriallayer.h" | ||
29 | 29 | | |||
30 | Q_LOGGING_CATEGORY(SERIAL_LAYER, "org.kde.atelier.core.serialLayer") | 30 | Q_LOGGING_CATEGORY(SERIAL_LAYER, "org.kde.atelier.core.serialLayer") | ||
31 | 31 | | |||
32 | namespace | | |||
33 | { | | |||
34 | QByteArray _return = QByteArray("\r"); | | |||
35 | QByteArray _newLine = QByteArray("\n"); | | |||
36 | QByteArray _newLineReturn = QByteArray("\n\r"); | | |||
37 | QStringList _validBaudRates = { | | |||
38 | QStringLiteral("9600"), | | |||
39 | QStringLiteral("14400"), | | |||
40 | QStringLiteral("19200"), | | |||
41 | QStringLiteral("28800"), | | |||
42 | QStringLiteral("38400"), | | |||
43 | QStringLiteral("57600"), | | |||
44 | QStringLiteral("76800"), | | |||
45 | QStringLiteral("115200"), | | |||
46 | QStringLiteral("230400"), | | |||
47 | QStringLiteral("250000"), | | |||
48 | QStringLiteral("500000"), | | |||
49 | QStringLiteral("1000000") | | |||
50 | }; | | |||
51 | } | | |||
52 | | ||||
53 | /** | 32 | /** | ||
54 | * @brief The SerialLayerPrivate class | 33 | * @brief The SerialLayerPrivate class | ||
55 | */ | 34 | */ | ||
56 | class SerialLayer::SerialLayerPrivate | 35 | struct SerialLayer::SerialLayerPrivate { | ||
57 | { | | |||
58 | public: | | |||
59 | /** _serialOpened: is Serial port opened */ | 36 | /** _serialOpened: is Serial port opened */ | ||
60 | bool _serialOpened = false; | 37 | bool _serialOpened = false; | ||
61 | /** _lastError: the last reported error */ | 38 | /** _lastError: the last reported error */ | ||
62 | QSerialPort::SerialPortError _lastError = QSerialPort::NoError; | 39 | QSerialPort::SerialPortError _lastError = QSerialPort::NoError; | ||
63 | /** _rawData: the raw serial data */ | 40 | /** _rawData: the raw serial data */ | ||
64 | QByteArray _rawData; | 41 | QByteArray _rawData; | ||
65 | /** _rByteCommand: received Messages */ | 42 | /** _rByteCommand: received Messages */ | ||
66 | QVector<QByteArray> _rByteCommands; | 43 | QVector<QByteArray> _rByteCommands; | ||
67 | /** _sByteCommand: sent Messages */ | 44 | /** _sByteCommand: sent Messages */ | ||
68 | QVector<QByteArray> _sByteCommands; | 45 | QVector<QByteArray> _sByteCommands; | ||
46 | /** Return String */ | ||||
47 | static const QByteArray _return; | ||||
48 | /** New Line String */ | ||||
49 | static const QByteArray _newLine; | ||||
50 | /** New Line Return String */ | ||||
51 | static const QByteArray _newLineReturn; | ||||
52 | /** List of valid Baud Rates */ | ||||
53 | static const QStringList _validBaudRates; | ||||
54 | }; | ||||
55 | | ||||
56 | const QByteArray SerialLayer::SerialLayerPrivate::_return = QByteArray("\r"); | ||||
57 | const QByteArray SerialLayer::SerialLayerPrivate::_newLine = QByteArray("\n"); | ||||
58 | const QByteArray SerialLayer::SerialLayerPrivate::_newLineReturn = QByteArray("\n\r"); | ||||
59 | const QStringList SerialLayer::SerialLayerPrivate::_validBaudRates = { | ||||
60 | QStringLiteral("9600"), | ||||
61 | QStringLiteral("14400"), | ||||
62 | QStringLiteral("19200"), | ||||
63 | QStringLiteral("28800"), | ||||
64 | QStringLiteral("38400"), | ||||
65 | QStringLiteral("57600"), | ||||
66 | QStringLiteral("76800"), | ||||
67 | QStringLiteral("115200"), | ||||
68 | QStringLiteral("230400"), | ||||
69 | QStringLiteral("250000"), | ||||
70 | QStringLiteral("500000"), | ||||
71 | QStringLiteral("1000000") | ||||
69 | }; | 72 | }; | ||
70 | 73 | | |||
71 | SerialLayer::SerialLayer(const QString &port, int32_t baud, QObject *parent) : | 74 | SerialLayer::SerialLayer(const QString &port, int32_t baud, QObject *parent) : | ||
72 | QSerialPort(parent), d(new SerialLayerPrivate()) | 75 | QSerialPort(parent), d(new SerialLayerPrivate()) | ||
73 | { | 76 | { | ||
74 | setPortName(port); | 77 | setPortName(port); | ||
75 | setBaudRate(baud); | 78 | setBaudRate(baud); | ||
76 | if (open(QIODevice::ReadWrite)) { | 79 | if (open(QIODevice::ReadWrite)) { | ||
77 | d->_serialOpened = true; | 80 | d->_serialOpened = true; | ||
78 | connect(this, &QSerialPort::readyRead, this, &SerialLayer::readAllData); | 81 | connect(this, &QSerialPort::readyRead, this, &SerialLayer::readAllData); | ||
79 | connect(this, &QSerialPort::errorOccurred, this, &SerialLayer::handleError); | 82 | connect(this, &QSerialPort::errorOccurred, this, &SerialLayer::handleError); | ||
80 | } | 83 | } | ||
81 | }; | 84 | }; | ||
82 | 85 | | |||
83 | void SerialLayer::readAllData() | 86 | void SerialLayer::readAllData() | ||
84 | { | 87 | { | ||
85 | d->_rawData.append(readAll()); | 88 | d->_rawData.append(readAll()); | ||
86 | 89 | | |||
87 | //Remove any \r in the string, then split by \n. | 90 | //Remove any \r in the string, then split by \n. | ||
88 | //This removes any trailing \r or \n from the commands | 91 | //This removes any trailing \r or \n from the commands | ||
89 | // Proper line endings are added when the command is pushed. | 92 | // Proper line endings are added when the command is pushed. | ||
90 | d->_rawData = d->_rawData.replace(_return, QByteArray()); | 93 | d->_rawData = d->_rawData.replace(d->_return, QByteArray()); | ||
91 | 94 | | |||
92 | QList<QByteArray> tempList = d->_rawData.split(_newLine.at(0)); | 95 | QList<QByteArray> tempList = d->_rawData.split(d->_newLine.at(0)); | ||
93 | for (auto i = tempList.begin(); i != tempList.end(); ++i) { | 96 | for (auto i = tempList.begin(); i != tempList.end(); ++i) { | ||
94 | // Get finished line to _byteCommands | 97 | // Get finished line to _byteCommands | ||
95 | if (i < tempList.end() - 1) { | 98 | if (i < tempList.end() - 1) { | ||
96 | d->_rByteCommands.append(*i); | 99 | d->_rByteCommands.append(*i); | ||
97 | emit receivedCommand(*i); | 100 | emit receivedCommand(*i); | ||
98 | } else { | 101 | } else { | ||
99 | d->_rawData.clear(); | 102 | d->_rawData.clear(); | ||
100 | d->_rawData.append(*i); | 103 | d->_rawData.append(*i); | ||
Show All 10 Lines | 109 | { | |||
111 | QByteArray tmp = comm + term; | 114 | QByteArray tmp = comm + term; | ||
112 | write(tmp); | 115 | write(tmp); | ||
113 | emit pushedCommand(tmp); | 116 | emit pushedCommand(tmp); | ||
114 | 117 | | |||
115 | } | 118 | } | ||
116 | 119 | | |||
117 | void SerialLayer::pushCommand(const QByteArray &comm) | 120 | void SerialLayer::pushCommand(const QByteArray &comm) | ||
118 | { | 121 | { | ||
119 | pushCommand(comm, _newLineReturn); | 122 | pushCommand(comm, d->_newLineReturn); | ||
120 | } | 123 | } | ||
121 | 124 | | |||
122 | void SerialLayer::add(const QByteArray &comm, const QByteArray &term) | 125 | void SerialLayer::add(const QByteArray &comm, const QByteArray &term) | ||
123 | { | 126 | { | ||
124 | QByteArray tmp = comm + term; | 127 | QByteArray tmp = comm + term; | ||
125 | d->_sByteCommands.append(tmp); | 128 | d->_sByteCommands.append(tmp); | ||
126 | } | 129 | } | ||
127 | 130 | | |||
128 | void SerialLayer::add(const QByteArray &comm) | 131 | void SerialLayer::add(const QByteArray &comm) | ||
129 | { | 132 | { | ||
130 | add(comm, _newLineReturn); | 133 | add(comm, d->_newLineReturn); | ||
131 | } | 134 | } | ||
132 | 135 | | |||
133 | void SerialLayer::push() | 136 | void SerialLayer::push() | ||
134 | { | 137 | { | ||
135 | if (!isOpen()) { | 138 | if (!isOpen()) { | ||
136 | qCDebug(SERIAL_LAYER) << "Serial not connected !"; | 139 | qCDebug(SERIAL_LAYER) << "Serial not connected !"; | ||
137 | return; | 140 | return; | ||
138 | } | 141 | } | ||
139 | for (const auto &comm : qAsConst(d->_sByteCommands)) { | 142 | for (const auto &comm : qAsConst(d->_sByteCommands)) { | ||
140 | write(comm); | 143 | write(comm); | ||
141 | emit pushedCommand(comm); | 144 | emit pushedCommand(comm); | ||
142 | } | 145 | } | ||
143 | d->_sByteCommands.clear(); | 146 | d->_sByteCommands.clear(); | ||
144 | } | 147 | } | ||
145 | 148 | | |||
146 | bool SerialLayer::commandAvailable() const | 149 | bool SerialLayer::commandAvailable() const | ||
147 | { | 150 | { | ||
148 | return !d->_rByteCommands.isEmpty(); | 151 | return !d->_rByteCommands.isEmpty(); | ||
149 | } | 152 | } | ||
150 | 153 | | |||
151 | QStringList SerialLayer::validBaudRates() const | 154 | QStringList SerialLayer::validBaudRates() const | ||
152 | { | 155 | { | ||
153 | return _validBaudRates; | 156 | return d->_validBaudRates; | ||
154 | } | 157 | } | ||
155 | 158 | | |||
156 | void SerialLayer::handleError(QSerialPort::SerialPortError error) | 159 | void SerialLayer::handleError(QSerialPort::SerialPortError error) | ||
157 | { | 160 | { | ||
158 | if (d->_lastError == error) { | 161 | if (d->_lastError == error) { | ||
159 | return; | 162 | return; | ||
160 | } | 163 | } | ||
161 | 164 | | |||
162 | d->_lastError = error; | 165 | d->_lastError = error; | ||
163 | emit serialError(error); | 166 | emit serialError(error); | ||
164 | } | 167 | } |