Changeset - b5af7d81e1f0
[Not reviewed]
Mehmet Aslan - 7 years ago 2019-03-24 12:44:14
aaslan-mehmet@hotmail.com
basic command line functionalities added.
5 files changed with 109 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/main.cpp
Show inline comments
 
@@ -33,16 +33,19 @@ void messageHandler(QtMsgType type, cons
 
    pMainWindow->messageHandler(type, context, msg);
 
}
 

	
 
int main(int argc, char *argv[])
 
{
 
    QApplication a(argc, argv);
 
    QApplication::setApplicationName("SerialPlot");
 
    QApplication::setApplicationVersion(VERSION_STRING);
 
    MainWindow w;
 
    pMainWindow = &w;
 

	
 
    qInstallMessageHandler(messageHandler);
 
    w.handleCommandLineOptions(a);
 

	
 
    ToolTipFilter ttf;
 
    a.installEventFilter(&ttf);
 

	
 
    // log application information
 
    qDebug() << "SerialPlot" << VERSION_STRING;
src/mainwindow.cpp
Show inline comments
 
@@ -26,12 +26,14 @@
 
#include <QFile>
 
#include <QTextStream>
 
#include <QMenu>
 
#include <QDesktopServices>
 
#include <QMap>
 
#include <QtDebug>
 
#include <QCommandLineParser>
 
#include <QFileInfo>
 
#include <qwt_plot.h>
 
#include <limits.h>
 
#include <cmath>
 
#include <iostream>
 

	
 
#include <plot.h>
 
@@ -613,6 +615,58 @@ void MainWindow::onLoadSettings()
 
    if (!fileName.isNull()) // user canceled
 
    {
 
        QSettings settings(fileName, QSettings::IniFormat);
 
        loadAllSettings(&settings);
 
    }
 
}
 

	
 
void MainWindow::handleCommandLineOptions(const QCoreApplication &app)
 
{
 
    QCommandLineParser parser;
 
    parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsCompactedShortOptions);
 
    parser.setApplicationDescription("Small and simple software for plotting data from serial port in realtime.");
 
    parser.addHelpOption();
 
    parser.addVersionOption();
 

	
 
    QCommandLineOption loadOpt({"l", "load"}, "Load settings from file.", "filename");
 
    QCommandLineOption portOpt({"p", "port"}, "Set port name.", "port name");
 
    QCommandLineOption baudrateOpt({"b" ,"baudrate"}, "Set port baudrate speed.", "baudrate speed");
 
    QCommandLineOption openPortOpt({"o", "open"}, "Open serial port.");
 

	
 
    parser.addOption(loadOpt);
 
    parser.addOption(portOpt);
 
    parser.addOption(baudrateOpt);
 
    parser.addOption(openPortOpt);
 

	
 
    parser.process(app);
 

	
 
    if (parser.isSet(loadOpt))
 
    {
 
        QString fileName = parser.value(loadOpt);
 
        QFileInfo fileInfo(fileName);
 

	
 
        if (fileInfo.exists() && fileInfo.isFile())
 
        {
 
            QSettings settings(fileName, QSettings::IniFormat);
 
            loadAllSettings(&settings);
 
        }
 
        else
 
        {
 
            qWarning() << "Ini file not exist";
 
        }
 
    }
 

	
 
    if (parser.isSet(portOpt))
 
    {
 
        portControl.selectPort(parser.value(portOpt));
 
    }
 

	
 
    if (parser.isSet(baudrateOpt))
 
    {
 
        portControl.selectBaudrate(parser.value(baudrateOpt));
 
    }
 

	
 
    if (parser.isSet(openPortOpt))
 
    {
 
        portControl.openPort();
 
    }
 
}
src/mainwindow.h
Show inline comments
 
@@ -61,12 +61,13 @@ public:
 
    ~MainWindow();
 

	
 
    PlotViewSettings viewSettings() const;
 

	
 
    void messageHandler(QtMsgType type, const QMessageLogContext &context,
 
                        const QString &msg);
 
    void handleCommandLineOptions(const QCoreApplication &app);
 

	
 
private:
 
    Ui::MainWindow *ui;
 

	
 
    QDialog aboutDialog;
 
    void setupAboutDialog();
src/portcontrol.cpp
Show inline comments
 
@@ -76,25 +76,25 @@ PortControl::PortControl(QSerialPort* po
 
                     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::selectPort);
 
                     this, &PortControl::_selectPort);
 
    QObject::connect(&tbPortList,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &PortControl::selectPort);
 
                     this, &PortControl::_selectPort);
 

	
 
    // setup buttons
 
    ui->pbOpenPort->setDefaultAction(&openAction);
 
    ui->pbReloadPorts->setDefaultAction(&loadPortListAction);
 

	
 
    // setup baud rate selection widget
 
    QObject::connect(ui->cbBaudRate,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &PortControl::selectBaudRate);
 
                     this, &PortControl::_selectBaudRate);
 

	
 
    // setup parity selection buttons
 
    parityButtons.addButton(ui->rbNoParity, (int) QSerialPort::NoParity);
 
    parityButtons.addButton(ui->rbEvenParity, (int) QSerialPort::EvenParity);
 
    parityButtons.addButton(ui->rbOddParity, (int) QSerialPort::OddParity);
 

	
 
@@ -195,13 +195,13 @@ void PortControl::loadBaudRateList()
 
    for (auto baudRate : QSerialPortInfo::standardBaudRates())
 
    {
 
        ui->cbBaudRate->addItem(QString::number(baudRate));
 
    }
 
}
 

	
 
void PortControl::selectBaudRate(QString baudRate)
 
void PortControl::_selectBaudRate(QString baudRate)
 
{
 
    if (serialPort->isOpen())
 
    {
 
        if (!serialPort->setBaudRate(baudRate.toInt()))
 
        {
 
            qCritical() << "Can't set baud rate!";
 
@@ -286,13 +286,13 @@ void PortControl::togglePort()
 
        serialPort->setPortName(ui->cbPortList->currentData(PortNameRole).toString());
 

	
 
        // open port
 
        if (serialPort->open(QIODevice::ReadWrite))
 
        {
 
            // set port settings
 
            selectBaudRate(ui->cbBaudRate->currentText());
 
            _selectBaudRate(ui->cbBaudRate->currentText());
 
            selectParity((QSerialPort::Parity) parityButtons.checkedId());
 
            selectDataBits((QSerialPort::DataBits) dataBitsButtons.checkedId());
 
            selectStopBits((QSerialPort::StopBits) stopBitsButtons.checkedId());
 
            selectFlowControl((QSerialPort::FlowControl) flowControlButtons.checkedId());
 

	
 
            // set output signals
 
@@ -307,13 +307,13 @@ void PortControl::togglePort()
 
            emit portToggled(true);
 
        }
 
    }
 
    openAction.setChecked(serialPort->isOpen());
 
}
 

	
 
void PortControl::selectPort(QString portName)
 
void PortControl::_selectPort(QString portName)
 
{
 
    // portName may be coming from combobox
 
    portName = portName.split(" ")[0];
 
    // has selection actually changed
 
    if (portName != serialPort->portName())
 
    {
 
@@ -467,12 +467,51 @@ QString PortControl::currentFlowControlT
 
    else // no parity
 
    {
 
        return "none";
 
    }
 
}
 

	
 
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));
 
        ui->cbPortList->setCurrentIndex(portList.rowCount()-1);
 
        tbPortList.setCurrentIndex(portList.rowCount()-1);
 
    }
 
    else
 
    {
 
        ui->cbPortList->setCurrentIndex(portIndex);
 
        tbPortList.setCurrentIndex(portIndex);
 
    }
 
    _selectPort(portName);
 
}
 

	
 
void PortControl::selectBaudrate(QString baudRate)
 
{
 
    int baudRateIndex = ui->cbBaudRate->findText(baudRate);
 
    if (baudRateIndex < 0)
 
    {
 
        ui->cbBaudRate->setCurrentText(baudRate);
 
    }
 
    else
 
    {
 
        ui->cbBaudRate->setCurrentIndex(baudRateIndex);
 
    }
 
    _selectBaudRate(baudRate);
 
}
 

	
 
void PortControl::openPort()
 
{
 
    if (!serialPort->isOpen())
 
    {
 
        openAction.trigger();
 
    }
 
}
 

	
 
void PortControl::saveSettings(QSettings* settings)
 
{
 
    settings->beginGroup(SettingGroup_Port);
 
    settings->setValue(SG_Port_SelectedPort, selectedPortName());
 
    settings->setValue(SG_Port_BaudRate, ui->cbBaudRate->currentText());
 
    settings->setValue(SG_Port_Parity, currentParityText());
src/portcontrol.h
Show inline comments
 
@@ -44,12 +44,16 @@ public:
 
    explicit PortControl(QSerialPort* port, QWidget* parent = 0);
 
    ~PortControl();
 

	
 
    QSerialPort* serialPort;
 
    QToolBar* toolBar();
 

	
 
    void selectPort(QString portName);
 
    void selectBaudrate(QString baudRate);
 
    void openPort();
 

	
 
    /// Stores port settings into a `QSettings`
 
    void saveSettings(QSettings* settings);
 
    /// Loads port settings from a `QSettings`. If open serial port is closed.
 
    void loadSettings(QSettings* settings);
 

	
 
private:
 
@@ -77,15 +81,15 @@ private:
 
    QString currentFlowControlText();
 

	
 
private slots:
 
    void loadPortList();
 
    void loadBaudRateList();
 
    void togglePort();
 
    void selectPort(QString portName);
 
    void _selectPort(QString portName);
 

	
 
    void selectBaudRate(QString baudRate);
 
    void _selectBaudRate(QString baudRate);
 
    void selectParity(int parity); // parity must be one of QSerialPort::Parity
 
    void selectDataBits(int dataBits); // bits must be one of QSerialPort::DataBits
 
    void selectStopBits(int stopBits); // stopBits must be one of QSerialPort::StopBits
 
    void selectFlowControl(int flowControl); // flowControl must be one of QSerialPort::FlowControl
 

	
 
    void openActionTriggered(bool checked);
0 comments (0 inline, 0 general)