qportsettings.cpp 8.32 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
/*
 * 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
 */

#include <QDebug>
#include <QStringList>
#include "qportsettings.h"

namespace TNX {

/*!
  Constructs a QPortSettings object with default values.
*/

QPortSettings::QPortSettings()
{
34 35 36 37 38
    setBaudRate(BAUDR_9600);
    dataBits_ = DB_8;
    parity_ = PAR_NONE;
    stopBits_ = STOP_1;
    flowControl_ = FLOW_OFF;
James Goppert's avatar
James Goppert committed
39 40 41 42 43 44 45 46
}

/*!
  Constructs a QPortSettings object with the given \a settings.
*/

QPortSettings::QPortSettings(const QString &settings)
{
47
    set(settings);
James Goppert's avatar
James Goppert committed
48 49 50 51
}

bool QPortSettings::set(const QString &settings)
{
52 53 54 55 56
    setBaudRate(BAUDR_9600);
    dataBits_ = DB_8;
    parity_ = PAR_NONE;
    stopBits_ = STOP_1;
    flowControl_ = FLOW_OFF;
James Goppert's avatar
James Goppert committed
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
    QStringList list = settings.split(",", QString::SkipEmptyParts);
    bool ok = true;
    for (int i = 0; i < list.size(); i++) {
        switch (i) {
        case 0:
            setBaudRate(baudRateFromInt(list.at(i).toInt(&ok), ok));
            if ( !ok ) {
                qWarning() << QString("QPortSettings::QPortSettings(%1) " \
                                      "failed when setting baud rate [value: %2]; using default value")
                              .arg(settings)
                              .arg(list.at(i));
            }
            break;
        case 1:
            dataBits_ = dataBitsFromString(list.at(i), ok);
            if ( !ok ) {
                qWarning() << QString("QPortSettings::QPortSettings(%1) " \
                                      "failed when setting data bits [value: %2]; using default value")
                              .arg(settings)
                              .arg(list.at(i));
            }
            break;
        case 2:
            parity_ = parityFromString(list.at(i), ok);
            if ( !ok ) {
                qWarning() << QString("QPortSettings::QPortSettings(%1) " \
                                      "failed when setting parity [value: %2]; using default value")
                              .arg(settings)
                              .arg(list.at(i));
            }
            break;
        case 3:
            stopBits_ = stopBitsFromString(list.at(i), ok);
            if ( !ok ) {
                qWarning() << QString("QPortSettings::QPortSettings(%1) " \
                                      "failed when setting stop bits [value: %2]; using default value")
                              .arg(settings)
                              .arg(list.at(i));
            }
            break;
        case 4:
            flowControl_ = flowControlFromString(list.at(i), ok);
            if ( !ok ) {
                qWarning() << QString("QPortSettings::QPortSettings(%1) " \
                                      "failed when setting flow control type [value: %2]; using default value")
                              .arg(settings)
                              .arg(list.at(i));
            }
            break;
        default:
            break;
        } //switch
    } //for
James Goppert's avatar
James Goppert committed
111

112
    return ok;
James Goppert's avatar
James Goppert committed
113 114 115 116 117 118 119
}

/*!

*/
QPortSettings::BaudRate QPortSettings::baudRateFromInt(int baud, bool &ok)
{
120
    ok = true;
James Goppert's avatar
James Goppert committed
121

122
    switch ( baud ) {
James Goppert's avatar
James Goppert committed
123 124
#ifdef TNX_POSIX_SERIAL_PORT
    case 50:
125
        return BAUDR_50;
James Goppert's avatar
James Goppert committed
126
    case 75:
127
        return BAUDR_75;
James Goppert's avatar
James Goppert committed
128
    case 134:
129
        return BAUDR_134;
James Goppert's avatar
James Goppert committed
130
    case 150:
131
        return BAUDR_150;
James Goppert's avatar
James Goppert committed
132
    case 200:
133
        return BAUDR_200;
James Goppert's avatar
James Goppert committed
134
    case 1800:
135 136 137
        return BAUDR_1800;
        //case 76800:
        //  return BAUDR_76800;
James Goppert's avatar
James Goppert committed
138 139 140
#endif
#ifdef TNX_WINDOWS_SERIAL_PORT
    case 14400:
141
        return BAUDR_14400;
James Goppert's avatar
James Goppert committed
142
    case 56000:
143
        return BAUDR_56000;
James Goppert's avatar
James Goppert committed
144
    case 128000:
145
        return BAUDR_128000;
James Goppert's avatar
James Goppert committed
146
    case 256000:
147
        return BAUDR_256000;
James Goppert's avatar
James Goppert committed
148 149 150
#endif
#if defined(Q_OS_LINUX)
    case 500000:
151
        return BAUDR_500000;
James Goppert's avatar
James Goppert committed
152
    case 576000:
153
        return BAUDR_576000;
James Goppert's avatar
James Goppert committed
154
#endif
155
        // baud rates supported by all platforms
James Goppert's avatar
James Goppert committed
156
    case 110:
157
        return BAUDR_110;
James Goppert's avatar
James Goppert committed
158
    case 300:
159
        return BAUDR_300;
James Goppert's avatar
James Goppert committed
160
    case 600:
161
        return BAUDR_600;
James Goppert's avatar
James Goppert committed
162
    case 1200:
163
        return BAUDR_1200;
James Goppert's avatar
James Goppert committed
164
    case 2400:
165
        return BAUDR_2400;
James Goppert's avatar
James Goppert committed
166
    case 4800:
167
        return BAUDR_4800;
James Goppert's avatar
James Goppert committed
168
    case 9600:
169
        return BAUDR_9600;
James Goppert's avatar
James Goppert committed
170
    case 19200:
171
        return BAUDR_19200;
James Goppert's avatar
James Goppert committed
172
    case 38400:
173
        return BAUDR_38400;
James Goppert's avatar
James Goppert committed
174
    case 57600:
175
        return BAUDR_57600;
James Goppert's avatar
James Goppert committed
176
    case 115200:
177 178 179 180 181 182 183
        return BAUDR_115200;
    case 230400:
        return BAUDR_230400;
    case 460800:
        return BAUDR_460800;
    case 921600:
        return BAUDR_921600;
James Goppert's avatar
James Goppert committed
184
    default:
185 186 187
        ok = false;
        return BAUDR_9600;
    }
James Goppert's avatar
James Goppert committed
188 189 190 191 192 193 194
}

/*!

*/
QPortSettings::DataBits QPortSettings::dataBitsFromString(const QString &dataBits, bool &ok)
{
195 196 197 198 199 200 201 202 203 204 205 206 207
    ok = true;
    if ( dataBits.trimmed() == "5" )
        return DB_5;
    else if ( dataBits.trimmed() == "6" )
        return DB_6;
    else if ( dataBits.trimmed() == "7" )
        return DB_7;
    else if ( dataBits.trimmed() == "8")
        return DB_8;
    else {
        ok = false;
        return DB_8;
    }
James Goppert's avatar
James Goppert committed
208 209 210 211 212 213 214
}

/*!

*/
QPortSettings::Parity QPortSettings::parityFromString(const QString &parity, bool &ok)
{
215 216 217 218 219 220 221
    ok = true;
    if ( !QString::compare(parity.trimmed(), "n", Qt::CaseInsensitive) )
        return PAR_NONE;
    else if ( !QString::compare(parity.trimmed(), "o", Qt::CaseInsensitive) )
        return PAR_ODD;
    else if ( !QString::compare(parity.trimmed(), "e", Qt::CaseInsensitive) )
        return PAR_EVEN;
James Goppert's avatar
James Goppert committed
222
#ifdef TNX_WINDOWS_SERIAL_PORT
223 224
    else if ( !QString::compare(parity.trimmed(), "m", Qt::CaseInsensitive) )
        return PAR_MARK;
James Goppert's avatar
James Goppert committed
225
#endif
226 227 228 229 230 231
    else if ( !QString::compare(parity.trimmed(), "s", Qt::CaseInsensitive) )
        return PAR_SPACE;
    else {
        ok = false;
        return PAR_NONE;
    }
James Goppert's avatar
James Goppert committed
232 233 234 235 236 237 238
}

/*!

*/
QPortSettings::StopBits QPortSettings::stopBitsFromString(const QString &stopBits, bool &ok)
{
239 240 241
    ok = true;
    if ( stopBits.trimmed() == "1" )
        return STOP_1;
James Goppert's avatar
James Goppert committed
242
#ifdef TNX_WINDOWS_SERIAL_PORT
243 244
    else if ( stopBits.trimmed() == "1.5" )
        return STOP_1_5;
James Goppert's avatar
James Goppert committed
245
#endif
246 247 248 249 250 251
    else if ( stopBits.trimmed() == "2" )
        return STOP_2;
    else {
        ok = false;
        return STOP_1;
    }
James Goppert's avatar
James Goppert committed
252 253 254 255 256 257 258
}

/*!

*/
QPortSettings::FlowControl QPortSettings::flowControlFromString(const QString &flow, bool &ok)
{
259 260 261 262 263 264 265 266 267 268 269
    ok = true;
    if ( !QString::compare(flow.trimmed(), "off", Qt::CaseInsensitive) )
        return FLOW_OFF;
    else if ( !QString::compare(flow.trimmed(), "xon/xoff", Qt::CaseInsensitive) )
        return FLOW_XONXOFF;
    else if ( !QString::compare(flow.trimmed(), "hardware", Qt::CaseInsensitive) )
        return FLOW_HARDWARE;
    else {
        ok = false;
        return FLOW_OFF;
    }
James Goppert's avatar
James Goppert committed
270 271 272 273 274 275 276
}

/*!

*/
QString QPortSettings::toString() const
{
277
    QString txt;
James Goppert's avatar
James Goppert committed
278

279 280
    txt.setNum(baudRateInt_, 10);
    txt.append(",");
James Goppert's avatar
James Goppert committed
281

282 283 284 285 286 287 288 289
    if ( dataBits() == DB_5 )
        txt.append("5,");
    else if ( dataBits() == DB_6 )
        txt.append("6,");
    else if( dataBits() == DB_7 )
        txt.append("7,");
    else if ( dataBits() == DB_8 )
        txt.append("8,");
James Goppert's avatar
James Goppert committed
290

291 292 293 294 295 296
    if ( parity() == PAR_NONE )
        txt.append("N,");
    else if ( parity() == PAR_ODD )
        txt.append("O,");
    else if ( parity() == PAR_EVEN )
        txt.append("E,");
James Goppert's avatar
James Goppert committed
297
#ifdef TNX_WINDOWS_SERIAL_PORT
298 299
    else if ( parity() == PAR_MARK  )
        txt.append("M,");
James Goppert's avatar
James Goppert committed
300
#endif
301 302
    else if ( parity() == PAR_SPACE )
        txt.append("S,");
James Goppert's avatar
James Goppert committed
303

304 305
    if ( stopBits() == STOP_1 )
        txt.append("1,");
James Goppert's avatar
James Goppert committed
306
#ifdef TNX_WINDOWS_SERIAL_PORT
307 308
    else if ( stopBits() == STOP_1_5 )
        txt.append("1.5,");
James Goppert's avatar
James Goppert committed
309
#endif
310 311
    else if ( stopBits() == STOP_2 )
        txt.append("2,");
James Goppert's avatar
James Goppert committed
312

313 314 315 316 317 318
    if ( flowControl() == FLOW_OFF )
        txt.append("off");
    else if ( flowControl() == FLOW_XONXOFF )
        txt.append("xon/xoff");
    else if ( flowControl() == FLOW_HARDWARE )
        txt.append("hardware");
James Goppert's avatar
James Goppert committed
319

320
    return txt;
James Goppert's avatar
James Goppert committed
321 322 323 324 325
}

} // namespace