SerialLink.h 5.08 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
/*=====================================================================
pixhawk's avatar
pixhawk committed
2 3 4 5 6 7 8 9

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
pixhawk's avatar
pixhawk committed
10 11 12
    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.
pixhawk's avatar
pixhawk committed
13 14

    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17
    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.
pixhawk's avatar
pixhawk committed
18

pixhawk's avatar
pixhawk committed
19
    You should have received a copy of the GNU General Public License
pixhawk's avatar
pixhawk committed
20 21
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

pixhawk's avatar
pixhawk committed
22
======================================================================*/
23

pixhawk's avatar
pixhawk committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/**
 * @file
 *   @brief Brief Description
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#ifndef SERIALLINK_H
#define SERIALLINK_H

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QString>
39
#include "QGCConfig.h"
pixhawk's avatar
pixhawk committed
40 41
#include "SerialLinkInterface.h"

42
// We use QSerialPort::SerialPortError in a signal so we must declare it as a meta type
43
#include <QSerialPort>
44 45 46
#include <QMetaType>
Q_DECLARE_METATYPE(QSerialPort::SerialPortError)

pixhawk's avatar
pixhawk committed
47 48 49 50 51 52 53 54
/**
 * @brief The SerialLink class provides cross-platform access to serial links.
 * It takes care of the link management and provides a common API to higher
 * level communication layers. It is implemented as a wrapper class for a thread
 * that handles the serial communication. All methods have therefore to be thread-
 * safe.
 *
 */
55 56
class SerialLink : public SerialLinkInterface
{
pixhawk's avatar
pixhawk committed
57 58 59
    Q_OBJECT
    //Q_INTERFACES(SerialLinkInterface:LinkInterface)

60

pixhawk's avatar
pixhawk committed
61
public:
62
    SerialLink(QString portname = "",
63 64 65 66 67
               int baudrate=57600,
               bool flow=false,
               bool parity=false,
               int dataBits=8,
               int stopBits=1);
pixhawk's avatar
pixhawk committed
68 69
    ~SerialLink();

70
    static const int poll_interval = SERIAL_POLL_INTERVAL; ///< Polling interval, defined in QGCConfig.h
pixhawk's avatar
pixhawk committed
71

72
    /** @brief Get a list of the currently available ports */
73
    QList<QString> getCurrentPorts();
74

75 76 77
    /** @brief Check if the current port is a bootloader */
    bool isBootloader();

78
    void requestReset();
79

80
    bool isConnected() const;
pixhawk's avatar
pixhawk committed
81 82 83 84

    /**
     * @brief The port handle
     */
85
    QString getPortName() const;
pixhawk's avatar
pixhawk committed
86 87 88
    /**
     * @brief The human readable port name
     */
89 90 91 92
    QString getName() const;
    int getBaudRate() const;
    int getDataBits() const;
    int getStopBits() const;
93 94

    // ENUM values
95 96 97 98 99
    int getBaudRateType() const;
    int getFlowType() const;
    int getParityType() const;
    int getDataBitsType() const;
    int getStopBitsType() const;
pixhawk's avatar
pixhawk committed
100

101 102 103
    qint64 getConnectionSpeed() const;
    qint64 getCurrentInDataRate() const;
    qint64 getCurrentOutDataRate() const;
pixhawk's avatar
pixhawk committed
104

105 106 107
    void loadSettings();
    void writeSettings();

108 109
    void checkIfCDC();

pixhawk's avatar
pixhawk committed
110
    void run();
111
    void run2();
pixhawk's avatar
pixhawk committed
112

113
    int getId() const;
114 115 116 117 118
    
    // These are left unimplemented in order to cause linker errors which indicate incorrect usage of
    // connect/disconnect on link directly. All connect/disconnect calls should be made through LinkManager.
    bool connect(void);
    bool disconnect(void);
pixhawk's avatar
pixhawk committed
119

120 121 122
signals: //[TODO] Refactor to Linkinterface
    void updateLink(LinkInterface*);

pixhawk's avatar
pixhawk committed
123 124 125
public slots:
    bool setPortName(QString portName);
    bool setBaudRate(int rate);
126 127 128
    bool setDataBits(int dataBits);
    bool setStopBits(int stopBits);

129 130 131
    // Set string rate
    bool setBaudRateString(const QString& rate);

132
    // Set ENUM values
pixhawk's avatar
pixhawk committed
133 134 135 136 137 138
    bool setBaudRateType(int rateIndex);
    bool setFlowType(int flow);
    bool setParityType(int parity);
    bool setDataBitsType(int dataBits);
    bool setStopBitsType(int stopBits);

139
    void readBytes();
pixhawk's avatar
pixhawk committed
140 141 142 143 144 145 146 147
    /**
     * @brief Write a number of bytes to the interface.
     *
     * @param data Pointer to the data byte array
     * @param size The size of the bytes array
     **/
    void writeBytes(const char* data, qint64 length);

148
    void linkError(QSerialPort::SerialPortError error);
149

pixhawk's avatar
pixhawk committed
150
protected:
Bill Bonney's avatar
Bill Bonney committed
151 152 153 154 155 156 157 158 159 160
    quint64 m_bytesRead;
    QSerialPort* m_port;
    int m_baud;
    int m_dataBits;
    int m_flowControl;
    int m_stopBits;
    int m_parity;
    QString m_portName;
    int m_timeout;
    int m_id;
161 162
    QMutex m_dataMutex;       // Mutex for reading data from m_port
    QMutex m_writeMutex;      // Mutex for accessing the m_transmitBuffer.
163
    QString type;
164
    bool m_is_cdc;
165 166 167
    
private slots:
    void _rerouteDisconnected(void);
pixhawk's avatar
pixhawk committed
168

oberion's avatar
oberion committed
169
private:
170 171 172 173
    // From LinkInterface
    virtual bool _connect(void);
    virtual bool _disconnect(void);

174 175
    volatile bool m_stopp;
    volatile bool m_reqReset;
176 177
    QMutex m_stoppMutex; // Mutex for accessing m_stopp
    QByteArray m_transmitBuffer; // An internal buffer for receiving data from member functions and actually transmitting them via the serial port.
oberion's avatar
oberion committed
178

179
    bool hardwareConnect(QString &type);
pixhawk's avatar
pixhawk committed
180 181

signals:
182
    void aboutToCloseFlag();
pixhawk's avatar
pixhawk committed
183 184 185 186

};

#endif // SERIALLINK_H