qportsettings.h 7.33 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
/*
 * 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
 * @brief Declares QPortSettings class that is required to set the port attributes.
 */

#ifndef TNX_QSERIALPORTSETTINGS_H__
#define TNX_QSERIALPORTSETTINGS_H__

#include <QString>
#include "qserialport_export.h"

#ifdef TNX_POSIX_SERIAL_PORT
#undef TNX_POSIX_SERIAL_PORT
#endif
#ifdef TNX_WINDOWS_SERIAL_PORT
#undef TNX_WINDOWS_SERIAL_PORT
#endif

#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
#define TNX_POSIX_SERIAL_PORT
#endif
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
#include <windows.h>
#define TNX_WINDOWS_SERIAL_PORT
#endif

namespace TNX {
45 46 47 48 49 50 51 52

enum ChangeApplyTypes { AllAppTy, PortAttrOnlyAppTy, CommTimeoutsOnlyAppTy };

/**
 * Communication timeout values for Win32/CE and Posix platforms.
 * @see www.unixwiz.net/techtips/termios-vmin-vtime.html
 */
struct CommTimeouts {
James Goppert's avatar
James Goppert committed
53
#ifdef TNX_WINDOWS_SERIAL_PORT
54 55
    typedef DWORD commt_t;
    static const DWORD NoTimeout = MAXDWORD;
James Goppert's avatar
James Goppert committed
56
#else
57 58
    typedef quint8 commt_t;
    static const qint8 NoTimeout = -1;
James Goppert's avatar
James Goppert committed
59
#endif
60

61 62 63 64 65 66
    // Win32 only section
    commt_t Win32ReadIntervalTimeout;          ///< Maximum time between read chars. Win32 only.
    commt_t Win32ReadTotalTimeoutMultiplier;   ///< Multiplier of characters. Win32 only.
    commt_t Win32ReadTotalTimeoutConstant;     ///< Constant in milliseconds. Win32 only.
    commt_t Win32WriteTotalTimeoutMultiplier;  ///< Multiplier of characters. Win32 only.
    commt_t Win32WriteTotalTimeoutConstant;    ///< Constant in milliseconds. Win32 only.
67

68 69 70 71 72 73 74 75 76 77
    // Posix only section
    commt_t PosixVTIME;                         ///< Read timeout. Posix only.
    commt_t PosixVMIN;                          ///< Minimum number of bytes before returning from
    ///< read operation. Posix only.
    CommTimeouts()
        : Win32ReadIntervalTimeout(NoTimeout), Win32ReadTotalTimeoutMultiplier(0), Win32ReadTotalTimeoutConstant(0),
          Win32WriteTotalTimeoutMultiplier(25), Win32WriteTotalTimeoutConstant(250),
          PosixVTIME(0), PosixVMIN(1)
    {
    }
78 79 80 81 82 83 84 85
};

/**
 * Wrapper class for serial port settings.
 */
class TONIX_EXPORT QPortSettings
{
public:
86 87
    enum BaudRate {
        BAUDR_UNKNOWN = 0,
James Goppert's avatar
James Goppert committed
88
#ifdef TNX_POSIX_SERIAL_PORT
89 90 91 92 93 94 95
        BAUDR_50,
        BAUDR_75,
        BAUDR_134,
        BAUDR_150,
        BAUDR_200,
        BAUDR_1800,
        //BAUDR_76800,
James Goppert's avatar
James Goppert committed
96 97
#endif
#ifdef Q_OS_LINUX
98 99
        //    BAUDR_500000,
        //    BAUDR_576000,
James Goppert's avatar
James Goppert committed
100 101
#endif
#ifdef TNX_WINDOWS_SERIAL_PORT
102 103 104 105
        BAUDR_14400,
        BAUDR_56000,
        BAUDR_128000,
        BAUDR_256000,
James Goppert's avatar
James Goppert committed
106
#endif
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        // baud rates supported by all OSs
        BAUDR_110,
        BAUDR_300,
        BAUDR_600,
        BAUDR_1200,
        BAUDR_2400,
        BAUDR_4800,
        BAUDR_9600,
        BAUDR_19200,
        BAUDR_38400,
        BAUDR_57600,
        BAUDR_115200,
        BAUDR_230400,
        BAUDR_460800,
        BAUDR_500000,
        BAUDR_576000,
        BAUDR_921600,
    };
125

126 127 128 129 130 131 132
    enum DataBits {
        DB_5,   // simulated in POSIX
        DB_6,
        DB_7,
        DB_8,
        DB_UNKNOWN
    };
133

134 135 136 137
    enum Parity {
        PAR_NONE,
        PAR_ODD,
        PAR_EVEN,
James Goppert's avatar
James Goppert committed
138
#ifdef TNX_WINDOWS_SERIAL_PORT
139
        PAR_MARK,
LM's avatar
LM committed
140
#endif
141 142 143
        PAR_SPACE,   // simulated in POSIX
        PAR_UNKNOWN
    };
144

145 146
    enum StopBits {
        STOP_1,
LM's avatar
LM committed
147
#ifdef TNX_WINDOWS_SERIAL_PORT
148
        STOP_1_5,
LM's avatar
LM committed
149
#endif
150 151 152
        STOP_2,
        STOP_UNKNOWN
    };
153

154 155 156 157 158 159
    enum FlowControl {
        FLOW_OFF,
        FLOW_HARDWARE,
        FLOW_XONXOFF,
        FLOW_UNKNOWN
    };
160

161 162
    QPortSettings();
    QPortSettings(const QString &settings);
163

164
    // port configuration methods
165

166
    bool set(const QString &settings);
167

168 169 170 171 172
    inline BaudRate baudRate() const {
        return baudRate_;
    }
    void setBaudRate(BaudRate baudRate) {
        baudRate_ = baudRate;
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
        switch ( baudRate_ ) {
#ifdef TNX_POSIX_SERIAL_PORT
        case BAUDR_50: baudRateInt_=50; break;
        case BAUDR_75: baudRateInt_=75; break;
        case BAUDR_134: baudRateInt_=134; break;
        case BAUDR_150: baudRateInt_=150; break;
        case BAUDR_200: baudRateInt_=200; break;
        case BAUDR_1800: baudRateInt_=1800; break;
            //case 76800: baudRateInt_=76800; break;
#endif
#ifdef TNX_WINDOWS_SERIAL_PORT
        case BAUDR_14400: baudRateInt_=14400; break;
        case BAUDR_56000: baudRateInt_=56000; break;
        case BAUDR_128000: baudRateInt_=128000; break;
        case BAUDR_256000: baudRateInt_=256000; break;
#endif
#if defined(Q_OS_LINUX)
        case BAUDR_500000: baudRateInt_=500000; break;
        case BAUDR_576000: baudRateInt_=576000; break;
#endif
            // baud rates supported by all platforms
        case BAUDR_110: baudRateInt_=110; break;
        case BAUDR_300: baudRateInt_=300; break;
        case BAUDR_600: baudRateInt_=600; break;
        case BAUDR_1200: baudRateInt_=1200; break;
        case BAUDR_2400: baudRateInt_=2400; break;
        case BAUDR_4800: baudRateInt_=4800; break;
        case BAUDR_9600: baudRateInt_=9600; break;
        case BAUDR_19200: baudRateInt_=19200; break;
        case BAUDR_38400: baudRateInt_=38400; break;
        case BAUDR_57600: baudRateInt_=57600; break;
        case BAUDR_115200: baudRateInt_=115200; break;
        case BAUDR_230400: baudRateInt_=230400; break;
        case BAUDR_460800: baudRateInt_=460800; break;
        case BAUDR_921600: baudRateInt_=921600; break;
        default:
            baudRateInt_ = 0; // unknown baudrate
        }
    }
213

214 215 216 217 218 219
    inline Parity parity() const {
        return parity_;
    }
    inline void setParity(Parity parity) {
        parity_ = parity;
    }
220

221 222 223 224 225 226
    inline StopBits stopBits() const {
        return stopBits_;
    }
    inline void setStopBits(StopBits stopBits) {
        stopBits_ = stopBits;
    }
227

228 229 230 231 232 233
    inline DataBits dataBits() const {
        return dataBits_;
    }
    inline void setDataBits(DataBits dataBits) {
        dataBits_ = dataBits;
    }
234

235 236 237 238 239 240
    inline FlowControl flowControl() const {
        return flowControl_;
    }
    inline void setFlowControl(FlowControl flowControl) {
        flowControl_ = flowControl;
    }
241

242
    QString toString() const;
243

244
    // helper methods to configure port settings
245
private:
246 247 248 249 250
    static BaudRate baudRateFromInt(int baud, bool &ok);
    static DataBits dataBitsFromString(const QString &databits, bool &ok);
    static Parity parityFromString(const QString &parity, bool &ok);
    static StopBits stopBitsFromString(const QString &stopbits, bool &ok);
    static FlowControl flowControlFromString(const QString &flow, bool &ok);
251 252

private:
253 254 255 256 257 258
    BaudRate baudRate_;
    DataBits dataBits_;
    Parity parity_;
    StopBits stopBits_;
    FlowControl flowControl_;
    qint32 baudRateInt_;
259 260
};

James Goppert's avatar
James Goppert committed
261 262 263
}

#endif // TNX_QSERIALPORTSETTINGS_H__