SerialLink.cc 20.9 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, BaudRateType baudrate, FlowType flow, ParityType parity, DataBitsType dataBits, StopBitsType stopBits) :
        port(NULL)
pixhawk's avatar
pixhawk committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40
{
    // 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();
41

lm's avatar
lm committed
42
    // *nix (Linux, MacOS tested) serial port support
43
//    port = new QextSerialPort(porthandle, QextSerialPort::Polling);
lm's avatar
lm committed
44 45
    //port = new QextSerialPort(porthandle, QextSerialPort::EventDriven);

46 47 48 49 50 51
    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.
52 53 54 55 56 57
//    port->setTimeout(timeout); // Timeout of 0 ms, we don't want to wait for data, we just poll again next time
//    port->setBaudRate(baudrate);
//    port->setFlowControl(flow);
//    port->setParity(parity);
//    port->setDataBits(dataBits);
//    port->setStopBits(stopBits);
pixhawk's avatar
pixhawk committed
58 59 60 61

    // Set the port name
    if (porthandle == "")
    {
62
        //        name = tr("serial link ") + QString::number(getId()) + tr(" (unconfigured)");
63
        name = tr("Serial Link ") + QString::number(getId());
pixhawk's avatar
pixhawk committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    }
    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
86

pixhawk's avatar
pixhawk committed
87 88
#endif

lm's avatar
lm committed
89
    loadSettings();
pixhawk's avatar
pixhawk committed
90 91 92 93 94
}

SerialLink::~SerialLink()
{
    disconnect();
95
    if(port) delete port;
pixhawk's avatar
pixhawk committed
96 97 98
    port = NULL;
}

99 100 101 102 103 104 105 106 107 108
void SerialLink::loadSettings()
{
    // Load defaults from settings
    QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
    settings.sync();
    if (settings.contains("SERIALLINK_COMM_PORT"))
    {
        setPortName(settings.value("SERIALLINK_COMM_PORT").toString());
        setBaudRateType(settings.value("SERIALLINK_COMM_BAUD").toInt());
        setParityType(settings.value("SERIALLINK_COMM_PARITY").toInt());
109 110 111
        setStopBits(settings.value("SERIALLINK_COMM_STOPBITS").toInt());
        setDataBits(settings.value("SERIALLINK_COMM_DATABITS").toInt());
        setFlowType(settings.value("SERIALLINK_COMM_FLOW_CONTROL").toInt());
112 113 114 115 116 117 118 119 120 121
    }
}

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());
122 123 124
    settings.setValue("SERIALLINK_COMM_STOPBITS", getStopBits());
    settings.setValue("SERIALLINK_COMM_DATABITS", getDataBits());
    settings.setValue("SERIALLINK_COMM_FLOW_CONTROL", getFlowType());
125 126 127
    settings.sync();
}

pixhawk's avatar
pixhawk committed
128 129 130 131 132

/**
 * @brief Runs the thread
 *
 **/
133 134
void SerialLink::run()
{
pixhawk's avatar
pixhawk committed
135 136 137 138
    // Initialize the connection
    hardwareConnect();

    // Qt way to make clear what a while(1) loop does
139 140
    forever
    {
pixhawk's avatar
pixhawk committed
141 142 143 144 145 146 147 148 149 150
        // 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);
    }
}


151 152
void SerialLink::checkForBytes()
{
pixhawk's avatar
pixhawk committed
153
    /* Check if bytes are available */
154
    if(port && port->isOpen() && port->isWritable())
155
    {
pixhawk's avatar
pixhawk committed
156 157 158 159
        dataMutex.lock();
        qint64 available = port->bytesAvailable();
        dataMutex.unlock();

160 161 162
        if(available > 0)
        {
            readBytes();
pixhawk's avatar
pixhawk committed
163
        }
164 165 166
    }
    else
    {
pixhawk's avatar
pixhawk committed
167 168 169 170 171 172
        emit disconnected();
    }

}


173 174
void SerialLink::writeBytes(const char* data, qint64 size)
{
175
    if(port && port->isOpen())
pixhawk's avatar
pixhawk committed
176
    {
pixhawk's avatar
pixhawk committed
177 178
        int b = port->write(data, size);

179 180 181
        if (b > 0)
        {

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

184 185 186
            // Increase write counter
            bitsSentTotal += size * 8;

187 188 189 190 191 192 193
//            int i;
//            for (i=0; i<size; i++)
//            {
//                unsigned char v =data[i];
//                qDebug("%02x ", v);
//            }
//            qDebug("\n");
194 195
        }
        else
196
        {
197 198 199
            disconnect();
            // Error occured
            emit communicationError(this->getName(), tr("Could not send data - link %1 is disconnected!").arg(this->getName()));
200
        }
pixhawk's avatar
pixhawk committed
201 202 203 204 205 206 207 208 209
    }
}

/**
 * @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
 **/
210 211
void SerialLink::readBytes()
{
pixhawk's avatar
pixhawk committed
212
    dataMutex.lock();
213
    if(port && port->isOpen())
214 215 216
    {
        const qint64 maxLength = 2048;
        char data[maxLength];
pixhawk's avatar
pixhawk committed
217
        qint64 numBytes = port->bytesAvailable();
218

219 220
        if(numBytes > 0)
        {
pixhawk's avatar
pixhawk committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            /* 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
 **/
248 249
qint64 SerialLink::bytesAvailable()
{
250 251 252 253 254 255 256 257
    if (port)
    {
        return port->bytesAvailable();
    }
    else
    {
        return 0;
    }
pixhawk's avatar
pixhawk committed
258 259 260 261 262 263 264
}

/**
 * @brief Disconnect the connection.
 *
 * @return True if connection has been disconnected, false if connection couldn't be disconnected.
 **/
265 266
bool SerialLink::disconnect()
{
267 268 269 270 271 272 273 274 275 276 277
    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
278

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

281 282
        bool closed = true;
        //port->isOpen();
pixhawk's avatar
pixhawk committed
283

284 285 286 287 288 289 290 291 292
        emit disconnected();
        emit connected(false);
        return ! closed;
    }
    else
    {
        // No port, so we're disconnected
        return true;
    }
pixhawk's avatar
pixhawk committed
293 294 295 296 297 298 299 300 301 302

}

/**
 * @brief Connect the connection.
 *
 * @return True if connection has been established, false if connection couldn't be established.
 **/
bool SerialLink::connect()
{
303
    if (!isConnected())
pixhawk's avatar
pixhawk committed
304
    {
305 306
        qDebug() << "CONNECTING LINK: " << __FILE__ << __LINE__ << "with settings" << porthandle << baudrate << dataBits << parity << stopBits;
        if (!this->isRunning())
pixhawk's avatar
pixhawk committed
307
        {
308
            this->start(LowPriority);
pixhawk's avatar
pixhawk committed
309 310
        }
    }
311
	return true;
pixhawk's avatar
pixhawk committed
312 313 314 315 316 317 318 319 320 321
}

/**
 * @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.
 **/
322 323
bool SerialLink::hardwareConnect()
{
324 325 326 327 328 329
    if(port)
    {
        port->close();
        delete port;
    }
    port = new QextSerialPort(porthandle, QextSerialPort::Polling);
pixhawk's avatar
pixhawk committed
330 331 332 333 334 335 336
    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);
337
    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
338 339 340 341

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

    bool connectionUp = isConnected();
342 343
    if(connectionUp)
    {
pixhawk's avatar
pixhawk committed
344 345 346 347
        emit connected();
        emit connected(true);
    }

348 349
    writeSettings();

pixhawk's avatar
pixhawk committed
350 351
    return connectionUp;
}
352 353


pixhawk's avatar
pixhawk committed
354 355 356 357 358
/**
 * @brief Check if connection is active.
 *
 * @return True if link is connected, false otherwise.
 **/
359 360
bool SerialLink::isConnected()
{
lm's avatar
lm committed
361 362 363 364 365 366 367 368
    if (port)
    {
        return port->isOpen();
    }
    else
    {
        return false;
    }
pixhawk's avatar
pixhawk committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
}

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

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

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


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

470 471
qint64 SerialLink::getTotalUpstream()
{
pixhawk's avatar
pixhawk committed
472 473 474 475 476
    statisticsMutex.lock();
    return bitsSentTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
}

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

482 483
qint64 SerialLink::getMaxUpstream()
{
pixhawk's avatar
pixhawk committed
484 485 486
    return 0; // TODO
}

487 488
qint64 SerialLink::getBitsSent()
{
pixhawk's avatar
pixhawk committed
489 490 491
    return bitsSentTotal;
}

492 493
qint64 SerialLink::getBitsReceived()
{
pixhawk's avatar
pixhawk committed
494 495 496
    return bitsReceivedTotal;
}

497 498
qint64 SerialLink::getTotalDownstream()
{
pixhawk's avatar
pixhawk committed
499 500 501 502 503
    statisticsMutex.lock();
    return bitsReceivedTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
    statisticsMutex.unlock();
}

504 505
qint64 SerialLink::getCurrentDownstream()
{
pixhawk's avatar
pixhawk committed
506 507 508
    return 0; // TODO
}

509 510
qint64 SerialLink::getMaxDownstream()
{
pixhawk's avatar
pixhawk committed
511 512 513
    return 0; // TODO
}

514 515
bool SerialLink::isFullDuplex()
{
pixhawk's avatar
pixhawk committed
516 517 518 519
    /* Serial connections are always half duplex */
    return false;
}

520 521
int SerialLink::getLinkQuality()
{
pixhawk's avatar
pixhawk committed
522 523 524 525
    /* This feature is not supported with this interface */
    return -1;
}

526 527
QString SerialLink::getPortName()
{
pixhawk's avatar
pixhawk committed
528 529 530
    return porthandle;
}

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

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

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

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

551 552
int SerialLink::getDataBitsType()
{
pixhawk's avatar
pixhawk committed
553 554 555
    return dataBits;
}

556 557
int SerialLink::getStopBitsType()
{
pixhawk's avatar
pixhawk committed
558 559 560
    return stopBits;
}

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
int SerialLink::getDataBits()
{
    int ret;
    switch (dataBits)
    {
    case DATA_5:
        ret = 5;
        break;
    case DATA_6:
        ret = 6;
        break;
    case DATA_7:
        ret = 7;
        break;
    case DATA_8:
        ret = 8;
        break;
    default:
        ret = 0;
        break;
    }
    return ret;
}

int SerialLink::getStopBits()
{
    int ret;
    switch (stopBits)
    {
    case STOP_1:
        ret = 1;
        break;
    case STOP_2:
        ret = 2;
        break;
    default:
        ret = 0;
        break;
    }
    return ret;
}

pixhawk's avatar
pixhawk committed
603 604 605 606 607
bool SerialLink::setPortName(QString portName)
{
    if(portName.trimmed().length() > 0)
    {
        bool reconnect = false;
608 609 610
        if (isConnected()) reconnect = true;
        disconnect();

pixhawk's avatar
pixhawk committed
611 612 613 614 615 616 617 618 619 620 621
        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
622

pixhawk's avatar
pixhawk committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636
        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
637 638 639
    if(isConnected()) reconnect = true;
    disconnect();

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

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

821 822 823
    if(reconnect) connect();
    return accepted;

pixhawk's avatar
pixhawk committed
824 825
}

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

833 834
    switch (flow)
    {
pixhawk's avatar
pixhawk committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848
    case FLOW_OFF:
        this->flow = FLOW_OFF;
        break;
    case FLOW_HARDWARE:
        this->flow = FLOW_HARDWARE;
        break;
    case FLOW_XONXOFF:
        this->flow = FLOW_XONXOFF;
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }
849

pixhawk's avatar
pixhawk committed
850 851 852 853
    if(reconnect) connect();
    return accepted;
}

854 855
bool SerialLink::setParityType(int parity)
{
pixhawk's avatar
pixhawk committed
856 857
    bool reconnect = false;
    bool accepted = true;
858 859
    if (isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
860

861 862
    switch (parity)
    {
863
    case (int)PAR_NONE:
pixhawk's avatar
pixhawk committed
864 865
        this->parity = PAR_NONE;
        break;
866
    case (int)PAR_ODD:
pixhawk's avatar
pixhawk committed
867 868
        this->parity = PAR_ODD;
        break;
869
    case (int)PAR_EVEN:
pixhawk's avatar
pixhawk committed
870 871
        this->parity = PAR_EVEN;
        break;
872
    case (int)PAR_MARK:
pixhawk's avatar
pixhawk committed
873 874
        this->parity = PAR_MARK;
        break;
875
    case (int)PAR_SPACE:
pixhawk's avatar
pixhawk committed
876 877 878 879 880 881 882 883
        this->parity = PAR_SPACE;
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

884
    if (reconnect) connect();
pixhawk's avatar
pixhawk committed
885 886 887
    return accepted;
}

888

889
bool SerialLink::setDataBits(int dataBits)
890
{
891 892
    bool reconnect = false;
    if (isConnected()) reconnect = true;
pixhawk's avatar
pixhawk committed
893
    bool accepted = true;
894
    disconnect();
pixhawk's avatar
pixhawk committed
895

896 897
    switch (dataBits)
    {
pixhawk's avatar
pixhawk committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
    case 5:
        this->dataBits = DATA_5;
        break;
    case 6:
        this->dataBits = DATA_6;
        break;
    case 7:
        this->dataBits = DATA_7;
        break;
    case 8:
        this->dataBits = DATA_8;
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

916
    if(reconnect) connect();
pixhawk's avatar
pixhawk committed
917 918 919 920

    return accepted;
}

921
bool SerialLink::setStopBits(int stopBits)
922
{
pixhawk's avatar
pixhawk committed
923 924
    bool reconnect = false;
    bool accepted = true;
925 926
    if(isConnected()) reconnect = true;
    disconnect();
pixhawk's avatar
pixhawk committed
927

928 929
    switch (stopBits)
    {
pixhawk's avatar
pixhawk committed
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
    case 1:
        this->stopBits = STOP_1;
        break;
    case 2:
        this->stopBits = STOP_2;
        break;
    default:
        // If none of the above cases matches, there must be an error
        accepted = false;
        break;
    }

    if(reconnect) connect();
    return accepted;
}
945 946 947 948 949 950

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

951 952
    if (isConnected()) reconnect = true;
    disconnect();
953 954 955

    if (dataBits >= (int)DATA_5 && dataBits <= (int)DATA_8)
    {
956
        this->dataBits = (DataBitsType) dataBits;
957

958
        if(reconnect) connect();
959 960 961 962 963 964 965 966 967 968
        accepted = true;
    }

    return accepted;
}

bool SerialLink::setStopBitsType(int stopBits)
{
    bool reconnect = false;
    bool accepted = false;
969 970
    if(isConnected()) reconnect = true;
    disconnect();
971 972 973 974 975 976 977 978 979 980 981 982

    if (stopBits >= (int)STOP_1 && dataBits <= (int)STOP_2)
    {
        StopBitsType newBits = (StopBitsType) stopBits;

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

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