/****************************************************************************
** Copyright (c) 2000-2003 Wayne Roth
** Copyright (c) 2004-2007 Stefan Sander
** Copyright (c) 2007 Michal Policht
** Copyright (c) 2008 Brandon Fosdick
** Copyright (c) 2009-2010 Liam Staskawicz
** Copyright (c) 2011 Debao Zhang
** All right reserved.
** Web: http://code.google.com/p/qextserialport/
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "qextserialport.h"
#include "qextserialport_p.h"
#include <stdio.h>
#include <QtCore/QDebug>
#include <QtCore/QReadLocker>
#include <QtCore/QWriteLocker>
/*!
\enum BaudRateType
baud rate values support.
*/
/*!
\enum DataBitsType
*/
/*!
\enum ParityType
*/
/*!
\enum StopBitsType
\value STOP_1
\value STOP_1_5
\value STOP_2
*/
/*!
\enum FlowType
*/
/*!
\class PortSettings
\brief The PortSettings class contain port settings
Structure to contain port settings.
\code
BaudRateType BaudRate;
DataBitsType DataBits;
ParityType Parity;
StopBitsType StopBits;
FlowType FlowControl;
long Timeout_Millisec;
\endcode
*/
QextSerialPortPrivate::QextSerialPortPrivate(QextSerialPort *q)
:lock(QReadWriteLock::Recursive), q_ptr(q)
{
lastErr = E_NO_ERROR;
Settings.BaudRate = BAUD9600;
Settings.Parity = PAR_NONE;
Settings.FlowControl = FLOW_OFF;
Settings.DataBits = DATA_8;
Settings.StopBits = STOP_1;
Settings.Timeout_Millisec = 10;
settingsDirtyFlags = DFE_ALL;
platformSpecificInit();
}
QextSerialPortPrivate::~QextSerialPortPrivate()
{
platformSpecificDestruct();
}
void QextSerialPortPrivate::setBaudRate(BaudRateType baudRate, bool update)
{
switch (baudRate) {
#ifdef Q_OS_WIN
//Windows Special
case BAUD14400:
case BAUD56000:
case BAUD128000:
case BAUD256000:
QESP_PORTABILITY_WARNING()<<"QextSerialPort Portability Warning: POSIX does not support baudRate:"<<baudRate;
#elif defined(Q_OS_UNIX)
//Unix Special
case BAUD50:
case BAUD75:
case BAUD134:
case BAUD150:
case BAUD200:
case BAUD1800:
#ifdef B76800
case BAUD76800:
#endif
#if defined(B230400) && defined(B4000000)
case BAUD230400:
case BAUD460800:
case BAUD500000:
case BAUD576000:
case BAUD921600:
case BAUD1000000:
case BAUD1152000:
case BAUD1500000:
case BAUD2000000:
case BAUD2500000:
case BAUD3000000:
case BAUD3500000:
case BAUD4000000:
#endif
QESP_PORTABILITY_WARNING()<<"QextSerialPort Portability Warning: Windows does not support baudRate:"<<baudRate;
#endif
case BAUD110:
case BAUD300:
case BAUD600:
case BAUD1200:
case BAUD2400:
case BAUD4800:
case BAUD9600:
case BAUD19200:
case BAUD38400:
case BAUD57600:
case BAUD115200:
Settings.BaudRate=baudRate;
settingsDirtyFlags |= DFE_BaudRate;
if (update && q_func()->isOpen())
updatePortSettings();
break;
default:
QESP_WARNING()<<"QextSerialPort does not support baudRate:"<<baudRate;
}
}
void QextSerialPortPrivate::setParity(ParityType parity, bool update)
{
switch (parity) {
case PAR_SPACE:
if (Settings.DataBits==DATA_8) {
#ifdef Q_OS_WIN
QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: Space parity with 8 data bits is not supported by POSIX systems.");
#else
QESP_WARNING("Space parity with 8 data bits is not supported by POSIX systems.");
#endif
}
break;
#ifdef Q_OS_WIN
/*mark parity - WINDOWS ONLY*/
case PAR_MARK:
QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: Mark parity is not supported by POSIX systems");
break;
#endif
case PAR_NONE:
case PAR_EVEN:
case PAR_ODD:
break;
default:
QESP_WARNING()<<"QextSerialPort does not support Parity:" << parity;
}
Settings.Parity=parity;
settingsDirtyFlags |= DFE_Parity;
if (update && q_func()->isOpen())
updatePortSettings();
}
void QextSerialPortPrivate::setDataBits(DataBitsType dataBits, bool update)
{
switch(dataBits) {
case DATA_5:
if (Settings.StopBits==STOP_2) {
QESP_WARNING("QextSerialPort: 5 Data bits cannot be used with 2 stop bits.");
}
else {
Settings.DataBits=dataBits;
settingsDirtyFlags |= DFE_DataBits;
}
break;
case DATA_6:
#ifdef Q_OS_WIN
if (Settings.StopBits==STOP_1_5) {
QESP_WARNING("QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits.");
}
else
#endif
{
Settings.DataBits=dataBits;
settingsDirtyFlags |= DFE_DataBits;
}
break;
case DATA_7:
#ifdef Q_OS_WIN
if (Settings.StopBits==STOP_1_5) {
QESP_WARNING("QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits.");
}
else
#endif
{
Settings.DataBits=dataBits;
settingsDirtyFlags |= DFE_DataBits;
}
break;
case DATA_8:
#ifdef Q_OS_WIN
if (Settings.StopBits==STOP_1_5) {
QESP_WARNING("QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits.");
}
else
#endif
{
Settings.DataBits=dataBits;
settingsDirtyFlags |= DFE_DataBits;
}
break;
default:
QESP_WARNING()<<"QextSerialPort does not support Data bits:"<<dataBits;
}
if (update && q_func()->isOpen())
updatePortSettings();
}
void QextSerialPortPrivate::setStopBits(StopBitsType stopBits, bool update)
{
switch (stopBits) {
/*one stop bit*/
case STOP_1:
Settings.StopBits = stopBits;
settingsDirtyFlags |= DFE_StopBits;
break;
#ifdef Q_OS_WIN
/*1.5 stop bits*/
case STOP_1_5:
QESP_PORTABILITY_WARNING("QextSerialPort Portability Warning: 1.5 stop bit operation is not supported by POSIX.");
if (Settings.DataBits!=DATA_5) {
QESP_WARNING("QextSerialPort: 1.5 stop bits can only be used with 5 data bits");
}
else {
Settings.StopBits = stopBits;
settingsDirtyFlags |= DFE_StopBits;
}
break;
#endif
/*two stop bits*/
case STOP_2:
if (Settings.DataBits==DATA_5) {
QESP_WARNING("QextSerialPort: 2 stop bits cannot be used with 5 data bits");
}
else {
Settings.StopBits = stopBits;
settingsDirtyFlags |= DFE_StopBits;
}
break;
default:
QESP_WARNING()<<"QextSerialPort does not support stop bits: "<<stopBits;
}
if (update && q_func()->isOpen())
updatePortSettings();
}
void QextSerialPortPrivate::setFlowControl(FlowType flow, bool update)
{
Settings.FlowControl=
sp.tar.gz_Qt 串口_mini2440 qt
版权申诉
70 浏览量
2022-09-23
12:01:35
上传
评论
收藏 30KB GZ 举报
weixin_42651887
- 粉丝: 79
- 资源: 1万+