Files
        @ 5c9db4e54bbd
    
        
              Branch filter: 
        
    Location: tempo-plotter/src/asciireader.cpp
        
            
            5c9db4e54bbd
            4.9 KiB
            text/x-c++hdr
        
        
    
    fix crash during update check #16
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176  | /*
  Copyright © 2019 Hasan Yavuz Özderya
  This file is part of serialplot.
  serialplot is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  serialplot is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtDebug>
#include "asciireader.h"
/// If set to this value number of channels is determined from input
#define NUMOFCHANNELS_AUTO   (0)
AsciiReader::AsciiReader(QIODevice* device, QObject* parent) :
    AbstractReader(device, parent)
{
    paused = false;
    _numChannels = _settingsWidget.numOfChannels();
    autoNumOfChannels = (_numChannels == NUMOFCHANNELS_AUTO);
    delimiter = _settingsWidget.delimiter();
    connect(&_settingsWidget, &AsciiReaderSettings::numOfChannelsChanged,
            [this](unsigned value)
            {
                _numChannels = value;
                updateNumChannels(); // TODO: setting numchannels = 0, should remove all buffers
                                     // do we want this?
                autoNumOfChannels = (_numChannels == NUMOFCHANNELS_AUTO);
                if (!autoNumOfChannels)
                {
                    emit numOfChannelsChanged(value);
                }
            });
    connect(&_settingsWidget, &AsciiReaderSettings::delimiterChanged,
            [this](QChar d)
            {
                delimiter = d;
            });
}
QWidget* AsciiReader::settingsWidget()
{
    return &_settingsWidget;
}
unsigned AsciiReader::numChannels() const
{
    // TODO: an alternative is to never set _numChannels to '0'
    // do not allow '0'
    return _numChannels == 0 ? 1 : _numChannels;
}
void AsciiReader::enable(bool enabled)
{
    if (enabled)
    {
        firstReadAfterEnable = true;
    }
    AbstractReader::enable(enabled);
}
unsigned AsciiReader::readData()
{
    unsigned numBytesRead = 0;
    while(_device->canReadLine())
    {
        QByteArray bytes = _device->readLine();
        QString line = QString(bytes);
        numBytesRead += bytes.size();
        // discard only once when we just started reading
        if (firstReadAfterEnable)
        {
            firstReadAfterEnable = false;
            continue;
        }
        // discard data if paused
        if (paused)
        {
            continue;
        }
        // parse data
        line = line.trimmed();
        // Note: When data coming from pseudo terminal is buffered by
        // system CR is converted to LF for some reason. This causes
        // empty lines in the input when the port is just opened.
        if (line.isEmpty())
        {
            continue;
        }
        const SamplePack* samples = parseLine(line);
        if (samples != nullptr) {
            // update number of channels if in auto mode
            if (autoNumOfChannels ) {
                unsigned nc = samples->numChannels();
                if (nc != _numChannels) {
                    _numChannels = nc;
                    updateNumChannels();
                    // TODO: is `numOfChannelsChanged` signal still used?
                    emit numOfChannelsChanged(nc);
                }
            }
            Q_ASSERT(samples->numChannels() == _numChannels);
            // commit data
            feedOut(*samples);
            delete samples;
        }
    }
    return numBytesRead;
}
SamplePack* AsciiReader::parseLine(const QString& line) const
{
    auto separatedValues = line.split(delimiter, QString::SkipEmptyParts);
    unsigned numComingChannels = separatedValues.length();
    // check number of channels (skipped if auto num channels is enabled)
    if ((!numComingChannels) || (!autoNumOfChannels && numComingChannels != _numChannels))
    {
        qWarning() << "Line parsing error: invalid number of channels!";
        qWarning() << "Read line: " << line;
        return nullptr;
    }
    // parse data per channel
    auto samples = new SamplePack(1, numComingChannels);
    for (unsigned ci = 0; ci < numComingChannels; ci++)
    {
        bool ok;
        samples->data(ci)[0] = separatedValues[ci].toDouble(&ok);
        if (!ok)
        {
            qWarning() << "Data parsing error for channel: " << ci;
            qWarning() << "Read line: " << line;
            delete samples;
            return nullptr;
        }
    }
    return samples;
}
void AsciiReader::saveSettings(QSettings* settings)
{
    _settingsWidget.saveSettings(settings);
}
void AsciiReader::loadSettings(QSettings* settings)
{
    _settingsWidget.loadSettings(settings);
}
 |