SerialInterface.h 5.39 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9

10

11 12 13 14 15 16 17 18 19 20 21 22
/**
 * @file
 *   @brief Brief Description
 *
 *   @author James Goppertr <james.goppert@gmail.edu>
 *
 */

#ifndef SERIALINTERFACE_H
#define SERIALINTERFACE_H

#include <QIODevice>
23
#include <QtSerialPort/QSerialPort>
24
#include <iostream>
25 26 27 28

/**
 * @brief The SerialInterface abstracts low level serial calls
 */
29 30 31
class SerialInterface : public QObject
{
    Q_OBJECT
32 33

signals:
34
    void aboutToClose();
35 36 37

public:

38 39
    enum baudRateType
    {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        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
    };

67 68
    enum dataBitsType
    {
69 70 71 72 73 74
        DATA_5,
        DATA_6,
        DATA_7,
        DATA_8
    };

75 76
    enum parityType
    {
77 78 79 80 81 82 83
        PAR_NONE,
        PAR_ODD,
        PAR_EVEN,
        PAR_MARK,               //WINDOWS ONLY
        PAR_SPACE
    };

84 85
    enum stopBitsType
    {
86 87 88 89 90
        STOP_1,
        STOP_1_5,               //WINDOWS ONLY
        STOP_2
    };

91 92
    enum flowType
    {
93 94 95 96 97 98 99 100
        FLOW_OFF,
        FLOW_HARDWARE,
        FLOW_XONXOFF
    };

    /**
     * structure to contain port settings
     */
101 102
    struct portSettings
    {
103 104 105 106 107 108 109 110 111
        baudRateType BaudRate;
        dataBitsType DataBits;
        parityType Parity;
        stopBitsType StopBits;
        flowType FlowControl;
        long timeout_Millisec;
    };

    virtual bool isOpen() = 0;
112
    virtual bool isWritable() = 0;
James Goppert's avatar
James Goppert committed
113
    virtual qint64 bytesAvailable() = 0;
114 115 116 117
    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;
118 119 120 121 122 123
    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;
124
    virtual void setFlow(flowType flow) = 0;
125 126
};

127 128 129
using namespace TNX;

class SerialQserial : public SerialInterface
130
{
131 132 133 134 135 136 137 138
    Q_OBJECT
private:
    QSerialPort * _port;
    TNX::QPortSettings settings;
signals:
    void aboutToClose();
public:
    SerialQserial(QString porthandle, QIODevice::OpenModeFlag flag=QIODevice::ReadWrite)
139
        : _port(NULL) {
140
        QObject::connect(_port,SIGNAL(aboutToClose()),this,SIGNAL(aboutToClose()));
James Goppert's avatar
James Goppert committed
141 142
        settings.setBaudRate(QPortSettings::BAUDR_57600);
        settings.setStopBits(QPortSettings::STOP_1);
143 144 145 146
        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
147 148 149 150 151
        _port->setCommTimeouts(QSerialPort::CtScheme_NonBlockingRead);
    }
    ~SerialQserial() {
        delete _port;
        _port = NULL;
152 153 154 155
    }
    virtual bool isOpen() {
        return _port->isOpen();
    }
156
    virtual bool isWritable() {
157 158
        _port->isWritable();
    }
James Goppert's avatar
James Goppert committed
159
    virtual qint64 bytesAvailable() {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        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
177
        //flush();
178 179
    }
    virtual void setBaudRate(SerialInterface::baudRateType baudrate) {
180
        // TODO get the baudrate enum to map to one another
James Goppert's avatar
James Goppert committed
181
        settings.setBaudRate(QPortSettings::BAUDR_57600);
182 183
    }
    virtual void setParity(SerialInterface::parityType parity) {
184
        settings.setParity(QPortSettings::PAR_NONE);
185 186
    }
    virtual void setStopBits(SerialInterface::stopBitsType stopBits) {
187 188
        // TODO map
        settings.setStopBits(QPortSettings::STOP_1);
189 190
    }
    virtual void setDataBits(SerialInterface::dataBitsType dataBits) {
191 192
        // TODO map
        settings.setDataBits(QPortSettings::DB_8);
193 194
    }
    virtual void setTimeout(qint64 timeout) {
195
        // TODO implement
196
        //_port->setTimeout(timeout);
197
    }
198 199 200
    virtual void setFlow(SerialInterface::flowType flow) {
        // TODO map
        settings.setFlowControl(QPortSettings::FLOW_OFF);
201
    }
202 203 204
};

#endif // SERIALINTERFACE_H
205 206

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