SerialLink.cc 22.3 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 24
#ifdef _WIN32
#include "windows.h"
#endif


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

    // Set the port name
    if (porthandle == "")
    {
53
        //        name = tr("serial link ") + QString::number(getId()) + tr(" (unconfigured)");
54
        name = tr("Serial Link ") + QString::number(getId());
pixhawk's avatar
pixhawk committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    }
    else
    {
        name = portname.trimmed();
    }

#ifdef _WIN3232
    // Windows 32bit & 64bit serial connection
    winPort = CreateFile(porthandle,
                         GENERIC_READ | GENERIC_WRITE,
                         0,
                         0,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         0);
    if(winPort==INVALID_HANDLE_VALUE){
        if(GetLastError()==ERROR_FILE_NOT_FOUND){
            //serial port does not exist. Inform user.
        }
        //some other error occurred. Inform user.
    }
#else
lm's avatar
lm committed
77

pixhawk's avatar
pixhawk committed
78 79
#endif

lm's avatar
lm committed
80
    loadSettings();
pixhawk's avatar
pixhawk committed
81 82 83 84 85
}

SerialLink::~SerialLink()
{
    disconnect();
86
    if(port) delete port;
pixhawk's avatar
pixhawk committed
87 88 89
    port = NULL;
}

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

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());
113 114 115
    settings.setValue("SERIALLINK_COMM_STOPBITS", getStopBits());
    settings.setValue("SERIALLINK_COMM_DATABITS", getDataBits());
    settings.setValue("SERIALLINK_COMM_FLOW_CONTROL", getFlowType());
116 117 118
    settings.sync();
}

pixhawk's avatar
pixhawk committed
119 120 121 122 123

/**
 * @brief Runs the thread
 *
 **/
124 125
void SerialLink::run()
{
pixhawk's avatar
pixhawk committed
126 127 128 129
    // Initialize the connection
    hardwareConnect();

    // Qt way to make clear what a while(1) loop does
130 131
    forever
    {
pixhawk's avatar
pixhawk committed
132 133 134 135 136 137 138 139 140 141
        // 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);
    }
}


142 143
void SerialLink::checkForBytes()
{
pixhawk's avatar
pixhawk committed
144
    /* Check if bytes are available */
145
    if(port && port->isOpen() && port->isWriteable())
146
    {
pixhawk's avatar
pixhawk committed
147 148 149 150
        dataMutex.lock();
        qint64 available = port->bytesAvailable();
        dataMutex.unlock();

151 152 153
        if(available > 0)
        {
            readBytes();
pixhawk's avatar
pixhawk committed
154
        }
155 156 157
    }
    else
    {
pixhawk's avatar
pixhawk committed
158 159 160 161 162
        emit disconnected();
    }

}

163 164
void SerialLink::writeBytes(const char* data, qint64 size)
{
165
    if(port && port->isOpen())
pixhawk's avatar
pixhawk committed
166
    {
pixhawk's avatar
pixhawk committed
167 168
        int b = port->write(data, size);

169 170 171
        if (b > 0)
        {

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

174 175 176
            // Increase write counter
            bitsSentTotal += size * 8;

177 178 179 180 181 182 183
//            int i;
//            for (i=0; i<size; i++)
//            {
//                unsigned char v =data[i];
//                qDebug("%02x ", v);
//            }
//            qDebug("\n");
184 185
        }
        else
186
        {
187 188 189
            disconnect();
            // Error occured
            emit communicationError(this->getName(), tr("Could not send data - link %1 is disconnected!").arg(this->getName()));
190
        }
pixhawk's avatar
pixhawk committed
191 192 193 194 195 196 197 198 199
    }
}

/**
 * @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
 **/
200 201
void SerialLink::readBytes()
{
pixhawk's avatar
pixhawk committed
202
    dataMutex.lock();
203
    if(port && port->isOpen())
204 205 206
    {
        const qint64 maxLength = 2048;
        char data[maxLength];
pixhawk's avatar
pixhawk committed
207
        qint64 numBytes = port->bytesAvailable();
208

209 210
        if(numBytes > 0)
        {
pixhawk's avatar
pixhawk committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
            /* 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
 **/
238 239
qint64 SerialLink::bytesAvailable()
{
240 241 242 243 244 245 246 247
    if (port)
    {
        return port->bytesAvailable();
    }
    else
    {
        return 0;
    }
pixhawk's avatar
pixhawk committed
248 249 250 251 252 253 254
}

/**
 * @brief Disconnect the connection.
 *
 * @return True if connection has been disconnected, false if connection couldn't be disconnected.
 **/
255 256
bool SerialLink::disconnect()
{
257 258 259 260 261 262 263 264 265 266 267
    if (port)
    {
        //#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
268

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

271 272
        bool closed = true;
        //port->isOpen();
pixhawk's avatar
pixhawk committed
273

274 275 276 277 278 279 280 281 282
        emit disconnected();
        emit connected(false);
        return ! closed;
    }
    else
    {
        // No port, so we're disconnected
        return true;
    }
pixhawk's avatar
pixhawk committed
283 284 285 286 287 288 289 290 291 292

}

/**
 * @brief Connect the connection.
 *
 * @return True if connection has been established, false if connection couldn't be established.
 **/
bool SerialLink::connect()
{
293
    if (!isConnected())
pixhawk's avatar
pixhawk committed
294
    {
295 296
        qDebug() << "CONNECTING LINK: " << __FILE__ << __LINE__ << "with settings" << porthandle << baudrate << dataBits << parity << stopBits;
        if (!this->isRunning())
pixhawk's avatar
pixhawk committed
297
        {
298
            this->start(LowPriority);
pixhawk's avatar
pixhawk committed
299 300
        }
    }
301
	return true;
pixhawk's avatar
pixhawk committed
302 303 304 305 306 307 308 309 310 311
}

/**
 * @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.
 **/
312 313
bool SerialLink::hardwareConnect()
{
314 315 316 317 318
    if(port)
    {
        port->close();
        delete port;
    }
319
    port = new SerialQextserial(porthandle, QextSerialPort::Polling);
pixhawk's avatar
pixhawk committed
320 321 322 323 324 325 326
    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);
327
    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
328 329 330 331

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

    bool connectionUp = isConnected();
332 333
    if(connectionUp)
    {
pixhawk's avatar
pixhawk committed
334 335 336 337
        emit connected();
        emit connected(true);
    }

338 339
    writeSettings();

pixhawk's avatar
pixhawk committed
340 341
    return connectionUp;
}
342 343


pixhawk's avatar
pixhawk committed
344 345 346 347 348
/**
 * @brief Check if connection is active.
 *
 * @return True if link is connected, false otherwise.
 **/
349 350
bool SerialLink::isConnected()
{
lm's avatar
lm committed
351 352 353 354 355 356 357 358
    if (port)
    {
        return port->isOpen();
    }
    else
    {
        return false;
    }
pixhawk's avatar
pixhawk committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
}

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

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

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


378 379
qint64 SerialLink::getNominalDataRate()
{
pixhawk's avatar
pixhawk committed
380
    qint64 dataRate = 0;
381 382
    switch (baudrate)
    {
383
    case SerialInterface::BAUD50:
pixhawk's avatar
pixhawk committed
384 385
        dataRate = 50;
        break;
386
    case SerialInterface::BAUD75:
pixhawk's avatar
pixhawk committed
387 388
        dataRate = 75;
        break;
389
    case SerialInterface::BAUD110:
pixhawk's avatar
pixhawk committed
390 391
        dataRate = 110;
        break;
392
    case SerialInterface::BAUD134:
pixhawk's avatar
pixhawk committed
393 394
        dataRate = 134;
        break;
395
    case SerialInterface::BAUD150:
pixhawk's avatar
pixhawk committed
396 397
        dataRate = 150;
        break;
398
    case SerialInterface::BAUD200:
pixhawk's avatar
pixhawk committed
399 400
        dataRate = 200;
        break;
401
    case SerialInterface::BAUD300:
pixhawk's avatar
pixhawk committed
402 403
        dataRate = 300;
        break;
404
    case SerialInterface::BAUD600:
pixhawk's avatar
pixhawk committed
405 406
        dataRate = 600;
        break;
407
    case SerialInterface::BAUD1200:
pixhawk's avatar
pixhawk committed
408 409
        dataRate = 1200;
        break;
410
    case SerialInterface::BAUD1800:
pixhawk's avatar
pixhawk committed
411 412
        dataRate = 1800;
        break;
413
    case SerialInterface::BAUD2400:
pixhawk's avatar
pixhawk committed
414 415
        dataRate = 2400;
        break;
416
    case SerialInterface::BAUD4800:
pixhawk's avatar
pixhawk committed
417 418
        dataRate = 4800;
        break;
419
    case SerialInterface::BAUD9600:
pixhawk's avatar
pixhawk committed
420 421
        dataRate = 9600;
        break;
422
    case SerialInterface::BAUD14400:
pixhawk's avatar
pixhawk committed
423 424
        dataRate = 14400;
        break;
425
    case SerialInterface::BAUD19200:
pixhawk's avatar
pixhawk committed
426 427
        dataRate = 19200;
        break;
428
    case SerialInterface::BAUD38400:
pixhawk's avatar
pixhawk committed
429 430
        dataRate = 38400;
        break;
431
    case SerialInterface::BAUD56000:
pixhawk's avatar
pixhawk committed
432 433
        dataRate = 56000;
        break;
434
    case SerialInterface::BAUD57600:
pixhawk's avatar
pixhawk committed
435 436
        dataRate = 57600;
        break;
437
    case SerialInterface::BAUD76800:
pixhawk's avatar
pixhawk committed
438 439
        dataRate = 76800;
        break;
440
    case SerialInterface::BAUD115200:
pixhawk's avatar
pixhawk committed
441 442
        dataRate = 115200;
        break;
443
    case SerialInterface::BAUD128000:
pixhawk's avatar
pixhawk committed
444 445
        dataRate = 128000;
        break;
446
    case SerialInterface::BAUD256000:
pixhawk's avatar
pixhawk committed
447
        dataRate = 256000;
448
        // Windows-specific high-end baudrates
449
    case SerialInterface::BAUD230400:
450
        dataRate = 230400;
451
    case SerialInterface::BAUD460800:
452
        dataRate = 460800;
453
    case SerialInterface::BAUD921600:
454
        dataRate = 921600;
pixhawk's avatar
pixhawk committed
455 456 457 458 459
        break;
    }
    return dataRate;
}

460 461
qint64 SerialLink::getTotalUpstream()
{
pixhawk's avatar
pixhawk committed
462 463 464 465 466
    statisticsMutex.lock();
    return bitsSentTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
}

467 468
qint64 SerialLink::getCurrentUpstream()
{
pixhawk's avatar
pixhawk committed
469 470 471
    return 0; // TODO
}

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

477 478
qint64 SerialLink::getBitsSent()
{
pixhawk's avatar
pixhawk committed
479 480 481
    return bitsSentTotal;
}

482 483
qint64 SerialLink::getBitsReceived()
{
pixhawk's avatar
pixhawk committed
484 485 486
    return bitsReceivedTotal;
}

487 488
qint64 SerialLink::getTotalDownstream()
{
pixhawk's avatar
pixhawk committed
489 490 491 492 493
    statisticsMutex.lock();
    return bitsReceivedTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
}

494 495
qint64 SerialLink::getCurrentDownstream()
{
pixhawk's avatar
pixhawk committed
496 497 498
    return 0; // TODO
}

499 500
qint64 SerialLink::getMaxDownstream()
{
pixhawk's avatar
pixhawk committed
501 502 503
    return 0; // TODO
}

504 505
bool SerialLink::isFullDuplex()
{
pixhawk's avatar
pixhawk committed
506 507 508 509
    /* Serial connections are always half duplex */
    return false;
}

510 511
int SerialLink::getLinkQuality()
{
pixhawk's avatar
pixhawk committed
512 513 514 515
    /* This feature is not supported with this interface */
    return -1;
}

516 517
QString SerialLink::getPortName()
{
pixhawk's avatar
pixhawk committed
518 519 520
    return porthandle;
}

521 522
int SerialLink::getBaudRate()
{
pixhawk's avatar
pixhawk committed
523 524 525
    return getNominalDataRate();
}

526 527
int SerialLink::getBaudRateType()
{
pixhawk's avatar
pixhawk committed
528 529 530
    return baudrate;
}

531 532
int SerialLink::getFlowType()
{
pixhawk's avatar
pixhawk committed
533 534 535
    return flow;
}

536 537
int SerialLink::getParityType()
{
pixhawk's avatar
pixhawk committed
538 539 540
    return parity;
}

541 542
int SerialLink::getDataBitsType()
{
pixhawk's avatar
pixhawk committed
543 544 545
    return dataBits;
}

546 547
int SerialLink::getStopBitsType()
{
pixhawk's avatar
pixhawk committed
548 549 550
    return stopBits;
}

551 552 553 554 555
int SerialLink::getDataBits()
{
    int ret;
    switch (dataBits)
    {
556
    case SerialInterface::DATA_5:
557 558
        ret = 5;
        break;
559
    case SerialInterface::DATA_6:
560 561
        ret = 6;
        break;
562
    case SerialInterface::DATA_7:
563 564
        ret = 7;
        break;
565
    case SerialInterface::DATA_8:
566 567 568 569 570 571 572 573 574 575 576 577 578 579
        ret = 8;
        break;
    default:
        ret = 0;
        break;
    }
    return ret;
}

int SerialLink::getStopBits()
{
    int ret;
    switch (stopBits)
    {
580
    case SerialInterface::STOP_1:
581 582
        ret = 1;
        break;
583
    case SerialInterface::STOP_2:
584 585 586 587 588 589 590 591 592
        ret = 2;
        break;
    default:
        ret = 0;
        break;
    }
    return ret;
}

pixhawk's avatar
pixhawk committed
593 594 595 596 597
bool SerialLink::setPortName(QString portName)
{
    if(portName.trimmed().length() > 0)
    {
        bool reconnect = false;
598 599 600
        if (isConnected()) reconnect = true;
        disconnect();

pixhawk's avatar
pixhawk committed
601 602 603 604 605 606 607 608 609 610 611
        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
        if (!this->porthandle.startsWith("\\"))
        {
            // Append \\.\ before the port handle. Additional backslashes are used for escaping.
            this->porthandle = "\\\\.\\" + this->porthandle;
        }
#endif
612

pixhawk's avatar
pixhawk committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626
        if(reconnect) connect();
        return true;
    }
    else
    {
        return false;
    }
}


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

pixhawk's avatar
pixhawk committed
630 631
    switch (rateIndex) {
    case 0:
632
        baudrate = SerialInterface::BAUD50;
pixhawk's avatar
pixhawk committed
633 634
        break;
    case 1:
635
        baudrate = SerialInterface::BAUD75;
pixhawk's avatar
pixhawk committed
636 637
        break;
    case 2:
638
        baudrate = SerialInterface::BAUD110;
pixhawk's avatar
pixhawk committed
639 640
        break;
    case 3:
641
        baudrate = SerialInterface::BAUD134;
pixhawk's avatar
pixhawk committed
642 643
        break;
    case 4:
644
        baudrate = SerialInterface::BAUD150;
pixhawk's avatar
pixhawk committed
645 646
        break;
    case 5:
647
        baudrate = SerialInterface::BAUD200;
pixhawk's avatar
pixhawk committed
648 649
        break;
    case 6:
650
        baudrate = SerialInterface::BAUD300;
pixhawk's avatar
pixhawk committed
651 652
        break;
    case 7:
653
        baudrate = SerialInterface::BAUD600;
pixhawk's avatar
pixhawk committed
654 655
        break;
    case 8:
656
        baudrate = SerialInterface::BAUD1200;
pixhawk's avatar
pixhawk committed
657 658
        break;
    case 9:
659
        baudrate = SerialInterface::BAUD1800;
pixhawk's avatar
pixhawk committed
660 661
        break;
    case 10:
662
        baudrate = SerialInterface::BAUD2400;
pixhawk's avatar
pixhawk committed
663 664
        break;
    case 11:
665
        baudrate = SerialInterface::BAUD4800;
pixhawk's avatar
pixhawk committed
666 667
        break;
    case 12:
668
        baudrate = SerialInterface::BAUD9600;
pixhawk's avatar
pixhawk committed
669 670
        break;
    case 13:
671
        baudrate = SerialInterface::BAUD14400;
pixhawk's avatar
pixhawk committed
672 673
        break;
    case 14:
674
        baudrate = SerialInterface::BAUD19200;
pixhawk's avatar
pixhawk committed
675 676
        break;
    case 15:
677
        baudrate = SerialInterface::BAUD38400;
pixhawk's avatar
pixhawk committed
678 679
        break;
    case 16:
680
        baudrate = SerialInterface::BAUD56000;
pixhawk's avatar
pixhawk committed
681 682
        break;
    case 17:
683
        baudrate = SerialInterface::BAUD57600;
pixhawk's avatar
pixhawk committed
684 685
        break;
    case 18:
686
        baudrate = SerialInterface::BAUD76800;
pixhawk's avatar
pixhawk committed
687 688
        break;
    case 19:
689
        baudrate = SerialInterface::BAUD115200;
pixhawk's avatar
pixhawk committed
690 691
        break;
    case 20:
692
        baudrate = SerialInterface::BAUD128000;
pixhawk's avatar
pixhawk committed
693 694
        break;
    case 21:
695
        baudrate = SerialInterface::BAUD230400;
696 697
        break;
    case 22:
698
        baudrate = SerialInterface::BAUD256000;
pixhawk's avatar
pixhawk committed
699
        break;
700
    case 23:
701
        baudrate = SerialInterface::BAUD460800;
702 703
        break;
    case 24:
704
        baudrate = SerialInterface::BAUD921600;
705
        break;
pixhawk's avatar
pixhawk committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    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
722 723
    if(isConnected())
    {
pixhawk's avatar
pixhawk committed
724 725
        reconnect = true;
    }
726
    disconnect();
pixhawk's avatar
pixhawk committed
727

728 729
    switch (rate)
    {
pixhawk's avatar
pixhawk committed
730
    case 50:
731
        baudrate = SerialInterface::BAUD50;
pixhawk's avatar
pixhawk committed
732 733
        break;
    case 75:
734
        baudrate = SerialInterface::BAUD75;
pixhawk's avatar
pixhawk committed
735 736
        break;
    case 110:
737
        baudrate = SerialInterface::BAUD110;
pixhawk's avatar
pixhawk committed
738 739
        break;
    case 134:
740
        baudrate = SerialInterface::BAUD134;
pixhawk's avatar
pixhawk committed
741 742
        break;
    case 150:
743
        baudrate = SerialInterface::BAUD150;
pixhawk's avatar
pixhawk committed
744 745
        break;
    case 200:
746
        baudrate = SerialInterface::BAUD200;
pixhawk's avatar
pixhawk committed
747 748
        break;
    case 300:
749
        baudrate = SerialInterface::BAUD300;
pixhawk's avatar
pixhawk committed
750 751
        break;
    case 600:
752
        baudrate = SerialInterface::BAUD600;
pixhawk's avatar
pixhawk committed
753 754
        break;
    case 1200:
755
        baudrate = SerialInterface::BAUD1200;
pixhawk's avatar
pixhawk committed
756 757
        break;
    case 1800:
758
        baudrate = SerialInterface::BAUD1800;
pixhawk's avatar
pixhawk committed
759 760
        break;
    case 2400:
761
        baudrate = SerialInterface::BAUD2400;
pixhawk's avatar
pixhawk committed
762 763
        break;
    case 4800:
764
        baudrate = SerialInterface::BAUD4800;
pixhawk's avatar
pixhawk committed
765 766
        break;
    case 9600:
767
        baudrate = SerialInterface::BAUD9600;
pixhawk's avatar
pixhawk committed
768 769
        break;
    case 14400:
770
        baudrate = SerialInterface::BAUD14400;
pixhawk's avatar
pixhawk committed
771 772
        break;
    case 19200:
773
        baudrate = SerialInterface::BAUD19200;
pixhawk's avatar
pixhawk committed
774 775
        break;
    case 38400:
776
        baudrate = SerialInterface::BAUD38400;
pixhawk's avatar
pixhawk committed
777 778
        break;
    case 56000:
779
        baudrate = SerialInterface::BAUD56000;
pixhawk's avatar
pixhawk committed
780 781
        break;
    case 57600:
782
        baudrate = SerialInterface::BAUD57600;
pixhawk's avatar
pixhawk committed
783 784
        break;
    case 76800:
785
        baudrate = SerialInterface::BAUD76800;
pixhawk's avatar
pixhawk committed
786 787
        break;
    case 115200:
788
        baudrate = SerialInterface::BAUD115200;
pixhawk's avatar
pixhawk committed
789 790
        break;
    case 128000:
791
        baudrate = SerialInterface::BAUD128000;
pixhawk's avatar
pixhawk committed
792
        break;
793
    case 230400:
794
        baudrate = SerialInterface::BAUD230400;
795
        break;
pixhawk's avatar
pixhawk committed
796
    case 256000:
797
        baudrate = SerialInterface::BAUD256000;
pixhawk's avatar
pixhawk committed
798
        break;
799
    case 460800:
800
        baudrate = SerialInterface::BAUD460800;
801 802
        break;
    case 921600:
803
        baudrate = SerialInterface::BAUD921600;
804
        break;
pixhawk's avatar
pixhawk committed
805 806 807 808 809 810
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

811 812 813
    if(reconnect) connect();
    return accepted;

pixhawk's avatar
pixhawk committed
814 815
}

816 817
bool SerialLink::setFlowType(int flow)
{
pixhawk's avatar
pixhawk committed
818 819
    bool reconnect = false;
    bool accepted = true;
820 821
    if(isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
822

823 824
    switch (flow)
    {
825 826
    case SerialInterface::FLOW_OFF:
        this->flow = SerialInterface::FLOW_OFF;
pixhawk's avatar
pixhawk committed
827
        break;
828 829
    case SerialInterface::FLOW_HARDWARE:
        this->flow = SerialInterface::FLOW_HARDWARE;
pixhawk's avatar
pixhawk committed
830
        break;
831 832
    case SerialInterface::FLOW_XONXOFF:
        this->flow = SerialInterface::FLOW_XONXOFF;
pixhawk's avatar
pixhawk committed
833 834 835 836 837 838
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }
839

pixhawk's avatar
pixhawk committed
840 841 842 843
    if(reconnect) connect();
    return accepted;
}

844 845
bool SerialLink::setParityType(int parity)
{
pixhawk's avatar
pixhawk committed
846 847
    bool reconnect = false;
    bool accepted = true;
848 849
    if (isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
850

851 852
    switch (parity)
    {
853
    case (int)PAR_NONE:
854
        this->parity = SerialInterface::PAR_NONE;
pixhawk's avatar
pixhawk committed
855
        break;
856
    case (int)PAR_ODD:
857
        this->parity = SerialInterface::PAR_ODD;
pixhawk's avatar
pixhawk committed
858
        break;
859
    case (int)PAR_EVEN:
860
        this->parity = SerialInterface::PAR_EVEN;
pixhawk's avatar
pixhawk committed
861
        break;
862
    case (int)PAR_MARK:
863
        this->parity = SerialInterface::PAR_MARK;
pixhawk's avatar
pixhawk committed
864
        break;
865
    case (int)PAR_SPACE:
866
        this->parity = SerialInterface::PAR_SPACE;
pixhawk's avatar
pixhawk committed
867 868 869 870 871 872 873
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

874
    if (reconnect) connect();
pixhawk's avatar
pixhawk committed
875 876 877
    return accepted;
}

878

879
bool SerialLink::setDataBits(int dataBits)
880
{
881 882
    bool reconnect = false;
    if (isConnected()) reconnect = true;
pixhawk's avatar
pixhawk committed
883
    bool accepted = true;
884
    disconnect();
pixhawk's avatar
pixhawk committed
885

886 887
    switch (dataBits)
    {
pixhawk's avatar
pixhawk committed
888
    case 5:
889
        this->dataBits = SerialInterface::DATA_5;
pixhawk's avatar
pixhawk committed
890 891
        break;
    case 6:
892
        this->dataBits = SerialInterface::DATA_6;
pixhawk's avatar
pixhawk committed
893 894
        break;
    case 7:
895
        this->dataBits = SerialInterface::DATA_7;
pixhawk's avatar
pixhawk committed
896 897
        break;
    case 8:
898
        this->dataBits = SerialInterface::DATA_8;
pixhawk's avatar
pixhawk committed
899 900 901 902 903 904 905
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

906
    if(reconnect) connect();
pixhawk's avatar
pixhawk committed
907 908 909 910

    return accepted;
}

911
bool SerialLink::setStopBits(int stopBits)
912
{
pixhawk's avatar
pixhawk committed
913 914
    bool reconnect = false;
    bool accepted = true;
915 916
    if(isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
917

918 919
    switch (stopBits)
    {
pixhawk's avatar
pixhawk committed
920
    case 1:
921
        this->stopBits = SerialInterface::STOP_1;
pixhawk's avatar
pixhawk committed
922 923
        break;
    case 2:
924
        this->stopBits = SerialInterface::STOP_2;
pixhawk's avatar
pixhawk committed
925 926 927 928 929 930 931 932 933 934
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

    if(reconnect) connect();
    return accepted;
}
935 936 937 938 939 940

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

941 942
    if (isConnected()) reconnect = true;
    disconnect();
943

944
    if (dataBits >= (int)SerialInterface::DATA_5 && dataBits <= (int)SerialInterface::DATA_8)
945
    {
946
        this->dataBits = (SerialInterface::dataBitsType) dataBits;
947

948
        if(reconnect) connect();
949 950 951 952 953 954 955 956 957 958
        accepted = true;
    }

    return accepted;
}

bool SerialLink::setStopBitsType(int stopBits)
{
    bool reconnect = false;
    bool accepted = false;
959 960
    if(isConnected()) reconnect = true;
    disconnect();
961

962
    if (stopBits >= (int)SerialInterface::STOP_1 && dataBits <= (int)SerialInterface::STOP_2)
963
    {
964
		SerialInterface::stopBitsType newBits = (SerialInterface::stopBitsType) stopBits;
965 966 967 968 969 970 971 972

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

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