terminalconsole.cpp 9.6 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*=====================================================================

APM_PLANNER Open Source Ground Control Station

(c) 2013, Bill Bonney <billbonney@communistech.com>

This file is part of the APM_PLANNER project

    APM_PLANNER 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.

    APM_PLANNER 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 APM_PLANNER. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Terminal Console display View.
 *
 *   @author Bill Bonney <billbonney@communistech.com>
 *
 * Influenced from Qt examples by :-
 * Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
 * Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
 *
 */

#include "SerialSettingsDialog.h"
#include "terminalconsole.h"
#include "ui_terminalconsole.h"
#include "console.h"
40
#include "QGCConfig.h"
41 42 43 44 45 46 47

#include <QDebug>
#include <QSettings>
#include <QStatusBar>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QComboBox>
48 49
#include <QSerialPort>
#include <QSerialPortInfo>
50 51 52

TerminalConsole::TerminalConsole(QWidget *parent) :
    QWidget(parent),
53 54
    ui(new Ui::TerminalConsole),
    m_consoleMode(APM)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
{
    ui->setupUi(this);

    // create the cosole and add it to the centralwidget
    m_console = new Console;
    m_console->setEnabled(false);

    m_statusBar = new QStatusBar;

    QLayout* layout = ui->terminalGroupBox->layout();
    layout->addWidget(m_console);
    layout->addWidget(m_statusBar);

    m_serial = new QSerialPort(this);
    m_settingsDialog = new SettingsDialog;

    ui->connectButton->setEnabled(true);
    ui->disconnectButton->setEnabled(false);
    ui->settingsButton->setEnabled(true);

75

76 77 78 79 80 81 82 83 84 85 86
    addBaudComboBoxConfig();
    fillPortsInfo(*ui->linkComboBox);

    loadSettings();

    if (m_settings.name == "") {
        setLink(ui->linkComboBox->currentIndex());
    } else {
        ui->linkComboBox->setCurrentIndex(0);
    }

87
    addConsoleModesComboBoxConfig();
88 89 90 91 92

    initConnections();
}

void TerminalConsole::addBaudComboBoxConfig()
93 94 95 96 97 98
{
    ui->consoleModeBox->addItem(QLatin1String("APM"), APM);
    ui->consoleModeBox->addItem(QLatin1String("PX4"), PX4);
}

void TerminalConsole::addConsoleModesComboBoxConfig()
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
{
    ui->baudComboBox->addItem(QLatin1String("115200"), QSerialPort::Baud115200);
    ui->baudComboBox->addItem(QLatin1String("57600"), QSerialPort::Baud57600);
    ui->baudComboBox->addItem(QLatin1String("38400"), QSerialPort::Baud38400);
    ui->baudComboBox->addItem(QLatin1String("19200"), QSerialPort::Baud19200);
    ui->baudComboBox->addItem(QLatin1String("19200"), QSerialPort::Baud19200);
    ui->baudComboBox->addItem(QLatin1String("9600"), QSerialPort::Baud9600);
}

void TerminalConsole::fillPortsInfo(QComboBox &comboxBox)
{
    comboxBox.clear();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        QStringList list;
        list << info.portName()
             << info.description()
             << info.manufacturer()
             << info.systemLocation()
             << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
             << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());

        comboxBox.insertItem(0,list.first(), list);
        qDebug() << "Inserting " << list.first();
    }
}

TerminalConsole::~TerminalConsole()
{
    delete m_console;
    delete m_statusBar;
    delete m_settingsDialog;
    delete ui;
}

void TerminalConsole::openSerialPort()
{
    openSerialPort(m_settings);
}

void TerminalConsole::openSerialPort(const SerialSettings &settings)
{
    m_serial->setPortName(settings.name);
    if (m_serial->open(QIODevice::ReadWrite)) {
        if (m_serial->setBaudRate(settings.baudRate)
                && m_serial->setDataBits(settings.dataBits)
                && m_serial->setParity(settings.parity)
                && m_serial->setStopBits(settings.stopBits)
                && m_serial->setFlowControl(settings.flowControl)) {

            m_console->setEnabled(true);
            m_console->setLocalEchoEnabled(false);
            ui->connectButton->setEnabled(false);
            ui->disconnectButton->setEnabled(true);
            ui->settingsButton->setEnabled(false);
            m_statusBar->showMessage(tr("Connected to %1 : baud %2z")
                                       .arg(settings.name).arg(QString::number(settings.baudRate)));
            qDebug() << "Open Terminal Console Serial Port";
156
            writeSettings(); // Save last successful connection
157

158 159
            sendResetCommand();

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        } else {
            m_serial->close();
            QMessageBox::critical(this, tr("Error"), m_serial->errorString());

            m_statusBar->showMessage(tr("Open error"));
        }
    } else {
        QMessageBox::critical(this, tr("Error"), m_serial->errorString());

        m_statusBar->showMessage(tr("Configure error"));
    }
}

void TerminalConsole::closeSerialPort()
{
    m_serial->close();
    m_console->setEnabled(false);
    ui->connectButton->setEnabled(true);
    ui->disconnectButton->setEnabled(false);
    ui->settingsButton->setEnabled(true);
    m_statusBar->showMessage(tr("Disconnected"));
}

183 184 185 186 187 188 189 190 191
void TerminalConsole::sendResetCommand()
{
    if (m_serial->isOpen()) {
        m_serial->setDataTerminalReady(true);
        m_serial->waitForBytesWritten(250);
        m_serial->setDataTerminalReady(false);
    }
}

192 193 194 195 196 197 198 199 200 201 202
void TerminalConsole::writeData(const QByteArray &data)
{
//    qDebug() << "writeData:" << data;
    m_serial->write(data);
}

void TerminalConsole::readData()
{
    QByteArray data = m_serial->readAll();
//    qDebug() << "readData:" << data;
    m_console->putData(data);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

    switch(m_consoleMode)
    {
    case APM: // APM
        // On reset, send the break sequence and display help
        if (data.contains("ENTER 3")) {
            m_serial->write("\r\r\r");
            m_serial->waitForBytesWritten(10);
            m_serial->write("HELP\r");
        }
        break;
    case PX4:
        // Do nothing
    default:
        qDebug() << "Mode not yet implemented";
    }

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
}

void TerminalConsole::handleError(QSerialPort::SerialPortError error)
{
    if (error == QSerialPort::ResourceError) {
        QMessageBox::critical(this, tr("Critical Error"), m_serial->errorString());
        closeSerialPort();
    }
}

void TerminalConsole::initConnections()
{
    // Ui Connections
    connect(ui->connectButton, SIGNAL(released()), this, SLOT(openSerialPort()));
    connect(ui->disconnectButton, SIGNAL(released()), this, SLOT(closeSerialPort()));
    connect(ui->settingsButton, SIGNAL(released()), m_settingsDialog, SLOT(show()));
    connect(ui->clearButton, SIGNAL(released()), m_console, SLOT(clear()));

    connect(ui->baudComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setBaudRate(int)));
    connect(ui->linkComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setLink(int)));
//    connect(ui->linkComboBox, SIGNAL()), this, SLOT(setLink(int)));

    // Serial Port Connections
    connect(m_serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
            SLOT(handleError(QSerialPort::SerialPortError)));

    connect(m_serial, SIGNAL(readyRead()), this, SLOT(readData()));
    connect(m_console, SIGNAL(getData(QByteArray)), this, SLOT(writeData(QByteArray)));
}

void TerminalConsole::setBaudRate(int index)
{
    m_settings.baudRate = static_cast<QSerialPort::BaudRate>(
                ui->baudComboBox->itemData(index).toInt());
    qDebug() << "Changed Baud to:" << m_settings.baudRate;

}

void TerminalConsole::setLink(int index)
{
260
    Q_UNUSED(index);
261 262 263 264 265 266 267 268
    m_settings.name = ui->linkComboBox->currentText();
    qDebug() << "Changed Link to:" << m_settings.name;

}

void TerminalConsole::loadSettings()
{
    // Load defaults from settings
269
    QSettings settings(QGC::ORG_NAME, QGC::APPNAME);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    settings.sync();
    if (settings.contains("TERMINALCONSOLE_COMM_PORT"))
    {
        m_settings.name = settings.value("TERMINALCONSOLE_COMM_PORT").toString();
        m_settings.baudRate = settings.value("TERMINALCONSOLE_COMM_BAUD").toInt();
        m_settings.parity = static_cast<QSerialPort::Parity>
                (settings.value("TERMINALCONSOLE_COMM_PARITY").toInt());
        m_settings.stopBits = static_cast<QSerialPort::StopBits>
                (settings.value("TERMINALCONSOLE_COMM_STOPBITS").toInt());
        m_settings.dataBits = static_cast<QSerialPort::DataBits>
                (settings.value("TERMINALCONSOLE_COMM_DATABITS").toInt());
        m_settings.flowControl = static_cast<QSerialPort::FlowControl>
                (settings.value("TERMINALCONSOLE_COMM_FLOW_CONTROL").toInt());
    } else {
        // init the structure
    }
}

void TerminalConsole::writeSettings()
{
    // Store settings
291
    QSettings settings(QGC::ORG_NAME, QGC::APPNAME);
292 293 294 295 296 297 298 299 300 301 302
    settings.setValue("TERMINALCONSOLE_COMM_PORT", m_settings.name);
    settings.setValue("TERMINALCONSOLE_COMM_BAUD", m_settings.baudRate);
    settings.setValue("TERMINALCONSOLE_COMM_PARITY", m_settings.parity);
    settings.setValue("TERMINALCONSOLE_COMM_STOPBITS", m_settings.stopBits);
    settings.setValue("TERMINALCONSOLE_COMM_DATABITS", m_settings.dataBits);
    settings.setValue("TERMINALCONSOLE_COMM_FLOW_CONTROL", m_settings.flowControl);
    settings.sync();
}