/****************************************************************************
**
** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com>
** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com>
** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
** Copyright (C) 2012 Andre Hartmann <aha_1980@gmx.de>
** 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
没有合适的资源?快使用搜索试试~ 我知道了~
Qt6SerialPort.zip
共122个文件
h:16个
0:14个
1:14个
需积分: 50 86 下载量 169 浏览量
2021-02-04
12:15:40
上传
评论 1
收藏 1.43MB ZIP 举报
温馨提示
在QT6中实现串口模块Qt6SerialPort,文章链接https://blog.csdn.net/qq_40732350/article/details/113634422
资源详情
资源评论
资源推荐
收起资源包目录
Qt6SerialPort.zip (122个子文件)
changes-5.2.0 5KB
changes-5.3.0 4KB
changes-5.6.0 3KB
changes-5.7.0 2KB
changes-5.4.0 2KB
changes-5.5.0 1KB
changes-5.10.0 1KB
changes-5.11.0 1KB
changes-5.14.0 960B
changes-5.9.0 864B
changes-5.8.0 857B
changes-5.15.0 730B
changes-5.12.0 730B
changes-5.13.0 730B
changes-5.4.1 3KB
changes-5.3.1 2KB
changes-5.5.1 2KB
changes-5.2.1 1KB
changes-5.6.1 1KB
changes-5.7.1 1KB
changes-5.10.1 1KB
changes-5.1.1 993B
changes-5.11.1 991B
changes-5.9.1 984B
changes-5.15.1 756B
changes-5.13.1 756B
changes-5.14.1 756B
changes-5.12.1 755B
changes-5.12.10 1KB
changes-5.3.2 2KB
changes-5.15.2 1KB
changes-5.6.2 1KB
changes-5.13.2 1KB
changes-5.11.2 1006B
changes-5.9.2 984B
changes-5.14.2 771B
changes-5.12.2 771B
changes-5.9.3 1KB
changes-5.6.3 1KB
changes-5.12.3 771B
changes-5.11.3 770B
changes-5.12.4 1KB
changes-5.9.4 993B
changes-5.9.5 1KB
changes-5.12.5 771B
changes-5.9.6 999B
.qmake.conf 123B
qserialport.cpp 47KB
qserialport_unix.cpp 34KB
moc_qserialport.cpp 27KB
qserialport_win.cpp 27KB
qserialportinfo_unix.cpp 17KB
qserialportinfo_win.cpp 15KB
qwinoverlappedionotifier.cpp 14KB
qserialportinfo_freebsd.cpp 13KB
qserialportinfo_osx.cpp 10KB
qserialportinfo.cpp 8KB
style.css 2KB
Qt6SerialPort.dll 75KB
Qt6SerialPort.exp 20KB
LICENSE.FDL 23KB
LICENSE.GPL2 18KB
LICENSE.GPL3 35KB
LICENSE.GPL3-EXCEPT 36KB
qserialport.h 10KB
qserialport_p.h 9KB
qtudev_p.h 6KB
qtntdll_p.h 5KB
qserialportinfo.h 4KB
qserialportinfo_p.h 3KB
qserialportglobal.h 2KB
moc_predefs.h 244B
qtserialportversion.h 228B
qserialportinfo_p.h 61B
qserialport_p.h 57B
qtntdll_p.h 53B
qserialportglobal.h 52B
qtudev_p.h 52B
qserialportinfo.h 50B
qserialport.h 46B
configure.json 1KB
configure.json 61B
LICENSE.LGPL3 8KB
Qt6SerialPort.lib 34KB
qserialport.obj 355KB
qserialportinfo_win.obj 253KB
qserialport_win.obj 201KB
qserialportinfo.obj 127KB
Qt6SerialPort.pdb 1.79MB
Qt6SerialPort.vc.pdb 1.54MB
enumerator-example.png 15KB
creadersync-example.png 13KB
creaderasync-example.png 12KB
cwritersync-example.png 12KB
cwriterasync-example.png 11KB
terminal-example.png 8KB
blockingslave-example.png 4KB
blockingmaster-example.png 4KB
cenumerator-example.png 3KB
serialport-lib.pri 1KB
共 122 条
- 1
- 2
QtHalcon
- 粉丝: 2379
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0