# HG changeset patch # User Hasan Yavuz Ă–ZDERYA # Date 2015-05-10 12:34:50 # Node ID 508c8437347e2357e4409674301c79782e6da5b0 # Parent 4a8b4a813d3ab0b620e05bf714a2f73c1d358be9 added ASCII (Comma Separated Values) data support diff --git a/mainwindow.cpp b/mainwindow.cpp --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -20,6 +20,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" #include +#include #include #include #include @@ -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; 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); } } diff --git a/mainwindow.h b/mainwindow.h --- a/mainwindow.h +++ b/mainwindow.h @@ -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(); diff --git a/mainwindow.ui b/mainwindow.ui --- a/mainwindow.ui +++ b/mainwindow.ui @@ -434,7 +434,7 @@ - + Qt::Vertical @@ -447,6 +447,16 @@ + + + + Comma Separated Values + + + ASCII(CSV) + + +