Changeset - 6a311d722818
[Not reviewed]
default
0 5 3
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-05-23 20:55:10
hy@ozderya.net
moved port settings tab to a separate class, PortControl
8 files changed with 661 insertions and 528 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -49,10 +49,11 @@ endif (QWT_USE_STATIC)
 
include_directories(${QWT_INCLUDE_DIR})
 

	
 
# wrap UI files
 
qt5_wrap_ui(UI_FILES mainwindow.ui about_dialog.ui)
 
qt5_wrap_ui(UI_FILES mainwindow.ui portcontrol.ui about_dialog.ui)
 

	
 
# Tell CMake to create the helloworld executable
 
add_executable(serialplot main.cpp mainwindow.cpp customcheckablebutton.cpp ${UI_FILES})
 
add_executable(serialplot main.cpp mainwindow.cpp portcontrol.cpp
 
  customcheckablebutton.cpp ${UI_FILES})
 

	
 
# Use the Widgets module from Qt 5.
 
target_link_libraries(serialplot ${QWT_LIBRARY})
mainwindow.cpp
Show inline comments
 
@@ -19,7 +19,6 @@
 

	
 
#include "mainwindow.h"
 
#include "ui_mainwindow.h"
 
#include <QSerialPortInfo>
 
#include <QByteArray>
 
#include <QApplication>
 
#include <QtDebug>
 
@@ -31,9 +30,13 @@
 

	
 
MainWindow::MainWindow(QWidget *parent) :
 
    QMainWindow(parent),
 
    ui(new Ui::MainWindow)
 
    ui(new Ui::MainWindow),
 
    portControl(&serialPort)
 
{
 
    ui->setupUi(this);
 
    ui->tabWidget->insertTab(0, &portControl, "Port");
 
    ui->tabWidget->setCurrentIndex(0);
 

	
 
    setupAboutDialog();
 

	
 
    // init UI signals
 
@@ -42,23 +45,11 @@ MainWindow::MainWindow(QWidget *parent) 
 
    QObject::connect(ui->actionHelpAbout, &QAction::triggered,
 
              &aboutDialog, &QWidget::show);
 

	
 
    // port tab signals
 
    QObject::connect(ui->pbReloadPorts, &QPushButton::clicked,
 
                     this, &MainWindow::loadPortList);
 

	
 
    QObject::connect(ui->pbOpenPort, &QPushButton::clicked,
 
                     this, &MainWindow::togglePort);
 

	
 
    QObject::connect(this, &MainWindow::portToggled,
 
    QObject::connect(&portControl, &PortControl::portToggled,
 
                     this, &MainWindow::onPortToggled);
 

	
 
    QObject::connect(ui->cbPortList,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &MainWindow::selectPort);
 

	
 
    QObject::connect(ui->cbBaudRate,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &MainWindow::selectBaudRate);
 
    QObject::connect(&portControl, &PortControl::skipByteRequested,
 
                     this, &MainWindow::skipByte);
 

	
 
    QObject::connect(ui->spNumOfSamples, SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged),
 
                     this, &MainWindow::onNumOfSamplesChanged);
 
@@ -93,58 +84,10 @@ MainWindow::MainWindow(QWidget *parent) 
 
    QObject::connect(&numberFormatButtons, SIGNAL(buttonToggled(int, bool)),
 
                     this, SLOT(onNumberFormatButtonToggled(int, bool)));
 

	
 
    // 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);
 

	
 
    QObject::connect(&parityButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &MainWindow::selectParity);
 

	
 
    // setup data bits selection buttons
 
    dataBitsButtons.addButton(ui->rb8Bits, (int) QSerialPort::Data8);
 
    dataBitsButtons.addButton(ui->rb7Bits, (int) QSerialPort::Data7);
 
    dataBitsButtons.addButton(ui->rb6Bits, (int) QSerialPort::Data6);
 
    dataBitsButtons.addButton(ui->rb5Bits, (int) QSerialPort::Data5);
 

	
 
    QObject::connect(&dataBitsButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &MainWindow::selectDataBits);
 

	
 
    // setup stop bits selection buttons
 
    stopBitsButtons.addButton(ui->rb1StopBit, (int) QSerialPort::OneStop);
 
    stopBitsButtons.addButton(ui->rb2StopBit, (int) QSerialPort::TwoStop);
 

	
 
    QObject::connect(&stopBitsButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &MainWindow::selectStopBits);
 

	
 
    // setup flow control selection buttons
 
    flowControlButtons.addButton(ui->rbNoFlowControl,
 
                                 (int) QSerialPort::NoFlowControl);
 
    flowControlButtons.addButton(ui->rbHardwareControl,
 
                                 (int) QSerialPort::HardwareControl);
 
    flowControlButtons.addButton(ui->rbSoftwareControl,
 
                                 (int) QSerialPort::SoftwareControl);
 

	
 
    QObject::connect(&flowControlButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &MainWindow::selectFlowControl);
 

	
 
    // init port signals
 
    QObject::connect(&(this->serialPort), SIGNAL(error(QSerialPort::SerialPortError)),
 
                     this, SLOT(onPortError(QSerialPort::SerialPortError)));
 

	
 
    // init skip byte button
 
    skipByteRequested = false;
 
    QObject::connect(ui->pbSkipByte, &QPushButton::clicked,
 
                     this, &MainWindow::skipByte);
 

	
 
    loadPortList();
 
    loadBaudRateList();
 
    ui->cbBaudRate->setCurrentIndex(ui->cbBaudRate->findText("9600"));
 

	
 
    // set limits for axis limit boxes
 
    ui->spYmin->setRange((-1) * std::numeric_limits<double>::max(),
 
                         std::numeric_limits<double>::max());
 
@@ -220,150 +163,8 @@ void MainWindow::setupAboutDialog()
 
    uiAboutDialog.lbAbout->setText(aboutText);
 
}
 

	
 
void MainWindow::loadPortList()
 
{
 
    QString currentSelection = ui->cbPortList->currentText();
 

	
 
    ui->cbPortList->clear();
 

	
 
    for (auto port : QSerialPortInfo::availablePorts())
 
    {
 
        ui->cbPortList->addItem(port.portName());
 
    }
 

	
 
    // find current selection in the new list, maybe it doesn't exist anymore?
 
    int currentSelectionIndex = ui->cbPortList->findText(currentSelection);
 
    if (currentSelectionIndex >= 0)
 
    {
 
        ui->cbPortList->setCurrentIndex(currentSelectionIndex);
 
    }
 
    else // our port doesn't exist anymore, close port if it's open
 
    {
 
        if (serialPort.isOpen()) togglePort();
 
    }
 
}
 

	
 
void MainWindow::loadBaudRateList()
 
{
 
    ui->cbBaudRate->clear();
 

	
 
    for (auto baudRate : QSerialPortInfo::standardBaudRates())
 
    {
 
        ui->cbBaudRate->addItem(QString::number(baudRate));
 
    }
 
}
 

	
 
void MainWindow::togglePort()
 
{
 
    if (serialPort.isOpen())
 
    {
 
        serialPort.close();
 
        qDebug() << "Port closed, " << serialPort.portName();
 
        emit portToggled(false);
 
    }
 
    else
 
    {
 
        serialPort.setPortName(ui->cbPortList->currentText());
 

	
 
        // open port
 
        if (serialPort.open(QIODevice::ReadWrite))
 
        {
 
            qDebug() << "Port opened, " << serialPort.portName();
 
            emit portToggled(true);
 

	
 
            // set baud rate
 
            if (!serialPort.setBaudRate(ui->cbBaudRate->currentText().toInt()))
 
            {
 
                qDebug() << "Set baud rate failed during port opening: "
 
                         << serialPort.error();
 
            }
 
        }
 
        else
 
        {
 
            qDebug() << "Port open error: " << serialPort.error();
 
        }
 
    }
 
}
 

	
 
void MainWindow::selectPort(QString 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();
 
        }
 
    }
 
}
 

	
 
void MainWindow::selectBaudRate(QString baudRate)
 
{
 
    if (serialPort.isOpen())
 
    {
 
        if (!serialPort.setBaudRate(baudRate.toInt()))
 
        {
 
            qDebug() << "Set baud rate failed during select: "
 
                     << serialPort.error();
 
        }
 
        else
 
        {
 
            qDebug() << "Baud rate changed: " << serialPort.baudRate();
 
        }
 
    }
 
}
 

	
 
void MainWindow::selectParity(int parity)
 
{
 
    if (serialPort.isOpen())
 
    {
 
        if(!serialPort.setParity((QSerialPort::Parity) parity))
 
        {
 
            qDebug() << "Set parity failed: " << serialPort.error();
 
        }
 
    }
 
}
 

	
 
void MainWindow::selectDataBits(int dataBits)
 
{
 
    if (serialPort.isOpen())
 
    {
 
        if(!serialPort.setDataBits((QSerialPort::DataBits) dataBits))
 
        {
 
            qDebug() << "Set data bits failed: " << serialPort.error();
 
        }
 
    }
 
}
 

	
 
void MainWindow::selectStopBits(int stopBits)
 
{
 
    if (serialPort.isOpen())
 
    {
 
        if(!serialPort.setStopBits((QSerialPort::StopBits) stopBits))
 
        {
 
            qDebug() << "Set stop bits failed: " << serialPort.error();
 
        }
 
    }
 
}
 

	
 
void MainWindow::selectFlowControl(int flowControl)
 
{
 
    if (serialPort.isOpen())
 
    {
 
        if(!serialPort.setFlowControl((QSerialPort::FlowControl) flowControl))
 
        {
 
            qDebug() << "Set flow control failed: " << serialPort.error();
 
        }
 
    }
 
}
 

	
 
void MainWindow::onPortToggled(bool open)
 
{
 
    ui->pbOpenPort->setChecked(open);
 
    // make sure demo mode is disabled
 
    if (open && isDemoRunning()) enableDemo(false);
 
    ui->actionDemoMode->setEnabled(!open);
 
@@ -459,9 +260,9 @@ void MainWindow::onPortError(QSerialPort
 
            if (serialPort.isOpen())
 
            {
 
                qDebug() << "Closing port on resource error: " << serialPort.portName();
 
                togglePort();
 
                portControl.togglePort();
 
            }
 
            loadPortList();
 
            portControl.loadPortList();
 
            break;
 
        default:
 
            qDebug() << "Unhandled port error: " << error;
 
@@ -659,14 +460,14 @@ void MainWindow::selectNumberFormat(Numb
 
        QObject::disconnect(&(this->serialPort), &QSerialPort::readyRead, 0, 0);
 
        QObject::connect(&(this->serialPort), &QSerialPort::readyRead,
 
                         this, &MainWindow::onDataReadyASCII);
 
        ui->pbSkipByte->setDisabled(true);
 
        portControl.enableSkipByte();
 
    }
 
    else
 
    {
 
        QObject::disconnect(&(this->serialPort), &QSerialPort::readyRead, 0, 0);
 
        QObject::connect(&(this->serialPort), &QSerialPort::readyRead,
 
                         this, &MainWindow::onDataReady);
 
        ui->pbSkipByte->setEnabled(true);
 
        portControl.enableSkipByte(false);
 
    }
 
}
 

	
mainwindow.h
Show inline comments
 
@@ -31,6 +31,7 @@
 
#include <QColor>
 
#include <qwt_plot_curve.h>
 

	
 
#include "portcontrol.h"
 
#include "ui_about_dialog.h"
 

	
 
namespace Ui {
 
@@ -60,13 +61,10 @@ private:
 

	
 
    Ui::MainWindow *ui;
 
    QButtonGroup numberFormatButtons;
 
    QButtonGroup parityButtons;
 
    QButtonGroup dataBitsButtons;
 
    QButtonGroup stopBitsButtons;
 
    QButtonGroup flowControlButtons;
 

	
 
    QDialog aboutDialog;
 
    void setupAboutDialog();
 
    PortControl portControl;
 

	
 
    QSerialPort serialPort;
 

	
 
@@ -98,17 +96,7 @@ private:
 
    QColor makeColor(unsigned int channelIndex);
 

	
 
private slots:
 
    void loadPortList();
 
    void loadBaudRateList();
 
    void togglePort();
 
    void selectPort(QString portName);
 
    void onPortToggled(bool open);
 
    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 onDataReady();      // used with binary number formats
 
    void onDataReadyASCII(); // used with ASCII number format
 
    void onPortError(QSerialPort::SerialPortError error);
 
@@ -127,9 +115,6 @@ private slots:
 

	
 
    void demoTimerTimeout();
 
    void enableDemo(bool enabled);
 

	
 
signals:
 
    void portToggled(bool open);
 
};
 

	
 
#endif // MAINWINDOW_H
mainwindow.ui
Show inline comments
 
@@ -51,296 +51,6 @@
 
      <property name="movable">
 
       <bool>false</bool>
 
      </property>
 
      <widget class="QWidget" name="tabPort">
 
       <attribute name="title">
 
        <string>Port</string>
 
       </attribute>
 
       <layout class="QHBoxLayout" name="horizontalLayout">
 
        <item>
 
         <layout class="QVBoxLayout" name="verticalLayout_3">
 
          <item>
 
           <layout class="QGridLayout" name="gridLayout">
 
            <item row="0" column="0">
 
             <widget class="QLabel" name="label">
 
              <property name="text">
 
               <string>Port:</string>
 
              </property>
 
             </widget>
 
            </item>
 
            <item row="0" column="1">
 
             <widget class="QComboBox" name="cbPortList">
 
              <property name="sizePolicy">
 
               <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
 
                <horstretch>0</horstretch>
 
                <verstretch>0</verstretch>
 
               </sizepolicy>
 
              </property>
 
              <property name="editable">
 
               <bool>true</bool>
 
              </property>
 
             </widget>
 
            </item>
 
            <item row="1" column="1">
 
             <widget class="QComboBox" name="cbBaudRate">
 
              <property name="inputMethodHints">
 
               <set>Qt::ImhPreferNumbers</set>
 
              </property>
 
              <property name="editable">
 
               <bool>true</bool>
 
              </property>
 
             </widget>
 
            </item>
 
            <item row="1" column="0">
 
             <widget class="QLabel" name="label_2">
 
              <property name="text">
 
               <string>Baud Rate:</string>
 
              </property>
 
             </widget>
 
            </item>
 
            <item row="0" column="2">
 
             <widget class="QPushButton" name="pbReloadPorts">
 
              <property name="sizePolicy">
 
               <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
 
                <horstretch>0</horstretch>
 
                <verstretch>0</verstretch>
 
               </sizepolicy>
 
              </property>
 
              <property name="maximumSize">
 
               <size>
 
                <width>30</width>
 
                <height>16777215</height>
 
               </size>
 
              </property>
 
              <property name="text">
 
               <string>↺</string>
 
              </property>
 
             </widget>
 
            </item>
 
           </layout>
 
          </item>
 
          <item>
 
           <layout class="QHBoxLayout" name="horizontalLayout_2">
 
            <item>
 
             <widget class="QFrame" name="frame">
 
              <property name="frameShape">
 
               <enum>QFrame::NoFrame</enum>
 
              </property>
 
              <layout class="QVBoxLayout" name="verticalLayout_5">
 
               <item>
 
                <widget class="QRadioButton" name="rbNoParity">
 
                 <property name="text">
 
                  <string>No Parity</string>
 
                 </property>
 
                 <property name="checked">
 
                  <bool>true</bool>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <widget class="QRadioButton" name="rbOddParity">
 
                 <property name="text">
 
                  <string>Odd Parity</string>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <widget class="QRadioButton" name="rbEvenParity">
 
                 <property name="text">
 
                  <string>Even Parity</string>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <spacer name="verticalSpacer_2">
 
                 <property name="orientation">
 
                  <enum>Qt::Vertical</enum>
 
                 </property>
 
                 <property name="sizeHint" stdset="0">
 
                  <size>
 
                   <width>20</width>
 
                   <height>2</height>
 
                  </size>
 
                 </property>
 
                </spacer>
 
               </item>
 
              </layout>
 
             </widget>
 
            </item>
 
            <item>
 
             <widget class="QFrame" name="frame_3">
 
              <layout class="QVBoxLayout" name="verticalLayout_7">
 
               <item>
 
                <widget class="QRadioButton" name="rb8Bits">
 
                 <property name="text">
 
                  <string>8 bits</string>
 
                 </property>
 
                 <property name="checked">
 
                  <bool>true</bool>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <widget class="QRadioButton" name="rb7Bits">
 
                 <property name="text">
 
                  <string>7 bits</string>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <widget class="QRadioButton" name="rb6Bits">
 
                 <property name="text">
 
                  <string>6 bits</string>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <widget class="QRadioButton" name="rb5Bits">
 
                 <property name="text">
 
                  <string>5 bits</string>
 
                 </property>
 
                </widget>
 
               </item>
 
              </layout>
 
             </widget>
 
            </item>
 
            <item>
 
             <widget class="QFrame" name="frame_4">
 
              <layout class="QVBoxLayout" name="verticalLayout">
 
               <item>
 
                <widget class="QRadioButton" name="rb1StopBit">
 
                 <property name="text">
 
                  <string>1 Stop Bit</string>
 
                 </property>
 
                 <property name="checked">
 
                  <bool>true</bool>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <widget class="QRadioButton" name="rb2StopBit">
 
                 <property name="text">
 
                  <string>2 Stop Bit</string>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <spacer name="verticalSpacer_4">
 
                 <property name="orientation">
 
                  <enum>Qt::Vertical</enum>
 
                 </property>
 
                 <property name="sizeHint" stdset="0">
 
                  <size>
 
                   <width>20</width>
 
                   <height>2</height>
 
                  </size>
 
                 </property>
 
                </spacer>
 
               </item>
 
              </layout>
 
             </widget>
 
            </item>
 
            <item>
 
             <widget class="QFrame" name="frame_2">
 
              <layout class="QVBoxLayout" name="verticalLayout_6">
 
               <item>
 
                <widget class="QRadioButton" name="rbNoFlowControl">
 
                 <property name="text">
 
                  <string>No Flow Control</string>
 
                 </property>
 
                 <property name="checked">
 
                  <bool>true</bool>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <widget class="QRadioButton" name="rbHardwareControl">
 
                 <property name="text">
 
                  <string>Hardware Control</string>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <widget class="QRadioButton" name="rbSoftwareControl">
 
                 <property name="text">
 
                  <string>Software Control</string>
 
                 </property>
 
                </widget>
 
               </item>
 
               <item>
 
                <spacer name="verticalSpacer_5">
 
                 <property name="orientation">
 
                  <enum>Qt::Vertical</enum>
 
                 </property>
 
                 <property name="sizeHint" stdset="0">
 
                  <size>
 
                   <width>20</width>
 
                   <height>2</height>
 
                  </size>
 
                 </property>
 
                </spacer>
 
               </item>
 
              </layout>
 
             </widget>
 
            </item>
 
           </layout>
 
          </item>
 
         </layout>
 
        </item>
 
        <item>
 
         <spacer name="horizontalSpacer">
 
          <property name="orientation">
 
           <enum>Qt::Horizontal</enum>
 
          </property>
 
          <property name="sizeHint" stdset="0">
 
           <size>
 
            <width>10000</width>
 
            <height>20</height>
 
           </size>
 
          </property>
 
         </spacer>
 
        </item>
 
        <item>
 
         <layout class="QVBoxLayout" name="verticalLayout_4">
 
          <item>
 
           <widget class="CustomCheckableButton" name="pbOpenPort">
 
            <property name="minimumSize">
 
             <size>
 
              <width>0</width>
 
              <height>50</height>
 
             </size>
 
            </property>
 
            <property name="text">
 
             <string>Open</string>
 
            </property>
 
            <property name="checkable">
 
             <bool>true</bool>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QPushButton" name="pbSkipByte">
 
            <property name="text">
 
             <string>Skip Byte</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <spacer name="verticalSpacer">
 
            <property name="orientation">
 
             <enum>Qt::Vertical</enum>
 
            </property>
 
            <property name="sizeHint" stdset="0">
 
             <size>
 
              <width>20</width>
 
              <height>40</height>
 
             </size>
 
            </property>
 
           </spacer>
 
          </item>
 
         </layout>
 
        </item>
 
       </layout>
 
      </widget>
 
      <widget class="QWidget" name="tabDataFormat">
 
       <attribute name="title">
 
        <string>Data Format</string>
 
@@ -684,11 +394,6 @@
 
   <header>qwt_plot.h</header>
 
   <container>1</container>
 
  </customwidget>
 
  <customwidget>
 
   <class>CustomCheckableButton</class>
 
   <extends>QPushButton</extends>
 
   <header>customcheckablebutton.h</header>
 
  </customwidget>
 
 </customwidgets>
 
 <resources/>
 
 <connections/>
portcontrol.cpp
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2015 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 <QtDebug>
 
#include "utils.h"
 

	
 
PortControl::PortControl(QSerialPort* port, QWidget* parent) :
 
    QWidget(parent),
 
    ui(new Ui::PortControl)
 
{
 
    ui->setupUi(this);
 

	
 
    serialPort = port;
 

	
 
    QObject::connect(ui->pbReloadPorts, &QPushButton::clicked,
 
                     this, &PortControl::loadPortList);
 

	
 
    QObject::connect(ui->pbOpenPort, &QPushButton::clicked,
 
                     this, &PortControl::togglePort);
 

	
 
    QObject::connect(ui->cbPortList,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     this, &PortControl::selectPort);
 

	
 
    QObject::connect(ui->cbBaudRate,
 
                     SELECT<const QString&>::OVERLOAD_OF(&QComboBox::activated),
 
                     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);
 

	
 
    QObject::connect(&parityButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &PortControl::selectParity);
 

	
 
    // setup data bits selection buttons
 
    dataBitsButtons.addButton(ui->rb8Bits, (int) QSerialPort::Data8);
 
    dataBitsButtons.addButton(ui->rb7Bits, (int) QSerialPort::Data7);
 
    dataBitsButtons.addButton(ui->rb6Bits, (int) QSerialPort::Data6);
 
    dataBitsButtons.addButton(ui->rb5Bits, (int) QSerialPort::Data5);
 

	
 
    QObject::connect(&dataBitsButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &PortControl::selectDataBits);
 

	
 
    // setup stop bits selection buttons
 
    stopBitsButtons.addButton(ui->rb1StopBit, (int) QSerialPort::OneStop);
 
    stopBitsButtons.addButton(ui->rb2StopBit, (int) QSerialPort::TwoStop);
 

	
 
    QObject::connect(&stopBitsButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &PortControl::selectStopBits);
 

	
 
    // setup flow control selection buttons
 
    flowControlButtons.addButton(ui->rbNoFlowControl,
 
                                 (int) QSerialPort::NoFlowControl);
 
    flowControlButtons.addButton(ui->rbHardwareControl,
 
                                 (int) QSerialPort::HardwareControl);
 
    flowControlButtons.addButton(ui->rbSoftwareControl,
 
                                 (int) QSerialPort::SoftwareControl);
 

	
 
    QObject::connect(&flowControlButtons,
 
                     SELECT<int>::OVERLOAD_OF(&QButtonGroup::buttonClicked),
 
                     this, &PortControl::selectFlowControl);
 

	
 
    // init skip byte button
 
    QObject::connect(ui->pbSkipByte, &QPushButton::clicked,
 
                     [this](){emit skipByteRequested();});
 

	
 
    loadPortList();
 
    loadBaudRateList();
 
    ui->cbBaudRate->setCurrentIndex(ui->cbBaudRate->findText("9600"));
 
}
 

	
 
PortControl::~PortControl()
 
{
 
    delete ui;
 
}
 

	
 
void PortControl::loadPortList()
 
{
 
    QString currentSelection = ui->cbPortList->currentText();
 

	
 
    ui->cbPortList->clear();
 

	
 
    for (auto port : QSerialPortInfo::availablePorts())
 
    {
 
        ui->cbPortList->addItem(port.portName());
 
    }
 

	
 
    // find current selection in the new list, maybe it doesn't exist anymore?
 
    int currentSelectionIndex = ui->cbPortList->findText(currentSelection);
 
    if (currentSelectionIndex >= 0)
 
    {
 
        ui->cbPortList->setCurrentIndex(currentSelectionIndex);
 
    }
 
    else // our port doesn't exist anymore, close port if it's open
 
    {
 
        if (serialPort->isOpen()) togglePort();
 
    }
 
}
 

	
 
void PortControl::loadBaudRateList()
 
{
 
    ui->cbBaudRate->clear();
 

	
 
    for (auto baudRate : QSerialPortInfo::standardBaudRates())
 
    {
 
        ui->cbBaudRate->addItem(QString::number(baudRate));
 
    }
 
}
 

	
 
void PortControl::selectBaudRate(QString baudRate)
 
{
 
    if (serialPort->isOpen())
 
    {
 
        if (!serialPort->setBaudRate(baudRate.toInt()))
 
        {
 
            qDebug() << "Set baud rate failed during select: "
 
                     << serialPort->error();
 
        }
 
        else
 
        {
 
            qDebug() << "Baud rate changed: " << serialPort->baudRate();
 
        }
 
    }
 
}
 

	
 
void PortControl::selectParity(int parity)
 
{
 
    if (serialPort->isOpen())
 
    {
 
        if(!serialPort->setParity((QSerialPort::Parity) parity))
 
        {
 
            qDebug() << "Set parity failed: " << serialPort->error();
 
        }
 
    }
 
}
 

	
 
void PortControl::selectDataBits(int dataBits)
 
{
 
    if (serialPort->isOpen())
 
    {
 
        if(!serialPort->setDataBits((QSerialPort::DataBits) dataBits))
 
        {
 
            qDebug() << "Set data bits failed: " << serialPort->error();
 
        }
 
    }
 
}
 

	
 
void PortControl::selectStopBits(int stopBits)
 
{
 
    if (serialPort->isOpen())
 
    {
 
        if(!serialPort->setStopBits((QSerialPort::StopBits) stopBits))
 
        {
 
            qDebug() << "Set stop bits failed: " << serialPort->error();
 
        }
 
    }
 
}
 

	
 
void PortControl::selectFlowControl(int flowControl)
 
{
 
    if (serialPort->isOpen())
 
    {
 
        if(!serialPort->setFlowControl((QSerialPort::FlowControl) flowControl))
 
        {
 
            qDebug() << "Set flow control failed: " << serialPort->error();
 
        }
 
    }
 
}
 

	
 
void PortControl::togglePort()
 
{
 
    if (serialPort->isOpen())
 
    {
 
        serialPort->close();
 
        qDebug() << "Port closed, " << serialPort->portName();
 
        emit portToggled(false);
 
    }
 
    else
 
    {
 
        serialPort->setPortName(ui->cbPortList->currentText());
 

	
 
        // open port
 
        if (serialPort->open(QIODevice::ReadWrite))
 
        {
 
            qDebug() << "Port opened, " << serialPort->portName();
 
            emit portToggled(true);
 

	
 
            // set baud rate
 
            if (!serialPort->setBaudRate(ui->cbBaudRate->currentText().toInt()))
 
            {
 
                qDebug() << "Set baud rate failed during port opening: "
 
                         << serialPort->error();
 
            }
 
        }
 
        else
 
        {
 
            qDebug() << "Port open error: " << serialPort->error();
 
        }
 
    }
 
    ui->pbOpenPort->setChecked(serialPort->isOpen());
 
}
 

	
 
void PortControl::selectPort(QString 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();
 
        }
 
    }
 
}
 

	
 
void PortControl::enableSkipByte(bool enabled)
 
{
 
    ui->pbSkipByte->setDisabled(enabled);
 
}
portcontrol.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2015 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/>.
 
*/
 

	
 
#ifndef PORTCONTROL_H
 
#define PORTCONTROL_H
 

	
 
#include <QWidget>
 
#include <QButtonGroup>
 
#include <QSerialPort>
 

	
 
namespace Ui {
 
class PortControl;
 
}
 

	
 
class PortControl : public QWidget
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit PortControl(QSerialPort* port, QWidget* parent = 0);
 
    ~PortControl();
 

	
 
    QSerialPort* serialPort;
 

	
 
private:
 
    Ui::PortControl *ui;
 

	
 
    QButtonGroup parityButtons;
 
    QButtonGroup dataBitsButtons;
 
    QButtonGroup stopBitsButtons;
 
    QButtonGroup flowControlButtons;
 

	
 
public slots:
 
    void loadPortList();
 
    void loadBaudRateList();
 
    void togglePort();
 
    void selectPort(QString portName);
 
    void enableSkipByte(bool enabled = true);
 

	
 
    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
 

	
 
signals:
 
    void skipByteRequested();
 
    void portToggled(bool open);
 
};
 

	
 
#endif // PORTCONTROL_H
portcontrol.ui
Show inline comments
 
new file 100644
 
<?xml version="1.0" encoding="UTF-8"?>
 
<ui version="4.0">
 
 <class>PortControl</class>
 
 <widget class="QWidget" name="PortControl">
 
  <property name="geometry">
 
   <rect>
 
    <x>0</x>
 
    <y>0</y>
 
    <width>631</width>
 
    <height>213</height>
 
   </rect>
 
  </property>
 
  <property name="windowTitle">
 
   <string>Form</string>
 
  </property>
 
  <layout class="QHBoxLayout" name="horizontalLayout">
 
   <item>
 
    <layout class="QVBoxLayout" name="verticalLayout_3">
 
     <item>
 
      <layout class="QGridLayout" name="gridLayout">
 
       <item row="0" column="0">
 
        <widget class="QLabel" name="label">
 
         <property name="text">
 
          <string>Port:</string>
 
         </property>
 
        </widget>
 
       </item>
 
       <item row="0" column="1">
 
        <widget class="QComboBox" name="cbPortList">
 
         <property name="sizePolicy">
 
          <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
 
           <horstretch>0</horstretch>
 
           <verstretch>0</verstretch>
 
          </sizepolicy>
 
         </property>
 
         <property name="editable">
 
          <bool>true</bool>
 
         </property>
 
        </widget>
 
       </item>
 
       <item row="1" column="1">
 
        <widget class="QComboBox" name="cbBaudRate">
 
         <property name="inputMethodHints">
 
          <set>Qt::ImhPreferNumbers</set>
 
         </property>
 
         <property name="editable">
 
          <bool>true</bool>
 
         </property>
 
        </widget>
 
       </item>
 
       <item row="1" column="0">
 
        <widget class="QLabel" name="label_2">
 
         <property name="text">
 
          <string>Baud Rate:</string>
 
         </property>
 
        </widget>
 
       </item>
 
       <item row="0" column="2">
 
        <widget class="QPushButton" name="pbReloadPorts">
 
         <property name="sizePolicy">
 
          <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
 
           <horstretch>0</horstretch>
 
           <verstretch>0</verstretch>
 
          </sizepolicy>
 
         </property>
 
         <property name="maximumSize">
 
          <size>
 
           <width>30</width>
 
           <height>16777215</height>
 
          </size>
 
         </property>
 
         <property name="text">
 
          <string>↺</string>
 
         </property>
 
        </widget>
 
       </item>
 
      </layout>
 
     </item>
 
     <item>
 
      <layout class="QHBoxLayout" name="horizontalLayout_2">
 
       <item>
 
        <widget class="QFrame" name="frame">
 
         <property name="frameShape">
 
          <enum>QFrame::NoFrame</enum>
 
         </property>
 
         <layout class="QVBoxLayout" name="verticalLayout_5">
 
          <item>
 
           <widget class="QRadioButton" name="rbNoParity">
 
            <property name="text">
 
             <string>No Parity</string>
 
            </property>
 
            <property name="checked">
 
             <bool>true</bool>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QRadioButton" name="rbOddParity">
 
            <property name="text">
 
             <string>Odd Parity</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QRadioButton" name="rbEvenParity">
 
            <property name="text">
 
             <string>Even Parity</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <spacer name="verticalSpacer_2">
 
            <property name="orientation">
 
             <enum>Qt::Vertical</enum>
 
            </property>
 
            <property name="sizeHint" stdset="0">
 
             <size>
 
              <width>20</width>
 
              <height>2</height>
 
             </size>
 
            </property>
 
           </spacer>
 
          </item>
 
         </layout>
 
        </widget>
 
       </item>
 
       <item>
 
        <widget class="QFrame" name="frame_3">
 
         <layout class="QVBoxLayout" name="verticalLayout_7">
 
          <item>
 
           <widget class="QRadioButton" name="rb8Bits">
 
            <property name="text">
 
             <string>8 bits</string>
 
            </property>
 
            <property name="checked">
 
             <bool>true</bool>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QRadioButton" name="rb7Bits">
 
            <property name="text">
 
             <string>7 bits</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QRadioButton" name="rb6Bits">
 
            <property name="text">
 
             <string>6 bits</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QRadioButton" name="rb5Bits">
 
            <property name="text">
 
             <string>5 bits</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <spacer name="verticalSpacer_3">
 
            <property name="orientation">
 
             <enum>Qt::Vertical</enum>
 
            </property>
 
            <property name="sizeHint" stdset="0">
 
             <size>
 
              <width>20</width>
 
              <height>40</height>
 
             </size>
 
            </property>
 
           </spacer>
 
          </item>
 
         </layout>
 
        </widget>
 
       </item>
 
       <item>
 
        <widget class="QFrame" name="frame_4">
 
         <layout class="QVBoxLayout" name="verticalLayout">
 
          <item>
 
           <widget class="QRadioButton" name="rb1StopBit">
 
            <property name="text">
 
             <string>1 Stop Bit</string>
 
            </property>
 
            <property name="checked">
 
             <bool>true</bool>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QRadioButton" name="rb2StopBit">
 
            <property name="text">
 
             <string>2 Stop Bit</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <spacer name="verticalSpacer_4">
 
            <property name="orientation">
 
             <enum>Qt::Vertical</enum>
 
            </property>
 
            <property name="sizeHint" stdset="0">
 
             <size>
 
              <width>20</width>
 
              <height>2</height>
 
             </size>
 
            </property>
 
           </spacer>
 
          </item>
 
         </layout>
 
        </widget>
 
       </item>
 
       <item>
 
        <widget class="QFrame" name="frame_2">
 
         <layout class="QVBoxLayout" name="verticalLayout_6">
 
          <item>
 
           <widget class="QRadioButton" name="rbNoFlowControl">
 
            <property name="text">
 
             <string>No Flow Control</string>
 
            </property>
 
            <property name="checked">
 
             <bool>true</bool>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QRadioButton" name="rbHardwareControl">
 
            <property name="text">
 
             <string>Hardware Control</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QRadioButton" name="rbSoftwareControl">
 
            <property name="text">
 
             <string>Software Control</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <spacer name="verticalSpacer_5">
 
            <property name="orientation">
 
             <enum>Qt::Vertical</enum>
 
            </property>
 
            <property name="sizeHint" stdset="0">
 
             <size>
 
              <width>20</width>
 
              <height>2</height>
 
             </size>
 
            </property>
 
           </spacer>
 
          </item>
 
         </layout>
 
        </widget>
 
       </item>
 
      </layout>
 
     </item>
 
    </layout>
 
   </item>
 
   <item>
 
    <spacer name="horizontalSpacer">
 
     <property name="orientation">
 
      <enum>Qt::Horizontal</enum>
 
     </property>
 
     <property name="sizeHint" stdset="0">
 
      <size>
 
       <width>10000</width>
 
       <height>20</height>
 
      </size>
 
     </property>
 
    </spacer>
 
   </item>
 
   <item>
 
    <layout class="QVBoxLayout" name="verticalLayout_4">
 
     <item>
 
      <widget class="CustomCheckableButton" name="pbOpenPort">
 
       <property name="minimumSize">
 
        <size>
 
         <width>0</width>
 
         <height>50</height>
 
        </size>
 
       </property>
 
       <property name="text">
 
        <string>Open</string>
 
       </property>
 
       <property name="checkable">
 
        <bool>true</bool>
 
       </property>
 
      </widget>
 
     </item>
 
     <item>
 
      <widget class="QPushButton" name="pbSkipByte">
 
       <property name="text">
 
        <string>Skip Byte</string>
 
       </property>
 
      </widget>
 
     </item>
 
     <item>
 
      <spacer name="verticalSpacer">
 
       <property name="orientation">
 
        <enum>Qt::Vertical</enum>
 
       </property>
 
       <property name="sizeHint" stdset="0">
 
        <size>
 
         <width>20</width>
 
         <height>40</height>
 
        </size>
 
       </property>
 
      </spacer>
 
     </item>
 
    </layout>
 
   </item>
 
  </layout>
 
 </widget>
 
 <customwidgets>
 
  <customwidget>
 
   <class>CustomCheckableButton</class>
 
   <extends>QPushButton</extends>
 
   <header>customcheckablebutton.h</header>
 
  </customwidget>
 
 </customwidgets>
 
 <resources/>
 
 <connections/>
 
</ui>
serialplot.pro
Show inline comments
 
@@ -35,14 +35,17 @@ CONFIG += qwt
 

	
 
SOURCES += main.cpp\
 
        mainwindow.cpp \
 
    customcheckablebutton.cpp
 
    customcheckablebutton.cpp \
 
    portcontrol.cpp
 

	
 
HEADERS  += mainwindow.h \
 
    utils.h \
 
    customcheckablebutton.h
 
    customcheckablebutton.h \
 
    portcontrol.h
 

	
 
FORMS    += mainwindow.ui \
 
    about_dialog.ui
 
    about_dialog.ui \
 
    portcontrol.ui
 

	
 
INCLUDEPATH += qmake/
 

	
0 comments (0 inline, 0 general)