Changeset - a6e52579723b
[Not reviewed]
default
0 3 0
Hasan Yavuz Ă–ZDERYA - 11 years ago 2015-03-07 17:43:49
hy@ozderya.net
added numberOfSamples selection
3 files changed with 96 insertions and 6 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
#include "mainwindow.h"
 
#include "ui_mainwindow.h"
 
#include <QSerialPortInfo>
 
#include <QtDebug>
 

	
 
#include "utils.h"
 

	
 
MainWindow::MainWindow(QWidget *parent) :
 
    QMainWindow(parent),
 
    ui(new Ui::MainWindow)
 
{
 
    ui->setupUi(this);
 

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

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

	
 
    QObject::connect(this, &MainWindow::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(&(this->serialPort), &QSerialPort::readyRead,
 
                     this, &MainWindow::onDataReady);
 

	
 
    QObject::connect(ui->spNumOfSamples, SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged),
 
                     this, &MainWindow::onNumOfSamplesChanged);
 

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

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

	
 
    // init plot
 
    numOfSamples = 100;
 
    numOfSamples = ui->spNumOfSamples->value();
 
    dataArray.resize(numOfSamples);
 
    dataX.resize(numOfSamples);
 
    for (int i = 0; i < dataX.size(); i++)
 
    {
 
        dataX[i] = i;
 
    }
 
    curve.setSamples(dataX, dataArray);
 
    curve.attach(ui->plot);
 
}
 

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

	
 
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::onPortToggled(bool open)
 
{
 
    ui->pbOpenPort->setChecked(open);
 
}
 

	
 
void MainWindow::onDataReady()
 
{
 
    QByteArray data = serialPort.readAll();
 
    addData((unsigned char)(data[0]));
 
}
 

	
 
void MainWindow::onPortError(QSerialPort::SerialPortError error)
 
{
 
    switch(error)
 
    {
 
        case QSerialPort::NoError :
 
            break;
 
        case QSerialPort::ResourceError :
 
            qDebug() << "Port error: resource unavaliable; most likely device removed.";
 
            if (serialPort.isOpen())
 
            {
 
                qDebug() << "Closing port on resource error: " << serialPort.portName();
 
                togglePort();
 
            }
 
            loadPortList();
 
            break;
 
        default:
 
            qDebug() << "Unhandled port error: " << error;
 
            break;
 
    }
 

	
 
}
 

	
 
void MainWindow::addData(double data)
 
{
 
    // shift data array and place new data at the end
 
    for (int i = 0; i < dataArray.size()-1; i++)
 
    {
 
        dataArray[i] = dataArray[i+1];
 
    }
 
    dataArray.last() = data;
 

	
 
    // update plot
 
    curve.setSamples(dataX, dataArray);
 
    ui->plot->replot();
 
}
 

	
 
void MainWindow::onNumOfSamplesChanged(int value)
 
{
 
    int oldNum = this->numOfSamples;
 
    numOfSamples = value;
 

	
 
    // resize data arrays
 
    if (numOfSamples < oldNum)
 
    {
 
        dataX.resize(numOfSamples);
 
        dataArray.remove(0, oldNum - numOfSamples);
 
    }
 
    else if(numOfSamples > oldNum)
 
    {
 
        dataX.resize(numOfSamples);
 
        for (int i = oldNum; i < numOfSamples; i++)
 
        {
 
            dataX[i] = i;
 
            dataArray.prepend(0);
 
        }
 
    }
 
}
mainwindow.h
Show inline comments
 
#ifndef MAINWINDOW_H
 
#define MAINWINDOW_H
 

	
 
#include <QMainWindow>
 
#include <QString>
 
#include <QVector>
 
#include <QSerialPort>
 
#include <qwt_plot_curve.h>
 

	
 
namespace Ui {
 
class MainWindow;
 
}
 

	
 
class MainWindow : public QMainWindow
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit MainWindow(QWidget *parent = 0);
 
    ~MainWindow();
 

	
 
private:
 
    Ui::MainWindow *ui;
 
    QSerialPort serialPort;
 

	
 
    unsigned int numOfSamples;
 
    QwtPlotCurve curve;
 
    QVector<double> dataArray;
 
    QVector<double> dataX;
 
    void addData(double data);
 

	
 
private slots:
 
    void loadPortList();
 
    void loadBaudRateList();
 
    void togglePort();
 
    void selectPort(QString portName);
 
    void onPortToggled(bool open);
 
    void selectBaudRate(QString baudRate);
 

	
 
    void onDataReady();
 
    void onPortError(QSerialPort::SerialPortError error);
 

	
 
    void onNumOfSamplesChanged(int value);
 

	
 
signals:
 
    void portToggled(bool open);
 
};
 

	
 
#endif // MAINWINDOW_H
mainwindow.ui
Show inline comments
 
<?xml version="1.0" encoding="UTF-8"?>
 
<ui version="4.0">
 
 <class>MainWindow</class>
 
 <widget class="QMainWindow" name="MainWindow">
 
  <property name="geometry">
 
   <rect>
 
    <x>0</x>
 
    <y>0</y>
 
    <width>652</width>
 
    <width>653</width>
 
    <height>534</height>
 
   </rect>
 
  </property>
 
  <property name="windowTitle">
 
   <string>MainWindow</string>
 
  </property>
 
  <widget class="QWidget" name="centralWidget">
 
   <layout class="QVBoxLayout" name="verticalLayout_2">
 
    <item>
 
     <widget class="QwtPlot" name="plot" native="true">
 
      <property name="sizePolicy">
 
       <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
 
        <horstretch>0</horstretch>
 
        <verstretch>0</verstretch>
 
       </sizepolicy>
 
      </property>
 
     </widget>
 
    </item>
 
    <item>
 
     <widget class="QTabWidget" name="tabWidget">
 
      <property name="sizePolicy">
 
       <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
 
        <horstretch>0</horstretch>
 
        <verstretch>0</verstretch>
 
       </sizepolicy>
 
      </property>
 
      <property name="minimumSize">
 
       <size>
 
        <width>0</width>
 
        <height>0</height>
 
       </size>
 
      </property>
 
      <property name="maximumSize">
 
       <size>
 
        <width>16777215</width>
 
        <height>16777215</height>
 
       </size>
 
      </property>
 
      <property name="currentIndex">
 
       <number>0</number>
 
      </property>
 
      <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>
 
             </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">
 
@@ -233,193 +233,256 @@
 
               <item>
 
                <widget class="QRadioButton" name="rbNoFlowControl">
 
                 <property name="text">
 
                  <string>No Flow Control</string>
 
                 </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="tab_2">
 
      <widget class="QWidget" name="tabPlot">
 
       <attribute name="title">
 
        <string>Tab 2</string>
 
        <string>Plot</string>
 
       </attribute>
 
       <layout class="QHBoxLayout" name="horizontalLayout_4">
 
        <item>
 
         <spacer name="horizontalSpacer_2">
 
          <property name="orientation">
 
           <enum>Qt::Horizontal</enum>
 
          </property>
 
          <property name="sizeHint" stdset="0">
 
           <size>
 
            <width>370</width>
 
            <height>20</height>
 
           </size>
 
          </property>
 
         </spacer>
 
        </item>
 
        <item>
 
         <widget class="Line" name="line">
 
          <property name="orientation">
 
           <enum>Qt::Vertical</enum>
 
          </property>
 
         </widget>
 
        </item>
 
        <item>
 
         <layout class="QVBoxLayout" name="verticalLayout_8">
 
          <item>
 
           <layout class="QHBoxLayout" name="horizontalLayout_3">
 
            <item>
 
             <widget class="QLabel" name="label_3">
 
              <property name="text">
 
               <string>Number Of Samples:</string>
 
              </property>
 
             </widget>
 
            </item>
 
            <item>
 
             <widget class="QSpinBox" name="spNumOfSamples">
 
              <property name="minimum">
 
               <number>2</number>
 
              </property>
 
              <property name="maximum">
 
               <number>10000</number>
 
              </property>
 
              <property name="value">
 
               <number>1000</number>
 
              </property>
 
             </widget>
 
            </item>
 
           </layout>
 
          </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>
 
        </item>
 
       </layout>
 
      </widget>
 
     </widget>
 
    </item>
 
   </layout>
 
  </widget>
 
  <widget class="QMenuBar" name="menuBar">
 
   <property name="geometry">
 
    <rect>
 
     <x>0</x>
 
     <y>0</y>
 
     <width>652</width>
 
     <height>20</height>
 
     <width>653</width>
 
     <height>23</height>
 
    </rect>
 
   </property>
 
   <widget class="QMenu" name="menuFile">
 
    <property name="title">
 
     <string>File</string>
 
    </property>
 
    <addaction name="actionFileLoadProfile"/>
 
    <addaction name="actionFileSaveProfile"/>
 
   </widget>
 
   <widget class="QMenu" name="menuHelp">
 
    <property name="title">
 
     <string>Help</string>
 
    </property>
 
    <addaction name="actionHelpDataFormats"/>
 
    <addaction name="actionHelpAbout"/>
 
   </widget>
 
   <addaction name="menuFile"/>
 
   <addaction name="menuHelp"/>
 
  </widget>
 
  <widget class="QToolBar" name="mainToolBar">
 
   <attribute name="toolBarArea">
 
    <enum>TopToolBarArea</enum>
 
   </attribute>
 
   <attribute name="toolBarBreak">
 
    <bool>false</bool>
 
   </attribute>
 
   <addaction name="actionStart"/>
 
   <addaction name="actionClear"/>
 
  </widget>
 
  <widget class="QStatusBar" name="statusBar"/>
 
  <action name="actionStart">
 
   <property name="checkable">
 
    <bool>true</bool>
 
   </property>
 
   <property name="text">
 
    <string>Start</string>
 
   </property>
 
  </action>
 
  <action name="actionClear">
 
   <property name="text">
 
    <string>Clear</string>
 
   </property>
 
  </action>
 
  <action name="actionHelpDataFormats">
 
   <property name="text">
 
    <string>Data Formats</string>
 
   </property>
 
  </action>
 
  <action name="actionHelpAbout">
 
   <property name="text">
 
    <string>About</string>
 
   </property>
 
  </action>
 
  <action name="actionFileLoadProfile">
 
   <property name="text">
 
    <string>Load Profile</string>
 
   </property>
 
  </action>
 
  <action name="actionFileSaveProfile">
 
   <property name="text">
 
    <string>Save Profile</string>
 
   </property>
 
  </action>
 
 </widget>
 
 <layoutdefault spacing="6" margin="11"/>
 
 <customwidgets>
 
  <customwidget>
 
   <class>QwtPlot</class>
 
   <extends>QWidget</extends>
 
   <header>qwt_plot.h</header>
 
   <container>1</container>
 
  </customwidget>
 
  <customwidget>
 
   <class>CustomCheckableButton</class>
 
   <extends>QPushButton</extends>
 
   <header>customcheckablebutton.h</header>
 
  </customwidget>
 
 </customwidgets>
 
 <resources/>
 
 <connections/>
 
</ui>
0 comments (0 inline, 0 general)