Changeset - 50d42af49411
[Not reviewed]
default
0 2 0
Hasan Yavuz Ă–ZDERYA - 9 years ago 2016-06-14 15:40:59
hy@ozderya.net
discard the very first line when reading ascii data since it may be incomplete
2 files changed with 16 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/asciireader.cpp
Show inline comments
 
@@ -22,19 +22,22 @@
 
#include "asciireader.h"
 

	
 
AsciiReader::AsciiReader(QIODevice* device, ChannelManager* channelMan, QObject *parent) :
 
    AbstractReader(device, channelMan, parent)
 
{
 
    paused = false;
 
    discardFirstLine = true;
 
    sampleCount = 0;
 

	
 
    _numOfChannels = _settingsWidget.numOfChannels();
 
    connect(&_settingsWidget, &AsciiReaderSettings::numOfChannelsChanged,
 
            this, &AsciiReader::numOfChannelsChanged);
 
    connect(&_settingsWidget, &AsciiReaderSettings::numOfChannelsChanged,
 
            [this](unsigned value){_numOfChannels = value;});
 

	
 
    connect(device, &QIODevice::aboutToClose, [this](){discardFirstLine=true;});
 
}
 

	
 
QWidget* AsciiReader::settingsWidget()
 
{
 
    return &_settingsWidget;
 
}
 
@@ -46,12 +49,13 @@ unsigned AsciiReader::numOfChannels()
 

	
 
// TODO: this could be a part of AbstractReader
 
void AsciiReader::enable(bool enabled)
 
{
 
    if (enabled)
 
    {
 
        discardFirstLine = true;
 
        QObject::connect(_device, &QIODevice::readyRead,
 
                         this, &AsciiReader::onDataReady);
 
    }
 
    else
 
    {
 
        QObject::disconnect(_device, 0, this, 0);
 
@@ -66,18 +70,26 @@ void AsciiReader::pause(bool enabled)
 
void AsciiReader::onDataReady()
 
{
 
    while(_device->canReadLine())
 
    {
 
        QByteArray line = _device->readLine();
 

	
 
        // discard only once when we just started reading
 
        if (discardFirstLine)
 
        {
 
            discardFirstLine = false;
 
            continue;
 
        }
 

	
 
        // discard data if paused
 
        if (paused)
 
        {
 
            return;
 
        }
 

	
 
        // parse data
 
        line = line.trimmed();
 
        auto separatedValues = line.split(',');
 

	
 
        int numReadChannels; // effective number of channels to read
 
        if (separatedValues.length() >= int(_numOfChannels))
 
        {
src/asciireader.h
Show inline comments
 
@@ -38,11 +38,15 @@ public slots:
 

	
 
private:
 
    AsciiReaderSettings _settingsWidget;
 
    unsigned _numOfChannels;
 
    bool paused;
 

	
 
    // We may have (usually true) started reading in the middle of a
 
    // line, so its a better idea to just discard first line.
 
    bool discardFirstLine;
 

	
 
private slots:
 
    void onDataReady();
 
};
 

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