Changeset - 508c8437347e
[Not reviewed]
default
0 3 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-05-10 12:34:50
hy@ozderya.net
added ASCII (Comma Separated Values) data support
3 files changed with 63 insertions and 3 deletions:
0 comments (0 inline, 0 general)
mainwindow.cpp
Show inline comments
 
@@ -20,6 +20,7 @@
 
#include "mainwindow.h"
 
#include "ui_mainwindow.h"
 
#include <QSerialPortInfo>
 
#include <QByteArray>
 
#include <QApplication>
 
#include <QtDebug>
 
#include <qwt_plot.h>
 
@@ -86,6 +87,7 @@ MainWindow::MainWindow(QWidget *parent) 
 
    numberFormatButtons.addButton(ui->rbInt8,   NumberFormat_int8);
 
    numberFormatButtons.addButton(ui->rbInt16,  NumberFormat_int16);
 
    numberFormatButtons.addButton(ui->rbInt32,  NumberFormat_int32);
 
    numberFormatButtons.addButton(ui->rbASCII,  NumberFormat_ASCII);
 

	
 
    QObject::connect(&numberFormatButtons, SIGNAL(buttonToggled(int, bool)),
 
                     this, SLOT(onNumberFormatButtonToggled(int, bool)));
 
@@ -419,6 +421,34 @@ void MainWindow::onDataReady()
 
    }
 
}
 

	
 
void MainWindow::onDataReadyASCII()
 
{
 
    while(serialPort.canReadLine())
 
    {
 
        QByteArray line = serialPort.readLine();
 
        line = line.trimmed();
 
        auto separatedValues = line.split(',');
 

	
 
        if (separatedValues.length() >= numOfChannels)
 
        {
 
            for (int ci = 0; ci < numOfChannels; ci++)
 
            {
 
                double channelSample = separatedValues[ci].toDouble();
 
                addChannelData(ci, DataArray({channelSample}));
 
            }
 
        }
 
        else // there is missing channel data
 
        {
 
            qDebug() << "Incoming data is missing data for some channels!";
 
            for (int ci = 0; ci < separatedValues.length(); ci++)
 
            {
 
                double channelSample = separatedValues[ci].toDouble();
 
                addChannelData(ci, DataArray({channelSample}));
 
            }
 
        }
 
    }
 
}
 

	
 
void MainWindow::onPortError(QSerialPort::SerialPortError error)
 
{
 
    switch(error)
 
@@ -615,6 +645,24 @@ void MainWindow::selectNumberFormat(Numb
 
            sampleSize = 4;
 
            readSample = &MainWindow::readSampleAs<qint32>;
 
            break;
 
        case NumberFormat_ASCII:
 
            sampleSize = 0;    // these two members should not be used
 
            readSample = NULL; // in this mode
 
            break;
 
    }
 

	
 
    if (numberFormat == NumberFormat_ASCII)
 
    {
 
        QObject::disconnect(&(this->serialPort), &QSerialPort::readyRead, 0, 0);
 
        QObject::connect(&(this->serialPort), &QSerialPort::readyRead,
 
                         this, &MainWindow::onDataReadyASCII);
 

	
 
    }
 
    else
 
    {
 
        QObject::disconnect(&(this->serialPort), &QSerialPort::readyRead, 0, 0);
 
        QObject::connect(&(this->serialPort), &QSerialPort::readyRead,
 
                         this, &MainWindow::onDataReady);
 
    }
 
}
 

	
mainwindow.h
Show inline comments
 
@@ -53,7 +53,8 @@ private:
 
        NumberFormat_uint32,
 
        NumberFormat_int8,
 
        NumberFormat_int16,
 
        NumberFormat_int32
 
        NumberFormat_int32,
 
        NumberFormat_ASCII
 
    };
 

	
 
    Ui::MainWindow *ui;
 
@@ -107,7 +108,8 @@ private slots:
 
    void selectStopBits(int stopBits); // stopBits must be one of QSerialPort::StopBits
 
    void selectFlowControl(int flowControl); // flowControl must be one of QSerialPort::FlowControl
 

	
 
    void onDataReady();
 
    void onDataReady();      // used with binary number formats
 
    void onDataReadyASCII(); // used with ASCII number format
 
    void onPortError(QSerialPort::SerialPortError error);
 

	
 
    void skipByte();
mainwindow.ui
Show inline comments
 
@@ -434,7 +434,7 @@
 
             </property>
 
            </widget>
 
           </item>
 
           <item row="3" column="0">
 
           <item row="4" column="0">
 
            <spacer name="verticalSpacer_6">
 
             <property name="orientation">
 
              <enum>Qt::Vertical</enum>
 
@@ -447,6 +447,16 @@
 
             </property>
 
            </spacer>
 
           </item>
 
           <item row="3" column="0">
 
            <widget class="QRadioButton" name="rbASCII">
 
             <property name="toolTip">
 
              <string>Comma Separated Values</string>
 
             </property>
 
             <property name="text">
 
              <string>ASCII(CSV)</string>
 
             </property>
 
            </widget>
 
           </item>
 
          </layout>
 
         </widget>
 
        </item>
0 comments (0 inline, 0 general)