Changeset View
Changeset View
Standalone View
Standalone View
src/core/seriallayer.cpp
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Line(s) | |||||
52 | 52 | | |||
53 | /** | 53 | /** | ||
54 | * @brief The SerialLayerPrivate class | 54 | * @brief The SerialLayerPrivate class | ||
55 | */ | 55 | */ | ||
56 | class SerialLayer::SerialLayerPrivate | 56 | class SerialLayer::SerialLayerPrivate | ||
57 | { | 57 | { | ||
58 | public: | 58 | public: | ||
59 | bool _serialOpened; //!< @param _serialOpened: is serial port opened | 59 | bool _serialOpened; //!< @param _serialOpened: is serial port opened | ||
60 | QSerialPort::SerialPortError _lastError; //!< @param _lastError: the last reported error | ||||
60 | QByteArray _rawData; //!< @param _rawData: the raw serial data | 61 | QByteArray _rawData; //!< @param _rawData: the raw serial data | ||
61 | QVector<QByteArray> _rByteCommands; //!< @param _rByteCommand: received Messages | 62 | QVector<QByteArray> _rByteCommands; //!< @param _rByteCommand: received Messages | ||
62 | QVector<QByteArray> _sByteCommands; //!< @param _sByteCommand: sent Messages | 63 | QVector<QByteArray> _sByteCommands; //!< @param _sByteCommand: sent Messages | ||
63 | }; | 64 | }; | ||
64 | 65 | | |||
65 | SerialLayer::SerialLayer(const QString &port, uint baud, QObject *parent) : | 66 | SerialLayer::SerialLayer(const QString &port, uint baud, QObject *parent) : | ||
66 | QSerialPort(parent), d(new SerialLayerPrivate()) | 67 | QSerialPort(parent), d(new SerialLayerPrivate()) | ||
67 | { | 68 | { | ||
68 | setPortName(port); | 69 | setPortName(port); | ||
69 | setBaudRate(baud); | 70 | setBaudRate(baud); | ||
70 | if (open(QIODevice::ReadWrite)) { | 71 | if (open(QIODevice::ReadWrite)) { | ||
71 | d->_serialOpened = true; | 72 | d->_serialOpened = true; | ||
72 | connect(this, &QSerialPort::readyRead, this, &SerialLayer::readAllData); | 73 | connect(this, &QSerialPort::readyRead, this, &SerialLayer::readAllData); | ||
74 | connect(this, &QSerialPort::errorOccurred, this, &SerialLayer::handleError); | ||||
73 | } | 75 | } | ||
74 | }; | 76 | }; | ||
75 | 77 | | |||
76 | void SerialLayer::readAllData() | 78 | void SerialLayer::readAllData() | ||
77 | { | 79 | { | ||
78 | d->_rawData.append(readAll()); | 80 | d->_rawData.append(readAll()); | ||
79 | 81 | | |||
80 | //Remove any \r in the string, then split by \n. | 82 | //Remove any \r in the string, then split by \n. | ||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Line(s) | |||||
140 | { | 142 | { | ||
141 | return !d->_rByteCommands.isEmpty(); | 143 | return !d->_rByteCommands.isEmpty(); | ||
142 | } | 144 | } | ||
143 | 145 | | |||
144 | QStringList SerialLayer::validBaudRates() const | 146 | QStringList SerialLayer::validBaudRates() const | ||
145 | { | 147 | { | ||
146 | return _validBaudRates; | 148 | return _validBaudRates; | ||
147 | } | 149 | } | ||
150 | | ||||
151 | void SerialLayer::handleError(QSerialPort::SerialPortError error) | ||||
152 | { | ||||
153 | if (d->_lastError == error) { | ||||
154 | return; | ||||
155 | } | ||||
156 | | ||||
157 | d->_lastError = error; | ||||
158 | emit serialError(error); | ||||
159 | } | ||||
patrickelectric: You should emit only the error enum and create a function that translate this enum to human… | |||||
The host will only need to worry about the atcore state . If the error is serious atcore will change its state and the client will react. Most of the errors will just be logged. rizzitello: The host will only need to worry about the atcore state . If the error is serious atcore will… | |||||
The emitting of enum approach will make the API more flexible and powerful.. Giving liberty to the printer host. patrickelectric: The emitting of enum approach will make the API more flexible and powerful.. Giving liberty to… |
You should emit only the error enum and create a function that translate this enum to human string.
Emitting strings will make harder to any printer host to identify if the error is really serious or not.