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 | | ||||
159 | switch (error) { | ||||
160 | case (QSerialPort::DeviceNotFoundError): | ||||
161 | emit serialError(tr("Device not found")); | ||||
162 | break; | ||||
163 | case (QSerialPort::PermissionError): | ||||
164 | emit serialError(tr("Device is in use or user lacks permission to use it.")); | ||||
165 | break; | ||||
166 | case (QSerialPort::OpenError): | ||||
167 | emit serialError(tr("Device is already connected")); | ||||
168 | break; | ||||
169 | case (QSerialPort::NotOpenError): | ||||
170 | emit serialError(tr("Device needs to be connected first")); | ||||
171 | break; | ||||
172 | case (QSerialPort::WriteError): | ||||
173 | emit serialError(tr("Unable to write to device")); | ||||
174 | break; | ||||
175 | case (QSerialPort::ReadError): | ||||
176 | emit serialError(tr("Unable to read from device")); | ||||
177 | break; | ||||
178 | case (QSerialPort::ResourceError): | ||||
179 | emit serialError(tr("The device is unavailable")); | ||||
180 | break; | ||||
181 | case (QSerialPort::UnsupportedOperationError): | ||||
182 | emit serialError(tr("Device does not support opperation")); | ||||
183 | break; | ||||
184 | case (QSerialPort::TimeoutError): | ||||
185 | emit serialError(tr("Device timeout")); | ||||
186 | break; | ||||
187 | case (QSerialPort::UnknownError): | ||||
188 | emit serialError(tr("Unknown Error")); | ||||
189 | break; | ||||
190 | default: | ||||
191 | //Not Directly processed errors | ||||
192 | //QSerialPort::NoError, No error has happened | ||||
193 | //QSerialPort::ParityError, Its Obsolete. Qt Docs "We strongly advise against using it in new code." | ||||
194 | //QSerialPort::FramingError, Its Obsolete. Qt Docs "We strongly advise against using it in new code." | ||||
195 | //QSerialPort::BreakConditionError, Its Obsolete. Qt Docs "We strongly advise against using it in new code." | ||||
196 | break; | ||||
197 | };//End of Switch | ||||
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… | |||||
198 | } |
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.