SerialInterface.h 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009 - 2011 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL 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 General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/
23

24 25 26 27 28 29 30 31 32 33 34 35 36
/**
 * @file
 *   @brief Brief Description
 *
 *   @author James Goppertr <james.goppert@gmail.edu>
 *
 */

#ifndef SERIALINTERFACE_H
#define SERIALINTERFACE_H

#include <QIODevice>
#include "qextserialport.h"
37
#include <QtSerialPort/QSerialPort>
38
#include <iostream>
39 40 41 42

/**
 * @brief The SerialInterface abstracts low level serial calls
 */
43 44 45
class SerialInterface : public QObject
{
    Q_OBJECT
46 47

signals:
48
    void aboutToClose();
49 50 51

public:

52 53
    enum baudRateType
    {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        BAUD50,                //POSIX ONLY
        BAUD75,                //POSIX ONLY
        BAUD110,
        BAUD134,               //POSIX ONLY
        BAUD150,               //POSIX ONLY
        BAUD200,               //POSIX ONLY
        BAUD300,
        BAUD600,
        BAUD1200,
        BAUD1800,              //POSIX ONLY
        BAUD2400,
        BAUD4800,
        BAUD9600,
        BAUD14400,             //WINDOWS ONLY
        BAUD19200,
        BAUD38400,
        BAUD56000,             //WINDOWS ONLY
        BAUD57600,
        BAUD76800,             //POSIX ONLY
        BAUD115200,
        BAUD128000,            // WINDOWS ONLY
        BAUD230400,            // WINDOWS ONLY
        BAUD256000,            // WINDOWS ONLY
        BAUD460800,            // WINDOWS ONLY
        BAUD921600             // WINDOWS ONLY
    };

81 82
    enum dataBitsType
    {
83 84 85 86 87 88
        DATA_5,
        DATA_6,
        DATA_7,
        DATA_8
    };

89 90
    enum parityType
    {
91 92 93 94 95 96 97
        PAR_NONE,
        PAR_ODD,
        PAR_EVEN,
        PAR_MARK,               //WINDOWS ONLY
        PAR_SPACE
    };

98 99
    enum stopBitsType
    {
100 101 102 103 104
        STOP_1,
        STOP_1_5,               //WINDOWS ONLY
        STOP_2
    };

105 106
    enum flowType
    {
107 108 109 110 111 112 113 114
        FLOW_OFF,
        FLOW_HARDWARE,
        FLOW_XONXOFF
    };

    /**
     * structure to contain port settings
     */
115 116
    struct portSettings
    {
117 118 119 120 121 122 123 124 125
        baudRateType BaudRate;
        dataBitsType DataBits;
        parityType Parity;
        stopBitsType StopBits;
        flowType FlowControl;
        long timeout_Millisec;
    };

    virtual bool isOpen() = 0;
126
    virtual bool isWritable() = 0;
James Goppert's avatar
James Goppert committed
127
    virtual qint64 bytesAvailable() = 0;
128 129 130 131
    virtual int write(const char * data, qint64 size) = 0;
    virtual void read(char * data, qint64 numBytes) = 0;
    virtual void flush() = 0;
    virtual void close() = 0;
132 133 134 135 136 137
    virtual void open(QIODevice::OpenModeFlag flag) = 0;
    virtual void setBaudRate(baudRateType baudrate) = 0;
    virtual void setParity(parityType parity) = 0;
    virtual void setStopBits(stopBitsType stopBits) = 0;
    virtual void setDataBits(dataBitsType dataBits) = 0;
    virtual void setTimeout(qint64 timeout) = 0;
138
    virtual void setFlow(flowType flow) = 0;
139 140 141 142
};

class SerialQextserial : public SerialInterface
{
143
    Q_OBJECT
144
private:
145
    QextSerialPort * _port;
146
signals:
147
    void aboutToClose();
148
public:
149 150 151 152
    SerialQextserial(QString porthandle, QextSerialPort::QueryMode mode) : _port(NULL) {
        _port = new QextSerialPort(porthandle, QextSerialPort::Polling);
        QObject::connect(_port,SIGNAL(aboutToClose()),this,SIGNAL(aboutToClose()));
    }
James Goppert's avatar
James Goppert committed
153 154 155 156
    ~SerialQextserial() {
        delete _port;
        _port = NULL;
    }
157 158 159
    virtual bool isOpen() {
        return _port->isOpen();
    }
160 161
    virtual bool isWritable() {
        return _port->isWritable();
162
    }
James Goppert's avatar
James Goppert committed
163
    virtual qint64 bytesAvailable() {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        return _port->bytesAvailable();
    }
    virtual int write(const char * data, qint64 size) {
        return _port->write(data,size);
    }
    virtual void read(char * data, qint64 numBytes) {
        _port->read(data,numBytes);
    }
    virtual void flush() {
        _port->flush();
    }
    virtual void close() {
        _port->close();
    }
    virtual void open(QIODevice::OpenModeFlag flag) {
        _port->open(flag);
    }
    virtual void setBaudRate(SerialInterface::baudRateType baudrate) {
        _port->setBaudRate((BaudRateType)baudrate);
    }
    virtual void setParity(SerialInterface::parityType parity) {
        _port->setParity((ParityType)parity);
    }
    virtual void setStopBits(SerialInterface::stopBitsType stopBits) {
        _port->setStopBits((StopBitsType)stopBits);
    }
    virtual void setDataBits(SerialInterface::dataBitsType dataBits) {
        _port->setDataBits((DataBitsType)dataBits);
    }
    virtual void setTimeout(qint64 timeout) {
        _port->setTimeout(timeout);
195
    }
196 197
    virtual void setFlow(SerialInterface::flowType flow) {
        // TODO implement
198 199
        _port->setFlowControl((FlowType)flow);
    }
200 201
};

202 203 204
using namespace TNX;

class SerialQserial : public SerialInterface
205
{
206 207 208 209 210 211 212 213
    Q_OBJECT
private:
    QSerialPort * _port;
    TNX::QPortSettings settings;
signals:
    void aboutToClose();
public:
    SerialQserial(QString porthandle, QIODevice::OpenModeFlag flag=QIODevice::ReadWrite)
214
        : _port(NULL) {
215
        QObject::connect(_port,SIGNAL(aboutToClose()),this,SIGNAL(aboutToClose()));
James Goppert's avatar
James Goppert committed
216 217
        settings.setBaudRate(QPortSettings::BAUDR_57600);
        settings.setStopBits(QPortSettings::STOP_1);
218 219 220 221
        settings.setDataBits(QPortSettings::DB_8);
        settings.setFlowControl(QPortSettings::FLOW_OFF);
        settings.setParity(QPortSettings::PAR_NONE);
        _port = new QSerialPort(porthandle,settings);
James Goppert's avatar
James Goppert committed
222 223 224 225 226
        _port->setCommTimeouts(QSerialPort::CtScheme_NonBlockingRead);
    }
    ~SerialQserial() {
        delete _port;
        _port = NULL;
227 228 229 230
    }
    virtual bool isOpen() {
        return _port->isOpen();
    }
231
    virtual bool isWritable() {
232 233
        _port->isWritable();
    }
James Goppert's avatar
James Goppert committed
234
    virtual qint64 bytesAvailable() {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        return _port->bytesAvailable();
    }
    virtual int write(const char * data, qint64 size) {
        return _port->write(data,size);
    }
    virtual void read(char * data, qint64 numBytes) {
        _port->read(data,numBytes);
    }
    virtual void flush() {
        _port->flushInBuffer();
        _port->flushOutBuffer();
    }
    virtual void close() {
        _port->close();
    }
    virtual void open(QIODevice::OpenModeFlag flag) {
        _port->open(flag);
James Goppert's avatar
James Goppert committed
252
        //flush();
253 254
    }
    virtual void setBaudRate(SerialInterface::baudRateType baudrate) {
255
        // TODO get the baudrate enum to map to one another
James Goppert's avatar
James Goppert committed
256
        settings.setBaudRate(QPortSettings::BAUDR_57600);
257 258
    }
    virtual void setParity(SerialInterface::parityType parity) {
259
        settings.setParity(QPortSettings::PAR_NONE);
260 261
    }
    virtual void setStopBits(SerialInterface::stopBitsType stopBits) {
262 263
        // TODO map
        settings.setStopBits(QPortSettings::STOP_1);
264 265
    }
    virtual void setDataBits(SerialInterface::dataBitsType dataBits) {
266 267
        // TODO map
        settings.setDataBits(QPortSettings::DB_8);
268 269
    }
    virtual void setTimeout(qint64 timeout) {
270
        // TODO implement
271
        //_port->setTimeout(timeout);
272
    }
273 274 275
    virtual void setFlow(SerialInterface::flowType flow) {
        // TODO map
        settings.setFlowControl(QPortSettings::FLOW_OFF);
276
    }
277 278 279
};

#endif // SERIALINTERFACE_H
280 281

// vim:ts=4:sw=4:tw=78:expandtab: