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
 
@@ -17,12 +17,13 @@
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "mainwindow.h"
 
#include "ui_mainwindow.h"
 
#include <QSerialPortInfo>
 
#include <QByteArray>
 
#include <QApplication>
 
#include <QtDebug>
 
#include <qwt_plot.h>
 
#include <limits.h>
 
#include <cmath>
 
#include "utils.h"
 
@@ -83,12 +84,13 @@ MainWindow::MainWindow(QWidget *parent) 
 
    numberFormatButtons.addButton(ui->rbUint8,  NumberFormat_uint8);
 
    numberFormatButtons.addButton(ui->rbUint16, NumberFormat_uint16);
 
    numberFormatButtons.addButton(ui->rbUint32, NumberFormat_uint32);
 
    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)));
 

	
 
    // setup parity selection buttons
 
    parityButtons.addButton(ui->rbNoParity, (int) QSerialPort::NoParity);
 
@@ -416,12 +418,40 @@ void MainWindow::onDataReady()
 
    else
 
    {
 
        serialPort.clear(QSerialPort::Input);
 
    }
 
}
 

	
 
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)
 
    {
 
        case QSerialPort::NoError :
 
            break;
 
@@ -612,12 +642,30 @@ void MainWindow::selectNumberFormat(Numb
 
            readSample = &MainWindow::readSampleAs<quint32>;
 
            break;
 
        case NumberFormat_int32:
 
            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);
 
    }
 
}
 

	
 
template<typename T> double MainWindow::readSampleAs()
 
{
 
    T data;
mainwindow.h
Show inline comments
 
@@ -50,13 +50,14 @@ private:
 
    {
 
        NumberFormat_uint8,
 
        NumberFormat_uint16,
 
        NumberFormat_uint32,
 
        NumberFormat_int8,
 
        NumberFormat_int16,
 
        NumberFormat_int32
 
        NumberFormat_int32,
 
        NumberFormat_ASCII
 
    };
 

	
 
    Ui::MainWindow *ui;
 
    QButtonGroup numberFormatButtons;
 
    QButtonGroup parityButtons;
 
    QButtonGroup dataBitsButtons;
 
@@ -104,13 +105,14 @@ private slots:
 
    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();
 
    void onDataReady();      // used with binary number formats
 
    void onDataReadyASCII(); // used with ASCII number format
 
    void onPortError(QSerialPort::SerialPortError error);
 

	
 
    void skipByte();
 

	
 
    void onNumOfSamplesChanged(int value);
 
    void onAutoScaleChecked(bool checked);
mainwindow.ui
Show inline comments
 
@@ -431,25 +431,35 @@
 
            <widget class="QRadioButton" name="rbInt8">
 
             <property name="text">
 
              <string>int8</string>
 
             </property>
 
            </widget>
 
           </item>
 
           <item row="3" column="0">
 
           <item row="4" column="0">
 
            <spacer name="verticalSpacer_6">
 
             <property name="orientation">
 
              <enum>Qt::Vertical</enum>
 
             </property>
 
             <property name="sizeHint" stdset="0">
 
              <size>
 
               <width>20</width>
 
               <height>40</height>
 
              </size>
 
             </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>
 
        <item>
 
         <spacer name="horizontalSpacer_3">
 
          <property name="orientation">
0 comments (0 inline, 0 general)