SerialLink.cc 21.7 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
//#define USE_QEXTSERIAL // this allows us to revert to old serial library during transition
pixhawk's avatar
pixhawk committed
25

26 27 28
SerialLink::SerialLink(QString portname, SerialInterface::baudRateType baudrate, SerialInterface::flowType flow, SerialInterface::parityType parity,
                       SerialInterface::dataBitsType dataBits, SerialInterface::stopBitsType stopBits) :
    port(NULL)
pixhawk's avatar
pixhawk committed
29 30 31 32 33 34
{
    // 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
35
    if (this->porthandle.size() > 0 && !this->porthandle.startsWith("\\")) {
pixhawk's avatar
pixhawk committed
36 37 38 39 40 41
        // 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();
42

43 44 45 46 47 48
    this->baudrate = baudrate;
    this->flow = flow;
    this->parity = parity;
    this->dataBits = dataBits;
    this->stopBits = stopBits;
    this->timeout = 1; ///< The timeout controls how long the program flow should wait for new serial bytes. As we're polling, we don't want to wait at all.
pixhawk's avatar
pixhawk committed
49 50

    // Set the port name
51 52
    if (porthandle == "")
    {
53
        name = tr("Serial Link ") + QString::number(getId());
pixhawk's avatar
pixhawk committed
54
    }
55 56 57
    else
    {
        name = portname.trimmed();
pixhawk's avatar
pixhawk committed
58
    }
lm's avatar
lm committed
59
    loadSettings();
pixhawk's avatar
pixhawk committed
60 61 62 63 64
}

SerialLink::~SerialLink()
{
    disconnect();
65
    if(port) delete port;
pixhawk's avatar
pixhawk committed
66 67 68
    port = NULL;
}

69 70 71 72 73
void SerialLink::loadSettings()
{
    // Load defaults from settings
    QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
    settings.sync();
74
    if (settings.contains("SERIALLINK_COMM_PORT")) {
75
        if (porthandle == "") setPortName(settings.value("SERIALLINK_COMM_PORT").toString());
76 77
        setBaudRateType(settings.value("SERIALLINK_COMM_BAUD").toInt());
        setParityType(settings.value("SERIALLINK_COMM_PARITY").toInt());
78 79 80
        setStopBits(settings.value("SERIALLINK_COMM_STOPBITS").toInt());
        setDataBits(settings.value("SERIALLINK_COMM_DATABITS").toInt());
        setFlowType(settings.value("SERIALLINK_COMM_FLOW_CONTROL").toInt());
81 82 83 84 85 86 87 88 89 90
    }
}

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());
91 92 93
    settings.setValue("SERIALLINK_COMM_STOPBITS", getStopBits());
    settings.setValue("SERIALLINK_COMM_DATABITS", getDataBits());
    settings.setValue("SERIALLINK_COMM_FLOW_CONTROL", getFlowType());
94 95 96
    settings.sync();
}

pixhawk's avatar
pixhawk committed
97 98 99 100 101

/**
 * @brief Runs the thread
 *
 **/
102 103
void SerialLink::run()
{
pixhawk's avatar
pixhawk committed
104 105 106 107
    // Initialize the connection
    hardwareConnect();

    // Qt way to make clear what a while(1) loop does
108
    forever {
pixhawk's avatar
pixhawk committed
109 110 111 112 113 114 115 116 117 118
        // 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);
    }
}


119 120
void SerialLink::checkForBytes()
{
pixhawk's avatar
pixhawk committed
121
    /* Check if bytes are available */
122
    if(port && port->isOpen() && port->isWritable()) {
pixhawk's avatar
pixhawk committed
123 124 125 126
        dataMutex.lock();
        qint64 available = port->bytesAvailable();
        dataMutex.unlock();

127
        if(available > 0) {
128
            readBytes();
pixhawk's avatar
pixhawk committed
129
        }
130
    } else {
pixhawk's avatar
pixhawk committed
131 132 133 134 135
        emit disconnected();
    }

}

136 137
void SerialLink::writeBytes(const char* data, qint64 size)
{
138
    if(port && port->isOpen()) {
pixhawk's avatar
pixhawk committed
139 140
        int b = port->write(data, size);

141
        if (b > 0) {
142

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

145 146 147
            // Increase write counter
            bitsSentTotal += size * 8;

148 149 150 151 152 153 154
//            int i;
//            for (i=0; i<size; i++)
//            {
//                unsigned char v =data[i];
//                qDebug("%02x ", v);
//            }
//            qDebug("\n");
155
        } else {
156 157 158
            disconnect();
            // Error occured
            emit communicationError(this->getName(), tr("Could not send data - link %1 is disconnected!").arg(this->getName()));
159
        }
pixhawk's avatar
pixhawk committed
160 161 162 163 164 165 166 167 168
    }
}

/**
 * @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
 **/
169 170
void SerialLink::readBytes()
{
pixhawk's avatar
pixhawk committed
171
    dataMutex.lock();
172
    if(port && port->isOpen()) {
173 174
        const qint64 maxLength = 2048;
        char data[maxLength];
pixhawk's avatar
pixhawk committed
175
        qint64 numBytes = port->bytesAvailable();
James Goppert's avatar
James Goppert committed
176
        //qDebug() << "numBytes: " << numBytes;
177

178
        if(numBytes > 0) {
pixhawk's avatar
pixhawk committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            /* 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
 **/
206 207
qint64 SerialLink::bytesAvailable()
{
208
    if (port) {
209
        return port->bytesAvailable();
210
    } else {
211 212
        return 0;
    }
pixhawk's avatar
pixhawk committed
213 214 215 216 217 218 219
}

/**
 * @brief Disconnect the connection.
 *
 * @return True if connection has been disconnected, false if connection couldn't be disconnected.
 **/
220 221
bool SerialLink::disconnect()
{
222
    if (port) {
223 224 225 226 227 228 229 230 231
        //#if !defined _WIN32 || !defined _WIN64
        /* Block the thread until it returns from run() */
        //#endif
//        dataMutex.lock();
        port->flush();
        port->close();
        delete port;
        port = NULL;
//        dataMutex.unlock();
pixhawk's avatar
pixhawk committed
232

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

235 236
        bool closed = true;
        //port->isOpen();
pixhawk's avatar
pixhawk committed
237

238 239 240
        emit disconnected();
        emit connected(false);
        return ! closed;
241
    } else {
242 243 244
        // No port, so we're disconnected
        return true;
    }
pixhawk's avatar
pixhawk committed
245 246 247 248 249 250 251 252 253 254

}

/**
 * @brief Connect the connection.
 *
 * @return True if connection has been established, false if connection couldn't be established.
 **/
bool SerialLink::connect()
{
255
    if (!isConnected()) {
256
        qDebug() << "CONNECTING LINK: " << __FILE__ << __LINE__ << "with settings" << porthandle << baudrate << dataBits << parity << stopBits;
257
        if (!this->isRunning()) {
258
            this->start(LowPriority);
pixhawk's avatar
pixhawk committed
259 260
        }
    }
261
    return true;
pixhawk's avatar
pixhawk committed
262 263 264 265 266 267 268 269 270 271
}

/**
 * @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.
 **/
272 273
bool SerialLink::hardwareConnect()
{
274
    if(port) {
275 276 277
        port->close();
        delete port;
    }
278
#ifdef USE_QEXTSERIAL
279
    port = new SerialQextserial(porthandle, QextSerialPort::Polling);
280 281 282
#else
    port = new SerialQserial(porthandle, QIODevice::ReadWrite);
#endif
pixhawk's avatar
pixhawk committed
283 284 285 286 287 288
    QObject::connect(port, SIGNAL(aboutToClose()), this, SIGNAL(disconnected()));
    port->open(QIODevice::ReadWrite);
    port->setBaudRate(this->baudrate);
    port->setParity(this->parity);
    port->setStopBits(this->stopBits);
    port->setDataBits(this->dataBits);
289
    port->setTimeout(timeout); // Timeout of 0 ms, we don't want to wait for data, we just poll again next time
pixhawk's avatar
pixhawk committed
290 291 292 293

    connectionStartTime = MG::TIME::getGroundTimeNow();

    bool connectionUp = isConnected();
294
    if(connectionUp) {
pixhawk's avatar
pixhawk committed
295 296 297 298
        emit connected();
        emit connected(true);
    }

299 300
    writeSettings();

pixhawk's avatar
pixhawk committed
301 302
    return connectionUp;
}
303 304


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

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

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

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


336 337
qint64 SerialLink::getNominalDataRate()
{
pixhawk's avatar
pixhawk committed
338
    qint64 dataRate = 0;
339
    switch (baudrate) {
340
    case SerialInterface::BAUD50:
pixhawk's avatar
pixhawk committed
341 342
        dataRate = 50;
        break;
343
    case SerialInterface::BAUD75:
pixhawk's avatar
pixhawk committed
344 345
        dataRate = 75;
        break;
346
    case SerialInterface::BAUD110:
pixhawk's avatar
pixhawk committed
347 348
        dataRate = 110;
        break;
349
    case SerialInterface::BAUD134:
pixhawk's avatar
pixhawk committed
350 351
        dataRate = 134;
        break;
352
    case SerialInterface::BAUD150:
pixhawk's avatar
pixhawk committed
353 354
        dataRate = 150;
        break;
355
    case SerialInterface::BAUD200:
pixhawk's avatar
pixhawk committed
356 357
        dataRate = 200;
        break;
358
    case SerialInterface::BAUD300:
pixhawk's avatar
pixhawk committed
359 360
        dataRate = 300;
        break;
361
    case SerialInterface::BAUD600:
pixhawk's avatar
pixhawk committed
362 363
        dataRate = 600;
        break;
364
    case SerialInterface::BAUD1200:
pixhawk's avatar
pixhawk committed
365 366
        dataRate = 1200;
        break;
367
    case SerialInterface::BAUD1800:
pixhawk's avatar
pixhawk committed
368 369
        dataRate = 1800;
        break;
370
    case SerialInterface::BAUD2400:
pixhawk's avatar
pixhawk committed
371 372
        dataRate = 2400;
        break;
373
    case SerialInterface::BAUD4800:
pixhawk's avatar
pixhawk committed
374 375
        dataRate = 4800;
        break;
376
    case SerialInterface::BAUD9600:
pixhawk's avatar
pixhawk committed
377 378
        dataRate = 9600;
        break;
379
    case SerialInterface::BAUD14400:
pixhawk's avatar
pixhawk committed
380 381
        dataRate = 14400;
        break;
382
    case SerialInterface::BAUD19200:
pixhawk's avatar
pixhawk committed
383 384
        dataRate = 19200;
        break;
385
    case SerialInterface::BAUD38400:
pixhawk's avatar
pixhawk committed
386 387
        dataRate = 38400;
        break;
388
    case SerialInterface::BAUD56000:
pixhawk's avatar
pixhawk committed
389 390
        dataRate = 56000;
        break;
391
    case SerialInterface::BAUD57600:
pixhawk's avatar
pixhawk committed
392 393
        dataRate = 57600;
        break;
394
    case SerialInterface::BAUD76800:
pixhawk's avatar
pixhawk committed
395 396
        dataRate = 76800;
        break;
397
    case SerialInterface::BAUD115200:
pixhawk's avatar
pixhawk committed
398 399
        dataRate = 115200;
        break;
400
    case SerialInterface::BAUD128000:
pixhawk's avatar
pixhawk committed
401 402
        dataRate = 128000;
        break;
403
    case SerialInterface::BAUD256000:
pixhawk's avatar
pixhawk committed
404
        dataRate = 256000;
405
        // Windows-specific high-end baudrates
406
    case SerialInterface::BAUD230400:
407
        dataRate = 230400;
408
    case SerialInterface::BAUD460800:
409
        dataRate = 460800;
410
    case SerialInterface::BAUD921600:
411
        dataRate = 921600;
pixhawk's avatar
pixhawk committed
412 413 414 415 416
        break;
    }
    return dataRate;
}

417 418
qint64 SerialLink::getTotalUpstream()
{
pixhawk's avatar
pixhawk committed
419 420 421 422 423
    statisticsMutex.lock();
    return bitsSentTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
}

424 425
qint64 SerialLink::getCurrentUpstream()
{
pixhawk's avatar
pixhawk committed
426 427 428
    return 0; // TODO
}

429 430
qint64 SerialLink::getMaxUpstream()
{
pixhawk's avatar
pixhawk committed
431 432 433
    return 0; // TODO
}

434 435
qint64 SerialLink::getBitsSent()
{
pixhawk's avatar
pixhawk committed
436 437 438
    return bitsSentTotal;
}

439 440
qint64 SerialLink::getBitsReceived()
{
pixhawk's avatar
pixhawk committed
441 442 443
    return bitsReceivedTotal;
}

444 445
qint64 SerialLink::getTotalDownstream()
{
pixhawk's avatar
pixhawk committed
446 447 448 449 450
    statisticsMutex.lock();
    return bitsReceivedTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
}

451 452
qint64 SerialLink::getCurrentDownstream()
{
pixhawk's avatar
pixhawk committed
453 454 455
    return 0; // TODO
}

456 457
qint64 SerialLink::getMaxDownstream()
{
pixhawk's avatar
pixhawk committed
458 459 460
    return 0; // TODO
}

461 462
bool SerialLink::isFullDuplex()
{
pixhawk's avatar
pixhawk committed
463 464 465 466
    /* Serial connections are always half duplex */
    return false;
}

467 468
int SerialLink::getLinkQuality()
{
pixhawk's avatar
pixhawk committed
469 470 471 472
    /* This feature is not supported with this interface */
    return -1;
}

473 474
QString SerialLink::getPortName()
{
pixhawk's avatar
pixhawk committed
475 476 477
    return porthandle;
}

478 479
int SerialLink::getBaudRate()
{
pixhawk's avatar
pixhawk committed
480 481 482
    return getNominalDataRate();
}

483 484
int SerialLink::getBaudRateType()
{
pixhawk's avatar
pixhawk committed
485 486 487
    return baudrate;
}

488 489
int SerialLink::getFlowType()
{
pixhawk's avatar
pixhawk committed
490 491 492
    return flow;
}

493 494
int SerialLink::getParityType()
{
pixhawk's avatar
pixhawk committed
495 496 497
    return parity;
}

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

503 504
int SerialLink::getStopBitsType()
{
pixhawk's avatar
pixhawk committed
505 506 507
    return stopBits;
}

508 509 510
int SerialLink::getDataBits()
{
    int ret;
511
    switch (dataBits) {
512
    case SerialInterface::DATA_5:
513 514
        ret = 5;
        break;
515
    case SerialInterface::DATA_6:
516 517
        ret = 6;
        break;
518
    case SerialInterface::DATA_7:
519 520
        ret = 7;
        break;
521
    case SerialInterface::DATA_8:
522 523 524 525 526 527 528 529 530 531 532 533
        ret = 8;
        break;
    default:
        ret = 0;
        break;
    }
    return ret;
}

int SerialLink::getStopBits()
{
    int ret;
534
    switch (stopBits) {
535
    case SerialInterface::STOP_1:
536 537
        ret = 1;
        break;
538
    case SerialInterface::STOP_2:
539 540 541 542 543 544 545 546 547
        ret = 2;
        break;
    default:
        ret = 0;
        break;
    }
    return ret;
}

pixhawk's avatar
pixhawk committed
548 549
bool SerialLink::setPortName(QString portName)
{
550
    if(portName.trimmed().length() > 0) {
pixhawk's avatar
pixhawk committed
551
        bool reconnect = false;
552 553 554
        if (isConnected()) reconnect = true;
        disconnect();

pixhawk's avatar
pixhawk committed
555 556 557 558 559
        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
560
        if (!this->porthandle.startsWith("\\")) {
pixhawk's avatar
pixhawk committed
561 562 563 564
            // Append \\.\ before the port handle. Additional backslashes are used for escaping.
            this->porthandle = "\\\\.\\" + this->porthandle;
        }
#endif
565

pixhawk's avatar
pixhawk committed
566 567
        if(reconnect) connect();
        return true;
568
    } else {
pixhawk's avatar
pixhawk committed
569 570 571 572 573 574 575 576 577
        return false;
    }
}


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

pixhawk's avatar
pixhawk committed
581 582
    switch (rateIndex) {
    case 0:
583
        baudrate = SerialInterface::BAUD50;
pixhawk's avatar
pixhawk committed
584 585
        break;
    case 1:
586
        baudrate = SerialInterface::BAUD75;
pixhawk's avatar
pixhawk committed
587 588
        break;
    case 2:
589
        baudrate = SerialInterface::BAUD110;
pixhawk's avatar
pixhawk committed
590 591
        break;
    case 3:
592
        baudrate = SerialInterface::BAUD134;
pixhawk's avatar
pixhawk committed
593 594
        break;
    case 4:
595
        baudrate = SerialInterface::BAUD150;
pixhawk's avatar
pixhawk committed
596 597
        break;
    case 5:
598
        baudrate = SerialInterface::BAUD200;
pixhawk's avatar
pixhawk committed
599 600
        break;
    case 6:
601
        baudrate = SerialInterface::BAUD300;
pixhawk's avatar
pixhawk committed
602 603
        break;
    case 7:
604
        baudrate = SerialInterface::BAUD600;
pixhawk's avatar
pixhawk committed
605 606
        break;
    case 8:
607
        baudrate = SerialInterface::BAUD1200;
pixhawk's avatar
pixhawk committed
608 609
        break;
    case 9:
610
        baudrate = SerialInterface::BAUD1800;
pixhawk's avatar
pixhawk committed
611 612
        break;
    case 10:
613
        baudrate = SerialInterface::BAUD2400;
pixhawk's avatar
pixhawk committed
614 615
        break;
    case 11:
616
        baudrate = SerialInterface::BAUD4800;
pixhawk's avatar
pixhawk committed
617 618
        break;
    case 12:
619
        baudrate = SerialInterface::BAUD9600;
pixhawk's avatar
pixhawk committed
620 621
        break;
    case 13:
622
        baudrate = SerialInterface::BAUD14400;
pixhawk's avatar
pixhawk committed
623 624
        break;
    case 14:
625
        baudrate = SerialInterface::BAUD19200;
pixhawk's avatar
pixhawk committed
626 627
        break;
    case 15:
628
        baudrate = SerialInterface::BAUD38400;
pixhawk's avatar
pixhawk committed
629 630
        break;
    case 16:
631
        baudrate = SerialInterface::BAUD56000;
pixhawk's avatar
pixhawk committed
632 633
        break;
    case 17:
634
        baudrate = SerialInterface::BAUD57600;
pixhawk's avatar
pixhawk committed
635 636
        break;
    case 18:
637
        baudrate = SerialInterface::BAUD76800;
pixhawk's avatar
pixhawk committed
638 639
        break;
    case 19:
640
        baudrate = SerialInterface::BAUD115200;
pixhawk's avatar
pixhawk committed
641 642
        break;
    case 20:
643
        baudrate = SerialInterface::BAUD128000;
pixhawk's avatar
pixhawk committed
644 645
        break;
    case 21:
646
        baudrate = SerialInterface::BAUD230400;
647 648
        break;
    case 22:
649
        baudrate = SerialInterface::BAUD256000;
pixhawk's avatar
pixhawk committed
650
        break;
651
    case 23:
652
        baudrate = SerialInterface::BAUD460800;
653 654
        break;
    case 24:
655
        baudrate = SerialInterface::BAUD921600;
656
        break;
pixhawk's avatar
pixhawk committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

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



bool SerialLink::setBaudRate(int rate)
{
    bool reconnect = false;
    bool accepted = true; // This is changed if none of the data rates matches
673
    if(isConnected()) {
pixhawk's avatar
pixhawk committed
674 675
        reconnect = true;
    }
676
    disconnect();
pixhawk's avatar
pixhawk committed
677

678
    switch (rate) {
pixhawk's avatar
pixhawk committed
679
    case 50:
680
        baudrate = SerialInterface::BAUD50;
pixhawk's avatar
pixhawk committed
681 682
        break;
    case 75:
683
        baudrate = SerialInterface::BAUD75;
pixhawk's avatar
pixhawk committed
684 685
        break;
    case 110:
686
        baudrate = SerialInterface::BAUD110;
pixhawk's avatar
pixhawk committed
687 688
        break;
    case 134:
689
        baudrate = SerialInterface::BAUD134;
pixhawk's avatar
pixhawk committed
690 691
        break;
    case 150:
692
        baudrate = SerialInterface::BAUD150;
pixhawk's avatar
pixhawk committed
693 694
        break;
    case 200:
695
        baudrate = SerialInterface::BAUD200;
pixhawk's avatar
pixhawk committed
696 697
        break;
    case 300:
698
        baudrate = SerialInterface::BAUD300;
pixhawk's avatar
pixhawk committed
699 700
        break;
    case 600:
701
        baudrate = SerialInterface::BAUD600;
pixhawk's avatar
pixhawk committed
702 703
        break;
    case 1200:
704
        baudrate = SerialInterface::BAUD1200;
pixhawk's avatar
pixhawk committed
705 706
        break;
    case 1800:
707
        baudrate = SerialInterface::BAUD1800;
pixhawk's avatar
pixhawk committed
708 709
        break;
    case 2400:
710
        baudrate = SerialInterface::BAUD2400;
pixhawk's avatar
pixhawk committed
711 712
        break;
    case 4800:
713
        baudrate = SerialInterface::BAUD4800;
pixhawk's avatar
pixhawk committed
714 715
        break;
    case 9600:
716
        baudrate = SerialInterface::BAUD9600;
pixhawk's avatar
pixhawk committed
717 718
        break;
    case 14400:
719
        baudrate = SerialInterface::BAUD14400;
pixhawk's avatar
pixhawk committed
720 721
        break;
    case 19200:
722
        baudrate = SerialInterface::BAUD19200;
pixhawk's avatar
pixhawk committed
723 724
        break;
    case 38400:
725
        baudrate = SerialInterface::BAUD38400;
pixhawk's avatar
pixhawk committed
726 727
        break;
    case 56000:
728
        baudrate = SerialInterface::BAUD56000;
pixhawk's avatar
pixhawk committed
729 730
        break;
    case 57600:
731
        baudrate = SerialInterface::BAUD57600;
pixhawk's avatar
pixhawk committed
732 733
        break;
    case 76800:
734
        baudrate = SerialInterface::BAUD76800;
pixhawk's avatar
pixhawk committed
735 736
        break;
    case 115200:
737
        baudrate = SerialInterface::BAUD115200;
pixhawk's avatar
pixhawk committed
738 739
        break;
    case 128000:
740
        baudrate = SerialInterface::BAUD128000;
pixhawk's avatar
pixhawk committed
741
        break;
742
    case 230400:
743
        baudrate = SerialInterface::BAUD230400;
744
        break;
pixhawk's avatar
pixhawk committed
745
    case 256000:
746
        baudrate = SerialInterface::BAUD256000;
pixhawk's avatar
pixhawk committed
747
        break;
748
    case 460800:
749
        baudrate = SerialInterface::BAUD460800;
750 751
        break;
    case 921600:
752
        baudrate = SerialInterface::BAUD921600;
753
        break;
pixhawk's avatar
pixhawk committed
754 755 756 757 758 759
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

760 761 762
    if(reconnect) connect();
    return accepted;

pixhawk's avatar
pixhawk committed
763 764
}

765 766
bool SerialLink::setFlowType(int flow)
{
pixhawk's avatar
pixhawk committed
767 768
    bool reconnect = false;
    bool accepted = true;
769 770
    if(isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
771

772
    switch (flow) {
773 774
    case SerialInterface::FLOW_OFF:
        this->flow = SerialInterface::FLOW_OFF;
pixhawk's avatar
pixhawk committed
775
        break;
776 777
    case SerialInterface::FLOW_HARDWARE:
        this->flow = SerialInterface::FLOW_HARDWARE;
pixhawk's avatar
pixhawk committed
778
        break;
779 780
    case SerialInterface::FLOW_XONXOFF:
        this->flow = SerialInterface::FLOW_XONXOFF;
pixhawk's avatar
pixhawk committed
781 782 783 784 785 786
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }
787

pixhawk's avatar
pixhawk committed
788 789 790 791
    if(reconnect) connect();
    return accepted;
}

792 793
bool SerialLink::setParityType(int parity)
{
pixhawk's avatar
pixhawk committed
794 795
    bool reconnect = false;
    bool accepted = true;
796 797
    if (isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
798

799
    switch (parity) {
800
    case (int)PAR_NONE:
801
        this->parity = SerialInterface::PAR_NONE;
pixhawk's avatar
pixhawk committed
802
        break;
803
    case (int)PAR_ODD:
804
        this->parity = SerialInterface::PAR_ODD;
pixhawk's avatar
pixhawk committed
805
        break;
806
    case (int)PAR_EVEN:
807
        this->parity = SerialInterface::PAR_EVEN;
pixhawk's avatar
pixhawk committed
808
        break;
809
    case (int)PAR_MARK:
810
        this->parity = SerialInterface::PAR_MARK;
pixhawk's avatar
pixhawk committed
811
        break;
812
    case (int)PAR_SPACE:
813
        this->parity = SerialInterface::PAR_SPACE;
pixhawk's avatar
pixhawk committed
814 815 816 817 818 819 820
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

821
    if (reconnect) connect();
pixhawk's avatar
pixhawk committed
822 823 824
    return accepted;
}

825

826
bool SerialLink::setDataBits(int dataBits)
827
{
828 829
    bool reconnect = false;
    if (isConnected()) reconnect = true;
pixhawk's avatar
pixhawk committed
830
    bool accepted = true;
831
    disconnect();
pixhawk's avatar
pixhawk committed
832

833
    switch (dataBits) {
pixhawk's avatar
pixhawk committed
834
    case 5:
835
        this->dataBits = SerialInterface::DATA_5;
pixhawk's avatar
pixhawk committed
836 837
        break;
    case 6:
838
        this->dataBits = SerialInterface::DATA_6;
pixhawk's avatar
pixhawk committed
839 840
        break;
    case 7:
841
        this->dataBits = SerialInterface::DATA_7;
pixhawk's avatar
pixhawk committed
842 843
        break;
    case 8:
844
        this->dataBits = SerialInterface::DATA_8;
pixhawk's avatar
pixhawk committed
845 846 847 848 849 850 851
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

852
    if(reconnect) connect();
pixhawk's avatar
pixhawk committed
853 854 855 856

    return accepted;
}

857
bool SerialLink::setStopBits(int stopBits)
858
{
pixhawk's avatar
pixhawk committed
859 860
    bool reconnect = false;
    bool accepted = true;
861 862
    if(isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
863

864
    switch (stopBits) {
pixhawk's avatar
pixhawk committed
865
    case 1:
866
        this->stopBits = SerialInterface::STOP_1;
pixhawk's avatar
pixhawk committed
867 868
        break;
    case 2:
869
        this->stopBits = SerialInterface::STOP_2;
pixhawk's avatar
pixhawk committed
870 871 872 873 874 875 876 877 878 879
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

    if(reconnect) connect();
    return accepted;
}
880 881 882 883 884 885

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

886 887
    if (isConnected()) reconnect = true;
    disconnect();
888

889
    if (dataBits >= (int)SerialInterface::DATA_5 && dataBits <= (int)SerialInterface::DATA_8) {
890
        this->dataBits = (SerialInterface::dataBitsType) dataBits;
891

892
        if(reconnect) connect();
893 894 895 896 897 898 899 900 901 902
        accepted = true;
    }

    return accepted;
}

bool SerialLink::setStopBitsType(int stopBits)
{
    bool reconnect = false;
    bool accepted = false;
903 904
    if(isConnected()) reconnect = true;
    disconnect();
905

906 907
    if (stopBits >= (int)SerialInterface::STOP_1 && dataBits <= (int)SerialInterface::STOP_2) {
        SerialInterface::stopBitsType newBits = (SerialInterface::stopBitsType) stopBits;
908 909 910 911 912 913 914 915

        port->setStopBits(newBits);
        accepted = true;
    }

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