SerialLink.h 6.28 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>
Bill Bonney's avatar
Bill Bonney committed
39
#include <qserialport.h>
pixhawk's avatar
pixhawk committed
40 41 42
#include <configuration.h>
#include "SerialLinkInterface.h"

43 44 45
// convenience type for passing errors
typedef  QSerialPort::SerialPortError SerialLinkPortError_t;

pixhawk's avatar
pixhawk committed
46 47 48 49 50 51 52 53
/**
 * @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.
 *
 */
54 55
class SerialLink : public SerialLinkInterface
{
pixhawk's avatar
pixhawk committed
56 57 58
    Q_OBJECT
    //Q_INTERFACES(SerialLinkInterface:LinkInterface)

59

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

    static const int poll_interval = SERIAL_POLL_INTERVAL; ///< Polling interval, defined in configuration.h

71 72 73 74
    static const int buffer_size = 20; ///< Specify how many data points to capture for data rate calculations.

    static const qint64 stats_timespan = 500; ///< Set the maximum age of samples to use for data calculations (ms).

75
    /** @brief Get a list of the currently available ports */
76
    QList<QString> getCurrentPorts();
77

78
    void requestReset();
79

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

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

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

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

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

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

112
    int getId() const;
pixhawk's avatar
pixhawk committed
113

114 115 116
signals: //[TODO] Refactor to Linkinterface
    void updateLink(LinkInterface*);

pixhawk's avatar
pixhawk committed
117 118 119
public slots:
    bool setPortName(QString portName);
    bool setBaudRate(int rate);
120 121 122
    bool setDataBits(int dataBits);
    bool setStopBits(int stopBits);

123 124 125
    // Set string rate
    bool setBaudRateString(const QString& rate);

126
    // Set ENUM values
pixhawk's avatar
pixhawk committed
127 128 129 130 131 132
    bool setBaudRateType(int rateIndex);
    bool setFlowType(int flow);
    bool setParityType(int parity);
    bool setDataBitsType(int dataBits);
    bool setStopBitsType(int stopBits);

133
    void readBytes();
pixhawk's avatar
pixhawk committed
134 135 136 137 138 139 140 141 142 143
    /**
     * @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);
    bool connect();
    bool disconnect();

144
    void linkError(SerialLinkPortError_t error);
145

pixhawk's avatar
pixhawk committed
146
protected:
Bill Bonney's avatar
Bill Bonney committed
147 148 149 150 151 152 153 154 155 156
    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;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    // Implement a simple circular buffer for storing when and how much data was received.
    // Used for calculating the incoming data rate. Use with *StatsBuffer() functions.
    int inDataIndex;
    quint64 inDataWriteAmounts[buffer_size]; // In bytes
    qint64 inDataWriteTimes[buffer_size]; // in ms

    // Implement a simple circular buffer for storing when and how much data was transmit.
    // Used for calculating the outgoing data rate. Use with *StatsBuffer() functions.
    int outDataIndex;
    quint64 outDataWriteAmounts[buffer_size]; // In bytes
    qint64 outDataWriteTimes[buffer_size]; // in ms

    quint64 m_connectionStartTime; // Connection start time (ms since epoch)
    mutable QMutex m_statisticsMutex; // Mutex for accessing the statistics member variables (inData*,outData*,bitsSent,bitsReceived)
    QMutex m_dataMutex;       // Mutex for reading data from m_port
    QMutex m_writeMutex;      // Mutex for accessing the m_transmitBuffer.
173
    QList<QString> m_ports;
pixhawk's avatar
pixhawk committed
174

oberion's avatar
oberion committed
175
private:
176 177 178 179 180 181 182 183 184 185 186 187 188 189
    /**
     * @brief WriteDataStatsBuffer Stores transmission times/amounts for statistics
     *
     * This function logs the send times and amounts of datas to the given circular buffers.
     * This data is used for calculating the transmission rate.
     *
     * @param bytesBuffer The buffer to write the bytes value into.
     * @param timeBuffer The buffer to write the time value into
     * @param writeIndex The write index used for this buffer.
     * @param bytes The amount of bytes transmit.
     * @param time The time (in ms) this transmission occurred.
     */
    void WriteDataStatsBuffer(quint64 *bytesBuffer, qint64 *timeBuffer, int *writeIndex, quint64 bytes, qint64 time);
    
190 191
    volatile bool m_stopp;
    volatile bool m_reqReset;
192 193
    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
194

pixhawk's avatar
pixhawk committed
195 196 197
    bool hardwareConnect();

signals:
198
    void aboutToCloseFlag();
pixhawk's avatar
pixhawk committed
199 200 201 202

};

#endif // SERIALLINK_H