qserialportnative_wince.cpp 6.96 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
/*
 * 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
 */

// See http://msdn.microsoft.com/en-us/library/ms810467.aspx#serial_topic1
// for Win32 Serial Port programming.
// Also see the following forum http://www.codeguru.com/forum/archive/index.php/t-324660.html

#include <QDebug>
#include <QMutexLocker>
#include "qwincommevtnotifier.h"
#include "wincommevtbreaker.h"
#include "commdcbhelper.h"
#include "qserialport.h"

namespace TNX {

WinCommEvtBreaker* QSerialPortNative::waitBreakerThread_ = NULL;
QMutex* QSerialPortNative::mutex_ = new QMutex();  // this will leak once

/*!
  Constructs a QSerialPortNative object with the given \a port name and \a parent.
*/
QSerialPortNative::QSerialPortNative(const QString &portName, QObject *parent)
  : QIODevice(parent), portName_(portName), fileDescriptor_(0), portHelper_(NULL),
    readNotifier_(NULL)
{
}

/*!
  Constructs a QSerialPortNative object with the given \a port name, \a settings and \a parent.
*/
QSerialPortNative::QSerialPortNative(const QString &portName, const QPortSettings &settings, QObject *parent)
  : QIODevice(parent), portName_(portName), portSettings_(settings), fileDescriptor_(0), portHelper_(NULL),
    readNotifier_(NULL)
{
}

/*!
*/
bool QSerialPortNative::open_impl()
{
  // WinCE requires ':' suffix for all comport names
  if ( portName_.right(1) != ":" )
    portName_.append(":");

  fileDescriptor_ = CreateFile(portName_.utf16(), GENERIC_READ|GENERIC_WRITE,
                                0, NULL, OPEN_EXISTING, 0, NULL );
 
  if ( INVALID_HANDLE_VALUE == fileDescriptor_ )
    return false;
  
  readNotifier_ = new QWinCommEvtNotifier(fileDescriptor_, this);

  Q_CHECK_PTR(readNotifier_);

  if ( !readNotifier_ || !connect(readNotifier_, SIGNAL(activated(int)), 
                                  this, SLOT(onDataReceived()), Qt::QueuedConnection ) ) {
    qWarning() << QString("QSerialPort::open(%1) failed when connecting to read notifier")
                    .arg(portName_);
  }
  
  // Create a dcb helper object

  portHelper_ = new CommDCBHelper(fileDescriptor_);

  Q_CHECK_PTR(portHelper_);

  return true;
}

/*!
*/
void QSerialPortNative::close_impl()
{
  // Close the serial port, and init the handle

  if ( !CloseHandle(fileDescriptor_) ) {
    qWarning() << QString("QSerialPort::close(%1) failed: %2(Err #%3)")
                    .arg(portName_)
                    .arg(lastErrorText_impl())
                    .arg(lastError_impl());
  }
  fileDescriptor_ = INVALID_HANDLE_VALUE;
}

/*!
*/
QString QSerialPortNative::lastErrorText_impl() const
{
  return CommDCBHelper::errorText(GetLastError());
}

/*!
*/
int QSerialPortNative::lastError_impl() const
{
  return GetLastError();
}

/*!
*/
qint64 QSerialPortNative::bytesAvailable_impl() const
{
  DWORD errorMask = 0;
  COMSTAT comStat;
  if ( !ClearCommError(fileDescriptor_, &errorMask, &comStat) )
    return -1LL;

  return QIODevice::bytesAvailable() + (qint64)comStat.cbInQue;
}

/*!
 * @return -1 if error, 0 if timed-out, 1 if incoming data
 */
int QSerialPortNative::waitForReadyRead_impl(int timeout)
{
  DWORD orgFlags;
  DWORD dwMask;
  int result = -1;

  readNotifier_->suspend();

  // store the current event mask.

  if ( !GetCommMask(fileDescriptor_, &orgFlags) )
    goto LExit;

  // this one is to release the notifier thread.

  if ( !SetCommMask(fileDescriptor_, 0) )
    goto LExit;


  // change the event mask to wait incoming bytes.

  if ( !SetCommMask(fileDescriptor_, EV_RXCHAR | EV_ERR) )
    goto LExit;

  // timeout == -1 means no timeout

  if ( timeout != -1 ) {
    // if it is not already done, lazily create the thread that will release
    // the WaitCommEvent() function when timeout occurs.

    if ( !waitBreakerThread_ ) {
      QMutexLocker locker(mutex_);
      if ( !waitBreakerThread_ ) {
        waitBreakerThread_ = new WinCommEvtBreaker();
        waitBreakerThread_->start();
        // wait until thread is ready to run
        waitBreakerThread_->waitUntilReady();
      }
    }

    waitBreakerThread_->startWaitTimer(fileDescriptor_, timeout);
  }
   
  if ( WaitCommEvent(fileDescriptor_, &dwMask, 0) ) {
    // Use the flags returned in dwMask to determine the reason for return
    if ( dwMask & EV_RXCHAR )
      result = 1;
    else if ( dwMask & EV_ERR )
      result = -1;
    else
      result = 0;
  }

  if ( timeout != -1 )
    waitBreakerThread_->stopWaitTimer(fileDescriptor_);

  // rollback the original event mask.

  if ( !SetCommMask(fileDescriptor_, orgFlags) )
    result = -1;

LExit:
  readNotifier_->resume();
  return result;
}

/*!
*/
bool QSerialPortNative::flushInBuffer_impl()
{
  // PURGE_RXABORT: Terminates all outstanding overlapped read operations and returns immediately, 
  // even if the read operations have not been completed.
  // PURGE_TXABORT: Terminates all outstanding overlapped write operations and returns immediately, 
  // even if the write operations have not been completed.

  // clear internal buffers
  if ( !PurgeComm(fileDescriptor_, PURGE_RXABORT | PURGE_RXCLEAR ) )
    return false;

  return true;
}

/*!
*/
bool QSerialPortNative::flushOutBuffer_impl()
{
  // PURGE_RXABORT: Terminates all outstanding overlapped read operations and returns immediately, 
  // even if the read operations have not been completed.
  // PURGE_TXABORT: Terminates all outstanding overlapped write operations and returns immediately, 
  // even if the write operations have not been completed.

  // clear internal buffers
  if ( !PurgeComm(fileDescriptor_, PURGE_TXABORT | PURGE_TXCLEAR ) ) 
    return false;

  return true;
}

/*!
*/
bool QSerialPortNative::sendBreak_impl(int timeout)
{
  // send a break for a very short time

  if ( !SetCommBreak(fileDescriptor_) )
    return false;

  Sleep(timeout); 

  // clear the break and continue

  if ( !ClearCommBreak(fileDescriptor_) )
    return false;

  return true;
}

/*!
*/
qint64 QSerialPortNative::readData_impl(char *data, qint64 maxlen)
{
  qint64 numBytes = 0LL;
  if ( !ReadFile(fileDescriptor_, (void*)data, (DWORD)maxlen, (LPDWORD)&numBytes, NULL) )
    numBytes = -1LL;
  
  return numBytes;
}

/*!
*/
qint64 QSerialPortNative::writeData_impl(const char *data, qint64 len)
{
  qint64 numBytes = 0LL;
  if ( !WriteFile(fileDescriptor_, (void*)data, (DWORD)len, (DWORD*)&numBytes, NULL) )
    numBytes = -1LL;

  return numBytes;
}

} // namespace