Files
        @ 48e24c542d4e
    
        
              Branch filter: 
        
    Location: tempo-plotter/src/portcontrol.cpp
        
            
            48e24c542d4e
            10.2 KiB
            text/x-c++hdr
        
        
    
    Change binary name
    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 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346  | /*
  Copyright © 2019 Hasan Yavuz Özderya
  This file is part of serialplot.
  serialplot 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.
  serialplot 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 serialplot.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "portcontrol.h"
#include "ui_portcontrol.h"
#include <QSerialPortInfo>
#include <QKeySequence>
#include <QLabel>
#include <QMap>
#include <QtDebug>
#include "setting_defines.h"
#include "utils.h"
#define TBPORTLIST_MINWIDTH (200)
PortControl::PortControl(QSerialPort* port, QWidget* parent) :
    QWidget(parent),
    ui(new Ui::PortControl),
    portToolBar("Port Toolbar"),
    openAction("Open", this),
    loadPortListAction("↺", this)
{
    ui->setupUi(this);
    serialPort = port;
    connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)),
            this, SLOT(onPortError(QSerialPort::SerialPortError)));
    // setup actions
    openAction.setCheckable(true);
    openAction.setShortcut(QKeySequence("F12"));
    openAction.setToolTip("Open Port");
    QObject::connect(&openAction, &QAction::triggered,
                     this, &PortControl::openActionTriggered);
    loadPortListAction.setToolTip("Reload port list");
    QObject::connect(&loadPortListAction, &QAction::triggered,
                     [this](bool checked){loadPortList();});
    // setup toolbar
    portToolBar.addWidget(&tbPortList);
    portToolBar.addAction(&loadPortListAction);
    portToolBar.addAction(&openAction);
    // setup port selection widgets
    tbPortList.setMinimumWidth(TBPORTLIST_MINWIDTH);
    tbPortList.setModel(&portList);
    ui->cbPortList->setModel(&portList);
    QObject::connect(ui->cbPortList,
                     SELECT<int>::OVERLOAD_OF(&QComboBox::activated),
                     this, &PortControl::onCbPortListActivated);
    QObject::connect(&tbPortList,
                     SELECT<int>::OVERLOAD_OF(&QComboBox::activated),
                     this, &PortControl::onTbPortListActivated);
    QObject::connect(ui->cbPortList,
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
                     this, &PortControl::selectListedPort);
    QObject::connect(&tbPortList,
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
                     this, &PortControl::selectListedPort);
    // setup buttons
    ui->pbOpenPort->setDefaultAction(&openAction);
    ui->pbReloadPorts->setDefaultAction(&loadPortListAction);
    loadPortList();
}
PortControl::~PortControl()
{
    delete ui;
}
void PortControl::loadPortList()
{
    QString currentSelection = ui->cbPortList->currentData(PortNameRole).toString();
    portList.loadPortList();
    int index = portList.indexOf(currentSelection);
    if (index >= 0)
    {
        ui->cbPortList->setCurrentIndex(index);
        tbPortList.setCurrentIndex(index);
    }
}
void PortControl::_selectBaudRate(QString baudRate)
{
    if (serialPort->isOpen())
    {
        if (!serialPort->setBaudRate(baudRate.toInt()))
        {
            qCritical() << "Can't set baud rate!";
        }
    }
}
void PortControl::togglePort()
{
    if (serialPort->isOpen())
    {
        serialPort->close();
        qDebug() << "Closed port:" << serialPort->portName();
        emit portToggled(false);
    }
    else
    {
        // we get the port name from the edit text, which may not be
        // in the portList if user hasn't pressed Enter
        // Also note that, portText may not be the `portName`
        QString portText = ui->cbPortList->currentText();
        QString portName;
        int portIndex = portList.indexOf(portText);
        if (portIndex < 0) // not in list, add to model and update the selections
        {
            portList.appendRow(new PortListItem(portText));
            ui->cbPortList->setCurrentIndex(portList.rowCount()-1);
            tbPortList.setCurrentIndex(portList.rowCount()-1);
            portName = portText;
        }
        else
        {
            // get the port name from the data field
            portName = static_cast<PortListItem*>(portList.item(portIndex))->portName();
        }
        serialPort->setPortName(ui->cbPortList->currentData(PortNameRole).toString());
        // open port
        if (serialPort->open(QIODevice::ReadWrite))
        {
            // set port settings
            _selectBaudRate("115200");
            qDebug() << "Opened port:" << serialPort->portName();
            emit portToggled(true);
        }
    }
    openAction.setChecked(serialPort->isOpen());
}
void PortControl::selectListedPort(QString portName)
{
    // portName may be coming from combobox
    portName = portName.split(" ")[0];
    QSerialPortInfo portInfo(portName);
    if (portInfo.isNull())
    {
        qWarning() << "Device doesn't exists:" << portName;
    }
    // has selection actually changed
    if (portName != serialPort->portName())
    {
        // if another port is already open, close it by toggling
        if (serialPort->isOpen())
        {
            togglePort();
            // open new selection by toggling
            togglePort();
        }
    }
}
QString PortControl::selectedPortName()
{
    QString portText = ui->cbPortList->currentText();
    int portIndex = portList.indexOf(portText);
    if (portIndex < 0) // not in the list yet
    {
        // return the displayed name as port name
        return portText;
    }
    else
    {
        // get the port name from the 'port list'
        return static_cast<PortListItem*>(portList.item(portIndex))->portName();
    }
}
QToolBar* PortControl::toolBar()
{
    return &portToolBar;
}
void PortControl::openActionTriggered(bool checked)
{
    togglePort();
}
void PortControl::onCbPortListActivated(int index)
{
    tbPortList.setCurrentIndex(index);
}
void PortControl::onTbPortListActivated(int index)
{
    ui->cbPortList->setCurrentIndex(index);
}
void PortControl::onPortError(QSerialPort::SerialPortError error)
{
#ifdef Q_OS_UNIX
    // For suppressing "Invalid argument" errors that happens with pseudo terminals
    auto isPtsInvalidArgErr = [this] () -> bool {
        return serialPort->portName().contains("pts/") && serialPort->errorString().contains("Invalid argument");
    };
#endif
    switch(error)
    {
        case QSerialPort::NoError :
            break;
        case QSerialPort::ResourceError :
            qWarning() << "Port error: resource unavaliable; most likely device removed.";
            if (serialPort->isOpen())
            {
                qWarning() << "Closing port on resource error: " << serialPort->portName();
                togglePort();
            }
            loadPortList();
            break;
        case QSerialPort::DeviceNotFoundError:
            qCritical() << "Device doesn't exists: " << serialPort->portName();
            break;
        case QSerialPort::PermissionError:
            qCritical() << "Permission denied. Either you don't have \
required privileges or device is already opened by another process.";
            break;
        case QSerialPort::OpenError:
            qWarning() << "Device is already opened!";
            break;
        case QSerialPort::NotOpenError:
            qCritical() << "Device is not open!";
            break;
        case QSerialPort::ParityError:
            qCritical() << "Parity error detected.";
            break;
        case QSerialPort::FramingError:
            qCritical() << "Framing error detected.";
            break;
        case QSerialPort::BreakConditionError:
            qCritical() << "Break condition is detected.";
            break;
        case QSerialPort::WriteError:
            qCritical() << "An error occurred while writing data.";
            break;
        case QSerialPort::ReadError:
            qCritical() << "An error occurred while reading data.";
            break;
        case QSerialPort::UnsupportedOperationError:
#ifdef Q_OS_UNIX
            // Qt 5.5 gives "Invalid argument" with 'UnsupportedOperationError'
            if (isPtsInvalidArgErr())
                break;
#endif
            qCritical() << "Operation is not supported.";
            break;
        case QSerialPort::TimeoutError:
            qCritical() << "A timeout error occurred.";
            break;
        case QSerialPort::UnknownError:
#ifdef Q_OS_UNIX
            // Qt 5.2 gives "Invalid argument" with 'UnknownError'
            if (isPtsInvalidArgErr())
                break;
#endif
            qCritical() << "Unknown error! Error: " << serialPort->errorString();
            break;
        default:
            qCritical() << "Unhandled port error: " << error;
            break;
    }
}
void PortControl::selectPort(QString portName)
{
    int portIndex = portList.indexOfName(portName);
    if (portIndex < 0) // not in list, add to model and update the selections
    {
        portList.appendRow(new PortListItem(portName));
        portIndex = portList.rowCount()-1;
    }
    ui->cbPortList->setCurrentIndex(portIndex);
    tbPortList.setCurrentIndex(portIndex);
    selectListedPort(portName);
}
void PortControl::openPort()
{
    if (!serialPort->isOpen())
    {
        openAction.trigger();
    }
}
void PortControl::saveSettings(QSettings* settings)
{
    settings->beginGroup(SettingGroup_Port);
    settings->setValue(SG_Port_SelectedPort, selectedPortName());
    settings->endGroup();
}
void PortControl::loadSettings(QSettings* settings)
{
    // make sure the port is closed
    if (serialPort->isOpen()) togglePort();
    settings->beginGroup(SettingGroup_Port);
    // set port name if it exists in the current list otherwise ignore
    QString portName = settings->value(SG_Port_SelectedPort, QString()).toString();
    if (!portName.isEmpty())
    {
        int index = portList.indexOfName(portName);
        if (index > -1) ui->cbPortList->setCurrentIndex(index);
    }
    settings->endGroup();
}
 |