SerialLink.h 4.7 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 "SerialInterface.h"
pixhawk's avatar
pixhawk committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <configuration.h>
#include "SerialLinkInterface.h"
#ifdef _WIN32
#include "windows.h"
#endif


/**
 * @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 60
    Q_OBJECT
    //Q_INTERFACES(SerialLinkInterface:LinkInterface)

public:
61 62 63 64 65 66
    SerialLink(QString portname = "",
               SerialInterface::baudRateType baudrate=SerialInterface::BAUD57600,
               SerialInterface::flowType flow=SerialInterface::FLOW_OFF,
               SerialInterface::parityType parity=SerialInterface::PAR_NONE,
               SerialInterface::dataBitsType dataBits=SerialInterface::DATA_8,
               SerialInterface::stopBitsType stopBits=SerialInterface::STOP_1);
pixhawk's avatar
pixhawk committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    ~SerialLink();

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

    bool isConnected();
    qint64 bytesAvailable();

    /**
     * @brief The port handle
     */
    QString getPortName();
    /**
     * @brief The human readable port name
     */
    QString getName();
    int getBaudRate();
83 84 85 86
    int getDataBits();
    int getStopBits();

    // ENUM values
pixhawk's avatar
pixhawk committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    int getBaudRateType();
    int getFlowType();
    int getParityType();
    int getDataBitsType();
    int getStopBitsType();

    /* Extensive statistics for scientific purposes */
    qint64 getNominalDataRate();
    qint64 getTotalUpstream();
    qint64 getCurrentUpstream();
    qint64 getMaxUpstream();
    qint64 getTotalDownstream();
    qint64 getCurrentDownstream();
    qint64 getMaxDownstream();
    qint64 getBitsSent();
    qint64 getBitsReceived();

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

pixhawk's avatar
pixhawk committed
107 108 109 110 111 112 113 114 115
    void run();

    int getLinkQuality();
    bool isFullDuplex();
    int getId();

public slots:
    bool setPortName(QString portName);
    bool setBaudRate(int rate);
116 117 118 119
    bool setDataBits(int dataBits);
    bool setStopBits(int stopBits);

    // Set ENUM values
pixhawk's avatar
pixhawk committed
120 121 122 123 124 125
    bool setBaudRateType(int rateIndex);
    bool setFlowType(int flow);
    bool setParityType(int parity);
    bool setDataBitsType(int dataBits);
    bool setStopBitsType(int stopBits);

126
    void readBytes();
pixhawk's avatar
pixhawk committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140
    /**
     * @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();

protected slots:
    void checkForBytes();

protected:
141
    SerialInterface * port;
pixhawk's avatar
pixhawk committed
142 143 144 145 146 147
#ifdef _WIN32
    HANDLE winPort;
    DCB winPortSettings;
#endif
    QString porthandle;
    QString name;
148 149 150 151 152
    SerialInterface::baudRateType baudrate;
    SerialInterface::flowType flow;
    SerialInterface::parityType parity;
    SerialInterface::dataBitsType dataBits;
    SerialInterface::stopBitsType stopBits;
pixhawk's avatar
pixhawk committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    int timeout;
    int id;

    quint64 bitsSentTotal;
    quint64 bitsSentShortTerm;
    quint64 bitsSentCurrent;
    quint64 bitsSentMax;
    quint64 bitsReceivedTotal;
    quint64 bitsReceivedShortTerm;
    quint64 bitsReceivedCurrent;
    quint64 bitsReceivedMax;
    quint64 connectionStartTime;
    QMutex statisticsMutex;
    QMutex dataMutex;

    void setName(QString name);
    bool hardwareConnect();

signals:
172
    void aboutToCloseFlag();
pixhawk's avatar
pixhawk committed
173 174 175 176

};

#endif // SERIALLINK_H