-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathserialportreader.cpp
72 lines (62 loc) · 2.22 KB
/
serialportreader.cpp
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
/* Copyright (C) 2013-2015 Karl Phillip Buhr <[email protected]>
*
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
* To view a copy of this license, visit:
* https://creativecommons.org/licenses/by-sa/2.5/legalcode
*
* Or to read the human-readable summary of the license:
* https://creativecommons.org/licenses/by-sa/2.5/
*/
#include "serialportreader.h"
#include <iostream>
#include <QCoreApplication>
#include <QDebug>
#include <QByteArray>
#include <QTextCodec>
SerialPortReader::SerialPortReader(QSerialPort* serial, QObject* parent)
: QObject(parent), _serial(serial)
{
// The magic call that makes QSerialPort outputs from a serial connected with Arduino
_serial->setDataTerminalReady(true);
QObject::connect(_serial, SIGNAL(readyRead()),
SLOT(_read_callback()));
QObject::connect(_serial, SIGNAL(error(QSerialPort::SerialPortError)),
SLOT(_error_callback(QSerialPort::SerialPortError)));
}
SerialPortReader::~SerialPortReader()
{
}
void SerialPortReader::_read_callback()
{
if (!_serial)
return;
QByteArray data = _serial->readAll();
int sz = data.size();
/* On my Arduino data is always 3 bytes:
* data[0]: is the value I'm interested at
* data[1]: is always 13 (carriage return)
* data[2]: is always 10 (new line)
*
* which causes std::cout to jump 2 lines, and I find it annoying,
* hence the *if* statement below just to print the value I need.
*/
if (sz == 3 && (int)data[1] == 13 && (int)data[2] == 10)
{
std::cout << data[0] << std::endl;
}
else
{
// To print data from other serial devices
QString str = QString::fromUtf8(data);
std::cout << str.toStdString();
}
}
void SerialPortReader::_error_callback(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ReadError)
{
std::cout << "!!! An I/O error occurred while reading the data from port " << _serial->portName().toStdString() <<
". Error: " << _serial->errorString().toStdString() << std::endl;
QCoreApplication::exit(1);
}
}