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