commdcbhelper.cpp 22 KB
Newer Older
James Goppert's avatar
James Goppert committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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 206 207 208 209 210 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
/*
 * Unofficial Qt Serial Port Library
 *
 * Copyright (c) 2010 Inbiza Systems Inc. All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * author labs@inbiza.com
 */

#include <QDebug>
#include "commdcbhelper.h"

namespace TNX {

/*!
  Constructs a CommDCBHelper object with the given \a file handle.
*/
CommDCBHelper::CommDCBHelper(HANDLE fileHandle)
  : fileHandle_(fileHandle), originalAttrs_(NULL), currentAttrs_(NULL)
{
  Q_ASSERT(fileHandle_ > 0);
  
  originalAttrs_ = new DCB();
  currentAttrs_ = new DCB();

  // save the current serial port attributes
  // see restoreDCB()

  saveDCB();
    
  // clone the original attributes

  *currentAttrs_ = *originalAttrs_;

  // initialize port attributes for serial port communication

  initDCB();
}


CommDCBHelper::~CommDCBHelper()
{
  // It is good practice to reset a serial port back to the state in
  // which you found it. This is why we saved the original DCB struct
  
  restoreDCB();

  delete originalAttrs_;
  delete currentAttrs_;
}

/*!
   Sets the DCB structure.
 */
bool CommDCBHelper::applyChanges(ChangeApplyTypes apptype)
{
  if ( apptype == PortAttrOnlyAppTy || apptype == AllAppTy ) {
    if ( !SetCommState(fileHandle_, currentAttrs_) ) {
      qDebug() << QString("CommDCBHelper::applyChanges(file: %1, PortAttributes) failed: %2(Err #%3)")
                       .arg((quintptr)fileHandle_)
                       .arg(errorText(GetLastError()))
                       .arg(GetLastError());
      return false;
    }
  }

  // Communication Timeouts

  if ( apptype == CommTimeoutsOnlyAppTy || apptype == AllAppTy ) {
    if ( !SetCommTimeouts(fileHandle_, &commTimeouts_) ) {
      qDebug() << QString("CommDCBHelper::applyChanges(file: %1, CommTimeouts) failed: %2(Err #%3)")
                      .arg((quintptr)fileHandle_)
                      .arg(errorText(GetLastError()))
                      .arg(GetLastError());
      return false;
    }
  }

  return true;
}

/*!

 */
bool CommDCBHelper::setCtrSignal(ControlSignals csig, bool value)
{
  DWORD sig;

  switch ( csig ) {
    case CSIGNAL_RTS:
      sig = value ? SETRTS : CLRRTS;
      break;

    case CSIGNAL_DTR:
      sig = value ? SETDTR : CLRDTR;
      break;

    default:
      qDebug() << QString("CommDCBHelper::setCtrSignal(file: %1, csig: %2) failed." \
                            "Given signal is read only.")
                 .arg((quintptr)fileHandle_)
                 .arg(csig);
      return false;
  }

  if ( !EscapeCommFunction(fileHandle_, sig) ) {
    qDebug() <<  QString("CommDCBHelper::setCtrSignal(file: %1, csig: %2) failed" \
                         "when fetching control signal values : %3(Err #%4)")
                 .arg((quintptr)fileHandle_)
                 .arg(csig)
                 .arg(errorText(GetLastError()))
                 .arg(GetLastError());
    return false;
  }

  return true;
}

/*!

 */
QSerialPort::CommSignalValues CommDCBHelper::ctrSignal(ControlSignals csig) const
{
  DWORD status = 0;

  if ( !GetCommModemStatus(fileHandle_, &status) ) {
    qDebug() << QString("CommDCBHelper::ctrSignal(file: %1, csig: %2) failed" \
                        "when fetching control signal values : %3(Err #%4)")
                 .arg((quintptr)fileHandle_)
                 .arg(csig)
                 .arg(errorText(GetLastError()))
                 .arg(GetLastError());
    return QSerialPort::Signal_Unknown;
  }

  DWORD sig;
  switch ( csig ) {
    case CSIGNAL_CTS:
      sig = MS_CTS_ON;
      break;

    case CSIGNAL_DSR:
      sig = MS_DSR_ON;
      break;

    case CSIGNAL_DCD:
      sig = MS_RLSD_ON;
      break;

    case CSIGNAL_RNG:
      sig = MS_RING_ON;
      break;

    default:
      qDebug() << QString("CommDCBHelper::ctrSignal(file: %1, csig: %2) failed." \
                            "Invalid signal.")
                 .arg((quintptr)fileHandle_)
                 .arg(csig);
      return QSerialPort::Signal_Unknown;
  }

  return ((status & sig) ? QSerialPort::Signal_On : QSerialPort::Signal_Off);
}

/*!

 */
void CommDCBHelper::initDCB()
{   
  currentAttrs_->DCBlength          = sizeof(DCB);

  currentAttrs_->EvtChar            =   '\0';
  currentAttrs_->EofChar            =   '\0';
  currentAttrs_->ErrorChar          =   '\0';
  currentAttrs_->XonChar            =   '\0';
  currentAttrs_->XoffChar           =   '\0';
  currentAttrs_->fDtrControl        =   DTR_CONTROL_DISABLE;
  currentAttrs_->fRtsControl        =   RTS_CONTROL_DISABLE;
  currentAttrs_->fOutxCtsFlow       =   0;
  currentAttrs_->fOutxDsrFlow       =   0;
  currentAttrs_->fDsrSensitivity    =   0;
  currentAttrs_->fOutX              =   0;
  currentAttrs_->fInX               =   0;
  currentAttrs_->fTXContinueOnXoff  =   0;
  currentAttrs_->fErrorChar         =   0;
  currentAttrs_->XonLim             =   0;
  currentAttrs_->XoffLim            =   0;
  currentAttrs_->fBinary            =   1;
  currentAttrs_->fNull              =   0;

  // If a communications port has been set up with a TRUE value for the fAbortOnError member 
  // of the setup DCB structure, the communications software will terminate all read and write 
  // operations on the communications port when a communications error occurs. No new read or write 
  // operations will be accepted until the application acknowledges the communications error by 
  // calling the ClearCommError function.

  currentAttrs_->fAbortOnError      =   0;
  currentAttrs_->BaudRate           =   CBR_9600;
  currentAttrs_->fParity            =   0;
  currentAttrs_->Parity             =   0;
  currentAttrs_->StopBits           =   0;
  currentAttrs_->ByteSize           =   8;
  
  // ensure the new attributes take effect immediately

  applyChanges(AllAppTy);
}

/*!

*/
void CommDCBHelper::saveDCB()
{
  // get the current serial port attributes

  if ( !GetCommState(fileHandle_, originalAttrs_) ) {
    qDebug() << QString("CommDCBHelper::saveDCB(file: %1) failed when" \
                          " getting original port attributes: %2(Err #%3)")
                 .arg((quintptr)fileHandle_)
                 .arg(errorText(GetLastError()))
                 .arg(GetLastError());
  }
}

/*!

*/
bool CommDCBHelper::getCommTimeouts()
{
  // get the current communication timeouts

  if ( !GetCommTimeouts(fileHandle_, &commTimeouts_) ) {
    qDebug() << QString("CommDCBHelper::getCommTimeouts(file: %1) failed when" \
                          " getting communication timeout values: %2(Err #%3)")
                 .arg((quintptr)fileHandle_)
                 .arg(errorText(GetLastError()))
                 .arg(GetLastError());
    return false;
  }
  return true;
}

/*!

*/
void CommDCBHelper::restoreDCB()
{
  if ( !originalAttrs_ || !SetCommState(fileHandle_, originalAttrs_) ) {
    qDebug() << QString("CommDCBHelper::restoreDCB(file: %1) failed when resetting " \
                        "serial port attributes: %2(Err #%3)")
                  .arg((quintptr)fileHandle_)
                  .arg(errorText(GetLastError()))
                  .arg(GetLastError());
  }
}

/*!

*/
void CommDCBHelper::setBaudRate(QPortSettings::BaudRate baudRate)
{
  DWORD baud = CBR_9600;

  switch ( baudRate ) {
    case QPortSettings::BAUDR_110:
      baud = CBR_110;
      break;      
    case QPortSettings::BAUDR_300:
      baud = CBR_300;
      break;      
    case QPortSettings::BAUDR_600:
      baud = CBR_600;
      break;
    case QPortSettings::BAUDR_1200:
      baud = CBR_1200;
      break;
    case QPortSettings::BAUDR_2400:
      baud = CBR_2400;
      break;
    case QPortSettings::BAUDR_4800:
      baud = CBR_4800;
      break;
    case QPortSettings::BAUDR_9600:
      baud = CBR_9600;
      break;
    case QPortSettings::BAUDR_14400:
      baud = CBR_14400;
      break;
    case QPortSettings::BAUDR_19200:
      baud = CBR_19200;
      break;
    case QPortSettings::BAUDR_38400:
      baud = CBR_38400;
      break;
    case QPortSettings::BAUDR_56000:
      baud = CBR_56000;
      break;
    case QPortSettings::BAUDR_57600:
      baud = CBR_57600;
      break;
    case QPortSettings::BAUDR_115200:
      baud = CBR_115200;
      break;
    case QPortSettings::BAUDR_128000:
      baud = CBR_128000;
      break;
319 320 321
    case QPortSettings::BAUDR_230400:
      baud = 230400;
      break;
James Goppert's avatar
James Goppert committed
322 323 324
    case QPortSettings::BAUDR_256000:
      baud = CBR_256000;
      break;
325 326 327 328 329 330
    case QPortSettings::BAUDR_460800:
      baud = 460800;
      break;
    case QPortSettings::BAUDR_921600:
      baud = 921600;
      break;
James Goppert's avatar
James Goppert committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    default:
      qWarning() << "CommDCBHelper::setBaudRate(" << baudRate << "): " \
                    "Unsupported baud rate";
  }
  currentAttrs_->BaudRate = baud;
}

/*!

*/
QPortSettings::BaudRate CommDCBHelper::baudRate() const
{
  DWORD baud;
  DCB dcb;

  // although we store the last value of the baud rate attribute in currentAttrs_ structure, 
  // it is better practice to request the actual value from the OS.

  if ( !GetCommState(fileHandle_, &dcb) ) {
    qDebug() << QString("CommDCBHelper::baudRate(file: %1) failed when" \
                          " getting baud rate: %2(Err #%3)")
                 .arg((quintptr)fileHandle_)
                 .arg(errorText(GetLastError()))
                 .arg(GetLastError());

    baud = currentAttrs_->BaudRate;
  }
  else {
359
      baud = dcb.BaudRate;
James Goppert's avatar
James Goppert committed
360 361 362 363 364
  }

  Q_ASSERT(currentAttrs_->BaudRate == baud);

  switch ( baud ) {
365
  case CBR_110:
James Goppert's avatar
James Goppert committed
366
      return QPortSettings::BAUDR_110;
367
  case CBR_300:
James Goppert's avatar
James Goppert committed
368
      return QPortSettings::BAUDR_300;
369
  case CBR_600:
James Goppert's avatar
James Goppert committed
370
      return QPortSettings::BAUDR_600;
371
  case CBR_1200:
James Goppert's avatar
James Goppert committed
372
      return QPortSettings::BAUDR_1200;
373
  case CBR_2400:
James Goppert's avatar
James Goppert committed
374
      return QPortSettings::BAUDR_2400;
375
  case CBR_4800:
James Goppert's avatar
James Goppert committed
376
      return QPortSettings::BAUDR_4800;
377
  case CBR_9600:
James Goppert's avatar
James Goppert committed
378
      return QPortSettings::BAUDR_9600;
379
  case CBR_14400:
James Goppert's avatar
James Goppert committed
380
      return QPortSettings::BAUDR_14400;
381
  case CBR_19200:
James Goppert's avatar
James Goppert committed
382
      return QPortSettings::BAUDR_19200;
383
  case CBR_38400:
James Goppert's avatar
James Goppert committed
384
      return QPortSettings::BAUDR_38400;
385
  case CBR_56000:
James Goppert's avatar
James Goppert committed
386
      return QPortSettings::BAUDR_56000;
387
  case CBR_57600:
James Goppert's avatar
James Goppert committed
388
      return QPortSettings::BAUDR_57600;
389
  case CBR_115200:
James Goppert's avatar
James Goppert committed
390
      return QPortSettings::BAUDR_115200;
391
  case CBR_128000:
James Goppert's avatar
James Goppert committed
392
      return QPortSettings::BAUDR_128000;
393 394 395
  case 230400:
      return QPortSettings::BAUDR_230400;
  case CBR_256000:
James Goppert's avatar
James Goppert committed
396
      return QPortSettings::BAUDR_256000;
397 398 399 400 401 402
  case 460800:
      return QPortSettings::BAUDR_460800;
  case 921600:
      return QPortSettings::BAUDR_921600;
      break;
  default:
James Goppert's avatar
James Goppert committed
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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 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 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 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
      qWarning() << "CommDCBHelper::baudRate(): Unknown baud rate";
  }

  return QPortSettings::BAUDR_UNKNOWN;
}

/*!

*/
QPortSettings::DataBits CommDCBHelper::dataBits() const
{
  DCB dcb;
  BYTE dataBits;

  // get the current serial port attributes

  if ( !GetCommState(fileHandle_, &dcb) ) {
    qDebug() << QString("CommDCBHelper::dataBits(file: %1) failed when" \
                        " getting original port attributes: %2(Err #%3)")
                 .arg((quintptr)fileHandle_)
                 .arg(errorText(GetLastError()))
                 .arg(GetLastError());

    dataBits = currentAttrs_->ByteSize;
  }
  else {
    dataBits = dcb.ByteSize;
  }

  Q_ASSERT(currentAttrs_->ByteSize == dataBits);

  if ( dataBits == 5 )
    return QPortSettings::DB_5;
  else if ( dataBits == 6 )
    return QPortSettings::DB_6;
  else if ( dataBits == 7 )
    return QPortSettings::DB_7;
  else if ( dataBits == 8 )
    return QPortSettings::DB_8;
  else
    return QPortSettings::DB_UNKNOWN;
}

/*!

*/
void CommDCBHelper::setDataBits(QPortSettings::DataBits dataBits)
{
  switch( dataBits ) {
    /*5 data bits*/
    case QPortSettings::DB_5:
      currentAttrs_->ByteSize = 5;
      break;

    /*6 data bits*/
    case QPortSettings::DB_6:
      currentAttrs_->ByteSize = 6;
      break;

    /*7 data bits*/
    case QPortSettings::DB_7:
      currentAttrs_->ByteSize = 7;
      break;

    /*8 data bits*/
    case QPortSettings::DB_8:
      currentAttrs_->ByteSize = 8;
      break;

    default:
      currentAttrs_->ByteSize = 8;
      qWarning() << "CommDCBHelper::setDataBits(enum[" << dataBits << "]): Unsupported data bits";
  }
}

/*!

 */
QPortSettings::Parity CommDCBHelper::parity() const
{
  DCB dcb;
  BYTE parity;
  DWORD fParity;

  // get the current serial port attributes

  if ( !GetCommState(fileHandle_, &dcb) ) {
    qDebug() << QString("CommDCBHelper::parity(file: %1) failed when" \
                        " getting original port attributes: %2(Err #%3)")
                 .arg((quintptr)fileHandle_)
                 .arg(errorText(GetLastError()))
                 .arg(GetLastError());

    fParity = currentAttrs_->fParity;
    parity = currentAttrs_->Parity;
  }
  else {
    fParity = dcb.fParity;
    parity = dcb.Parity;
  }

  // For some reason windows keep changing fParity value even Parity field is set
  // something other than 0 (no parity)

  //Q_ASSERT(currentAttrs_->fParity == fParity && currentAttrs_->Parity == parity);

  //if ( fParity != 1 ) {
  //  return QPortSettings::PAR_NONE;
  //}
  //else {
    if ( parity == 0 )
      return QPortSettings::PAR_NONE;
    else if ( parity == 1 )
      return QPortSettings::PAR_ODD;
    else if ( parity == 2 )
      return QPortSettings::PAR_EVEN;
    else if ( parity == 3 )
      return QPortSettings::PAR_MARK;
    else if ( parity == 4 )
      return QPortSettings::PAR_SPACE;
    else
      return QPortSettings::PAR_UNKNOWN;
  //}
}

/*!

*/
void CommDCBHelper::setParity(QPortSettings::Parity parity)
{
  switch ( parity ) {
    /*no parity*/
    case QPortSettings::PAR_NONE:
      currentAttrs_->fParity = 0;
      currentAttrs_->Parity = 0;
      break;

    /*odd parity*/
    case QPortSettings::PAR_ODD:
      currentAttrs_->fParity = 1;
      currentAttrs_->Parity = 1;
      break;

    /*even parity*/
    case QPortSettings::PAR_EVEN:
      currentAttrs_->fParity = 1;
      currentAttrs_->Parity = 2;
      break;

    /*mark parity*/
    case QPortSettings::PAR_MARK:
      currentAttrs_->fParity = 1;
      currentAttrs_->Parity = 3;
      break;

    /*space parity*/
    case QPortSettings::PAR_SPACE:
      currentAttrs_->fParity = 1;
      currentAttrs_->Parity = 4;
      break;
      
    default:
      currentAttrs_->fParity = 0;
      currentAttrs_->Parity = 0;
      qWarning() << "CommDCBHelper::setParity(enum[" << parity << "]): Unsupported parity";
  }
}

/*!

 */
QPortSettings::StopBits CommDCBHelper::stopBits() const
{
  DCB dcb;
  BYTE stopBits;

  // get the current serial port attributes

  if ( !GetCommState(fileHandle_, &dcb) ) {
    qDebug() << QString("CommDCBHelper::stopBits(file: %1) failed when" \
                        " getting original port attributes: %2(Err #%3)")
                 .arg((quintptr)fileHandle_)
                 .arg(errorText(GetLastError()))
                 .arg(GetLastError());

    stopBits = currentAttrs_->StopBits;
  }
  else {
    stopBits = dcb.StopBits;
  }

  Q_ASSERT(currentAttrs_->StopBits == stopBits);

  if ( stopBits == 0 )
    return QPortSettings::STOP_1;
  else if ( stopBits == 1 )
    return QPortSettings::STOP_1_5;
  else if ( stopBits == 2 )
    return QPortSettings::STOP_2;
  else
    return QPortSettings::STOP_UNKNOWN;
}

/*!

*/
void CommDCBHelper::setStopBits(QPortSettings::StopBits stopBits)
{
  switch( stopBits ) {
    /*one stop bit*/
    case QPortSettings::STOP_1:
      currentAttrs_->StopBits = 0;
      break;

    /*one and half bit*/
    case QPortSettings::STOP_1_5:
      currentAttrs_->StopBits = 1;
      break;

    /*two stop bits*/
    case QPortSettings::STOP_2:
      currentAttrs_->StopBits = 2;
      break;

    default:
      currentAttrs_->StopBits = 0;
      qWarning() << "CommDCBHelper::setStopBits(enum[" << stopBits << "]): Unsupported stop bits";
  }
}

/*!
  @return FLOW_UNKNOWN in error case
 */
QPortSettings::FlowControl CommDCBHelper::flowControl() const
{
  DCB dcb;

  // get the current serial port attributes.

  if ( !GetCommState(fileHandle_, &dcb) ) {
    qWarning() << QString("CommDCBHelper::flowControl(file: %1) failed when" \
                          " getting original port attributes: %2(Err #%3)")
                 .arg((quintptr)fileHandle_)
                 .arg(errorText(GetLastError()))
                 .arg(GetLastError());

    return QPortSettings::FLOW_UNKNOWN;
  }
  
  if ( !dcb.fOutxCtsFlow && dcb.fRtsControl == RTS_CONTROL_DISABLE ) {
    if ( dcb.fInX && dcb.fOutX )
      return QPortSettings::FLOW_XONXOFF;
    else if ( !dcb.fInX && !dcb.fOutX )
      return QPortSettings::FLOW_OFF;
    else
      return QPortSettings::FLOW_UNKNOWN;
  }
  else if ( dcb.fOutxCtsFlow && dcb.fRtsControl == RTS_CONTROL_HANDSHAKE &&
            !dcb.fInX && !dcb.fOutX ) {
    return QPortSettings::FLOW_HARDWARE;
  }
  else {
    return QPortSettings::FLOW_UNKNOWN;
  }
}

/*!

*/
void CommDCBHelper::setFlowControl(QPortSettings::FlowControl flow)
{
  switch( flow ) {
    /*no flow control*/
    case QPortSettings::FLOW_OFF:
      currentAttrs_->fOutxCtsFlow = false;
      currentAttrs_->fRtsControl = RTS_CONTROL_DISABLE;
      currentAttrs_->fInX = false;
      currentAttrs_->fOutX = false;
      break;

    /*software (XON/XOFF) flow control*/
    case QPortSettings::FLOW_XONXOFF:
      currentAttrs_->fOutxCtsFlow = false;
      currentAttrs_->fRtsControl = RTS_CONTROL_DISABLE;
      currentAttrs_->fInX = true;
      currentAttrs_->fOutX = true;
      break;

    case QPortSettings::FLOW_HARDWARE:
      currentAttrs_->fOutxCtsFlow = true;
      currentAttrs_->fRtsControl = RTS_CONTROL_HANDSHAKE;
      currentAttrs_->fInX = false;
      currentAttrs_->fOutX = false;
      break;

    default:
      currentAttrs_->fOutxCtsFlow = false;
      currentAttrs_->fRtsControl = RTS_CONTROL_DISABLE;
      currentAttrs_->fInX = false;
      currentAttrs_->fOutX = false;
      qWarning("CommDCBHelper::setFlowControl(%i): Unsupported FlowType", flow);
  }
}

//
//  ReadIntervalTimeout
//    Maximum time allowed to elapse between the arrival of two characters on the communications
//    line, in milliseconds. During a ReadFile operation, the time period begins when the first
//    character is received. If the interval between the arrival of any two characters
//    exceeds this amount, the ReadFile operation is completed and any buffered data is returned.
//    A value of zero indicates that interval time-outs are not used. A value of MAXDWORD,
//    combined with zero values for both the ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier
//    parameters, specifies that the read operation is to return immediately with the characters that
//    have already been received, even if no characters have been received.
//
//  ReadTotalTimeoutMultiplier
//    Multiplier used to calculate the total time-out period for read operations,
//    in milliseconds. For each read operation, this value is multiplied by the requested
//    number of bytes to be read.
//
//  ReadTotalTimeoutConstant
//    Constant used to calculate the total time-out period for read operations, in milliseconds.
//    For each read operation, this value is added to the product of the ReadTotalTimeoutMultiplier
//    parameter and the requested number of bytes. A value of zero for both the ReadTotalTimeoutMultiplier
//    and ReadTotalTimeoutConstant members indicates that total time-outs are not used for read operations.
//
//  WriteTotalTimeoutMultiplier
//    Multiplier used to calculate the total time-out period for write operations, in milliseconds.
//    For each write operation, this value is multiplied by the number of bytes to be written.
//
//  WriteTotalTimeoutConstant
//    Constant used to calculate the total time-out period for write operations, in milliseconds.
//    For each write operation, this value is added to the product of the WriteTotalTimeoutMultiplier
//    member and the number of bytes to be written. A value of zero for both the WriteTotalTimeoutMultiplier
//    and WriteTotalTimeoutConstant parameters indicates that total time-outs are not used for write operations.
//

bool CommDCBHelper::commTimeouts(CommTimeouts &commtimeouts)
{
  // get the current serial port attributes

  if ( !getCommTimeouts() )
    return false;

  commtimeouts.Win32ReadIntervalTimeout = commTimeouts_.ReadIntervalTimeout;
  commtimeouts.Win32ReadTotalTimeoutConstant = commTimeouts_.ReadTotalTimeoutConstant;
  commtimeouts.Win32ReadTotalTimeoutMultiplier = commTimeouts_.ReadTotalTimeoutMultiplier;
  commtimeouts.Win32WriteTotalTimeoutConstant = commTimeouts_.WriteTotalTimeoutConstant;
  commtimeouts.Win32WriteTotalTimeoutMultiplier = commTimeouts_.WriteTotalTimeoutMultiplier;

  return true;
}

void CommDCBHelper::setCommTimeouts(const CommTimeouts commtimeouts)
{
  commTimeouts_.ReadIntervalTimeout = commtimeouts.Win32ReadIntervalTimeout;
  commTimeouts_.ReadTotalTimeoutConstant = commtimeouts.Win32ReadTotalTimeoutConstant;
  commTimeouts_.ReadTotalTimeoutMultiplier = commtimeouts.Win32ReadTotalTimeoutMultiplier;
  commTimeouts_.WriteTotalTimeoutConstant = commtimeouts.Win32WriteTotalTimeoutConstant;
  commTimeouts_.WriteTotalTimeoutMultiplier = commtimeouts.Win32WriteTotalTimeoutMultiplier;
}

/*!

*/
QString CommDCBHelper::errorText(DWORD err)
{
  unsigned short *pMsg = 0;

  // if character set is Ascii, pMsg should be defined as char *string = 0
  // and QString::fromLocal8Bit(pMsg) should be used instead.

  if ( FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
                     0,
                     err,
                     MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
                     (LPTSTR)&pMsg,
                     0,
                     NULL) == 0 ) {
    return "There is no message associated with that error code.";
  }
  else {
    QString errMsg = QString::fromUtf16(pMsg);
    LocalFree(pMsg); // free memory allocated by the OS

    return errMsg;
  }
}

} // namespace