Skip to content

Commit 908f67a

Browse files
committed
Added QtSerial to the project
1 parent e2cbc64 commit 908f67a

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

QtSerial/QtSerial.pro

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
QT += core serialport
2+
3+
SOURCES += \
4+
main.cpp \
5+
serialportreader.cpp
6+
7+
HEADERS += \
8+
serialportreader.h

QtSerial/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "serialportreader.h"
2+
#include <iostream>
3+
4+
#include <QCoreApplication>
5+
#include <QtSerialPort/QSerialPort>
6+
7+
8+
int main(int argc, char *argv[])
9+
{
10+
QCoreApplication coreApplication(argc, argv);
11+
12+
QSerialPort serial;
13+
QString port("COM6");
14+
serial.setPortName(port);
15+
serial.setBaudRate(QSerialPort::Baud9600);
16+
serial.setDataBits(QSerialPort::Data8);
17+
serial.setStopBits(QSerialPort::OneStop);
18+
serial.setParity(QSerialPort::NoParity);
19+
serial.setFlowControl(QSerialPort::NoFlowControl);
20+
21+
if (!serial.open(QIODevice::ReadOnly))
22+
{
23+
std::cout << "!!! Failed to open port " << port.toStdString() <<
24+
". Error: " << serial.errorString().toStdString() << std::endl;
25+
return 1;
26+
}
27+
28+
SerialPortReader reader(&serial);
29+
30+
return coreApplication.exec();
31+
}

QtSerial/serialportreader.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "serialportreader.h"
2+
#include <iostream>
3+
4+
#include <QCoreApplication>
5+
#include <QDebug>
6+
7+
8+
SerialPortReader::SerialPortReader(QSerialPort* serial, QObject* parent)
9+
: QObject(parent), _serial(serial)
10+
{
11+
// The magic call that makes QSerialPort outputs from a serial connected with Arduino
12+
_serial->setDataTerminalReady(true);
13+
14+
QObject::connect(_serial, SIGNAL(readyRead()),
15+
SLOT(_read_callback()));
16+
17+
QObject::connect(_serial, SIGNAL(error(QSerialPort::SerialPortError)),
18+
SLOT(_error_callback(QSerialPort::SerialPortError)));
19+
}
20+
21+
SerialPortReader::~SerialPortReader()
22+
{
23+
}
24+
25+
void SerialPortReader::_read_callback()
26+
{
27+
if (!_serial)
28+
return;
29+
30+
QByteArray data = _serial->readAll();
31+
int sz = data.size();
32+
33+
/* On my Arduino data is always 3 bytes:
34+
* data[0]: is the value I'm interested at
35+
* data[1]: is always 13 (carriage return)
36+
* data[2]: is always 10 (new line)
37+
*
38+
* which causes std::cout to jump 2 lines, and I find it annoying,
39+
* hence the *if* statement below just to print the value I need.
40+
*/
41+
if (sz == 3 && (int)data[1] == 13 && (int)data[2] == 10)
42+
std::cout << data[0] << std::endl;
43+
else
44+
std::cout << data.toStdString() << std::endl;
45+
}
46+
47+
void SerialPortReader::_error_callback(QSerialPort::SerialPortError error)
48+
{
49+
if (error == QSerialPort::ReadError)
50+
{
51+
std::cout << "!!! An I/O error occurred while reading the data from port " << _serial->portName().toStdString() <<
52+
". Error: " << _serial->errorString().toStdString() << std::endl;
53+
QCoreApplication::exit(1);
54+
}
55+
}

QtSerial/serialportreader.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
#include <QByteArray>
3+
#include <QObject>
4+
#include <QtSerialPort/QSerialPort>
5+
6+
7+
class SerialPortReader : public QObject
8+
{
9+
Q_OBJECT
10+
11+
public:
12+
SerialPortReader(QSerialPort* serial, QObject* parent = 0);
13+
~SerialPortReader();
14+
15+
private slots:
16+
void _read_callback();
17+
void _error_callback(QSerialPort::SerialPortError error);
18+
19+
private:
20+
QSerialPort* _serial;
21+
};

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ http://blog.sklambert.com/using-time-based-animation-implement/
3535
cvWatershedSegmentation
3636
--------------
3737
A simple watershed segmentation example using distance transform to segment beans.
38+
39+
QtSerial
40+
--------------
41+
An application that prints data from a serial (COM) port connected to an Arduino.

0 commit comments

Comments
 (0)