Changeset - f38042ba2eb0
[Not reviewed]
default
0 2 0
Hasan Yavuz Ă–ZDERYA - 11 years ago 2015-03-07 10:30:22
hy@ozderya.net
experimental plotting enabled
2 files changed with 35 insertions and 1 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);
 

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

	
 
	// init plot
 
	numOfSamples = 100;
 
	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);
 
    }
 
@@ -101,50 +112,65 @@ void MainWindow::togglePort()
 
            }
 
        }
 
        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()
 
{
 
    qDebug() << "Data: " << serialPort.readAll().toHex();
 
	QByteArray data = serialPort.readAll();
 
	addData((unsigned char)(data[0]));
 
}
 

	
 
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();
 
}
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();
 

	
 
signals:
 
    void portToggled(bool open);
 
};
 

	
 
#endif // MAINWINDOW_H
0 comments (0 inline, 0 general)