3#include <QtCore/QSettings>
4#include <QtCore/QSignalBlocker>
5#include <QtCore/QThread>
20 qCDebug(SerialLinkLog) <<
this;
26 qCDebug(SerialLinkLog) <<
this;
33 qCDebug(SerialLinkLog) <<
this;
60 qCWarning(SerialLinkLog) <<
"Invalid source configuration type";
80 settings.beginGroup(root);
82 setBaud(settings.value(
"baud", _baud).toInt());
90 setPortName(settings.value(
"portName", _portName).toString());
91 setdtrForceLow(settings.value(
"dtrForceLow", _dtrForceLow).toBool());
98 settings.beginGroup(root);
100 settings.setValue(
"baud", _baud);
101 settings.setValue(
"dataBits", _dataBits);
102 settings.setValue(
"flowControl", _flowControl);
103 settings.setValue(
"stopBits", _stopBits);
104 settings.setValue(
"parity", _parity);
105 settings.setValue(
"portName", _portName);
106 settings.setValue(
"portDisplayName", _portDisplayName);
107 settings.setValue(
"dtrForceLow", _dtrForceLow);
120 const bool bootloader =
121 std::ranges::any_of(ports, [
this](
const auto& port) {
return port.hasName(_portName) && port.bootloader; });
122 return {_portName, _baud, _dataBits, _flowControl, _stopBits, _parity, _dtrForceLow,
isAutoConnect(), bootloader};
134 : QObject(parent), _settings(std::move(settings)), _reservation(std::move(reservation))
136 qCDebug(SerialLinkLog) <<
this;
138 (void) qRegisterMetaType<QSerialPort::SerialPortError>(
"QSerialPort::SerialPortError");
145 qCDebug(SerialLinkLog) <<
this;
161 (void) connect(_port, &QSerialPort::aboutToClose,
this, &SerialWorker::_onPortDisconnected);
162 (void) connect(_port, &QSerialPort::readyRead,
this, &SerialWorker::_onPortReadyRead);
176 qCWarning(SerialLinkLog) <<
"Already connected to" << _port->portName();
180 _port->setPortName(_settings.portName);
182 if (_settings.bootloader) {
183 qCWarning(SerialLinkLog) <<
"Not connecting to bootloader" << _port->portName();
185 _onPortDisconnected();
189 _errorEmitted =
false;
191 qCDebug(SerialLinkLog) <<
"Attempting to open port" << _port->portName();
192 if (!_port->open(QIODevice::ReadWrite)) {
195 if (busyAutoConnect) {
196 qCDebug(SerialLinkLog) <<
"Auto-connect port unavailable:" << _port->portName() << _port->errorString();
198 qCWarning(SerialLinkLog) <<
"Opening port" << _port->portName() <<
"failed:" << _port->errorString();
202 if (!_errorEmitted && !busyAutoConnect) {
203 emit
errorOccurred(tr(
"Could not open port: %1").arg(_port->errorString()));
204 _errorEmitted =
true;
207 _onPortDisconnected();
212 if (!_configurePort()) {
213 const QString error = _port->errorString();
214 emit
errorOccurred(tr(
"Could not configure port: %1").arg(error));
223 if (!_port || !_port->isOpen()) {
227 qCDebug(SerialLinkLog) <<
"Attempting to close port:" << _port->portName();
234 if (data.isEmpty()) {
244 if (!_port->isWritable()) {
249 qint64 totalBytesWritten = 0;
250 while (totalBytesWritten < data.size()) {
251 const qint64 bytesWritten = _port->write(data.constData() + totalBytesWritten, data.size() - totalBytesWritten);
252 if (bytesWritten == -1) {
253 emit
errorOccurred(tr(
"Could Not Send Data - Write Failed: %1").arg(_port->errorString()));
255 }
else if (bytesWritten == 0) {
256 emit
errorOccurred(tr(
"Could Not Send Data - Write Returned 0 Bytes"));
259 totalBytesWritten += bytesWritten;
262 const QByteArray sent = data.first(totalBytesWritten);
266void SerialWorker::_onPortConnected()
268 qCDebug(SerialLinkLog) <<
"Port connected:" << _port->
portName();
271 _errorEmitted =
false;
275void SerialWorker::_onPortDisconnected()
277 qCDebug(SerialLinkLog) <<
"Port disconnected:" << _port->portName();
279 _isConnected =
false;
280 _errorEmitted =
false;
284void SerialWorker::_onPortReadyRead()
286 const QByteArray data = _port->readAll();
287 if (!data.isEmpty()) {
293void SerialWorker::_onPortBytesWritten(qint64 bytes)
const
295 qCDebug(SerialLinkLog) << _port->portName() <<
"Wrote" << bytes <<
"bytes";
302 qCDebug(SerialLinkLog) <<
"About to open port" << _port->portName();
306 qCDebug(SerialLinkLog) <<
"Resource error (likely USB disconnect):" << _port->errorString();
311 if (_settings.autoConnect) {
319 const QString errorString = _port->errorString();
320 qCWarning(SerialLinkLog) <<
"Port error:" << portError << errorString;
322 if (!_errorEmitted) {
324 _errorEmitted =
true;
330 if (
isConnected() && !availablePorts.contains(_settings.portName)) {
335bool SerialWorker::_configurePort()
338 const QSignalBlocker blocker(_port);
344 if (!_port->setDataTerminalReady(!_settings.dtrForceLow)) {
349 qCDebug(SerialLinkLog) <<
"DTR is unavailable on" << _settings.portName;
355 if (!_port->setRequestToSend(
true)) {
359 qCDebug(SerialLinkLog) <<
"RTS is unavailable on" << _settings.portName;
371 _reservation(std::move(reservation)),
372 _worker(new
SerialWorker(_serialConfig->connectionSettings(), _reservation)),
373 _workerThread(new QThread(this))
375 qCDebug(SerialLinkLog) <<
this;
377 _workerThread->setObjectName(QStringLiteral(
"Serial_%1").arg(_serialConfig->name()));
379 (void) _worker->moveToThread(_workerThread);
382 (void) connect(_workerThread, &QThread::finished, _worker, &QObject::deleteLater);
393 _workerThread->start();
402 (void) QMetaObject::invokeMethod(_worker,
"disconnectFromPort", Qt::QueuedConnection);
408 qCDebug(SerialLinkLog) <<
this;
413 return _worker && _worker->isConnected();
416bool SerialLink::_connect()
418 return QMetaObject::invokeMethod(_worker,
"connectToPort", Qt::QueuedConnection);
424 (void) QMetaObject::invokeMethod(_worker,
"disconnectFromPort", Qt::QueuedConnection);
428void SerialLink::_onConnected()
430 _disconnectedEmitted =
false;
434void SerialLink::_onDisconnected()
436 if (!_disconnectedEmitted.exchange(
true)) {
441void SerialLink::_onErrorOccurred(
const QString &errorString)
443 qCWarning(SerialLinkLog) <<
"Communication error:" << errorString;
444 emit
communicationError(tr(
"Serial Link Error"), tr(
"Link %1: (Port: %2) %3").arg(_serialConfig->name(), _serialConfig->portName(), errorString));
447void SerialLink::_onDataReceived(
const QByteArray &data)
452void SerialLink::_onDataSent(
const QByteArray &data)
457void SerialLink::_writeBytes(
const QByteArray &data)
459 (void) QMetaObject::invokeMethod(_worker,
"writeData", Qt::QueuedConnection, Q_ARG(QByteArray, data));
std::shared_ptr< LinkConfiguration > SharedLinkConfigurationPtr
#define QGC_LOGGING_CATEGORY(name, categoryStr)
virtual void copyFrom(const LinkConfiguration *source)
bool isAutoConnect() const
LinkConfiguration(const QString &name, QObject *parent=nullptr)
void bytesReceived(LinkInterface *link, const QByteArray &data)
void _shutdownWorkerThread(QThread *thread, const QLoggingCategory &category, bool allowTerminate=true)
LinkInterface(SharedLinkConfigurationPtr &config, QObject *parent=nullptr)
Links are only created by LinkManager so constructor is not public.
void communicationError(const QString &title, const QString &error)
void bytesSent(LinkInterface *link, const QByteArray &data)
bool setStopBits(StopBits stopBits)
void errorOccurred(QSerialPort::SerialPortError error)
Emitted whenever the error is set, also to NoError.
@ UnsupportedOperationError
bool setBaudRate(qint32 baudRate, Directions directions=AllDirections)
bool setDataBits(DataBits dataBits)
bool setParity(Parity parity)
bool setFlowControl(FlowControl flowControl)
void setParity(QSerialPort::Parity parity)
QSerialPort::FlowControl flowControl() const
bool sikRadio() const
Port was identified as a SiK radio by USB VID/PID at auto-connect. Runtime only, not persisted.
void loadSettings(QSettings &settings, const QString &root) override
QSerialPort::StopBits stopBits() const
void saveSettings(QSettings &settings, const QString &root) const override
SerialConnectionSettings connectionSettings() const
void setStopBits(QSerialPort::StopBits stopBits)
void setPortDisplayName(const QString &portDisplayName)
void setBaud(qint32 baud)
SerialConfiguration(const QString &name, QObject *parent=nullptr)
void setDataBits(QSerialPort::DataBits databits)
void setdtrForceLow(bool dtrForceLow)
static QStringList supportedBaudRates()
QString portDisplayName() const
QSerialPort::Parity parity() const
void setUsbDirect(bool usbDirect)
QSerialPort::DataBits dataBits() const
void copyFrom(const LinkConfiguration *source) override
static QString cleanPortDisplayName(const QString &name)
void setFlowControl(QSerialPort::FlowControl flowControl)
void setSikRadio(bool sikRadio)
void setPortName(const QString &name)
virtual ~SerialConfiguration()
SerialLink(SharedLinkConfigurationPtr &config, SerialPortManager::ReservationPtr reservation, QObject *parent=nullptr)
void disconnect() override
bool isConnected() const override
static SerialPortManager * instance()
std::shared_ptr< const Reservation > ReservationPtr
static QStringList supportedBaudRates()
QList< Port > availablePorts()
QString displayName(const QString &systemLocation)
void portsEnumerated(const QStringList &availablePorts)
Emitted only after a fresh scan, with every name of the ports it lists.
void dataReceived(const QByteArray &data)
void disconnectFromPort()
void checkPortAvailability(const QStringList &availablePorts)
void dataSent(const QByteArray &data)
void writeData(const QByteArray &data)
SerialWorker(SerialConnectionSettings settings, SerialPortManager::ReservationPtr reservation={}, QObject *parent=nullptr)
void errorOccurred(const QString &errorString)
Captured on the application thread; changes take effect on the next connection.
QSerialPort::StopBits stopBits
QSerialPort::DataBits dataBits
QSerialPort::FlowControl flowControl
QSerialPort::Parity parity