/****************************************************************************
**
** Copyright (C) 2011-2012 Denis Shienkov <
[email protected]>
** Copyright (C) 2011 Sergey Belyashov <
[email protected]>
** Copyright (C) 2012 Laszlo Papp <
[email protected]>
** Copyright (C) 2012 Andre Hartmann <
[email protected]>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qserialport.h"
#include "qserialportinfo.h"
#include "qserialportinfo_p.h"
#include "qserialport_p.h"
#include <QtCore/qdebug.h>
QT_BEGIN_NAMESPACE
QSerialPortErrorInfo::QSerialPortErrorInfo(QSerialPort::SerialPortError newErrorCode,
const QString &newErrorString)
: errorCode(newErrorCode)
, errorString(newErrorString)
{
if (errorString.isNull()) {
switch (errorCode) {
case QSerialPort::NoError:
errorString = QSerialPort::tr("No error");
break;
case QSerialPort::OpenError:
errorString = QSerialPort::tr("Device is already open");
break;
case QSerialPort::NotOpenError:
errorString = QSerialPort::tr("Device is not open");
break;
case QSerialPort::TimeoutError:
errorString = QSerialPort::tr("Operation timed out");
break;
case QSerialPort::ReadError:
errorString = QSerialPort::tr("Error reading from device");
break;
case QSerialPort::WriteError:
errorString = QSerialPort::tr("Error writing to device");
break;
case QSerialPort::ResourceError:
errorString = QSerialPort::tr("Device disappeared from the system");
break;
default:
// an empty string will be interpreted as "Unknown error"
// from the QIODevice::errorString()
break;
}
}
}
QSerialPortPrivate::QSerialPortPrivate()
#if defined(Q_OS_WIN32)
: readChunkBuffer(QSERIALPORT_BUFFERSIZE, 0)
#endif
{
writeBufferChunkSize = QSERIALPORT_BUFFERSIZE;
readBufferChunkSize = QSERIALPORT_BUFFERSIZE;
}
void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo)
{
Q_Q(QSerialPort);
error = errorInfo.errorCode;
q->setErrorString(errorInfo.errorString);
emit q->errorOccurred(error);
#if QT_DEPRECATED_SINCE(5, 8)
emit q->error(error);
#endif
}
/*!
\class QSerialPort
\brief Provides functions to access serial ports.
\reentrant
\ingroup serialport-main
\inmodule QtSerialPort
\since 5.1
You can get information about the available serial ports using the
QSerialPortInfo helper class, which allows an enumeration of all the serial
ports in the system. This is useful to obtain the correct name of the
serial port you want to use. You can pass an object
of the helper class as an argument to the setPort() or setPortName()
methods to assign the desired serial device.
After setting the port, you can open it in read-only (r/o), write-only
(w/o), or read-write (r/w) mode using the open() method.
\note The serial port is always opened with exclusive access
(that is, no other process or thread can access an already opened serial port).
Use the close() method to close the port and cancel the I/O operations.
Having successfully opened, QSerialPort tries to determine the current
configuration of the port and initializes itself. You can reconfigure the
port to the desired setting using the setBaudRate(), setDataBits(),
setParity(), setStopBits(), and setFlowControl() methods.
There are a couple of properties to work with the pinout signals namely:
QSerialPort::dataTerminalReady, QSerialPort::requestToSend. It is also
possible to use the pinoutSignals() method to query the current pinout
signals set.
Once you know that the ports are ready to read or write, you can
use the read() or write() methods. Alternatively the
readLine() and readAll() convenience methods can also be invoked.
If not all the data is read at once, the remaining data will
be available for later as new incoming data is appended to the
QSerialPort's internal read buffer. You can limit the size of the read
buffer using setReadBufferSize().
QSerialPort provides a set of functions that suspend the
calling thread until certain signals are emitted. These functions
can be used to implement blocking serial ports:
\list
\li waitForReadyRead() blocks calls until new data is available for
reading.
\li waitForBytesWritten() blocks calls until one payload of data has
been written to the serial port.
\endlist
See the following example:
\code
int numRead = 0, numReadTotal = 0;
char buffer[50];
for (;;) {
numRead = serial.read(buffer, 50);
// Do whatever with the array
numReadTotal += numRead;
if (numRead == 0 && !serial.waitForReadyRead())
break;
}
\endcode
If \l{QIODevice::}{waitForReadyRead()} returns \c false, the
connection has been closed or an error has occurred.
If an error occurs at any point in time, QSerialPort will emit the
errorOccurred() signal. You can also call error() to find the type of
error that occurred last.
\note Not all error conditions are handled in a platform independent way in
QSerialport, as for example the Framing, Parity, and Break condition errors.
These kind of errors need to be handled by the application code, probably
using OS system specific ioctls on the device descriptor and/or parsing the
stream's byte-stuffing.
Programming with a blocking serial port is radically different from
programming with a non-blocking serial port. A blocking serial port
does not require an event loop and typically leads to simpler code.
However, in a GUI application, blocking serial port should only be
used in non-GUI threads, to avoid freezing the user interface.
Fo
评论0