SerialLink.cc 14.5 KB
Newer Older
1
/****************************************************************************
pixhawk's avatar
pixhawk committed
2
 *
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
pixhawk's avatar
pixhawk committed
4
 *
5 6 7 8
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
pixhawk's avatar
pixhawk committed
9 10 11

#include <QTimer>
#include <QDebug>
12
#include <QSettings>
pixhawk's avatar
pixhawk committed
13
#include <QMutexLocker>
dogmaphobic's avatar
dogmaphobic committed
14 15 16 17

#ifdef __android__
#include "qserialport.h"
#else
18
#include <QSerialPort>
dogmaphobic's avatar
dogmaphobic committed
19
#endif
20

pixhawk's avatar
pixhawk committed
21
#include "SerialLink.h"
22
#include "QGC.h"
23
#include "QGCLoggingCategory.h"
Don Gagne's avatar
Don Gagne committed
24
#include "QGCApplication.h"
25
#include "QGCSerialPortInfo.h"
26
#include "LinkManager.h"
27

28
QGC_LOGGING_CATEGORY(SerialLinkLog, "SerialLinkLog")
pixhawk's avatar
pixhawk committed
29

30 31
static QStringList kSupportedBaudRates;

32
SerialLink::SerialLink(SharedLinkConfigurationPtr& config, bool isPX4Flow)
33
    : LinkInterface(config, isPX4Flow)
34
    , _serialConfig(qobject_cast<SerialConfiguration*>(config.get()))
35
{
36
    qCDebug(SerialLinkLog) << "Create SerialLink portName:baud:flowControl:parity:dataButs:stopBits" << _serialConfig->portName() << _serialConfig->baud() << _serialConfig->flowControl()
DonLakeFlyer's avatar
DonLakeFlyer committed
37
                           << _serialConfig->parity() << _serialConfig->dataBits() << _serialConfig->stopBits();
38
}
pixhawk's avatar
pixhawk committed
39 40 41

SerialLink::~SerialLink()
{
42
    disconnect();
43 44
}

45
bool SerialLink::_isBootloader()
46
{
47
    QList<QSerialPortInfo> portList = QSerialPortInfo::availablePorts();
48 49 50
    if( portList.count() == 0){
        return false;
    }
51
    for (const QSerialPortInfo &info: portList)
52
    {
53 54
        qCDebug(SerialLinkLog) << "PortName    : " << info.portName() << "Description : " << info.description();
        qCDebug(SerialLinkLog) << "Manufacturer: " << info.manufacturer();
55
        if (info.portName().trimmed() == _serialConfig->portName() &&
56 57 58 59 60
                (info.description().toLower().contains("bootloader") ||
                 info.description().toLower().contains("px4 bl") ||
                 info.description().toLower().contains("px4 fmu v1.6"))) {
            qCDebug(SerialLinkLog) << "BOOTLOADER FOUND";
            return true;
DonLakeFlyer's avatar
DonLakeFlyer committed
61
        }
62 63 64 65 66
    }
    // Not found
    return false;
}

67
void SerialLink::_writeBytes(const QByteArray data)
68
{
69
    if(_port && _port->isOpen()) {
70
        emit bytesSent(this, data);
71
        _port->write(data);
72
    } else {
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
73
        // Error occurred
74
        qWarning() << "Serial port not writeable";
75
        _emitLinkError(tr("Could not send data - link %1 is disconnected!").arg(getName()));
pixhawk's avatar
pixhawk committed
76 77 78
    }
}

79
void SerialLink::disconnect(void)
80
{
81 82
    if (_port) {
        _port->close();
83
        _port->deleteLater();
84
        _port = nullptr;
85
        emit disconnected();
86
    }
87

dogmaphobic's avatar
dogmaphobic committed
88
#ifdef __android__
89
    qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(false);
dogmaphobic's avatar
dogmaphobic committed
90
#endif
pixhawk's avatar
pixhawk committed
91 92
}

93
bool SerialLink::_connect(void)
94
{
95
    qCDebug(SerialLinkLog) << "CONNECT CALLED";
96

97 98 99 100
    if (_port) {
        qCWarning(SerialLinkLog) << "connect called while already connected";
        return true;
    }
101

dogmaphobic's avatar
dogmaphobic committed
102
#ifdef __android__
103
    qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(true);
104
#endif
105

106 107 108
    QSerialPort::SerialPortError    error;
    QString                         errorString;

dogmaphobic's avatar
dogmaphobic committed
109
    // Initialize the connection
110
    if (!_hardwareConnect(error, errorString)) {
111
        if (_config->isAutoConnect()) {
112 113 114 115 116
            // Be careful with spitting out open error related to trying to open a busy port using autoconnect
            if (error == QSerialPort::PermissionError) {
                // Device already open, ignore and fail connect
                return false;
            }
dogmaphobic's avatar
dogmaphobic committed
117
        }
118

119
        _emitLinkError(tr("Error connecting: Could not create port. %1").arg(errorString));
dogmaphobic's avatar
dogmaphobic committed
120 121
        return false;
    }
122
    return true;
pixhawk's avatar
pixhawk committed
123 124
}

125 126 127 128 129
/// Performs the actual hardware port connection.
///     @param[out] error if failed
///     @param[out] error string if failed
/// @return success/fail
bool SerialLink::_hardwareConnect(QSerialPort::SerialPortError& error, QString& errorString)
130
{
131
    if (_port) {
DonLakeFlyer's avatar
DonLakeFlyer committed
132
        qCDebug(SerialLinkLog) << "SerialLink:" << QString::number((qulonglong)this, 16) << "closing port";
133
        _port->close();
134 135 136 137 138 139

        // Wait 50 ms while continuing to run the event queue
        for (unsigned i = 0; i < 10; i++) {
            QGC::SLEEP::usleep(5000);
            qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
        }
140
        delete _port;
141
        _port = nullptr;
142
    }
pixhawk's avatar
pixhawk committed
143

144
    qCDebug(SerialLinkLog) << "SerialLink: hardwareConnect to " << _serialConfig->portName();
145

146
    // If we are in the Pixhawk bootloader code wait for it to timeout
147
    if (_isBootloader()) {
148
        qCDebug(SerialLinkLog) << "Not connecting to a bootloader, waiting for 2nd chance";
149 150
        const unsigned retry_limit = 12;
        unsigned retries;
151

152
        for (retries = 0; retries < retry_limit; retries++) {
153
            if (!_isBootloader()) {
154 155 156 157 158
                // Wait 500 ms while continuing to run the event loop
                for (unsigned i = 0; i < 100; i++) {
                    QGC::SLEEP::msleep(5);
                    qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
                }
159 160
                break;
            }
161 162 163 164 165 166

            // Wait 500 ms while continuing to run the event loop
            for (unsigned i = 0; i < 100; i++) {
                QGC::SLEEP::msleep(5);
                qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
            }
167 168 169 170
        }
        // Check limit
        if (retries == retry_limit) {
            // bail out
171
            qWarning() << "Timeout waiting for something other than booloader";
172 173 174 175
            return false;
        }
    }

176
    _port = new QSerialPort(_serialConfig->portName(), this);
Bill Bonney's avatar
Bill Bonney committed
177

178
    QObject::connect(_port, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error), this, &SerialLink::linkError);
179
    QObject::connect(_port, &QIODevice::readyRead, this, &SerialLink::_readBytes);
Bill Bonney's avatar
Bill Bonney committed
180

181
    // After the bootloader times out, it still can take a second or so for the Pixhawk USB driver to come up and make
182
    // the port available for open. So we retry a few times to wait for it.
dogmaphobic's avatar
dogmaphobic committed
183 184 185
#ifdef __android__
    _port->open(QIODevice::ReadWrite);
#else
186 187 188

    // Try to open the port three times
    for (int openRetries = 0; openRetries < 3; openRetries++) {
189
        if (!_port->open(QIODevice::ReadWrite)) {
190
            qCDebug(SerialLinkLog) << "Port open failed, retrying";
191 192 193 194 195 196
            // Wait 250 ms while continuing to run the event loop
            for (unsigned i = 0; i < 50; i++) {
                QGC::SLEEP::msleep(5);
                qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
            }
            qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
197 198 199
        } else {
            break;
        }
200
    }
dogmaphobic's avatar
dogmaphobic committed
201
#endif
202
    if (!_port->isOpen() ) {
203
        qDebug() << "open failed" << _port->errorString() << _port->error() << getName() << "autconnect:" << _config->isAutoConnect();
204 205
        error = _port->error();
        errorString = _port->errorString();
206
        _port->close();
207
        delete _port;
208
        _port = nullptr;
209
        return false; // couldn't open serial port
210 211
    }

212 213
    _port->setDataTerminalReady(true);

214
    qCDebug(SerialLinkLog) << "Configuring port";
215 216 217 218 219
    _port->setBaudRate     (_serialConfig->baud());
    _port->setDataBits     (static_cast<QSerialPort::DataBits>     (_serialConfig->dataBits()));
    _port->setFlowControl  (static_cast<QSerialPort::FlowControl>  (_serialConfig->flowControl()));
    _port->setStopBits     (static_cast<QSerialPort::StopBits>     (_serialConfig->stopBits()));
    _port->setParity       (static_cast<QSerialPort::Parity>       (_serialConfig->parity()));
220

Bill Bonney's avatar
Bill Bonney committed
221
    emit connected();
222

223
    qCDebug(SerialLinkLog) << "Connection SeriaLink: " << "with settings" << _serialConfig->portName()
DonLakeFlyer's avatar
DonLakeFlyer committed
224
                           << _serialConfig->baud() << _serialConfig->dataBits() << _serialConfig->parity() << _serialConfig->stopBits();
225

Bill Bonney's avatar
Bill Bonney committed
226
    return true; // successful connection
pixhawk's avatar
pixhawk committed
227
}
228

229 230
void SerialLink::_readBytes(void)
{
231 232 233 234 235 236 237 238 239 240 241 242
    if (_port && _port->isOpen()) {
        qint64 byteCount = _port->bytesAvailable();
        if (byteCount) {
            QByteArray buffer;
            buffer.resize(byteCount);
            _port->read(buffer.data(), buffer.size());
            emit bytesReceived(this, buffer);
        }
    } else {
        // Error occurred
        qWarning() << "Serial port not readable";
        _emitLinkError(tr("Could not read data - link %1 is disconnected!").arg(getName()));
243 244 245
    }
}

246
void SerialLink::linkError(QSerialPort::SerialPortError error)
247
{
248 249 250 251
    switch (error) {
    case QSerialPort::NoError:
        break;
    case QSerialPort::ResourceError:
252 253
        // This indicates the hardware was pulled from the computer. For example usb cable unplugged.
        _connectionRemoved();
254 255
        break;
    default:
256 257 258 259
        // You can use the following qDebug output as needed during development. Make sure to comment it back out
        // when you are done. The reason for this is that this signal is very noisy. For example if you try to
        // connect to a PixHawk before it is ready to accept the connection it will output a continuous stream
        // of errors until the Pixhawk responds.
260
        //qCDebug(SerialLinkLog) << "SerialLink::linkError" << error;
261
        break;
262
    }
263 264
}

265
bool SerialLink::isConnected() const
266
{
267
    bool isConnected = false;
Bill Bonney's avatar
Bill Bonney committed
268

269
    if (_port) {
270
        isConnected = _port->isOpen();
lm's avatar
lm committed
271
    }
272

273
    return isConnected;
pixhawk's avatar
pixhawk committed
274 275
}

276
QString SerialLink::getName() const
pixhawk's avatar
pixhawk committed
277
{
278
    return _serialConfig->name();
pixhawk's avatar
pixhawk committed
279 280
}

281
void SerialLink::_emitLinkError(const QString& errorMsg)
282
{
283
    QString msg("Error on link %1. %2");
284
    qDebug() << errorMsg;
285
    emit communicationError(tr("Link Error"), msg.arg(getName()).arg(errorMsg));
pixhawk's avatar
pixhawk committed
286 287
}

288 289
//--------------------------------------------------------------------------
//-- SerialConfiguration
pixhawk's avatar
pixhawk committed
290

291
SerialConfiguration::SerialConfiguration(const QString& name) : LinkConfiguration(name)
292
{
293 294 295 296 297
    _baud       = 57600;
    _flowControl= QSerialPort::NoFlowControl;
    _parity     = QSerialPort::NoParity;
    _dataBits   = 8;
    _stopBits   = 1;
298
    _usbDirect  = false;
pixhawk's avatar
pixhawk committed
299 300
}

301
SerialConfiguration::SerialConfiguration(SerialConfiguration* copy) : LinkConfiguration(copy)
302
{
303 304 305 306 307 308 309
    _baud               = copy->baud();
    _flowControl        = copy->flowControl();
    _parity             = copy->parity();
    _dataBits           = copy->dataBits();
    _stopBits           = copy->stopBits();
    _portName           = copy->portName();
    _portDisplayName    = copy->portDisplayName();
310
    _usbDirect          = copy->_usbDirect;
pixhawk's avatar
pixhawk committed
311 312
}

313
void SerialConfiguration::copyFrom(LinkConfiguration *source)
314
{
315
    LinkConfiguration::copyFrom(source);
316
    auto* ssource = qobject_cast<SerialConfiguration*>(source);
DonLakeFlyer's avatar
DonLakeFlyer committed
317 318 319 320 321 322 323 324 325 326 327 328
    if (ssource) {
        _baud               = ssource->baud();
        _flowControl        = ssource->flowControl();
        _parity             = ssource->parity();
        _dataBits           = ssource->dataBits();
        _stopBits           = ssource->stopBits();
        _portName           = ssource->portName();
        _portDisplayName    = ssource->portDisplayName();
        _usbDirect          = ssource->_usbDirect;
    } else {
        qWarning() << "Internal error";
    }
329 330
}

331
void SerialConfiguration::setBaud(int baud)
pixhawk's avatar
pixhawk committed
332
{
333
    _baud = baud;
pixhawk's avatar
pixhawk committed
334 335
}

336
void SerialConfiguration::setDataBits(int databits)
pixhawk's avatar
pixhawk committed
337
{
338
    _dataBits = databits;
pixhawk's avatar
pixhawk committed
339 340
}

341
void SerialConfiguration::setFlowControl(int flowControl)
pixhawk's avatar
pixhawk committed
342
{
343
    _flowControl = flowControl;
pixhawk's avatar
pixhawk committed
344 345
}

346
void SerialConfiguration::setStopBits(int stopBits)
347
{
348
    _stopBits = stopBits;
pixhawk's avatar
pixhawk committed
349 350
}

351
void SerialConfiguration::setParity(int parity)
352
{
353
    _parity = parity;
pixhawk's avatar
pixhawk committed
354 355
}

356
void SerialConfiguration::setPortName(const QString& portName)
357
{
358 359 360 361
    // No effect on a running connection
    QString pname = portName.trimmed();
    if (!pname.isEmpty() && pname != _portName) {
        _portName = pname;
362
        _portDisplayName = cleanPortDisplayname(pname);
363 364 365
    }
}

366 367 368
QString SerialConfiguration::cleanPortDisplayname(const QString name)
{
    QString pname = name.trimmed();
369
#ifdef Q_OS_WIN
370 371 372 373 374 375 376 377
    pname.replace("\\\\.\\", "");
#else
    pname.replace("/dev/cu.", "");
    pname.replace("/dev/", "");
#endif
    return pname;
}

378
void SerialConfiguration::saveSettings(QSettings& settings, const QString& root)
379
{
380
    settings.beginGroup(root);
381 382 383 384 385 386 387
    settings.setValue("baud",           _baud);
    settings.setValue("dataBits",       _dataBits);
    settings.setValue("flowControl",    _flowControl);
    settings.setValue("stopBits",       _stopBits);
    settings.setValue("parity",         _parity);
    settings.setValue("portName",       _portName);
    settings.setValue("portDisplayName",_portDisplayName);
388
    settings.endGroup();
389
}
390

391
void SerialConfiguration::loadSettings(QSettings& settings, const QString& root)
392
{
393
    settings.beginGroup(root);
394 395 396 397 398 399 400
    if(settings.contains("baud"))           _baud           = settings.value("baud").toInt();
    if(settings.contains("dataBits"))       _dataBits       = settings.value("dataBits").toInt();
    if(settings.contains("flowControl"))    _flowControl    = settings.value("flowControl").toInt();
    if(settings.contains("stopBits"))       _stopBits       = settings.value("stopBits").toInt();
    if(settings.contains("parity"))         _parity         = settings.value("parity").toInt();
    if(settings.contains("portName"))       _portName       = settings.value("portName").toString();
    if(settings.contains("portDisplayName"))_portDisplayName= settings.value("portDisplayName").toString();
401
    settings.endGroup();
402
}
403 404 405 406 407 408 409 410 411 412 413

QStringList SerialConfiguration::supportedBaudRates()
{
    if(!kSupportedBaudRates.size())
        _initBaudRates();
    return kSupportedBaudRates;
}

void SerialConfiguration::_initBaudRates()
{
    kSupportedBaudRates.clear();
414
    kSupportedBaudRates = QStringList({
415 416
#if USE_ANCIENT_RATES
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
417 418
        "50",
        "75",
419
#endif
420
        "110",
421
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
422 423 424
        "150",
        "200" ,
        "134"  ,
425
#endif
426 427 428
        "300",
        "600",
        "1200",
429
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
430
        "1800",
431 432
#endif
#endif
433 434 435
        "2400",
        "4800",
        "9600",
436
#if defined(Q_OS_WIN)
437
        "14400",
438
#endif
439 440
        "19200",
        "38400",
441
#if defined(Q_OS_WIN)
442
        "56000",
443
#endif
444 445
        "57600",
        "115200",
446
#if defined(Q_OS_WIN)
447
        "128000",
448
#endif
449
        "230400",
450
#if defined(Q_OS_WIN)
451
        "256000",
452
#endif
453 454
        "460800",
        "500000",
455
#if defined(Q_OS_LINUX)
456
        "576000",
457
#endif
458 459
        "921600",
    });
460 461
}

462 463 464 465 466 467 468
void SerialConfiguration::setUsbDirect(bool usbDirect)
{
    if (_usbDirect != usbDirect) {
        _usbDirect = usbDirect;
        emit usbDirectChanged(_usbDirect);
    }
}