1 | // Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> |
2 | // Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> |
3 | // Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> |
4 | // Copyright (C) 2012 Andre Hartmann <aha_1980@gmx.de> |
5 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
6 | |
7 | #include "qserialport.h" |
8 | #include "qserialportinfo.h" |
9 | #include "qserialportinfo_p.h" |
10 | |
11 | #include "qserialport_p.h" |
12 | |
13 | #include <QtCore/qdebug.h> |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | QSerialPortErrorInfo::QSerialPortErrorInfo(QSerialPort::SerialPortError newErrorCode, |
18 | const QString &newErrorString) |
19 | : errorCode(newErrorCode) |
20 | , errorString(newErrorString) |
21 | { |
22 | if (errorString.isNull()) { |
23 | switch (errorCode) { |
24 | case QSerialPort::NoError: |
25 | errorString = QSerialPort::tr(s: "No error" ); |
26 | break; |
27 | case QSerialPort::OpenError: |
28 | errorString = QSerialPort::tr(s: "Device is already open" ); |
29 | break; |
30 | case QSerialPort::NotOpenError: |
31 | errorString = QSerialPort::tr(s: "Device is not open" ); |
32 | break; |
33 | case QSerialPort::TimeoutError: |
34 | errorString = QSerialPort::tr(s: "Operation timed out" ); |
35 | break; |
36 | case QSerialPort::ReadError: |
37 | errorString = QSerialPort::tr(s: "Error reading from device" ); |
38 | break; |
39 | case QSerialPort::WriteError: |
40 | errorString = QSerialPort::tr(s: "Error writing to device" ); |
41 | break; |
42 | case QSerialPort::ResourceError: |
43 | errorString = QSerialPort::tr(s: "Device disappeared from the system" ); |
44 | break; |
45 | default: |
46 | // an empty string will be interpreted as "Unknown error" |
47 | // from the QIODevice::errorString() |
48 | break; |
49 | } |
50 | } |
51 | } |
52 | |
53 | QSerialPortPrivate::QSerialPortPrivate() |
54 | #if defined(Q_OS_WIN32) |
55 | : readChunkBuffer(QSERIALPORT_BUFFERSIZE, 0) |
56 | #endif |
57 | { |
58 | writeBufferChunkSize = QSERIALPORT_BUFFERSIZE; |
59 | readBufferChunkSize = QSERIALPORT_BUFFERSIZE; |
60 | } |
61 | |
62 | void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) |
63 | { |
64 | Q_Q(QSerialPort); |
65 | |
66 | q->setErrorString(errorInfo.errorString); |
67 | error.setValue(errorInfo.errorCode); |
68 | error.notify(); |
69 | emit q->errorOccurred(error); |
70 | } |
71 | |
72 | /*! |
73 | \class QSerialPort |
74 | |
75 | \brief Provides functions to access serial ports. |
76 | |
77 | \reentrant |
78 | \ingroup serialport-main |
79 | \inmodule QtSerialPort |
80 | \since 5.1 |
81 | |
82 | You can get information about the available serial ports using the |
83 | QSerialPortInfo helper class, which allows an enumeration of all the serial |
84 | ports in the system. This is useful to obtain the correct name of the |
85 | serial port you want to use. You can pass an object |
86 | of the helper class as an argument to the setPort() or setPortName() |
87 | methods to assign the desired serial device. |
88 | |
89 | After setting the port, you can open it in read-only (r/o), write-only |
90 | (w/o), or read-write (r/w) mode using the open() method. |
91 | |
92 | \note The serial port is always opened with exclusive access |
93 | (that is, no other process or thread can access an already opened serial port). |
94 | |
95 | Use the close() method to close the port and cancel the I/O operations. |
96 | |
97 | Having successfully opened, QSerialPort tries to determine the current |
98 | configuration of the port and initializes itself. You can reconfigure the |
99 | port to the desired setting using the setBaudRate(), setDataBits(), |
100 | setParity(), setStopBits(), and setFlowControl() methods. |
101 | |
102 | There are a couple of properties to work with the pinout signals namely: |
103 | QSerialPort::dataTerminalReady, QSerialPort::requestToSend. It is also |
104 | possible to use the pinoutSignals() method to query the current pinout |
105 | signals set. |
106 | |
107 | Once you know that the ports are ready to read or write, you can |
108 | use the read() or write() methods. Alternatively the |
109 | readLine() and readAll() convenience methods can also be invoked. |
110 | If not all the data is read at once, the remaining data will |
111 | be available for later as new incoming data is appended to the |
112 | QSerialPort's internal read buffer. You can limit the size of the read |
113 | buffer using setReadBufferSize(). |
114 | |
115 | QSerialPort provides a set of functions that suspend the |
116 | calling thread until certain signals are emitted. These functions |
117 | can be used to implement blocking serial ports: |
118 | |
119 | \list |
120 | |
121 | \li waitForReadyRead() blocks calls until new data is available for |
122 | reading. |
123 | |
124 | \li waitForBytesWritten() blocks calls until one payload of data has |
125 | been written to the serial port. |
126 | |
127 | \endlist |
128 | |
129 | See the following example: |
130 | |
131 | \code |
132 | int numRead = 0, numReadTotal = 0; |
133 | char buffer[50]; |
134 | |
135 | for (;;) { |
136 | numRead = serial.read(buffer, 50); |
137 | |
138 | // Do whatever with the array |
139 | |
140 | numReadTotal += numRead; |
141 | if (numRead == 0 && !serial.waitForReadyRead()) |
142 | break; |
143 | } |
144 | \endcode |
145 | |
146 | If \l{QIODevice::}{waitForReadyRead()} returns \c false, the |
147 | connection has been closed or an error has occurred. |
148 | |
149 | If an error occurs at any point in time, QSerialPort will emit the |
150 | errorOccurred() signal. You can also call error() to find the type of |
151 | error that occurred last. |
152 | |
153 | Programming with a blocking serial port is radically different from |
154 | programming with a non-blocking serial port. A blocking serial port |
155 | does not require an event loop and typically leads to simpler code. |
156 | However, in a GUI application, blocking serial port should only be |
157 | used in non-GUI threads, to avoid freezing the user interface. |
158 | |
159 | For more details about these approaches, refer to the |
160 | \l {Qt Serial Port Examples}{example} applications. |
161 | |
162 | The QSerialPort class can also be used with QTextStream and QDataStream's |
163 | stream operators (operator<<() and operator>>()). There is one issue to be |
164 | aware of, though: make sure that enough data is available before attempting |
165 | to read by using the operator>>() overloaded operator. |
166 | |
167 | \sa QSerialPortInfo |
168 | */ |
169 | |
170 | /*! |
171 | \enum QSerialPort::Direction |
172 | |
173 | This enum describes the possible directions of the data transmission. |
174 | |
175 | \note This enumeration is used for setting the baud rate of the device |
176 | separately for each direction on some operating systems (for example, |
177 | POSIX-like). |
178 | |
179 | \value Input Input direction. |
180 | \value Output Output direction. |
181 | \value AllDirections Simultaneously in two directions. |
182 | */ |
183 | |
184 | /*! |
185 | \enum QSerialPort::BaudRate |
186 | |
187 | This enum describes the baud rate which the communication device operates |
188 | with. |
189 | |
190 | \note Only the most common standard baud rates are listed in this enum. |
191 | |
192 | \value Baud1200 1200 baud. |
193 | \value Baud2400 2400 baud. |
194 | \value Baud4800 4800 baud. |
195 | \value Baud9600 9600 baud. |
196 | \value Baud19200 19200 baud. |
197 | \value Baud38400 38400 baud. |
198 | \value Baud57600 57600 baud. |
199 | \value Baud115200 115200 baud. |
200 | |
201 | \sa QSerialPort::baudRate |
202 | */ |
203 | |
204 | /*! |
205 | \enum QSerialPort::DataBits |
206 | |
207 | This enum describes the number of data bits used. |
208 | |
209 | \value Data5 The number of data bits in each character is 5. It |
210 | is used for Baudot code. It generally only makes |
211 | sense with older equipment such as teleprinters. |
212 | \value Data6 The number of data bits in each character is 6. It |
213 | is rarely used. |
214 | \value Data7 The number of data bits in each character is 7. It |
215 | is used for true ASCII. It generally only makes |
216 | sense with older equipment such as teleprinters. |
217 | \value Data8 The number of data bits in each character is 8. It |
218 | is used for most kinds of data, as this size matches |
219 | the size of a byte. It is almost universally used in |
220 | newer applications. |
221 | |
222 | \sa QSerialPort::dataBits |
223 | */ |
224 | |
225 | /*! |
226 | \enum QSerialPort::Parity |
227 | |
228 | This enum describes the parity scheme used. |
229 | |
230 | \value NoParity No parity bit it sent. This is the most common |
231 | parity setting. Error detection is handled by the |
232 | communication protocol. |
233 | \value EvenParity The number of 1 bits in each character, including |
234 | the parity bit, is always even. |
235 | \value OddParity The number of 1 bits in each character, including |
236 | the parity bit, is always odd. It ensures that at |
237 | least one state transition occurs in each character. |
238 | \value SpaceParity Space parity. The parity bit is sent in the space |
239 | signal condition. It does not provide error |
240 | detection information. |
241 | \value MarkParity Mark parity. The parity bit is always set to the |
242 | mark signal condition (logical 1). It does not |
243 | provide error detection information. |
244 | |
245 | \sa QSerialPort::parity |
246 | */ |
247 | |
248 | /*! |
249 | \enum QSerialPort::StopBits |
250 | |
251 | This enum describes the number of stop bits used. |
252 | |
253 | \value OneStop 1 stop bit. |
254 | \value OneAndHalfStop 1.5 stop bits. This is only for the Windows platform. |
255 | \value TwoStop 2 stop bits. |
256 | |
257 | \sa QSerialPort::stopBits |
258 | */ |
259 | |
260 | /*! |
261 | \enum QSerialPort::FlowControl |
262 | |
263 | This enum describes the flow control used. |
264 | |
265 | \value NoFlowControl No flow control. |
266 | \value HardwareControl Hardware flow control (RTS/CTS). |
267 | \value SoftwareControl Software flow control (XON/XOFF). |
268 | |
269 | \sa QSerialPort::flowControl |
270 | */ |
271 | |
272 | /*! |
273 | \enum QSerialPort::PinoutSignal |
274 | |
275 | This enum describes the possible RS-232 pinout signals. |
276 | |
277 | \value NoSignal No line active |
278 | \value DataTerminalReadySignal DTR (Data Terminal Ready). |
279 | \value DataCarrierDetectSignal DCD (Data Carrier Detect). |
280 | \value DataSetReadySignal DSR (Data Set Ready). |
281 | \value RingIndicatorSignal RNG (Ring Indicator). |
282 | \value RequestToSendSignal RTS (Request To Send). |
283 | \value ClearToSendSignal CTS (Clear To Send). |
284 | \value SecondaryTransmittedDataSignal STD (Secondary Transmitted Data). |
285 | \value SecondaryReceivedDataSignal SRD (Secondary Received Data). |
286 | |
287 | \sa pinoutSignals(), QSerialPort::dataTerminalReady, |
288 | QSerialPort::requestToSend |
289 | */ |
290 | |
291 | /*! |
292 | \enum QSerialPort::SerialPortError |
293 | |
294 | This enum describes the errors that may be contained by the |
295 | QSerialPort::error property. |
296 | |
297 | \value NoError No error occurred. |
298 | |
299 | \value DeviceNotFoundError An error occurred while attempting to |
300 | open an non-existing device. |
301 | |
302 | \value PermissionError An error occurred while attempting to |
303 | open an already opened device by another |
304 | process or a user not having enough permission |
305 | and credentials to open. |
306 | |
307 | \value OpenError An error occurred while attempting to open an |
308 | already opened device in this object. |
309 | |
310 | \value NotOpenError This error occurs when an operation is executed |
311 | that can only be successfully performed if the |
312 | device is open. This value was introduced in |
313 | QtSerialPort 5.2. |
314 | |
315 | \value WriteError An I/O error occurred while writing the data. |
316 | |
317 | \value ReadError An I/O error occurred while reading the data. |
318 | |
319 | \value ResourceError An I/O error occurred when a resource becomes |
320 | unavailable, e.g. when the device is |
321 | unexpectedly removed from the system. |
322 | |
323 | \value UnsupportedOperationError The requested device operation is not |
324 | supported or prohibited by the running operating |
325 | system. |
326 | |
327 | \value TimeoutError A timeout error occurred. This value was |
328 | introduced in QtSerialPort 5.2. |
329 | |
330 | \value UnknownError An unidentified error occurred. |
331 | \sa QSerialPort::error |
332 | */ |
333 | |
334 | |
335 | |
336 | /*! |
337 | Constructs a new serial port object with the given \a parent. |
338 | */ |
339 | QSerialPort::QSerialPort(QObject *parent) |
340 | : QIODevice(*new QSerialPortPrivate, parent) |
341 | { |
342 | } |
343 | |
344 | /*! |
345 | Constructs a new serial port object with the given \a parent |
346 | to represent the serial port with the specified \a name. |
347 | |
348 | The name should have a specific format; see the setPort() method. |
349 | */ |
350 | QSerialPort::QSerialPort(const QString &name, QObject *parent) |
351 | : QIODevice(*new QSerialPortPrivate, parent) |
352 | { |
353 | setPortName(name); |
354 | } |
355 | |
356 | /*! |
357 | Constructs a new serial port object with the given \a parent |
358 | to represent the serial port with the specified helper class |
359 | \a serialPortInfo. |
360 | */ |
361 | QSerialPort::QSerialPort(const QSerialPortInfo &serialPortInfo, QObject *parent) |
362 | : QIODevice(*new QSerialPortPrivate, parent) |
363 | { |
364 | setPort(serialPortInfo); |
365 | } |
366 | |
367 | /*! |
368 | Closes the serial port, if necessary, and then destroys object. |
369 | */ |
370 | QSerialPort::~QSerialPort() |
371 | { |
372 | /**/ |
373 | if (isOpen()) |
374 | close(); |
375 | } |
376 | |
377 | /*! |
378 | Sets the \a name of the serial port. |
379 | |
380 | The name of the serial port can be passed as either a short name or |
381 | the long system location if necessary. |
382 | |
383 | \sa portName(), QSerialPortInfo |
384 | */ |
385 | void QSerialPort::setPortName(const QString &name) |
386 | { |
387 | Q_D(QSerialPort); |
388 | d->systemLocation = QSerialPortInfoPrivate::portNameToSystemLocation(source: name); |
389 | } |
390 | |
391 | /*! |
392 | Sets the port stored in the serial port info instance \a serialPortInfo. |
393 | |
394 | \sa portName(), QSerialPortInfo |
395 | */ |
396 | void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo) |
397 | { |
398 | Q_D(QSerialPort); |
399 | d->systemLocation = serialPortInfo.systemLocation(); |
400 | } |
401 | |
402 | /*! |
403 | Returns the name set by setPort() or passed to the QSerialPort constructor. |
404 | This name is short, i.e. it is extracted and converted from the internal |
405 | variable system location of the device. The conversion algorithm is |
406 | platform specific: |
407 | \table |
408 | \header |
409 | \li Platform |
410 | \li Brief Description |
411 | \row |
412 | \li Windows |
413 | \li Removes the prefix "\\\\.\\" or "//./" from the system location |
414 | and returns the remainder of the string. |
415 | \row |
416 | \li Unix, BSD |
417 | \li Removes the prefix "/dev/" from the system location |
418 | and returns the remainder of the string. |
419 | \endtable |
420 | |
421 | \sa setPort(), QSerialPortInfo::portName() |
422 | */ |
423 | QString QSerialPort::portName() const |
424 | { |
425 | Q_D(const QSerialPort); |
426 | return QSerialPortInfoPrivate::portNameFromSystemLocation(source: d->systemLocation); |
427 | } |
428 | |
429 | /*! |
430 | \reimp |
431 | |
432 | Opens the serial port using OpenMode \a mode, and then returns \c true if |
433 | successful; otherwise returns \c false and sets an error code which can be |
434 | obtained by calling the error() method. |
435 | |
436 | \note The method returns \c false if opening the port is successful, but could |
437 | not set any of the port settings successfully. In that case, the port is |
438 | closed automatically not to leave the port around with incorrect settings. |
439 | |
440 | \warning The \a mode has to be QIODeviceBase::ReadOnly, QIODeviceBase::WriteOnly, |
441 | or QIODeviceBase::ReadWrite. Other modes are unsupported. |
442 | |
443 | \sa QIODeviceBase::OpenMode, setPort() |
444 | */ |
445 | bool QSerialPort::open(OpenMode mode) |
446 | { |
447 | Q_D(QSerialPort); |
448 | |
449 | if (isOpen()) { |
450 | d->setError(QSerialPortErrorInfo(QSerialPort::OpenError)); |
451 | return false; |
452 | } |
453 | |
454 | // Define while not supported modes. |
455 | static const OpenMode unsupportedModes = Append | Truncate | Text | Unbuffered; |
456 | if ((mode & unsupportedModes) || mode == NotOpen) { |
457 | d->setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, tr(s: "Unsupported open mode" ))); |
458 | return false; |
459 | } |
460 | |
461 | clearError(); |
462 | if (!d->open(mode)) |
463 | return false; |
464 | |
465 | QIODevice::open(mode); |
466 | return true; |
467 | } |
468 | |
469 | /*! |
470 | \reimp |
471 | |
472 | \note The serial port has to be open before trying to close it; otherwise |
473 | sets the NotOpenError error code. |
474 | |
475 | \sa QIODevice::close() |
476 | */ |
477 | void QSerialPort::close() |
478 | { |
479 | Q_D(QSerialPort); |
480 | if (!isOpen()) { |
481 | d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); |
482 | return; |
483 | } |
484 | |
485 | d->close(); |
486 | d->isBreakEnabled.setValue(false); |
487 | QIODevice::close(); |
488 | } |
489 | |
490 | /*! |
491 | \property QSerialPort::baudRate |
492 | \brief the data baud rate for the desired direction |
493 | |
494 | If the setting is successful or set before opening the port, returns \c true; |
495 | otherwise returns \c false and sets an error code which can be obtained by |
496 | accessing the value of the QSerialPort::error property. To set the baud |
497 | rate, use the enumeration QSerialPort::BaudRate or any positive qint32 |
498 | value. |
499 | |
500 | \note If the setting is set before opening the port, the actual serial port |
501 | setting is done automatically in the \l{QSerialPort::open()} method right |
502 | after that the opening of the port succeeds. |
503 | |
504 | \warning Setting the AllDirections flag is supported on all platforms. |
505 | Windows supports only this mode. |
506 | |
507 | \warning Returns equal baud rate in any direction on Windows. |
508 | |
509 | The default value is Baud9600, i.e. 9600 bits per second. |
510 | */ |
511 | bool QSerialPort::setBaudRate(qint32 baudRate, Directions directions) |
512 | { |
513 | Q_D(QSerialPort); |
514 | |
515 | if (!isOpen() || d->setBaudRate(baudRate, directions)) { |
516 | if (directions & QSerialPort::Input) { |
517 | if (d->inputBaudRate != baudRate) |
518 | d->inputBaudRate = baudRate; |
519 | else |
520 | directions &= ~QSerialPort::Input; |
521 | } |
522 | |
523 | if (directions & QSerialPort::Output) { |
524 | if (d->outputBaudRate != baudRate) |
525 | d->outputBaudRate = baudRate; |
526 | else |
527 | directions &= ~QSerialPort::Output; |
528 | } |
529 | |
530 | if (directions) |
531 | emit baudRateChanged(baudRate, directions); |
532 | |
533 | return true; |
534 | } |
535 | |
536 | return false; |
537 | } |
538 | |
539 | qint32 QSerialPort::baudRate(Directions directions) const |
540 | { |
541 | Q_D(const QSerialPort); |
542 | if (directions == QSerialPort::AllDirections) |
543 | return d->inputBaudRate == d->outputBaudRate ? |
544 | d->inputBaudRate : -1; |
545 | return directions & QSerialPort::Input ? d->inputBaudRate : d->outputBaudRate; |
546 | } |
547 | |
548 | /*! |
549 | \fn void QSerialPort::baudRateChanged(qint32 baudRate, Directions directions) |
550 | |
551 | This signal is emitted after the baud rate has been changed. The new baud |
552 | rate is passed as \a baudRate and directions as \a directions. |
553 | |
554 | \sa QSerialPort::baudRate |
555 | */ |
556 | |
557 | /*! |
558 | \property QSerialPort::dataBits |
559 | \brief the data bits in a frame |
560 | |
561 | If the setting is successful or set before opening the port, returns |
562 | \c true; otherwise returns \c false and sets an error code which can be obtained |
563 | by accessing the value of the QSerialPort::error property. |
564 | |
565 | \note If the setting is set before opening the port, the actual serial port |
566 | setting is done automatically in the \l{QSerialPort::open()} method right |
567 | after that the opening of the port succeeds. |
568 | |
569 | The default value is Data8, i.e. 8 data bits. |
570 | */ |
571 | bool QSerialPort::setDataBits(DataBits dataBits) |
572 | { |
573 | Q_D(QSerialPort); |
574 | d->dataBits.removeBindingUnlessInWrapper(); |
575 | const auto currentDataBits = d->dataBits.value(); |
576 | if (!isOpen() || d->setDataBits(dataBits)) { |
577 | d->dataBits.setValueBypassingBindings(dataBits); |
578 | if (currentDataBits != dataBits) { |
579 | d->dataBits.notify(); |
580 | emit dataBitsChanged(dataBits); |
581 | } |
582 | return true; |
583 | } |
584 | return false; |
585 | } |
586 | |
587 | QSerialPort::DataBits QSerialPort::dataBits() const |
588 | { |
589 | Q_D(const QSerialPort); |
590 | return d->dataBits; |
591 | } |
592 | |
593 | QBindable<QSerialPort::DataBits> QSerialPort::bindableDataBits() |
594 | { |
595 | return &d_func()->dataBits; |
596 | } |
597 | |
598 | /*! |
599 | \fn void QSerialPort::dataBitsChanged(DataBits dataBits) |
600 | |
601 | This signal is emitted after the data bits in a frame has been changed. The |
602 | new data bits in a frame is passed as \a dataBits. |
603 | |
604 | \sa QSerialPort::dataBits |
605 | */ |
606 | |
607 | |
608 | /*! |
609 | \property QSerialPort::parity |
610 | \brief the parity checking mode |
611 | |
612 | If the setting is successful or set before opening the port, returns \c true; |
613 | otherwise returns \c false and sets an error code which can be obtained by |
614 | accessing the value of the QSerialPort::error property. |
615 | |
616 | \note If the setting is set before opening the port, the actual serial port |
617 | setting is done automatically in the \l{QSerialPort::open()} method right |
618 | after that the opening of the port succeeds. |
619 | |
620 | The default value is NoParity, i.e. no parity. |
621 | */ |
622 | bool QSerialPort::setParity(Parity parity) |
623 | { |
624 | Q_D(QSerialPort); |
625 | d->parity.removeBindingUnlessInWrapper(); |
626 | const auto currentParity = d->parity.value(); |
627 | if (!isOpen() || d->setParity(parity)) { |
628 | d->parity.setValueBypassingBindings(parity); |
629 | if (currentParity != parity) { |
630 | d->parity.notify(); |
631 | emit parityChanged(parity); |
632 | } |
633 | return true; |
634 | } |
635 | return false; |
636 | } |
637 | |
638 | QSerialPort::Parity QSerialPort::parity() const |
639 | { |
640 | Q_D(const QSerialPort); |
641 | return d->parity; |
642 | } |
643 | |
644 | QBindable<QSerialPort::Parity> QSerialPort::bindableParity() |
645 | { |
646 | return &d_func()->parity; |
647 | } |
648 | |
649 | /*! |
650 | \fn void QSerialPort::parityChanged(Parity parity) |
651 | |
652 | This signal is emitted after the parity checking mode has been changed. The |
653 | new parity checking mode is passed as \a parity. |
654 | |
655 | \sa QSerialPort::parity |
656 | */ |
657 | |
658 | /*! |
659 | \property QSerialPort::stopBits |
660 | \brief the number of stop bits in a frame |
661 | |
662 | If the setting is successful or set before opening the port, returns \c true; |
663 | otherwise returns \c false and sets an error code which can be obtained by |
664 | accessing the value of the QSerialPort::error property. |
665 | |
666 | \note If the setting is set before opening the port, the actual serial port |
667 | setting is done automatically in the \l{QSerialPort::open()} method right |
668 | after that the opening of the port succeeds. |
669 | |
670 | The default value is OneStop, i.e. 1 stop bit. |
671 | */ |
672 | bool QSerialPort::setStopBits(StopBits stopBits) |
673 | { |
674 | Q_D(QSerialPort); |
675 | d->stopBits.removeBindingUnlessInWrapper(); |
676 | const auto currentStopBits = d->stopBits.value(); |
677 | if (!isOpen() || d->setStopBits(stopBits)) { |
678 | d->stopBits.setValueBypassingBindings(stopBits); |
679 | if (currentStopBits != stopBits) { |
680 | d->stopBits.notify(); |
681 | emit stopBitsChanged(stopBits); |
682 | } |
683 | return true; |
684 | } |
685 | return false; |
686 | } |
687 | |
688 | QSerialPort::StopBits QSerialPort::stopBits() const |
689 | { |
690 | Q_D(const QSerialPort); |
691 | return d->stopBits; |
692 | } |
693 | |
694 | QBindable<bool> QSerialPort::bindableStopBits() |
695 | { |
696 | return &d_func()->stopBits; |
697 | } |
698 | |
699 | /*! |
700 | \fn void QSerialPort::stopBitsChanged(StopBits stopBits) |
701 | |
702 | This signal is emitted after the number of stop bits in a frame has been |
703 | changed. The new number of stop bits in a frame is passed as \a stopBits. |
704 | |
705 | \sa QSerialPort::stopBits |
706 | */ |
707 | |
708 | /*! |
709 | \property QSerialPort::flowControl |
710 | \brief the desired flow control mode |
711 | |
712 | If the setting is successful or set before opening the port, returns \c true; |
713 | otherwise returns \c false and sets an error code which can be obtained by |
714 | accessing the value of the QSerialPort::error property. |
715 | |
716 | \note If the setting is set before opening the port, the actual serial port |
717 | setting is done automatically in the \l{QSerialPort::open()} method right |
718 | after that the opening of the port succeeds. |
719 | |
720 | The default value is NoFlowControl, i.e. no flow control. |
721 | */ |
722 | bool QSerialPort::setFlowControl(FlowControl flowControl) |
723 | { |
724 | Q_D(QSerialPort); |
725 | d->flowControl.removeBindingUnlessInWrapper(); |
726 | const auto currentFlowControl = d->flowControl.value(); |
727 | if (!isOpen() || d->setFlowControl(flowControl)) { |
728 | d->flowControl.setValueBypassingBindings(flowControl); |
729 | if (currentFlowControl != flowControl) { |
730 | d->flowControl.notify(); |
731 | emit flowControlChanged(flowControl); |
732 | } |
733 | return true; |
734 | } |
735 | return false; |
736 | } |
737 | |
738 | QSerialPort::FlowControl QSerialPort::flowControl() const |
739 | { |
740 | Q_D(const QSerialPort); |
741 | return d->flowControl; |
742 | } |
743 | |
744 | QBindable<QSerialPort::FlowControl> QSerialPort::bindableFlowControl() |
745 | { |
746 | return &d_func()->flowControl; |
747 | } |
748 | |
749 | /*! |
750 | \fn void QSerialPort::flowControlChanged(FlowControl flow) |
751 | |
752 | This signal is emitted after the flow control mode has been changed. The |
753 | new flow control mode is passed as \a flow. |
754 | |
755 | \sa QSerialPort::flowControl |
756 | */ |
757 | |
758 | /*! |
759 | \property QSerialPort::dataTerminalReady |
760 | \brief the state (high or low) of the line signal DTR |
761 | |
762 | Returns \c true on success, \c false otherwise. |
763 | If the flag is \c true then the DTR signal is set to high; otherwise low. |
764 | |
765 | \note The serial port has to be open before trying to set or get this |
766 | property; otherwise \c false is returned and the error code is set to |
767 | NotOpenError. |
768 | |
769 | \sa pinoutSignals() |
770 | */ |
771 | bool QSerialPort::setDataTerminalReady(bool set) |
772 | { |
773 | Q_D(QSerialPort); |
774 | |
775 | if (!isOpen()) { |
776 | d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); |
777 | qWarning(msg: "%s: device not open" , Q_FUNC_INFO); |
778 | return false; |
779 | } |
780 | |
781 | const bool dataTerminalReady = isDataTerminalReady(); |
782 | const bool retval = d->setDataTerminalReady(set); |
783 | if (retval && (dataTerminalReady != set)) |
784 | emit dataTerminalReadyChanged(set); |
785 | |
786 | return retval; |
787 | } |
788 | |
789 | bool QSerialPort::isDataTerminalReady() |
790 | { |
791 | Q_D(QSerialPort); |
792 | return d->pinoutSignals() & QSerialPort::DataTerminalReadySignal; |
793 | } |
794 | |
795 | /*! |
796 | \fn void QSerialPort::dataTerminalReadyChanged(bool set) |
797 | |
798 | This signal is emitted after the state (high or low) of the line signal DTR |
799 | has been changed. The new the state (high or low) of the line signal DTR is |
800 | passed as \a set. |
801 | |
802 | \sa QSerialPort::dataTerminalReady |
803 | */ |
804 | |
805 | /*! |
806 | \property QSerialPort::requestToSend |
807 | \brief the state (high or low) of the line signal RTS |
808 | |
809 | Returns \c true on success, \c false otherwise. |
810 | If the flag is \c true then the RTS signal is set to high; otherwise low. |
811 | |
812 | \note The serial port has to be open before trying to set or get this |
813 | property; otherwise \c false is returned and the error code is set to |
814 | NotOpenError. |
815 | |
816 | \note An attempt to control the RTS signal in the HardwareControl mode |
817 | will fail with error code set to UnsupportedOperationError, because |
818 | the signal is automatically controlled by the driver. |
819 | |
820 | \sa pinoutSignals() |
821 | */ |
822 | bool QSerialPort::setRequestToSend(bool set) |
823 | { |
824 | Q_D(QSerialPort); |
825 | |
826 | if (!isOpen()) { |
827 | d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); |
828 | qWarning(msg: "%s: device not open" , Q_FUNC_INFO); |
829 | return false; |
830 | } |
831 | |
832 | if (d->flowControl == QSerialPort::HardwareControl) { |
833 | d->setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); |
834 | return false; |
835 | } |
836 | |
837 | const bool requestToSend = isRequestToSend(); |
838 | const bool retval = d->setRequestToSend(set); |
839 | if (retval && (requestToSend != set)) |
840 | emit requestToSendChanged(set); |
841 | |
842 | return retval; |
843 | } |
844 | |
845 | bool QSerialPort::isRequestToSend() |
846 | { |
847 | Q_D(QSerialPort); |
848 | return d->pinoutSignals() & QSerialPort::RequestToSendSignal; |
849 | } |
850 | |
851 | /*! |
852 | \fn void QSerialPort::requestToSendChanged(bool set) |
853 | |
854 | This signal is emitted after the state (high or low) of the line signal RTS |
855 | has been changed. The new the state (high or low) of the line signal RTS is |
856 | passed as \a set. |
857 | |
858 | \sa QSerialPort::requestToSend |
859 | */ |
860 | |
861 | /*! |
862 | Returns the state of the line signals in a bitmap format. |
863 | |
864 | From this result, it is possible to allocate the state of the |
865 | desired signal by applying a mask "AND", where the mask is |
866 | the desired enumeration value from QSerialPort::PinoutSignals. |
867 | |
868 | \note This method performs a system call, thus ensuring that the line signal |
869 | states are returned properly. This is necessary when the underlying |
870 | operating systems cannot provide proper notifications about the changes. |
871 | |
872 | \note The serial port has to be open before trying to get the pinout |
873 | signals; otherwise returns NoSignal and sets the NotOpenError error code. |
874 | |
875 | \sa QSerialPort::dataTerminalReady, QSerialPort::requestToSend |
876 | */ |
877 | QSerialPort::PinoutSignals QSerialPort::pinoutSignals() |
878 | { |
879 | Q_D(QSerialPort); |
880 | |
881 | if (!isOpen()) { |
882 | d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); |
883 | qWarning(msg: "%s: device not open" , Q_FUNC_INFO); |
884 | return QSerialPort::NoSignal; |
885 | } |
886 | |
887 | return d->pinoutSignals(); |
888 | } |
889 | |
890 | /*! |
891 | This function writes as much as possible from the internal write |
892 | buffer to the underlying serial port without blocking. If any data |
893 | was written, this function returns \c true; otherwise returns \c false. |
894 | |
895 | Call this function for sending the buffered data immediately to the serial |
896 | port. The number of bytes successfully written depends on the operating |
897 | system. In most cases, this function does not need to be called, because the |
898 | QSerialPort class will start sending data automatically once control is |
899 | returned to the event loop. In the absence of an event loop, call |
900 | waitForBytesWritten() instead. |
901 | |
902 | \note The serial port has to be open before trying to flush any buffered |
903 | data; otherwise returns \c false and sets the NotOpenError error code. |
904 | |
905 | \sa write(), waitForBytesWritten() |
906 | */ |
907 | bool QSerialPort::flush() |
908 | { |
909 | Q_D(QSerialPort); |
910 | |
911 | if (!isOpen()) { |
912 | d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); |
913 | qWarning(msg: "%s: device not open" , Q_FUNC_INFO); |
914 | return false; |
915 | } |
916 | |
917 | return d->flush(); |
918 | } |
919 | |
920 | /*! |
921 | Discards all characters from the output or input buffer, depending on |
922 | given directions \a directions. This includes clearing the internal class buffers and |
923 | the UART (driver) buffers. Also terminate pending read or write operations. |
924 | If successful, returns \c true; otherwise returns \c false. |
925 | |
926 | \note The serial port has to be open before trying to clear any buffered |
927 | data; otherwise returns \c false and sets the NotOpenError error code. |
928 | */ |
929 | bool QSerialPort::clear(Directions directions) |
930 | { |
931 | Q_D(QSerialPort); |
932 | |
933 | if (!isOpen()) { |
934 | d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); |
935 | qWarning(msg: "%s: device not open" , Q_FUNC_INFO); |
936 | return false; |
937 | } |
938 | |
939 | if (directions & Input) |
940 | d->buffer.clear(); |
941 | if (directions & Output) |
942 | d->writeBuffer.clear(); |
943 | return d->clear(directions); |
944 | } |
945 | |
946 | /*! |
947 | \property QSerialPort::error |
948 | \brief the error status of the serial port |
949 | |
950 | The I/O device status returns an error code. For example, if open() |
951 | returns \c false, or a read/write operation returns \c -1, this property can |
952 | be used to figure out the reason why the operation failed. |
953 | |
954 | The error code is set to the default QSerialPort::NoError after a call to |
955 | clearError() |
956 | */ |
957 | QSerialPort::SerialPortError QSerialPort::error() const |
958 | { |
959 | Q_D(const QSerialPort); |
960 | return d->error; |
961 | } |
962 | |
963 | void QSerialPort::clearError() |
964 | { |
965 | Q_D(QSerialPort); |
966 | d->setError(QSerialPortErrorInfo(QSerialPort::NoError)); |
967 | } |
968 | |
969 | QBindable<QSerialPort::SerialPortError> QSerialPort::bindableError() const |
970 | { |
971 | return &d_func()->error; |
972 | } |
973 | |
974 | /*! |
975 | \fn void QSerialPort::errorOccurred(SerialPortError error) |
976 | \since 5.8 |
977 | |
978 | This signal is emitted when an error occurs in the serial port. |
979 | The specified \a error describes the type of error that occurred. |
980 | |
981 | \sa QSerialPort::error |
982 | */ |
983 | |
984 | /*! |
985 | Returns the size of the internal read buffer. This limits the |
986 | amount of data that the client can receive before calling the read() |
987 | or readAll() methods. |
988 | |
989 | A read buffer size of \c 0 (the default) means that the buffer has |
990 | no size limit, ensuring that no data is lost. |
991 | |
992 | \sa setReadBufferSize(), read() |
993 | */ |
994 | qint64 QSerialPort::readBufferSize() const |
995 | { |
996 | Q_D(const QSerialPort); |
997 | return d->readBufferMaxSize; |
998 | } |
999 | |
1000 | /*! |
1001 | Sets the size of QSerialPort's internal read buffer to be \a |
1002 | size bytes. |
1003 | |
1004 | If the buffer size is limited to a certain size, QSerialPort |
1005 | will not buffer more than this size of data. The special case of a buffer |
1006 | size of \c 0 means that the read buffer is unlimited and all |
1007 | incoming data is buffered. This is the default. |
1008 | |
1009 | This option is useful if the data is only read at certain points |
1010 | in time (for instance in a real-time streaming application) or if the serial |
1011 | port should be protected against receiving too much data, which may |
1012 | eventually cause the application to run out of memory. |
1013 | |
1014 | \sa readBufferSize(), read() |
1015 | */ |
1016 | void QSerialPort::setReadBufferSize(qint64 size) |
1017 | { |
1018 | Q_D(QSerialPort); |
1019 | d->readBufferMaxSize = size; |
1020 | if (isReadable()) |
1021 | d->startAsyncRead(); |
1022 | } |
1023 | |
1024 | /*! |
1025 | \reimp |
1026 | |
1027 | Always returns \c true. The serial port is a sequential device. |
1028 | */ |
1029 | bool QSerialPort::isSequential() const |
1030 | { |
1031 | return true; |
1032 | } |
1033 | |
1034 | /*! |
1035 | \reimp |
1036 | |
1037 | Returns the number of incoming bytes that are waiting to be read. |
1038 | |
1039 | \sa bytesToWrite(), read() |
1040 | */ |
1041 | qint64 QSerialPort::bytesAvailable() const |
1042 | { |
1043 | return QIODevice::bytesAvailable(); |
1044 | } |
1045 | |
1046 | /*! |
1047 | \reimp |
1048 | |
1049 | Returns the number of bytes that are waiting to be written. The |
1050 | bytes are written when control goes back to the event loop or |
1051 | when flush() is called. |
1052 | |
1053 | \sa bytesAvailable(), flush() |
1054 | */ |
1055 | qint64 QSerialPort::bytesToWrite() const |
1056 | { |
1057 | qint64 pendingBytes = QIODevice::bytesToWrite(); |
1058 | #if defined(Q_OS_WIN32) |
1059 | pendingBytes += d_func()->writeChunkBuffer.size(); |
1060 | #endif |
1061 | return pendingBytes; |
1062 | } |
1063 | |
1064 | /*! |
1065 | \reimp |
1066 | |
1067 | Returns \c true if a line of data can be read from the serial port; |
1068 | otherwise returns \c false. |
1069 | |
1070 | \sa readLine() |
1071 | */ |
1072 | bool QSerialPort::canReadLine() const |
1073 | { |
1074 | return QIODevice::canReadLine(); |
1075 | } |
1076 | |
1077 | /*! |
1078 | \reimp |
1079 | |
1080 | This function blocks until new data is available for reading and the |
1081 | \l{QIODevice::}{readyRead()} signal has been emitted. The function |
1082 | will timeout after \a msecs milliseconds; the default timeout is |
1083 | 30000 milliseconds. If \a msecs is -1, this function will not time out. |
1084 | |
1085 | The function returns \c true if the readyRead() signal is emitted and |
1086 | there is new data available for reading; otherwise it returns \c false |
1087 | (if an error occurred or the operation timed out). |
1088 | |
1089 | \sa waitForBytesWritten() |
1090 | */ |
1091 | bool QSerialPort::waitForReadyRead(int msecs) |
1092 | { |
1093 | Q_D(QSerialPort); |
1094 | return d->waitForReadyRead(msec: msecs); |
1095 | } |
1096 | |
1097 | /*! |
1098 | \fn Handle QSerialPort::handle() const |
1099 | \since 5.2 |
1100 | |
1101 | If the platform is supported and the serial port is open, returns the native |
1102 | serial port handle; otherwise returns \c -1. |
1103 | |
1104 | \warning This function is for expert use only; use it at your own risk. |
1105 | Furthermore, this function carries no compatibility promise between minor |
1106 | Qt releases. |
1107 | */ |
1108 | |
1109 | /*! |
1110 | \reimp |
1111 | |
1112 | This function blocks until at least one byte has been written to the serial |
1113 | port and the \l{QIODevice::}{bytesWritten()} signal has been emitted. The |
1114 | function will timeout after \a msecs milliseconds; the default timeout is |
1115 | 30000 milliseconds. If \a msecs is -1, this function will not time out. |
1116 | |
1117 | The function returns \c true if the bytesWritten() signal is emitted; otherwise |
1118 | it returns \c false (if an error occurred or the operation timed out). |
1119 | */ |
1120 | bool QSerialPort::waitForBytesWritten(int msecs) |
1121 | { |
1122 | Q_D(QSerialPort); |
1123 | return d->waitForBytesWritten(msec: msecs); |
1124 | } |
1125 | |
1126 | /*! |
1127 | \property QSerialPort::breakEnabled |
1128 | \since 5.5 |
1129 | \brief the state of the transmission line in break |
1130 | |
1131 | Returns \c true on success, \c false otherwise. |
1132 | If the flag is \c true then the transmission line is in break state; |
1133 | otherwise is in non-break state. |
1134 | |
1135 | \note The serial port has to be open before trying to set or get this |
1136 | property; otherwise returns \c false and sets the NotOpenError error code. |
1137 | This is a bit unusual as opposed to the regular Qt property settings of |
1138 | a class. However, this is a special use case since the property is set |
1139 | through the interaction with the kernel and hardware. Hence, the two |
1140 | scenarios cannot be completely compared to each other. |
1141 | */ |
1142 | bool QSerialPort::setBreakEnabled(bool set) |
1143 | { |
1144 | Q_D(QSerialPort); |
1145 | d->isBreakEnabled.removeBindingUnlessInWrapper(); |
1146 | const auto currentSet = d->isBreakEnabled.value(); |
1147 | if (isOpen()) { |
1148 | if (d->setBreakEnabled(set)) { |
1149 | d->isBreakEnabled.setValueBypassingBindings(set); |
1150 | if (currentSet != set) { |
1151 | d->isBreakEnabled.notify(); |
1152 | emit breakEnabledChanged(set); |
1153 | } |
1154 | return true; |
1155 | } |
1156 | } else { |
1157 | d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); |
1158 | qWarning(msg: "%s: device not open" , Q_FUNC_INFO); |
1159 | } |
1160 | return false; |
1161 | } |
1162 | |
1163 | bool QSerialPort::isBreakEnabled() const |
1164 | { |
1165 | Q_D(const QSerialPort); |
1166 | return d->isBreakEnabled; |
1167 | } |
1168 | |
1169 | QBindable<bool> QSerialPort::bindableIsBreakEnabled() |
1170 | { |
1171 | return &d_func()->isBreakEnabled; |
1172 | } |
1173 | |
1174 | /*! |
1175 | \reimp |
1176 | |
1177 | \omit |
1178 | This function does not really read anything, as we use QIODevicePrivate's |
1179 | buffer. The buffer will be read inside of QIODevice before this |
1180 | method will be called. |
1181 | \endomit |
1182 | */ |
1183 | qint64 QSerialPort::readData(char *data, qint64 maxSize) |
1184 | { |
1185 | Q_UNUSED(data); |
1186 | Q_UNUSED(maxSize); |
1187 | |
1188 | // In any case we need to start the notifications if they were |
1189 | // disabled by the read handler. If enabled, next call does nothing. |
1190 | d_func()->startAsyncRead(); |
1191 | |
1192 | // return 0 indicating there may be more data in the future |
1193 | return qint64(0); |
1194 | } |
1195 | |
1196 | /*! |
1197 | \reimp |
1198 | */ |
1199 | qint64 QSerialPort::readLineData(char *data, qint64 maxSize) |
1200 | { |
1201 | return QIODevice::readLineData(data, maxlen: maxSize); |
1202 | } |
1203 | |
1204 | /*! |
1205 | \reimp |
1206 | */ |
1207 | qint64 QSerialPort::writeData(const char *data, qint64 maxSize) |
1208 | { |
1209 | Q_D(QSerialPort); |
1210 | return d->writeData(data, maxSize); |
1211 | } |
1212 | |
1213 | QT_END_NAMESPACE |
1214 | |
1215 | #include "moc_qserialport.cpp" |
1216 | |