QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AndroidSerialPort.cc
Go to the documentation of this file.
1#include "AndroidSerialPort.h"
2
3#include <cstring>
4#include <utility>
5
6#include <QtCore/QByteArrayView>
7#include <QtCore/QDeadlineTimer>
8#include <QtCore/QMetaObject>
9#include <QtCore/QScopedValueRollback>
10
13#include "QGCLoggingCategory.h"
14
15QGC_LOGGING_CATEGORY(AndroidSerialPortLog, "Android.AndroidSerialPort")
16// What crosses each port, for debugging a link in the field, where the app's own log is the one at hand. It is
17// named beside the port's category, not below it: --logging switches a category on with all those below it.
18QGC_LOGGING_CATEGORY(AndroidSerialPortTraceLog, "Android.AndroidSerialPort:trace")
19
20namespace {
21
22// Best-effort send of buffered writes when closing; a stalled link must not delay close.
23constexpr int CLOSE_WRITE_TIMEOUT_MS = 100;
24// A transfer may be thousands of bytes, and its start tells which message it is.
25constexpr qint64 MAX_TRACED_BYTES = 32;
26
28QString tracedTransfer(const char* direction, const char* bytes, qint64 length)
29{
30 const QByteArray shown = QByteArray::fromRawData(bytes, static_cast<qsizetype>(qMin(length, MAX_TRACED_BYTES)));
31 return QStringLiteral("%1 %2: %3%4")
32 .arg(QLatin1StringView(direction), QString::number(length), QString::fromLatin1(shown.toHex(' ').toUpper()),
33 (length > MAX_TRACED_BYTES) ? QStringLiteral(" ...") : QString());
34}
35
38QString errorText(QSerialPort::SerialPortError error)
39{
40 switch (error) {
42 return QSerialPort::tr("No error");
44 return QSerialPort::tr("Device is already open");
46 return QSerialPort::tr("Device is not open");
48 return QSerialPort::tr("Operation timed out");
50 return QSerialPort::tr("Error reading from device");
52 return QSerialPort::tr("Error writing to device");
54 return QSerialPort::tr("Device disappeared from the system");
55 default:
56 return QString();
57 }
58}
59
60} // namespace
61
63 : QIODevice(parent)
64{}
65
66QSerialPort::QSerialPort(const QString& name, QObject* parent)
67 : QIODevice(parent)
68{
69 setPortName(name);
70}
71
72QSerialPort::QSerialPort(const QSerialPortInfo& info, QObject* parent)
73 : QIODevice(parent)
74{
75 setPort(info);
76}
77
79{
80 if (isOpen()) {
81 close();
82 }
83}
84
85void QSerialPort::setPortName(const QString& name)
86{
87 _systemLocation = QSerialPortInfo::portNameToSystemLocation(name);
88}
89
90QString QSerialPort::portName() const
91{
92 return QSerialPortInfo::portNameFromSystemLocation(_systemLocation);
93}
94
96{
97 _systemLocation = info.systemLocation();
98}
99
100bool QSerialPort::open(OpenMode mode)
101{
102 if (isOpen()) {
103 _setError(OpenError);
104 return false;
105 }
106
107 if ((mode & (Append | Truncate | Text | Unbuffered)) || (mode == NotOpen)) {
108 _setError(UnsupportedOperationError, tr("Unsupported open mode"));
109 return false;
110 }
111
112 clearError();
113 if (!_openDevice(mode)) {
114 _closeDevice();
115 _discardBuffers();
116 return false;
117 }
118
119 // The port holds its input itself, so QIODevice is not to keep a copy of what the owner reads.
120 return QIODevice::open(mode | Unbuffered);
121}
122
123bool QSerialPort::_openDevice(OpenMode mode)
124{
125 qCDebug(AndroidSerialPortLog) << "Opening" << _systemLocation;
126
127 AndroidSerialBackend* const backend = _backendFor(_systemLocation);
128 if (!backend->open(mode)) {
129 return false;
130 }
131 _backend = backend;
132
133 if (!backend->setParameters(_baudRate, _dataBits, _stopBits, _parity, tr("Failed to set parameters"))) {
134 return false;
135 }
136
137 if (!backend->setFlowControl(_flowControl)) {
138 qCWarning(AndroidSerialPortLog) << "Failed to set serial port flow control for" << _systemLocation;
139 return false;
140 }
141
142 if ((mode & ReadOnly) && !_readOn()) {
143 qCWarning(AndroidSerialPortLog) << "Failed to start async read for" << _systemLocation;
144 return false;
145 }
146
147 if (!_clearDevice(backend, AllDirections)) {
148 qCWarning(AndroidSerialPortLog) << "Failed to clear buffers for" << _systemLocation;
149 return false;
150 }
151
152 return true;
153}
154
156{
157 if (!isOpen()) {
158 _setError(NotOpenError);
159 return;
160 }
161
162 // Buffered writes were accepted by write(); try to send them unless the port can no longer write.
163 if ((_error != WriteError) && (_error != ResourceError)) {
164 _flushBeforeClose();
165 }
166 _closeDevice();
167 _breakEnabled = false;
168 // Its aboutToClose() is the owner's last chance to read the input that it was told of.
169 QIODevice::close();
170 _discardBuffers();
171}
172
173void QSerialPort::_closeDevice()
174{
175 if (AndroidSerialBackend* const backend = std::exchange(_backend, nullptr)) {
176 qCDebug(AndroidSerialPortLog) << "Closing" << _systemLocation;
177 backend->close();
178 }
179 _dataTerminalReady.reset();
180 _requestToSend.reset();
181}
182
183void QSerialPort::_discardBuffers()
184{
185 _output.clear();
186 _bytesInFlight = 0;
187 _discardedInFlight = 0;
188
189 // The backend is closed, so no more input or report can arrive. Dropping what was not delivered keeps a
190 // queued admission, and a later re-open, from delivering stale bytes.
191 QMutexLocker locker(&_arrivalMutex);
192 _input.clear();
193 _admitted = 0;
194 _admissionPending.store(false);
195 _readingHeldBack = false;
196 _completedWriteBytes = 0;
197 _writeProgress = false;
198 _waitInterrupted = false;
199}
200
201AndroidSerialBackend* QSerialPort::_backendFor(const QString& location)
202{
203 const bool usb = QSerialPortInfo::isUsbLocation(location);
204 std::unique_ptr<AndroidSerialBackend>& backend = usb ? _usbBackend : _posixBackend;
205 if (!backend) {
206 const AndroidSerialBackendHost host(*this);
207 backend = usb ? createUsbSerialBackend(host) : createPosixSerialBackend(host);
208 }
209
210 return backend.get();
211}
212
213AndroidSerialBackend* QSerialPort::_openBackend()
214{
215 if (!_backend) {
216 _setError(NotOpenError);
217 }
218
219 return _backend;
220}
221
222void QSerialPort::_setError(SerialPortError error, const QString& text)
223{
224 setErrorString(text.isNull() ? errorText(error) : text);
225 _error = error;
226 emit errorOccurred(error);
227}
228
230{
231 _setError(NoError);
232}
233
234bool QSerialPort::setBaudRate(qint32 baudRate, Directions directions)
235{
236 if (directions != AllDirections) {
237 qCWarning(AndroidSerialPortLog) << "Custom baud rate direction is unsupported:" << directions;
238 _setError(UnsupportedOperationError, tr("Custom baud rate direction is unsupported"));
239 return false;
240 }
241
242 if (_backend) {
243 // A closed port keeps any rate it is given; its backend refuses a bad one when the port opens.
244 if (baudRate <= 0) {
245 qCWarning(AndroidSerialPortLog) << "Invalid baud rate value:" << baudRate;
246 _setError(UnsupportedOperationError, tr("Invalid baud rate value"));
247 return false;
248 }
249 if (!_backend->setParameters(baudRate, _dataBits, _stopBits, _parity, tr("Failed to set baud rate"))) {
250 return false;
251 }
252 }
253
254 if (_baudRate != baudRate) {
255 _baudRate = baudRate;
257 }
258
259 return true;
260}
261
262qint32 QSerialPort::baudRate(Directions) const
263{
264 return _baudRate;
265}
266
268{
269 if (_backend && !_backend->setParameters(_baudRate, dataBits, _stopBits, _parity, tr("Failed to set data bits"))) {
270 return false;
271 }
272
273 if (_dataBits != dataBits) {
274 _dataBits = dataBits;
276 }
277
278 return true;
279}
280
282{
283 if (_backend && !_backend->setParameters(_baudRate, _dataBits, _stopBits, parity, tr("Failed to set parity"))) {
284 return false;
285 }
286
287 if (_parity != parity) {
288 _parity = parity;
289 emit parityChanged(parity);
290 }
291
292 return true;
293}
294
296{
297 if (_backend && !_backend->setParameters(_baudRate, _dataBits, stopBits, _parity, tr("Failed to set stop bits"))) {
298 return false;
299 }
300
301 if (_stopBits != stopBits) {
302 _stopBits = stopBits;
304 }
305
306 return true;
307}
308
310{
311 if (_backend && !_backend->setFlowControl(flowControl)) {
312 return false;
313 }
314
315 if (_flowControl != flowControl) {
316 _flowControl = flowControl;
318 }
319
320 return true;
321}
322
324{
325 AndroidSerialBackend* const backend = _openBackend();
326 if (!backend || !backend->setDataTerminalReady(set)) {
327 return false;
328 }
329
330 if (_dataTerminalReady != set) {
331 _dataTerminalReady = set;
332 emit dataTerminalReadyChanged(set);
333 }
334
335 return true;
336}
337
342
344{
345 AndroidSerialBackend* const backend = _openBackend();
346 if (!backend) {
347 return false;
348 }
349
350 if (_flowControl == HardwareControl) {
351 _setError(UnsupportedOperationError);
352 return false;
353 }
354
355 if (!backend->setRequestToSend(set)) {
356 return false;
357 }
358
359 if (_requestToSend != set) {
360 _requestToSend = set;
361 emit requestToSendChanged(set);
362 }
363
364 return true;
365}
366
371
372QSerialPort::PinoutSignals QSerialPort::pinoutSignals()
373{
374 AndroidSerialBackend* const backend = _openBackend();
375 return backend ? backend->pinoutSignals() : PinoutSignals(NoSignal);
376}
377
379{
380 AndroidSerialBackend* const backend = _openBackend();
381 if (!backend || !backend->setBreakEnabled(set)) {
382 return false;
383 }
384
385 if (_breakEnabled != set) {
386 _breakEnabled = set;
387 emit breakEnabledChanged(set);
388 }
389
390 return true;
391}
392
394{
395 return _backend ? _backend->descriptor() : -1;
396}
397
399{
400 {
401 // The thread that delivers input reads the limit.
402 QMutexLocker locker(&_arrivalMutex);
403 _readBufferMaxSize = size;
404 }
405
406 if (isReadable()) {
407 (void) _readOn();
408 }
409}
410
412{
413 return true;
414}
415
417{
418 // QIODevice holds input of its own only after a peek or within a transaction.
419 return QIODevice::bytesAvailable() + _admitted;
420}
421
423{
424 // Output that the backend has taken is not written until it says so.
425 return _output.size() + _bytesInFlight;
426}
427
429{
430 if (QIODevice::canReadLine()) {
431 return true;
432 }
433
434 QMutexLocker locker(&_arrivalMutex);
435 return QByteArrayView(_input).first(_admitted).contains('\n');
436}
437
438qint64 QSerialPort::readData(char* data, qint64 maxSize)
439{
440 qsizetype size = 0;
441 {
442 QMutexLocker locker(&_arrivalMutex);
443 size = static_cast<qsizetype>(qMin<qint64>(maxSize, _admitted));
444 if (size > 0) {
445 std::memcpy(data, _input.constData(), static_cast<size_t>(size));
446 _input.remove(0, size);
447 _admitted -= size;
448 }
449 }
450
451 // Every read comes here, also one that finds nothing. It may have made room for input that waited, and it
452 // makes sure that the backend reads.
453 (void) _readOn();
454
455 return size;
456}
457
458bool QSerialPort::_readOn()
459{
460 if (!_backend) {
461 return false;
462 }
463
464 bool inputWaits = false;
465 bool hasRoom = false;
466 bool resumed = false;
467 {
468 QMutexLocker locker(&_arrivalMutex);
469 inputWaits = _hiddenSizeLocked() > 0;
470 hasRoom = _hasRoomForInputLocked();
471 if (hasRoom) {
472 resumed = std::exchange(_readingHeldBack, false);
473 }
474 }
475
476 if (resumed) {
477 qCDebug(AndroidSerialPortTraceLog).noquote() << _systemLocation << "RX resumed";
478 }
479 if (inputWaits) {
480 _scheduleAdmission();
481 }
482
483 // The backend reads only while the port has room. The admission that makes room asks it to read on.
484 return !hasRoom || _backend->startReading();
485}
486
487bool QSerialPort::_readOnIfRoom()
488{
489 {
490 QMutexLocker locker(&_arrivalMutex);
491 if (!_readingHeldBack || !_hasRoomForInputLocked()) {
492 return true;
493 }
494 }
495
496 return _readOn();
497}
498
499qsizetype QSerialPort::_hiddenSizeLocked() const
500{
501 return _input.size() - _admitted;
502}
503
504bool QSerialPort::_hasRoomForInputLocked() const
505{
506 return (_readBufferMaxSize <= 0) || (_hiddenSizeLocked() < _readBufferMaxSize);
507}
508
509bool QSerialPort::_canAdmitInputLocked() const
510{
511 return (_hiddenSizeLocked() > 0) && ((_readBufferMaxSize <= 0) || (bytesAvailable() < _readBufferMaxSize));
512}
513
514bool QSerialPort::_admitInputLocked()
515{
516 if (!_canAdmitInputLocked()) {
517 return false;
518 }
519
520 const qsizetype hidden = _hiddenSizeLocked();
521 _admitted += (_readBufferMaxSize > 0)
522 ? static_cast<qsizetype>(qMin<qint64>(hidden, _readBufferMaxSize - bytesAvailable()))
523 : hidden;
524 return true;
525}
526
527bool QSerialPort::_admitArrivedInput()
528{
529 QMutexLocker locker(&_arrivalMutex);
530 return _admitInputLocked();
531}
532
533void QSerialPort::_scheduleAdmission()
534{
535 if (!_admissionPending.exchange(true)) {
536 QMetaObject::invokeMethod(this, &QSerialPort::_admitQueuedInput, Qt::QueuedConnection);
537 }
538}
539
540void QSerialPort::_admitQueuedInput()
541{
542 bool admitted = false;
543 {
544 QMutexLocker locker(&_arrivalMutex);
545 admitted = isOpen() && _admitInputLocked();
546 // Reset only now, so that input arriving meanwhile does not queue a second admission. Input that the
547 // read buffer size keeps hidden waits for a read to make room, which schedules this again.
548 _admissionPending.store(false);
549 }
550
551 if (admitted) {
552 _announceAdmittedInput();
553 }
554}
555
556void QSerialPort::_announceAdmittedInput()
557{
558 // Before the announcement, whose receiver may wait for more input.
559 (void) _readOnIfRoom();
560 // The port's owner may have closed it over an error of that.
561 if (!isOpen() || _emittingReadyRead) {
562 return;
563 }
564
565 // Like every QIODevice, not from inside one of its own slots, which may be waiting for more input.
566 const QScopedValueRollback<bool> emitting(_emittingReadyRead, true);
567 emit readyRead();
568}
569
570bool QSerialPort::_newDataArrived(const char* bytes, int length)
571{
572 qCDebug(AndroidSerialPortTraceLog).noquote() << _systemLocation << tracedTransfer("RX", bytes, length);
573
574 bool hasRoom = false;
575 bool paused = false;
576 {
577 QMutexLocker locker(&_arrivalMutex);
578 // Never dropped, like the input of desktop QSerialPort, which leaves what its buffer has no room for with
579 // the kernel. Here the backend leaves it with the device once it hears that there is no room.
580 _input.append(bytes, length);
581 hasRoom = _hasRoomForInputLocked();
582 paused = !hasRoom && !_readingHeldBack;
583 _readingHeldBack = !hasRoom;
584 _arrivalCondition.wakeAll();
585 }
586 if (paused) {
587 qCDebug(AndroidSerialPortTraceLog).noquote() << _systemLocation << "RX paused";
588 }
589
590 _scheduleAdmission();
591 return hasRoom;
592}
593
594bool QSerialPort::_hasRoomForInput()
595{
596 QMutexLocker locker(&_arrivalMutex);
597 return _hasRoomForInputLocked();
598}
599
600bool QSerialPort::_inputArrived()
601{
602 QMutexLocker locker(&_arrivalMutex);
603 return _canAdmitInputLocked();
604}
605
606void QSerialPort::_waitForArrivedInput(const QDeadlineTimer& deadline)
607{
608 QMutexLocker locker(&_arrivalMutex);
609 if (!_canAdmitInputLocked() && !_writeProgress && !_waitInterrupted) {
610 (void) _arrivalCondition.wait(&_arrivalMutex, deadline);
611 }
612 _waitInterrupted = false;
613}
614
615void QSerialPort::_writeCompleted(qint64 bytes)
616{
617 {
618 QMutexLocker locker(&_arrivalMutex);
619 _completedWriteBytes += bytes;
620 _writeProgress = true;
621 }
622 _arrivalCondition.wakeAll();
623
624 _scheduleWrite();
625}
626
627void QSerialPort::_waitForWriteProgress(const QDeadlineTimer& deadline)
628{
629 QMutexLocker locker(&_arrivalMutex);
630 if (!_writeProgress && !_waitInterrupted) {
631 (void) _arrivalCondition.wait(&_arrivalMutex, deadline);
632 }
633 _waitInterrupted = false;
634}
635
636void QSerialPort::_interruptWait()
637{
638 {
639 QMutexLocker locker(&_arrivalMutex);
640 _waitInterrupted = true;
641 }
642 _arrivalCondition.wakeAll();
643}
644
646{
647 const QDeadlineTimer deadline(msecs);
648
649 // The backend looks for input at least once, even if the time is already up.
650 bool expired = false;
651 for (;;) {
652 // Desktop QSerialPort also goes on sending pending writes while it waits for input, and gives the wait up
653 // when one of them fails.
654 if (!_pumpWrites()) {
655 return false;
656 }
657 // Input that the owner may read already was announced before: only new input ends the wait.
658 if (_admitArrivedInput()) {
659 _announceAdmittedInput();
660 return true;
661 }
662 if (expired) {
663 qCDebug(AndroidSerialPortLog) << "Timeout while waiting for ready read on" << _systemLocation;
664 _setError(TimeoutError, tr("Timeout while waiting for ready read"));
665 return false;
666 }
667
668 if (!_backend) {
669 // Like desktop QSerialPort, a port that is not open waits its time out.
670 _waitForArrivedInput(deadline);
671 } else if (!_backend->waitForInput(deadline)) {
672 return false;
673 }
674 expired = deadline.hasExpired();
675 }
676}
677
679{
680 if (bytesToWrite() == 0) {
681 return false;
682 }
683
684 if (!_writeUntil(QDeadlineTimer(msecs))) {
685 return false;
686 }
687 if (bytesToWrite() == 0) {
688 return true;
689 }
690
691 qCDebug(AndroidSerialPortLog) << "Write timed out on" << _systemLocation << "with" << bytesToWrite()
692 << "bytes left";
693 _setError(TimeoutError, tr("Timeout while writing data"));
694 return false;
695}
696
697qint64 QSerialPort::writeData(const char* data, qint64 maxSize)
698{
699 // QIODevice passes a write of nothing on, which is no error: one would keep close() from sending what waits.
700 if (maxSize == 0) {
701 return 0;
702 }
703
704 if (!data || (maxSize < 0)) {
705 qCWarning(AndroidSerialPortLog) << "Invalid data or size in writeData for" << _systemLocation;
706 _setError(WriteError, tr("Invalid data or size"));
707 return -1;
708 }
709
710 // Like desktop QSerialPort, writes are buffered and handed to the device from the owner's event loop,
711 // waitForBytesWritten(), waitForReadyRead(), flush() or close(), none of which waits for a device longer
712 // than it was told to.
713 _output.append(data, static_cast<qsizetype>(maxSize));
714 _scheduleWrite();
715 return maxSize;
716}
717
718void QSerialPort::_scheduleWrite()
719{
720 if (!_writePending.exchange(true)) {
721 QMetaObject::invokeMethod(this, &QSerialPort::_pumpQueuedWrites, Qt::QueuedConnection);
722 }
723}
724
725void QSerialPort::_pumpQueuedWrites()
726{
727 _writePending.store(false);
728 if (isOpen()) {
729 (void) _pumpWrites();
730 }
731}
732
733bool QSerialPort::_pumpWrites()
734{
735 qint64 written = 0;
736 {
737 QMutexLocker locker(&_arrivalMutex);
738 written = std::exchange(_completedWriteBytes, 0);
739 _writeProgress = false;
740 }
741 const qint64 discarded = qMin(written, _discardedInFlight);
742 _discardedInFlight -= discarded;
743 written = qMin(written - discarded, _bytesInFlight);
744 _bytesInFlight -= written;
745
746 bool succeeded = true;
747 if (_backend && (_bytesInFlight == 0) && !_output.isEmpty()) {
748 const qint64 taken = _backend->startWrite(_output.constData(), _output.size());
749 if (taken < 0) {
750 succeeded = false;
751 } else {
752 if (taken > 0) {
753 qCDebug(AndroidSerialPortTraceLog).noquote()
754 << _systemLocation << tracedTransfer("TX", _output.constData(), taken);
755 }
756 _output.remove(0, static_cast<qsizetype>(taken));
757 _bytesInFlight += taken;
758 }
759 }
760
761 if (written > 0) {
762 emit bytesWritten(written);
763 }
764
765 return succeeded;
766}
767
768bool QSerialPort::_writeUntil(const QDeadlineTimer& deadline)
769{
770 for (;;) {
771 if (!_pumpWrites()) {
772 return false;
773 }
774 if (bytesToWrite() == 0) {
775 return true;
776 }
777
778 AndroidSerialBackend* const backend = _openBackend();
779 if (!backend) {
780 return false;
781 }
782
783 bool progressWaiting = false;
784 {
785 QMutexLocker locker(&_arrivalMutex);
786 progressWaiting = _writeProgress;
787 }
788 // Counted before the time is looked at: a device node takes output while it is handed over, and the
789 // caller tells by bytesToWrite() whether the time ran out.
790 if (progressWaiting) {
791 continue;
792 }
793 if (deadline.hasExpired()) {
794 return true;
795 }
796 if (!backend->waitForWritable(deadline)) {
797 return false;
798 }
799 }
800}
801
802void QSerialPort::_flushBeforeClose()
803{
804 // Running out of time is no error here: the owner asked for the close, and a link shows every error to the
805 // user.
806 if (_writeUntil(QDeadlineTimer(CLOSE_WRITE_TIMEOUT_MS)) && (bytesToWrite() > 0)) {
807 qCDebug(AndroidSerialPortLog) << "Closing" << _systemLocation << "with" << bytesToWrite() << "bytes unsent";
808 }
809}
810
812{
813 if (!_openBackend()) {
814 return false;
815 }
816
817 const qint64 queued = _output.size();
818 return _pumpWrites() && (_output.size() < queued);
819}
820
821bool QSerialPort::clear(Directions directions)
822{
823 AndroidSerialBackend* const backend = _openBackend();
824 if (!backend) {
825 return false;
826 }
827
828 if (directions & Input) {
829 QMutexLocker locker(&_arrivalMutex);
830 _input.clear();
831 _admitted = 0;
832 }
833 if (directions & Output) {
834 _output.clear();
835 _discardedInFlight += std::exchange(_bytesInFlight, 0);
836 }
837
838 const bool cleared = _clearDevice(backend, directions);
839 // Only now, because a read of QIODevice has the backend read on.
840 if (const qint64 held = QIODevice::bytesAvailable(); (directions & Input) && (held > 0)) {
841 (void) skip(held);
842 }
843
844 return cleared;
845}
846
847bool QSerialPort::_clearDevice(AndroidSerialBackend* backend, Directions directions)
848{
849 // Before the port reads on, so that it reads nothing that the device held from before the clear. Input that
850 // had already left a USB device by then still arrives: nothing can call it back.
851 const bool cleared = backend->clear(directions);
852 // The input that waited is discarded, and holds reading back no longer.
853 const bool readingOn = !(directions & Input) || _readOnIfRoom();
854 return cleared && readingOn;
855}
std::unique_ptr< AndroidSerialBackend > createPosixSerialBackend(AndroidSerialBackendHost port)
A serial device node, such as /dev/ttyS1.
std::unique_ptr< AndroidSerialBackend > createUsbSerialBackend(AndroidSerialBackendHost port)
A USB serial adapter, which Java drives (AndroidSerial).
#define QGC_LOGGING_CATEGORY(name, categoryStr)
virtual bool setBreakEnabled(bool set)=0
virtual void close()=0
Closes the device, after which no more of its input reaches the port. Does nothing if it is closed.
virtual bool open(QIODevice::OpenMode mode)=0
Opens the device. The port applies its settings afterwards.
virtual bool setRequestToSend(bool set)=0
virtual bool setFlowControl(QSerialPort::FlowControl flowControl)=0
virtual bool waitForWritable(const QDeadlineTimer &deadline)=0
Blocks until the port's writeCompleted() may have been called, or the deadline has passed.
virtual bool clear(QSerialPort::Directions directions)=0
virtual bool setParameters(qint32 baudRate, QSerialPort::DataBits dataBits, QSerialPort::StopBits stopBits, QSerialPort::Parity parity, const QString &failureText)=0
A backend that cannot tell why the device refused describes the failure by failureText.
virtual bool setDataTerminalReady(bool set)=0
virtual QSerialPort::PinoutSignals pinoutSignals()=0
static QString portNameFromSystemLocation(const QString &systemLocation)
static bool isUsbLocation(const QString &systemLocation)
QString systemLocation() const
static QString portNameToSystemLocation(const QString &portName)
qint64 readData(char *data, qint64 maxSize) override
qint32 baudRate(Directions directions=AllDirections) const
bool setDataTerminalReady(bool set)
void requestToSendChanged(bool set)
void dataTerminalReadyChanged(bool set)
Parity parity() const
bool isSequential() const override
bool open(OpenMode mode) override
void close() override
bool setStopBits(StopBits stopBits)
void setPortName(const QString &name)
void baudRateChanged(qint32 baudRate, QSerialPort::Directions directions)
void parityChanged(QSerialPort::Parity parity)
qint64 writeData(const char *data, qint64 maxSize) override
bool waitForBytesWritten(int msecs=30000) override
void errorOccurred(QSerialPort::SerialPortError error)
Emitted whenever the error is set, also to NoError.
void setPort(const QSerialPortInfo &info)
bool waitForReadyRead(int msecs=30000) override
Returns true only for input that was not announced before.
StopBits stopBits() const
qint64 bytesToWrite() const override
void breakEnabledChanged(bool set)
bool clear(Directions directions=AllDirections)
bool setRequestToSend(bool set)
bool canReadLine() const override
QSerialPort(QObject *parent=nullptr)
void stopBitsChanged(QSerialPort::StopBits stopBits)
void dataBitsChanged(QSerialPort::DataBits dataBits)
bool flush()
Hands buffered output to the device, as far as it takes it now, and returns whether it took any.
friend class AndroidSerialBackendHost
PinoutSignals pinoutSignals()
FlowControl flowControl() const
QString portName() const
void flowControlChanged(QSerialPort::FlowControl flowControl)
bool setBreakEnabled(bool set=true)
DataBits dataBits() const
int handle() const
The file descriptor of the open device, or -1.
SerialPortError error() const
bool setBaudRate(qint32 baudRate, Directions directions=AllDirections)
~QSerialPort() override
bool setDataBits(DataBits dataBits)
bool setParity(Parity parity)
qint64 bytesAvailable() const override
bool setFlowControl(FlowControl flowControl)
void setReadBufferSize(qint64 size)