termioshelper.cpp 18.7 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 319 320 321 322 323 324 325 326 327 328 329 330 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 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 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
/*
 * 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/>
 *
 *
 * @file termioshelper.cpp
 */

#include <QDebug>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include "termioshelper.h"

namespace TNX {

/*!
  Constructs a TermiosHelper object with the given \a file descriptor.
*/

TermiosHelper::TermiosHelper(int fileDescriptor)
  : fileDescriptor_(fileDescriptor), originalAttrs_(NULL), currentAttrs_(NULL)
{
  Q_ASSERT(fileDescriptor_ > 0);

  originalAttrs_ = new termios();
  currentAttrs_ = new termios();

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

  saveTermios();

  // clone the original attributes

  *currentAttrs_ = *originalAttrs_;

  // initialize port attributes for serial port communication

  initTermios();
}


TermiosHelper::~TermiosHelper()
{
  // 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 termios struct
  // The constant TCSANOW (defined in termios.h) indicates that
  // the change should take effect immediately.

  restoreTermios();

  delete originalAttrs_;
  delete currentAttrs_;
}

/*!
   Sets the termios structure.
 */

bool TermiosHelper::applyChanges(ChangeApplyTypes /*apptype*/)
{
  // there is only termios structure to be applied for posix compatible platforms

  if ( tcsetattr(fileDescriptor_, TCSANOW, currentAttrs_) == -1 ) {
    qCritical() << QString("TermiosHelper::applyChanges(file: %1, applyType: %2) failed " \
                           "when setting new attributes: %3(%4)")
                 .arg(fileDescriptor_)
                 .arg(TCSANOW)
                 .arg(strerror(errno))
                 .arg(errno);
    return false;
  }
  return true;
}

/*!

 */

bool TermiosHelper::setCtrSignal(ControlSignals csig, bool value)
{
  int status;

  if ( ioctl(fileDescriptor_, TIOCMGET, &status) == -1 ) {
    qCritical() <<  QString("TermiosHelper::setCtrSignal(file: %1, csig: %2) failed" \
                            "when fetching control signal values : %3(%4)")
                 .arg(fileDescriptor_)
                 .arg(csig)
                 .arg(strerror(errno))
                 .arg(errno);
    return false;
  }

  if ( value )
    status |= (int)csig;
  else
    status &= ~((int)csig);

  if ( ioctl(fileDescriptor_, TIOCMSET, &status) == -1 ) {
    qCritical() <<  QString("TermiosHelper::setCtrSignal(file: %1, csig: %2) failed" \
                            "when setting control signal values : %3(%4)")
                 .arg(fileDescriptor_)
                 .arg(csig)
                 .arg(strerror(errno))
                 .arg(errno);
    return false;
  }
  return true;
}

/*!

 */

QSerialPort::CommSignalValues TermiosHelper::ctrSignal(ControlSignals csig) const
{
  int status;

  if ( ioctl(fileDescriptor_, TIOCMGET, &status) == -1 ) {
    qCritical() <<  QString("TermiosHelper::ctrSignal(file: %1, csig: %2) failed" \
                            "when fetching control signal values : %3(%4)")
                 .arg(fileDescriptor_)
                 .arg(csig)
                 .arg(strerror(errno))
                 .arg(errno);
    return QSerialPort::Signal_Unknown;
  }

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

/*!

 */

void TermiosHelper::initTermios()
{
  // Set raw input (non-canonical) mode, with reads blocking until either a single character
  // has been received or a one second timeout expires.
  // See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios") for details.

  cfmakeraw(currentAttrs_);

  currentAttrs_->c_cflag |= (CLOCAL | CREAD);

  // set communication timeouts

  currentAttrs_->c_cc[VMIN] = kDefaultNumOfBytes;

  // converting ms to one-tenths-of-second (sec * 0.1)
  currentAttrs_->c_cc[VTIME] = (kDefaultReadTimeout / 100);

  // ensure the new attributes take effect immediately

  applyChanges();
}

/*!

*/

void TermiosHelper::saveTermios()
{
  // get the current serial port attributes

  if ( tcgetattr(fileDescriptor_, originalAttrs_) == -1 ) {
    qWarning() << QString("TermiosHelper::saveTermios(file: %1) failed when" \
                          " getting original port attributes: %2(%3)")
                 .arg(fileDescriptor_)
                 .arg(strerror(errno))
                 .arg(errno);
  }
}

/*!

*/

void TermiosHelper::restoreTermios()
{
  if ( !originalAttrs_ || tcsetattr(fileDescriptor_, TCSANOW, originalAttrs_) == -1 ) {
    qWarning() << QString("TermiosHelper::restoreTermios(file: %1) failed when resetting " \
                          "serial port attributes: %2(%3)")
                  .arg(fileDescriptor_)
                  .arg(strerror(errno))
                  .arg(errno);
  }

  // Block until all written output has been sent from the device.
  // Note that this call is simply passed on to the serial device driver.
  // See tcsendbreak(3) ("man 3 tcsendbreak") for details.

  if ( tcdrain(fileDescriptor_) == -1 ) {
    qWarning() << QString("TermiosHelper::restoreTermios(file: %1) failed while waiting for drain: %2(%3)")
                  .arg(fileDescriptor_)
                  .arg(strerror(errno))
                  .arg(errno);
  }
}

/*!

*/

void TermiosHelper::setBaudRate(QPortSettings::BaudRate baudRate)
{
  speed_t baud = B9600;

  switch ( baudRate ) {
    case QPortSettings::BAUDR_50:
      baud = B50;
      break;
    case QPortSettings::BAUDR_75:
      baud = B75;
      break;
    case QPortSettings::BAUDR_110:
      baud = B110;
      break;
    case QPortSettings::BAUDR_134:
      baud = B134;
      break;
    case QPortSettings::BAUDR_150:
      baud = B150;
      break;
    case QPortSettings::BAUDR_200:
      baud = B200;
      break;
    case QPortSettings::BAUDR_300:
      baud = B300;
      break;
    case QPortSettings::BAUDR_600:
      baud = B600;
      break;
    case QPortSettings::BAUDR_1200:
      baud = B1200;
      break;
    case QPortSettings::BAUDR_1800:
      baud = B1800;
      break;
    case QPortSettings::BAUDR_2400:
      baud = B2400;
      break;
    case QPortSettings::BAUDR_4800:
      baud = B4800;
      break;
    case QPortSettings::BAUDR_9600:
      baud = B9600;
      break;
    case QPortSettings::BAUDR_19200:
      baud = B19200;
      break;
    case QPortSettings::BAUDR_38400:
      baud = B38400;
      break;
    case QPortSettings::BAUDR_57600:
      baud = B57600;
      break;
    //case QPortSettings::BAUDR_76800:
    //  baud = B76800;
    //  break;
    case QPortSettings::BAUDR_115200:
      baud = B115200;
      break;
#if defined(Q_OS_LINUX)
  case QPortSettings::BAUDR_230400:
    baud = B230400;
    break;
  case QPortSettings::BAUDR_460800:
    baud = B460800;
    break;
  case QPortSettings::BAUDR_500000:
    baud = B500000;
    break;
  case QPortSettings::BAUDR_576000:
    baud = B576000;
    break;
  case QPortSettings::BAUDR_921600:
    baud = B921600;
    break;
#endif
  default:
    qWarning() << "TermiosHelper::setBaudRate(" << baudRate << "): " \
                  "Unsupported baud rate";
  }

  if ( cfsetspeed(currentAttrs_, baud) == -1 ) {
    qCritical() <<  QString("TermiosHelper::setBaudRate(file: %1) failed: %2(%3)")
                 .arg(fileDescriptor_)
                 .arg(strerror(errno))
                 .arg(errno);
  }
}

/*!

*/

QPortSettings::BaudRate TermiosHelper::baudRate() const
{
  speed_t ibaud = cfgetispeed(currentAttrs_);
  speed_t obaud = cfgetospeed(currentAttrs_);

  (obaud == ibaud);

  Q_ASSERT(ibaud == obaud);

  switch ( ibaud ) {
    case B50:
      return QPortSettings::BAUDR_50;
    case B75:
      return QPortSettings::BAUDR_75;
    case B110:
      return QPortSettings::BAUDR_110;
    case B134:
      return QPortSettings::BAUDR_134;
    case B150:
      return QPortSettings::BAUDR_150;
    case B200:
      return QPortSettings::BAUDR_200;
    case B300:
      return QPortSettings::BAUDR_300;
    case B600:
      return QPortSettings::BAUDR_600;
    case B1200:
      return QPortSettings::BAUDR_1200;
    case B1800:
      return QPortSettings::BAUDR_1800;
    case B2400:
      return QPortSettings::BAUDR_2400;
    case B4800:
      return QPortSettings::BAUDR_4800;
    case B9600:
      return QPortSettings::BAUDR_9600;
    case B19200:
      return QPortSettings::BAUDR_19200;
    case B38400:
      return QPortSettings::BAUDR_38400;
    case B57600:
      return QPortSettings::BAUDR_57600;
    //case B76800:
    //  return QPortSettings::BAUDR_76800;
    case B115200:
      return QPortSettings::BAUDR_115200;
#if defined(Q_OS_LINUX)
    case B230400:
      return QPortSettings::BAUDR_230400;
    // the baud rates listed below are not supported by all UARTs
    // and Linux distros
    case B460800:
      return QPortSettings::BAUDR_460800;
    case B500000:
      return QPortSettings::BAUDR_500000;
    case B576000:
      return QPortSettings::BAUDR_576000;
    case B921600:
      return QPortSettings::BAUDR_921600;
#endif
  default:
    qWarning() << "TermiosHelper::baudRate(): Unknown baud rate";
  }

  return QPortSettings::BAUDR_UNKNOWN;
}

/*!

*/

QPortSettings::DataBits TermiosHelper::dataBits() const
{
  struct termios options;

  // get the current serial port attributes

  if ( tcgetattr(fileDescriptor_, &options) == -1 ) {
    qWarning() << QString("TermiosHelper::dataBits(file: %1) failed when" \
                          " getting original port attributes: %2(%3)")
                 .arg(fileDescriptor_)
                 .arg(strerror(errno))
                 .arg(errno);
    return QPortSettings::DB_UNKNOWN;
  }

  if ( (options.c_cflag & CSIZE) == CS5 )
    return QPortSettings::DB_5;
  else if ( (options.c_cflag & CSIZE) == CS6 )
    return QPortSettings::DB_6;
  else if ( (options.c_cflag & CSIZE) == CS7 )
    return QPortSettings::DB_7;
  else if ( (options.c_cflag & CSIZE) == CS8 )
    return QPortSettings::DB_8;
  else
    return QPortSettings::DB_UNKNOWN;
}


/*!

*/
void TermiosHelper::setDataBits(QPortSettings::DataBits dataBits)
{
  switch( dataBits ) {
    /*5 data bits*/
    case QPortSettings::DB_5:
      currentAttrs_->c_cflag &= (~CSIZE);
      currentAttrs_->c_cflag |= CS5;
      break;
    /*6 data bits*/
    case QPortSettings::DB_6:
      currentAttrs_->c_cflag &= (~CSIZE);
      currentAttrs_->c_cflag |= CS6;
      break;
    /*7 data bits*/
    case QPortSettings::DB_7:
      currentAttrs_->c_cflag &= (~CSIZE);
      currentAttrs_->c_cflag |= CS7;
      break;
    /*8 data bits*/
    case QPortSettings::DB_8:
      currentAttrs_->c_cflag &= (~CSIZE);
      currentAttrs_->c_cflag |= CS8;
      break;
    default:
      currentAttrs_->c_cflag &= (~CSIZE);
      currentAttrs_->c_cflag |= CS8;
      qWarning() << "TermiosHelper::setDataBits(" << dataBits << "): Unsupported data bits";
  }
}

/*!

 */

QPortSettings::Parity TermiosHelper::parity() const
{
  struct termios options;

  // get the current serial port attributes

  if ( tcgetattr(fileDescriptor_, &options) == -1 ) {
    qWarning() << QString("TermiosHelper::parity(file: %1) failed when getting original " \
                          "port attributes: %2(%3)")
                 .arg(fileDescriptor_)
                 .arg(strerror(errno))
                 .arg(errno);
    return QPortSettings::PAR_UNKNOWN;
  }

  if ( options.c_cflag & PARENB ) {
    if ( options.c_cflag & PARODD )
      return QPortSettings::PAR_ODD;
    else
      return QPortSettings::PAR_EVEN;
  }
  else
    return QPortSettings::PAR_NONE;
}

/*!

*/
void TermiosHelper::setParity(QPortSettings::Parity parity)
{
  switch ( parity ) {
    /*no parity*/
    case QPortSettings::PAR_NONE:
      currentAttrs_->c_cflag &= (~PARENB);
      break;
    /*odd parity*/
    case QPortSettings::PAR_ODD:
      currentAttrs_->c_cflag |= (PARENB|PARODD);
      break;
    /*even parity*/
    case QPortSettings::PAR_EVEN:
      currentAttrs_->c_cflag &= (~PARODD);
      currentAttrs_->c_cflag |= PARENB;
      break;
    default:
      currentAttrs_->c_cflag &= (~PARENB);
      qWarning() << "TermiosHelper::setParity(" << parity << "): Unsupported parity";
  }
}

/*!

 */

QPortSettings::StopBits TermiosHelper::stopBits() const
{
  struct termios options;
  // get the current serial port attributes

  if ( tcgetattr(fileDescriptor_, &options) == -1 ) {
    qWarning() << QString("TermiosHelper::stopBits(file: %1) failed when getting original " \
                          "port attributes: %2(%3)")
                 .arg(fileDescriptor_)
                 .arg(strerror(errno))
                 .arg(errno);
    return QPortSettings::STOP_UNKNOWN;
  }

  if ( options.c_cflag & CSTOPB )
    return QPortSettings::STOP_2;
  else
    return QPortSettings::STOP_1;
}

/*!

*/
void TermiosHelper::setStopBits(QPortSettings::StopBits stopBits)
{
  switch( stopBits ) {
    /*one stop bit*/
    case QPortSettings::STOP_1:
      currentAttrs_->c_cflag &= (~CSTOPB);
      break;
    /*two stop bits*/
    case QPortSettings::STOP_2:
      currentAttrs_->c_cflag |= CSTOPB;
      break;
    default:
      currentAttrs_->c_cflag &= (~CSTOPB);
      qWarning() << "TermiosHelper::setStopBits(" << stopBits << "): Unsupported stop bits";
  }
}

/*!
  @return FLOW_UNKNOWN in error case
 */

QPortSettings::FlowControl TermiosHelper::flowControl() const
{
  struct termios options;
  // get the current serial port attributes

  if ( tcgetattr(fileDescriptor_, &options) == -1 ) {
    qWarning() << QString("TermiosHelper::flowControl(file: %1) failed when getting original " \
                          "port attributes: %2(%3)")
                 .arg(fileDescriptor_)
                 .arg(strerror(errno))
                 .arg(errno);
    return QPortSettings::FLOW_UNKNOWN;
  }

  if ( options.c_cflag & CRTSCTS ) {
    return QPortSettings::FLOW_HARDWARE;
  }
  else {
    if ( options.c_iflag & IXON )
      return QPortSettings::FLOW_XONXOFF;
    else
      return QPortSettings::FLOW_OFF;
  }
}

/*!

*/
void TermiosHelper::setFlowControl(QPortSettings::FlowControl flow)
{
  switch( flow ) {
    /*no flow control*/
    case QPortSettings::FLOW_OFF:
      currentAttrs_->c_cflag &= (~CRTSCTS);
      currentAttrs_->c_iflag &= (~(IXON|IXOFF|IXANY));
      break;
    /*software (XON/XOFF) flow control*/
    case QPortSettings::FLOW_XONXOFF:
      currentAttrs_->c_cflag &= (~CRTSCTS);
      currentAttrs_->c_iflag |= (IXON|IXOFF|IXANY);
      break;
    case QPortSettings::FLOW_HARDWARE:
      currentAttrs_->c_cflag |= CRTSCTS;
      currentAttrs_->c_iflag &= (~(IXON|IXOFF|IXANY));
      break;
    default:
      currentAttrs_->c_cflag &= (~CRTSCTS);
      currentAttrs_->c_iflag &= (~(IXON|IXOFF|IXANY));
      qWarning() << "TermiosHelper::setFlowControl(" << flow << "): Unsupported flow control type";
  }
}

//
// information about VMIN and VTIME
// @see www.unixwiz.net/techtips/termios-vmin-vtime.html
//
// VMIN is a character count ranging from 0 to 255 characters, and VTIME is time measured in 0.1
// second intervals, (0 to 25.5 seconds). The value of "zero" is special to both of these parameters,
// and this suggests four combinations that we'll discuss below. In every case, the question is when
// a read() system call is satisfied, and this is our prototype call:
//  int n = read(fd, buffer, nbytes);
//
// Keep in mind that the tty driver maintains an input queue of bytes already read from the
// serial line and not passed to the user, so not every read() call waits for actual I/O - the read
// may very well be satisfied directly from the input queue.
//
// VMIN = 0 and VTIME = 0
// This is a completely non-blocking read - the call is satisfied immediately directly from the
// driver's input queue. If data are available, it's transferred to the caller's buffer up to
// nbytes and returned. Otherwise zero is immediately returned to indicate "no data". We'll note
// that this is "polling" of the serial port, and it's almost always a bad idea. If done repeatedly,
// it can consume enormous amounts of processor time and is highly inefficient. Don't use this mode
// unless you really, really know what you're doing.
//
// VMIN = 0 and VTIME > 0
// This is a pure timed read. If data are available in the input queue, it's transferred to the caller's
// buffer up to a maximum of nbytes, and returned immediately to the caller. Otherwise the driver blocks
// until data arrives, or when VTIME tenths expire from the start of the call. If the timer expires
// without data, zero is returned. A single byte is sufficient to satisfy this read call, but if more
// is available in the input queue, it's returned to the caller. Note that this is an overall timer,
// not an intercharacter one.
//
// VMIN > 0 and VTIME > 0
// A read() is satisfied when either VMIN characters have been transferred to the caller's buffer,
// or when VTIME tenths expire between characters. Since this timer is not started until the first
// character arrives, this call can block indefinitely if the serial line is idle. This is the most
// common mode of operation, and we consider VTIME to be an intercharacter timeout, not an overall one.
// This call should never return zero bytes read.
//
// VMIN > 0 and VTIME = 0
// This is a counted read that is satisfied only when at least VMIN characters have been transferred
// to the caller's buffer - there is no timing component involved. This read can be satisfied from the
// driver's input queue (where the call could return immediately), or by waiting for new data to arrive:
// in this respect the call could block indefinitely. We believe that it's undefined behavior if nbytes
// is less then VMIN.
//

bool TermiosHelper::commTimeouts(CommTimeouts &commtimeouts) const
{
  struct termios options;

  // get the current serial port attributes

  if ( tcgetattr(fileDescriptor_, &options) == -1 ) {
    qWarning() << QString("TermiosHelper::commTimeouts(file: %1) failed when getting original " \
                          "port attributes: %2(%3)")
                 .arg(fileDescriptor_)
                 .arg(strerror(errno))
                 .arg(errno);
    return false;
  }
  commtimeouts.PosixVMIN = options.c_cc[VMIN];
  commtimeouts.PosixVTIME = options.c_cc[VTIME];

  return true;
}

void TermiosHelper::setCommTimeouts(const CommTimeouts commtimeouts)
{
  currentAttrs_->c_cc[VMIN] = (cc_t)commtimeouts.PosixVMIN;
  currentAttrs_->c_cc[VTIME] = (cc_t)commtimeouts.PosixVTIME;
}

} // namespace