Skip to content

Instantly share code, notes, and snippets.

@PRO2XY
Last active March 28, 2019 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PRO2XY/0c1d4082e6ecef401622b0986e5bcb5d to your computer and use it in GitHub Desktop.
Save PRO2XY/0c1d4082e6ecef401622b0986e5bcb5d to your computer and use it in GitHub Desktop.
#include "backend.hpp"
#include "serialportreader.h"
#include <QtDebug>
#include <QSerialPortInfo>
#include <QSerialPort>
void Backend::connBtnPressed(const QString msg){
qDebug() << msg;
if(conBtnTxt == "Disconnect"){
serial.close();
conBtnTxt = "Connect";
conBtnTxtChanged();
return;
}
serial.setPortName(msg);
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setStopBits(QSerialPort::OneStop);
serial.setParity(QSerialPort::NoParity);
serial.setFlowControl(QSerialPort::NoFlowControl);
if (!serial.open(QIODevice::ReadOnly))
{
qDebug() << "!!! Failed to open port " << msg <<
". Error: " << serial.errorString();
return;
}
qDebug() << "Port opened" ;
SerialPortReader reader(&serial);
conBtnTxt = "Disconnect";
conBtnTxtChanged();
}
void Backend::updateList(){
const auto infos = QSerialPortInfo::availablePorts();
QStringList dataList;
for (const QSerialPortInfo &info : infos)
dataList.append(info.portName());
if(!infos.count())
dataList.append("No serial ports");
portList = dataList;
portListChanged();
}
#ifndef BACKEND_HPP
#define BACKEND_HPP
#include <QObject>
#include <QtDebug>
#include <QtSerialPort>
#include "serialportreader.h"
class Backend : public QObject
{
Q_OBJECT
Q_PROPERTY(QStringList portList MEMBER portList NOTIFY portListChanged)
Q_PROPERTY(QString conBtnTxt MEMBER conBtnTxt NOTIFY conBtnTxtChanged)
QStringList portList;
QString conBtnTxt;
QStringList terminalText;
public:
// explicitBackend(QObject *parent = nullptr);
Backend() {
conBtnTxt = "Connect";
updateList();
}
public slots:
void connBtnPressed(const QString msg) ;
void updateList();
signals:
void portListChanged();
void conBtnTxtChanged();
private:
QSerialPort serial;
};
#endif // BACKEND_HPP
QML debugging is enabled. Only use this in a safe environment.
Styles ("Default", "Fusion", "Imagine", "Material", "Universal")
"ttyACM1"
Port opened
Read Callback
Data size: 36
"[CO2,6549, tVOC,936, millis,755262]\n"
Read Callback
Data size: 1
"\n"
Read Callback
Data size: 36
"[CO2,6207, tVOC,884, millis,756252]\n"
Read Callback
no serial port object QObject(0x0)
Read Callback
no serial port object QObject(0x0)
Program crashes after a few seconds
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QDebug>
#include <QObject>
#include "backend.hpp"
#include <QQmlContext>
#include <QtQuickControls2>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickStyle::setStyle("Fusion");
qDebug() << "Styles" << QQuickStyle::availableStyles();
QQmlApplicationEngine engine;
/***** expose backend *****/
Backend backend;
engine.rootContext()->setContextProperty("backend", &backend);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
/* Copyright (C) 2013-2015 Karl Phillip Buhr <karlphillip@gmail.com>
*
* 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>
#include <QtDebug>
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);
// original calls
// QObject::connect(_serial, SIGNAL(readyRead()),
// SLOT(_read_callback()));
// QObject::connect(_serial, SIGNAL(error(QSerialPort::SerialPortError)),
// SLOT(_error_callback(QSerialPort::SerialPortError)));
QObject::connect(_serial, &QIODevice::readyRead, [&]{_read_callback();});
//QObject::connect(_serial, &QSerialPort::errorOccurred,
// &SerialPortReader::_error_callback);
}
SerialPortReader::~SerialPortReader()
{
}
void SerialPortReader::_read_callback()
{
qDebug() << "Read Callback";
if (!_serial){
qDebug() << "no serial port object" << _serial;
return;
}
QByteArray data = _serial->readLine();
int sz = data.size();
qDebug() << "Data size:" << sz;
// To print data from other serial devices
QString str = QString::fromUtf8(data);
qDebug() << str;
}
void SerialPortReader::_error_callback(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ReadError)
{
qDebug() << "!!! An I/O error occurred while reading the data from port " << _serial->portName() <<
". Error: " << _serial->errorString();
//QCoreApplication::exit(1);
}
}
/* Copyright (C) 2013-2015 Karl Phillip Buhr <karlphillip@gmail.com>
*
* 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/
*/
#pragma once
#include <QByteArray>
#include <QObject>
#include <QtSerialPort/QSerialPort>
class SerialPortReader : public QObject
{
Q_OBJECT
public:
SerialPortReader(QSerialPort* serial, QObject* parent = nullptr);
~SerialPortReader();
private slots:
void _read_callback();
void _error_callback(QSerialPort::SerialPortError error);
private:
QSerialPort* _serial;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment