SerialLink.cc 20.8 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12
/*=====================================================================
======================================================================*/
/**
 * @file
 *   @brief Cross-platform support for serial ports
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QTimer>
#include <QDebug>
13
#include <QSettings>
pixhawk's avatar
pixhawk committed
14 15 16
#include <QMutexLocker>
#include "SerialLink.h"
#include "LinkManager.h"
17
#include "QGC.h"
pixhawk's avatar
pixhawk committed
18
#include <MG.h>
19
#include <iostream>
pixhawk's avatar
pixhawk committed
20 21 22 23
#ifdef _WIN32
#include "windows.h"
#endif

24 25
using namespace TNX;

26
//#define USE_QEXTSERIAL // this allows us to revert to old serial library during transition
pixhawk's avatar
pixhawk committed
27

28 29
SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl, bool parity,
                       int dataBits, int stopBits) :
30
    port(NULL)
pixhawk's avatar
pixhawk committed
31 32 33 34 35 36
{
    // Setup settings
    this->porthandle = portname.trimmed();
#ifdef _WIN32
    // Port names above 20 need the network path format - if the port name is not already in this format
    // catch this special case
37
    if (this->porthandle.size() > 0 && !this->porthandle.startsWith("\\")) {
pixhawk's avatar
pixhawk committed
38 39 40 41 42 43
        // Append \\.\ before the port handle. Additional backslashes are used for escaping.
        this->porthandle = "\\\\.\\" + this->porthandle;
    }
#endif
    // Set unique ID and add link to the list of links
    this->id = getNextLinkId();
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    setBaudRate(baudRate);
    if (hardwareFlowControl)
    {
        portSettings.setFlowControl(QPortSettings::FLOW_HARDWARE);
    }
    else
    {
        portSettings.setFlowControl(QPortSettings::FLOW_OFF);
    }
    if (parity)
    {
        portSettings.setParity(QPortSettings::PAR_EVEN);
    }
    else
    {
        portSettings.setParity(QPortSettings::PAR_NONE);
    }
    setDataBits(dataBits);
    setStopBits(stopBits);
pixhawk's avatar
pixhawk committed
64 65

    // Set the port name
66 67
    if (porthandle == "")
    {
68
        name = tr("Serial Link ") + QString::number(getId());
pixhawk's avatar
pixhawk committed
69
    }
70 71 72
    else
    {
        name = portname.trimmed();
pixhawk's avatar
pixhawk committed
73
    }
lm's avatar
lm committed
74
    loadSettings();
pixhawk's avatar
pixhawk committed
75 76 77 78 79
}

SerialLink::~SerialLink()
{
    disconnect();
80
    if(port) delete port;
pixhawk's avatar
pixhawk committed
81 82 83
    port = NULL;
}

84 85 86 87 88
void SerialLink::loadSettings()
{
    // Load defaults from settings
    QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
    settings.sync();
89
    if (settings.contains("SERIALLINK_COMM_PORT")) {
90
        if (porthandle == "") setPortName(settings.value("SERIALLINK_COMM_PORT").toString());
91 92
        setBaudRateType(settings.value("SERIALLINK_COMM_BAUD").toInt());
        setParityType(settings.value("SERIALLINK_COMM_PARITY").toInt());
93 94 95
        setStopBits(settings.value("SERIALLINK_COMM_STOPBITS").toInt());
        setDataBits(settings.value("SERIALLINK_COMM_DATABITS").toInt());
        setFlowType(settings.value("SERIALLINK_COMM_FLOW_CONTROL").toInt());
96 97 98 99 100 101 102 103 104 105
    }
}

void SerialLink::writeSettings()
{
    // Store settings
    QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
    settings.setValue("SERIALLINK_COMM_PORT", this->porthandle);
    settings.setValue("SERIALLINK_COMM_BAUD", getBaudRateType());
    settings.setValue("SERIALLINK_COMM_PARITY", getParityType());
106 107 108
    settings.setValue("SERIALLINK_COMM_STOPBITS", getStopBits());
    settings.setValue("SERIALLINK_COMM_DATABITS", getDataBits());
    settings.setValue("SERIALLINK_COMM_FLOW_CONTROL", getFlowType());
109 110 111
    settings.sync();
}

pixhawk's avatar
pixhawk committed
112 113 114 115 116

/**
 * @brief Runs the thread
 *
 **/
117 118
void SerialLink::run()
{
pixhawk's avatar
pixhawk committed
119 120 121 122
    // Initialize the connection
    hardwareConnect();

    // Qt way to make clear what a while(1) loop does
123
    forever {
pixhawk's avatar
pixhawk committed
124 125 126 127 128 129 130 131 132 133
        // Check if new bytes have arrived, if yes, emit the notification signal
        checkForBytes();
        /* Serial data isn't arriving that fast normally, this saves the thread
                 * from consuming too much processing time
                 */
        MG::SLEEP::msleep(SerialLink::poll_interval);
    }
}


134 135
void SerialLink::checkForBytes()
{
pixhawk's avatar
pixhawk committed
136
    /* Check if bytes are available */
137
    if(port && port->isOpen() && port->isWritable()) {
pixhawk's avatar
pixhawk committed
138 139 140 141
        dataMutex.lock();
        qint64 available = port->bytesAvailable();
        dataMutex.unlock();

142
        if(available > 0) {
143
            readBytes();
pixhawk's avatar
pixhawk committed
144
        }
145
    } else {
pixhawk's avatar
pixhawk committed
146 147 148 149 150
        emit disconnected();
    }

}

151 152
void SerialLink::writeBytes(const char* data, qint64 size)
{
153
    if(port && port->isOpen()) {
pixhawk's avatar
pixhawk committed
154 155
        int b = port->write(data, size);

156
        if (b > 0) {
157

158
//            qDebug() << "Serial link " << this->getName() << "transmitted" << b << "bytes:";
pixhawk's avatar
pixhawk committed
159

160 161 162
            // Increase write counter
            bitsSentTotal += size * 8;

163 164 165 166 167 168 169
//            int i;
//            for (i=0; i<size; i++)
//            {
//                unsigned char v =data[i];
//                qDebug("%02x ", v);
//            }
//            qDebug("\n");
170
        } else {
171 172 173
            disconnect();
            // Error occured
            emit communicationError(this->getName(), tr("Could not send data - link %1 is disconnected!").arg(this->getName()));
174
        }
pixhawk's avatar
pixhawk committed
175 176 177 178 179 180 181 182 183
    }
}

/**
 * @brief Read a number of bytes from the interface.
 *
 * @param data Pointer to the data byte array to write the bytes to
 * @param maxLength The maximum number of bytes to write
 **/
184 185
void SerialLink::readBytes()
{
pixhawk's avatar
pixhawk committed
186
    dataMutex.lock();
187
    if(port && port->isOpen()) {
188 189
        const qint64 maxLength = 2048;
        char data[maxLength];
pixhawk's avatar
pixhawk committed
190
        qint64 numBytes = port->bytesAvailable();
James Goppert's avatar
James Goppert committed
191
        //qDebug() << "numBytes: " << numBytes;
192

193
        if(numBytes > 0) {
pixhawk's avatar
pixhawk committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
            /* Read as much data in buffer as possible without overflow */
            if(maxLength < numBytes) numBytes = maxLength;

            port->read(data, numBytes);
            QByteArray b(data, numBytes);
            emit bytesReceived(this, b);

            //qDebug() << "SerialLink::readBytes()" << std::hex << data;
            //            int i;
            //            for (i=0; i<numBytes; i++){
            //                unsigned int v=data[i];
            //
            //                fprintf(stderr,"%02x ", v);
            //            }
            //            fprintf(stderr,"\n");
            bitsReceivedTotal += numBytes * 8;
        }
    }
    dataMutex.unlock();
}


/**
 * @brief Get the number of bytes to read.
 *
 * @return The number of bytes to read
 **/
221 222
qint64 SerialLink::bytesAvailable()
{
223
    if (port) {
224
        return port->bytesAvailable();
225
    } else {
226 227
        return 0;
    }
pixhawk's avatar
pixhawk committed
228 229 230 231 232 233 234
}

/**
 * @brief Disconnect the connection.
 *
 * @return True if connection has been disconnected, false if connection couldn't be disconnected.
 **/
235 236
bool SerialLink::disconnect()
{
237
    if (port) {
238 239 240 241
        //#if !defined _WIN32 || !defined _WIN64
        /* Block the thread until it returns from run() */
        //#endif
//        dataMutex.lock();
242 243
        port->flushInBuffer();
        port->flushOutBuffer();
244 245 246 247
        port->close();
        delete port;
        port = NULL;
//        dataMutex.unlock();
pixhawk's avatar
pixhawk committed
248

249
        if(this->isRunning()) this->terminate(); //stop running the thread, restart it upon connect
250

251 252
        bool closed = true;
        //port->isOpen();
pixhawk's avatar
pixhawk committed
253

254 255
        emit disconnected();
        emit connected(false);
256
        return closed;
257
    } else {
258 259 260
        // No port, so we're disconnected
        return true;
    }
pixhawk's avatar
pixhawk committed
261 262 263 264 265 266 267 268 269 270

}

/**
 * @brief Connect the connection.
 *
 * @return True if connection has been established, false if connection couldn't be established.
 **/
bool SerialLink::connect()
{
271 272
    if (this->isRunning()) this->disconnect();
    this->start(LowPriority);
273
    return true;
pixhawk's avatar
pixhawk committed
274 275 276 277 278 279 280 281 282 283
}

/**
 * @brief This function is called indirectly by the connect() call.
 *
 * The connect() function starts the thread and indirectly calls this method.
 *
 * @return True if the connection could be established, false otherwise
 * @see connect() For the right function to establish the connection.
 **/
284 285
bool SerialLink::hardwareConnect()
{
286
    if(port) {
287 288 289
        port->close();
        delete port;
    }
290 291 292
    port = new QSerialPort(porthandle, portSettings);
    QObject::connect(port,SIGNAL(aboutToClose()),this,SIGNAL(disconnected()));
    port->setCommTimeouts(QSerialPort::CtScheme_NonBlockingRead);
pixhawk's avatar
pixhawk committed
293 294
    connectionStartTime = MG::TIME::getGroundTimeNow();

295 296
    port->open();

pixhawk's avatar
pixhawk committed
297
    bool connectionUp = isConnected();
298
    if(connectionUp) {
pixhawk's avatar
pixhawk committed
299 300 301 302
        emit connected();
        emit connected(true);
    }

303 304 305
    qDebug() << "CONNECTING LINK: " << __FILE__ << __LINE__ << "with settings" << port->portName() << getBaudRate() << getDataBits() << getParityType() << getStopBits();


306 307
    writeSettings();

pixhawk's avatar
pixhawk committed
308 309
    return connectionUp;
}
310 311


pixhawk's avatar
pixhawk committed
312 313 314 315 316
/**
 * @brief Check if connection is active.
 *
 * @return True if link is connected, false otherwise.
 **/
317 318
bool SerialLink::isConnected()
{
319
    if (port) {
lm's avatar
lm committed
320
        return port->isOpen();
321
    } else {
lm's avatar
lm committed
322 323
        return false;
    }
pixhawk's avatar
pixhawk committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
}

int SerialLink::getId()
{
    return id;
}

QString SerialLink::getName()
{
    return name;
}

void SerialLink::setName(QString name)
{
    this->name = name;
    emit nameChanged(this->name);
}


343 344
qint64 SerialLink::getNominalDataRate()
{
pixhawk's avatar
pixhawk committed
345
    qint64 dataRate = 0;
346 347
    switch (portSettings.baudRate()) {
    case QPortSettings::BAUDR_50:
pixhawk's avatar
pixhawk committed
348 349
        dataRate = 50;
        break;
350
    case QPortSettings::BAUDR_75:
pixhawk's avatar
pixhawk committed
351 352
        dataRate = 75;
        break;
353
    case QPortSettings::BAUDR_110:
pixhawk's avatar
pixhawk committed
354 355
        dataRate = 110;
        break;
356
    case QPortSettings::BAUDR_134:
pixhawk's avatar
pixhawk committed
357 358
        dataRate = 134;
        break;
359
    case QPortSettings::BAUDR_150:
pixhawk's avatar
pixhawk committed
360 361
        dataRate = 150;
        break;
362
    case QPortSettings::BAUDR_200:
pixhawk's avatar
pixhawk committed
363 364
        dataRate = 200;
        break;
365
    case QPortSettings::BAUDR_300:
pixhawk's avatar
pixhawk committed
366 367
        dataRate = 300;
        break;
368
    case QPortSettings::BAUDR_600:
pixhawk's avatar
pixhawk committed
369 370
        dataRate = 600;
        break;
371
    case QPortSettings::BAUDR_1200:
pixhawk's avatar
pixhawk committed
372 373
        dataRate = 1200;
        break;
374
    case QPortSettings::BAUDR_1800:
pixhawk's avatar
pixhawk committed
375 376
        dataRate = 1800;
        break;
377
    case QPortSettings::BAUDR_2400:
pixhawk's avatar
pixhawk committed
378 379
        dataRate = 2400;
        break;
380
    case QPortSettings::BAUDR_4800:
pixhawk's avatar
pixhawk committed
381 382
        dataRate = 4800;
        break;
383
    case QPortSettings::BAUDR_9600:
pixhawk's avatar
pixhawk committed
384 385
        dataRate = 9600;
        break;
386 387
#ifdef Q_OS_WIN
    case QPortSettings::BAUDR_14400:
pixhawk's avatar
pixhawk committed
388 389
        dataRate = 14400;
        break;
390 391
#endif
    case QPortSettings::BAUDR_19200:
pixhawk's avatar
pixhawk committed
392 393
        dataRate = 19200;
        break;
394
    case QPortSettings::BAUDR_38400:
pixhawk's avatar
pixhawk committed
395 396
        dataRate = 38400;
        break;
397 398
#ifdef Q_OS_WIN
    case QPortSettings::BAUDR_56000:
pixhawk's avatar
pixhawk committed
399 400
        dataRate = 56000;
        break;
401 402
#endif
    case QPortSettings::BAUDR_57600:
pixhawk's avatar
pixhawk committed
403 404
        dataRate = 57600;
        break;
405 406
#ifdef Q_OS_WIN
    case QPortSettings::BAUDR_76800:
pixhawk's avatar
pixhawk committed
407 408
        dataRate = 76800;
        break;
409 410
#endif
    case QPortSettings::BAUDR_115200:
pixhawk's avatar
pixhawk committed
411 412
        dataRate = 115200;
        break;
413 414 415
#ifdef Q_OS_WIN
        // Windows-specific high-end baudrates
    case QPortSettings::BAUDR_128000:
pixhawk's avatar
pixhawk committed
416 417
        dataRate = 128000;
        break;
418
    case QPortSettings::BAUDR_256000:
pixhawk's avatar
pixhawk committed
419
        dataRate = 256000;
420
    case QPortSettings::BAUDR_230400:
421
        dataRate = 230400;
422
    case QPortSettings::BAUDR_460800:
423
        dataRate = 460800;
424 425 426
#endif
        // All-OS high-speed
    case QPortSettings::BAUDR_921600:
427
        dataRate = 921600;
pixhawk's avatar
pixhawk committed
428
        break;
429 430 431 432
    case QPortSettings::BAUDR_UNKNOWN:
        default:
        // Do nothing
        break;
pixhawk's avatar
pixhawk committed
433 434 435 436
    }
    return dataRate;
}

437 438
qint64 SerialLink::getTotalUpstream()
{
pixhawk's avatar
pixhawk committed
439 440 441 442 443
    statisticsMutex.lock();
    return bitsSentTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
}

444 445
qint64 SerialLink::getCurrentUpstream()
{
pixhawk's avatar
pixhawk committed
446 447 448
    return 0; // TODO
}

449 450
qint64 SerialLink::getMaxUpstream()
{
pixhawk's avatar
pixhawk committed
451 452 453
    return 0; // TODO
}

454 455
qint64 SerialLink::getBitsSent()
{
pixhawk's avatar
pixhawk committed
456 457 458
    return bitsSentTotal;
}

459 460
qint64 SerialLink::getBitsReceived()
{
pixhawk's avatar
pixhawk committed
461 462 463
    return bitsReceivedTotal;
}

464 465
qint64 SerialLink::getTotalDownstream()
{
pixhawk's avatar
pixhawk committed
466 467 468 469 470
    statisticsMutex.lock();
    return bitsReceivedTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
}

471 472
qint64 SerialLink::getCurrentDownstream()
{
pixhawk's avatar
pixhawk committed
473 474 475
    return 0; // TODO
}

476 477
qint64 SerialLink::getMaxDownstream()
{
pixhawk's avatar
pixhawk committed
478 479 480
    return 0; // TODO
}

481 482
bool SerialLink::isFullDuplex()
{
pixhawk's avatar
pixhawk committed
483 484 485 486
    /* Serial connections are always half duplex */
    return false;
}

487 488
int SerialLink::getLinkQuality()
{
pixhawk's avatar
pixhawk committed
489 490 491 492
    /* This feature is not supported with this interface */
    return -1;
}

493 494
QString SerialLink::getPortName()
{
pixhawk's avatar
pixhawk committed
495 496 497
    return porthandle;
}

498 499
int SerialLink::getBaudRate()
{
pixhawk's avatar
pixhawk committed
500 501 502
    return getNominalDataRate();
}

503 504
int SerialLink::getBaudRateType()
{
505
    return portSettings.baudRate();
pixhawk's avatar
pixhawk committed
506 507
}

508 509
int SerialLink::getFlowType()
{
510
    return portSettings.flowControl();
pixhawk's avatar
pixhawk committed
511 512
}

513 514
int SerialLink::getParityType()
{
515
    return portSettings.parity();
pixhawk's avatar
pixhawk committed
516 517
}

518 519
int SerialLink::getDataBitsType()
{
520
    return portSettings.dataBits();
pixhawk's avatar
pixhawk committed
521 522
}

523 524
int SerialLink::getStopBitsType()
{
525
    return portSettings.stopBits();
pixhawk's avatar
pixhawk committed
526 527
}

528 529
int SerialLink::getDataBits()
{
530 531 532
    int ret = -1;
    switch (portSettings.dataBits()) {
    case QPortSettings::DB_5:
533 534
        ret = 5;
        break;
535
    case QPortSettings::DB_6:
536 537
        ret = 6;
        break;
538
    case QPortSettings::DB_7:
539 540
        ret = 7;
        break;
541
    case QPortSettings::DB_8:
542 543 544
        ret = 8;
        break;
    default:
545
        ret = -1;
546 547 548 549 550 551 552
        break;
    }
    return ret;
}

int SerialLink::getStopBits()
{
553 554 555 556 557 558 559 560 561 562 563 564
    int ret = -1;
        switch (portSettings.stopBits()) {
        case QPortSettings::STOP_1:
            ret = 1;
            break;
        case QPortSettings::STOP_2:
            ret = 2;
            break;
        default:
            ret = -1;
            break;
        }
565 566 567
    return ret;
}

pixhawk's avatar
pixhawk committed
568 569
bool SerialLink::setPortName(QString portName)
{
570
    if(portName.trimmed().length() > 0) {
pixhawk's avatar
pixhawk committed
571
        bool reconnect = false;
572 573 574
        if (isConnected()) reconnect = true;
        disconnect();

pixhawk's avatar
pixhawk committed
575 576 577 578 579
        this->porthandle = portName.trimmed();
        setName(tr("serial port ") + portName.trimmed());
#ifdef _WIN32
        // Port names above 20 need the network path format - if the port name is not already in this format
        // catch this special case
580
        if (!this->porthandle.startsWith("\\")) {
pixhawk's avatar
pixhawk committed
581 582 583 584
            // Append \\.\ before the port handle. Additional backslashes are used for escaping.
            this->porthandle = "\\\\.\\" + this->porthandle;
        }
#endif
585

pixhawk's avatar
pixhawk committed
586 587
        if(reconnect) connect();
        return true;
588
    } else {
pixhawk's avatar
pixhawk committed
589 590 591 592 593 594 595 596 597
        return false;
    }
}


bool SerialLink::setBaudRateType(int rateIndex)
{
    bool reconnect = false;
    bool accepted = true; // This is changed if none of the data rates matches
598 599 600
    if(isConnected()) reconnect = true;
    disconnect();

601 602 603
    if (rateIndex >= (int)QPortSettings::BAUDR_50 && rateIndex <= (int)QPortSettings::BAUDR_921600)
    {
        portSettings.setBaudRate((QPortSettings::BaudRate)rateIndex);
pixhawk's avatar
pixhawk committed
604 605 606 607 608 609
    }

    if(reconnect) connect();
    return accepted;
}

610 611 612 613 614 615 616
bool SerialLink::setBaudRateString(const QString& rate)
{
    bool ok;
    int intrate = rate.toInt(&ok);
    if (!ok) return false;
    return setBaudRate(intrate);
}
pixhawk's avatar
pixhawk committed
617 618 619

bool SerialLink::setBaudRate(int rate)
{
620 621
    qDebug() << "BAUD RATE:" << rate;

pixhawk's avatar
pixhawk committed
622 623
    bool reconnect = false;
    bool accepted = true; // This is changed if none of the data rates matches
624
    if(isConnected()) {
pixhawk's avatar
pixhawk committed
625 626
        reconnect = true;
    }
627
    disconnect();
pixhawk's avatar
pixhawk committed
628

629
    switch (rate) {
pixhawk's avatar
pixhawk committed
630
    case 50:
631
        portSettings.setBaudRate(QPortSettings::BAUDR_50);
pixhawk's avatar
pixhawk committed
632 633
        break;
    case 75:
634
        portSettings.setBaudRate(QPortSettings::BAUDR_75);
pixhawk's avatar
pixhawk committed
635 636
        break;
    case 110:
637
        portSettings.setBaudRate(QPortSettings::BAUDR_110);
pixhawk's avatar
pixhawk committed
638 639
        break;
    case 134:
640
        portSettings.setBaudRate(QPortSettings::BAUDR_134);
pixhawk's avatar
pixhawk committed
641 642
        break;
    case 150:
643
        portSettings.setBaudRate(QPortSettings::BAUDR_150);
pixhawk's avatar
pixhawk committed
644 645
        break;
    case 200:
646
        portSettings.setBaudRate(QPortSettings::BAUDR_200);
pixhawk's avatar
pixhawk committed
647 648
        break;
    case 300:
649
        portSettings.setBaudRate(QPortSettings::BAUDR_300);
pixhawk's avatar
pixhawk committed
650 651
        break;
    case 600:
652
        portSettings.setBaudRate(QPortSettings::BAUDR_600);
pixhawk's avatar
pixhawk committed
653 654
        break;
    case 1200:
655
        portSettings.setBaudRate(QPortSettings::BAUDR_1200);
pixhawk's avatar
pixhawk committed
656 657
        break;
    case 1800:
658
        portSettings.setBaudRate(QPortSettings::BAUDR_1800);
pixhawk's avatar
pixhawk committed
659 660
        break;
    case 2400:
661
        portSettings.setBaudRate(QPortSettings::BAUDR_2400);
pixhawk's avatar
pixhawk committed
662 663
        break;
    case 4800:
664
        portSettings.setBaudRate(QPortSettings::BAUDR_4800);
pixhawk's avatar
pixhawk committed
665 666
        break;
    case 9600:
667
        portSettings.setBaudRate(QPortSettings::BAUDR_9600);
pixhawk's avatar
pixhawk committed
668
        break;
669
#ifdef Q_OS_WIN
pixhawk's avatar
pixhawk committed
670
    case 14400:
671
        portSettings.setBaudRate(QPortSettings::BAUDR_14400);
pixhawk's avatar
pixhawk committed
672
        break;
673
#endif
pixhawk's avatar
pixhawk committed
674
    case 19200:
675
        portSettings.setBaudRate(QPortSettings::BAUDR_19200);
pixhawk's avatar
pixhawk committed
676 677
        break;
    case 38400:
678
        portSettings.setBaudRate(QPortSettings::BAUDR_38400);
pixhawk's avatar
pixhawk committed
679
        break;
680
#ifdef Q_OS_WIN
pixhawk's avatar
pixhawk committed
681
    case 56000:
682
        portSettings.setBaudRate(QPortSettings::BAUDR_56000);
pixhawk's avatar
pixhawk committed
683
        break;
684
#endif
pixhawk's avatar
pixhawk committed
685
    case 57600:
686
        portSettings.setBaudRate(QPortSettings::BAUDR_57600);
pixhawk's avatar
pixhawk committed
687
        break;
688
#ifdef Q_OS_WIN
pixhawk's avatar
pixhawk committed
689
    case 76800:
690
        portSettings.setBaudRate(QPortSettings::BAUDR_76800);
pixhawk's avatar
pixhawk committed
691
        break;
692
#endif
pixhawk's avatar
pixhawk committed
693
    case 115200:
694
        portSettings.setBaudRate(QPortSettings::BAUDR_115200);
pixhawk's avatar
pixhawk committed
695
        break;
696
#ifdef Q_OS_WIN
pixhawk's avatar
pixhawk committed
697
    case 128000:
698
        portSettings.setBaudRate(QPortSettings::BAUDR_128000);
pixhawk's avatar
pixhawk committed
699
        break;
700
    case 230400:
701
        portSettings.setBaudRate(QPortSettings::BAUDR_230400);
702
        break;
pixhawk's avatar
pixhawk committed
703
    case 256000:
704
        portSettings.setBaudRate(QPortSettings::BAUDR_256000);
pixhawk's avatar
pixhawk committed
705
        break;
706
    case 460800:
707
        portSettings.setBaudRate(QPortSettings::BAUDR_460800);
708
        break;
709
#endif
710
    case 921600:
711
        portSettings.setBaudRate(QPortSettings::BAUDR_921600);
712
        break;
pixhawk's avatar
pixhawk committed
713 714 715 716 717 718
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

719 720 721
    if(reconnect) connect();
    return accepted;

pixhawk's avatar
pixhawk committed
722 723
}

724 725
bool SerialLink::setFlowType(int flow)
{
pixhawk's avatar
pixhawk committed
726 727
    bool reconnect = false;
    bool accepted = true;
728 729
    if(isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
730

731
    switch (flow) {
732 733
    case (int)QPortSettings::FLOW_OFF:
        portSettings.setFlowControl(QPortSettings::FLOW_OFF);
pixhawk's avatar
pixhawk committed
734
        break;
735 736
    case (int)QPortSettings::FLOW_HARDWARE:
        portSettings.setFlowControl(QPortSettings::FLOW_HARDWARE);
pixhawk's avatar
pixhawk committed
737
        break;
738 739
    case (int)QPortSettings::FLOW_XONXOFF:
        portSettings.setFlowControl(QPortSettings::FLOW_XONXOFF);
pixhawk's avatar
pixhawk committed
740 741 742 743 744 745
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }
746

pixhawk's avatar
pixhawk committed
747 748 749 750
    if(reconnect) connect();
    return accepted;
}

751 752
bool SerialLink::setParityType(int parity)
{
pixhawk's avatar
pixhawk committed
753 754
    bool reconnect = false;
    bool accepted = true;
755 756
    if (isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
757

758
    switch (parity) {
759 760
    case (int)QPortSettings::PAR_NONE:
        portSettings.setParity(QPortSettings::PAR_NONE);
pixhawk's avatar
pixhawk committed
761
        break;
762 763
    case (int)QPortSettings::PAR_ODD:
        portSettings.setParity(QPortSettings::PAR_ODD);
pixhawk's avatar
pixhawk committed
764
        break;
765 766
    case (int)QPortSettings::PAR_EVEN:
        portSettings.setParity(QPortSettings::PAR_EVEN);
pixhawk's avatar
pixhawk committed
767
        break;
768 769
    case (int)QPortSettings::PAR_SPACE:
        portSettings.setParity(QPortSettings::PAR_SPACE);
pixhawk's avatar
pixhawk committed
770 771 772 773 774 775 776
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

777
    if (reconnect) connect();
pixhawk's avatar
pixhawk committed
778 779 780
    return accepted;
}

781

782
bool SerialLink::setDataBits(int dataBits)
783
{
784
    qDebug() << "Setting" << dataBits << "data bits";
785 786
    bool reconnect = false;
    if (isConnected()) reconnect = true;
pixhawk's avatar
pixhawk committed
787
    bool accepted = true;
788
    disconnect();
pixhawk's avatar
pixhawk committed
789

790
    switch (dataBits) {
pixhawk's avatar
pixhawk committed
791
    case 5:
792
        portSettings.setDataBits(QPortSettings::DB_5);
pixhawk's avatar
pixhawk committed
793 794
        break;
    case 6:
795
        portSettings.setDataBits(QPortSettings::DB_6);
pixhawk's avatar
pixhawk committed
796 797
        break;
    case 7:
798
        portSettings.setDataBits(QPortSettings::DB_7);
pixhawk's avatar
pixhawk committed
799 800
        break;
    case 8:
801
        portSettings.setDataBits(QPortSettings::DB_8);
pixhawk's avatar
pixhawk committed
802 803 804 805 806 807 808
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

809
    if(reconnect) connect();
pixhawk's avatar
pixhawk committed
810 811 812 813

    return accepted;
}

814
bool SerialLink::setStopBits(int stopBits)
815
{
pixhawk's avatar
pixhawk committed
816 817
    bool reconnect = false;
    bool accepted = true;
818 819
    if(isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
820

821
    switch (stopBits) {
pixhawk's avatar
pixhawk committed
822
    case 1:
823
        portSettings.setStopBits(QPortSettings::STOP_1);
pixhawk's avatar
pixhawk committed
824 825
        break;
    case 2:
826
        portSettings.setStopBits(QPortSettings::STOP_2);
pixhawk's avatar
pixhawk committed
827 828 829 830 831 832 833 834 835 836
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

    if(reconnect) connect();
    return accepted;
}
837 838 839 840 841 842

bool SerialLink::setDataBitsType(int dataBits)
{
    bool reconnect = false;
    bool accepted = false;

843 844
    if (isConnected()) reconnect = true;
    disconnect();
845

846 847
    if (dataBits >= (int)QPortSettings::DB_5 && dataBits <= (int)QPortSettings::DB_8) {
        portSettings.setDataBits((QPortSettings::DataBits) dataBits);
848

849
        if(reconnect) connect();
850 851 852 853 854 855 856 857 858 859
        accepted = true;
    }

    return accepted;
}

bool SerialLink::setStopBitsType(int stopBits)
{
    bool reconnect = false;
    bool accepted = false;
860 861
    if(isConnected()) reconnect = true;
    disconnect();
862

863 864
    if (stopBits >= (int)QPortSettings::STOP_1 && stopBits <= (int)QPortSettings::STOP_2) {
        portSettings.setStopBits((QPortSettings::StopBits) stopBits);
865

866
        if(reconnect) connect();
867 868 869 870 871 872
        accepted = true;
    }

    if(reconnect) connect();
    return accepted;
}