@@ -3,13 +3,13 @@
# This script will create a pseudo terminal, and send dummy data over
# it for testing purposes. Note that pseuodo terminal is a unix thing,
# this script will not work on Windows.
#
# Currently this script only outputs ASCII(comma separated) data.
# Copyright © 2015 Hasan Yavuz Özderya
# Copyright © 2018 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
@@ -23,12 +23,28 @@
# You should have received a copy of the GNU General Public License
# along with serialplot. If not, see <http://www.gnu.org/licenses/>.
import os, pty, time, struct, math
def ascii_test_str(port):
text = """
1,2,3
2,4,6
3,8,11
-1,-1,-1
nana
0,0,0
1,na,na
"""
while True:
for line in text.splitlines():
os.write(port, bytes(line+"\r\n", 'utf8'))
time.sleep(1)
def ascii_test(port):
"""Put ASCII test data through pseudo terminal."""
print("\n")
nc = 4 # number of channels
for i in range(0, 1000):
data = []
@@ -106,14 +122,15 @@ def run():
master_name = os.ttyname(master)
slave_name = os.ttyname(slave)
print("Master terminal: {}\nSlave terminal: {}".format(master_name, slave_name))
try:
# float_sine(master)
frame_test(master)
# frame_test(master)
# ascii_test(master)
ascii_test_str(master)
finally:
# close the pseudo terminal files
os.close(master)
os.close(slave)
if __name__=="__main__":
@@ -107,63 +107,65 @@ void AsciiReader::onDataReady()
// empty lines in the input when the port is just opened.
if (line.isEmpty())
{
continue;
}
auto separatedValues = line.split(delimiter, QString::SkipEmptyParts);
unsigned numReadChannels; // effective number of channels to read
unsigned numComingChannels = separatedValues.length();
if (autoNumOfChannels)
// did number of channels changed?
if (numComingChannels != _numChannels)
_numChannels = numComingChannels;
updateNumChannels();
emit numOfChannelsChanged(numComingChannels);
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;
// TODO: is `numOfChannelsChanged` signal still used?
emit numOfChannelsChanged(nc);
numReadChannels = numComingChannels;
else if (numComingChannels >= _numChannels)
numReadChannels = _numChannels;
else // there is missing channel data
numReadChannels = separatedValues.length();
qWarning() << "Incoming data is missing data for some channels!";
qWarning() << "Read line: " << line;
Q_ASSERT(samples->numChannels() == _numChannels);
// parse read line
unsigned numDataBroken = 0;
SamplePack samples(1, _numChannels);
for (unsigned ci = 0; ci < numReadChannels; ci++)
bool ok;
samples.data(ci)[0] = separatedValues[ci].toDouble(&ok);
if (!ok)
qWarning() << "Data parsing error for channel: " << ci;
samples.data(ci)[0] = 0;
numDataBroken++;
if (numReadChannels > numDataBroken)
// commit data
feedOut(samples);
feedOut(*samples);
SamplePack* AsciiReader::parseLine(const QString& line) const
// check number of channels (skipped if auto num channels is enabled)
if ((!numComingChannels) || (!autoNumOfChannels && numComingChannels != _numChannels))
qWarning() << "Line parsing error: invalid number of channels!";
return nullptr;
// parse data per channel
auto samples = new SamplePack(1, numComingChannels);
for (unsigned ci = 0; ci < numComingChannels; ci++)
samples->data(ci)[0] = separatedValues[ci].toDouble(&ok);
delete samples;
return samples;
void AsciiReader::saveSettings(QSettings* settings)
_settingsWidget.saveSettings(settings);
void AsciiReader::loadSettings(QSettings* settings)
@@ -18,13 +18,15 @@
*/
#ifndef ASCIIREADER_H
#define ASCIIREADER_H
#include <QSettings>
#include <QString>
#include "samplepack.h"
#include "abstractreader.h"
#include "asciireadersettings.h"
class AsciiReader : public AbstractReader
Q_OBJECT
@@ -47,9 +49,15 @@ private:
QChar delimiter; ///< selected column delimiter
bool firstReadAfterEnable = false;
private slots:
void onDataReady() override;
/**
* Parses given line and returns sample pack.
*
* Returns `nullptr` in case of error.
SamplePack* parseLine(const QString& line) const;
};
#endif // ASCIIREADER_H
Status change: