/***mainwindow.h***/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QThread>
#include "deviceworker.h"
#include "algorithm.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
FirstDevice *device;
Algorithm *algorithm;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
/***mainwindow.cpp***/
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
device = new FirstDevice();
device->setReadTimer(333);
device->moveToThread(deviceThread);
algorithm = new Algorithm(device);
algorithm->moveToThread(algorithmThread);
deviceThread
->start
(QThread::TimeCriticalPriority);
algorithmThread->start();
}
void MainWindow::on_pushButton_clicked()
{
algorithm->run();
}
/***mainwindow.h***/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QThread>
#include "deviceworker.h"
#include "algorithm.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
FirstDevice *device;
Algorithm *algorithm;
QThread *deviceThread;
QThread *algorithmThread;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
/***mainwindow.cpp***/
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
QThread *deviceThread = new QThread();
QThread *algorithmThread = new QThread();
device = new FirstDevice();
device->setReadTimer(333);
device->moveToThread(deviceThread);
algorithm = new Algorithm(device);
algorithm->moveToThread(algorithmThread);
deviceThread->start(QThread::TimeCriticalPriority);
algorithmThread->start();
}
void MainWindow::on_pushButton_clicked()
{
algorithm->run();
}
To copy to clipboard, switch view to plain text mode
/***deviceworker.h***/
#ifndef DEVICEWORKER_H
#define DEVICEWORKER_H
#include <QTimer>
#include <QtSerialPort/QSerialPort>
#include <QMutex>
#include <QDebug>
#include <QObject>
using namespace std;
class FirstDevice
: public QObject { Q_OBJECT
private:
public:
explicit FirstDevice
(QObject *parent
= 0);
~FirstDevice(){}
void setReadTimer(int ms){
readTimer.start(ms);
}
public slots:
bool open(int id){
emit switchStateSig(id, true);
}
bool close(int id){
emit switchStateSig(id, false);
}
bool getAllStates();
private slots:
bool switchState(int id, bool state);
signals:
void remoteSignal(bool);
void status(int, bool, bool);
void switchStateSig(int, bool);
};
class ComPort
: public QObject { //singltone Q_OBJECT
private:
QSerialPort *serial;
explicit ComPort
(QObject *parent
= 0){ serial = new QSerialPort();
connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError)));
}
~ComPort(){}
ComPort(ComPort const&) = delete;
ComPort& operator= (ComPort const&) = delete;
public:
static ComPort& get()
{
static ComPort instance;
return instance;
}
void open(){
if(serial->isOpen())
close();
serial->setPortName("COM2");
if (serial->setBaudRate(QSerialPort::Baud115200)
&& serial->setFlowControl(QSerialPort::NoFlowControl)) {
qDebug() << "open";
} else {
qDebug
() <<
QString(serial
->errorString
());
serial->close();
}
}
}
void close(){serial->close();}
mutex->lock();
qDebug() << "-------------------------";
if(!serial->isOpen())
open();
int attempts = 1;
while (attempts <= 3) {
if (serial->isWritable())
{
serial->write(data);
serial->waitForBytesWritten(3);
while (serial->waitForReadyRead(300)) { //(wait 300ms). Here, randomly breaks down
readBuf += serial->readAll();
if (readBuf.size() == 4){
close();
mutex->unlock();
return readBuf;
}
}
readBuf.clear();
close();
open();
attempts++;
}
else
{
qDebug() << "port is not written";
close();
mutex->unlock();
return 0;
}
}
close();
mutex->unlock();
return 0;
}
private slots:
void handleError(QSerialPort::SerialPortError error){
if (error == QSerialPort::ResourceError) {
close();
qDebug() << "Error! ";
}
}
};
#endif // DEVICEWORKER_H
/***deviceworker.cpp***/
#include "deviceworker.h"
{
connect(this, SIGNAL(switchStateSig(int, bool)), this, SLOT(switchState(int, bool)));
connect(&readTimer, SIGNAL(timeout()), this, SLOT(getAllStates()));
}
bool FirstDevice::getAllStates()
{
arr.resize(2);
arr[0] = 0xAB;
arr[1] = 0x01;
QByteArray response
= ComPort
::get().
requestResponse(arr
);
if(response[0] == arr[0])
{
emit remoteSignal(1);
return 1;
}
emit remoteSignal(0);
return 0;
}
bool FirstDevice::switchState(int id, bool state)
{
arr.resize(2);
arr[0] = 0xAB;
arr[1] = 0x01;
QByteArray response
= ComPort
::get().
requestResponse(arr
);
if(response[0] == arr[0]) //ok
{
emit status(id, state, true);
return 1;
}
emit status(id, state, false);
return 0;
}
/***deviceworker.h***/
#ifndef DEVICEWORKER_H
#define DEVICEWORKER_H
#include <QTimer>
#include <QtSerialPort/QSerialPort>
#include <QMutex>
#include <QDebug>
#include <QObject>
using namespace std;
class FirstDevice : public QObject {
Q_OBJECT
private:
QTimer readTimer;
public:
explicit FirstDevice(QObject *parent = 0);
~FirstDevice(){}
void setReadTimer(int ms){
readTimer.start(ms);
}
public slots:
bool open(int id){
emit switchStateSig(id, true);
}
bool close(int id){
emit switchStateSig(id, false);
}
bool getAllStates();
private slots:
bool switchState(int id, bool state);
signals:
void remoteSignal(bool);
void status(int, bool, bool);
void switchStateSig(int, bool);
};
class ComPort : public QObject { //singltone
Q_OBJECT
private:
QSerialPort *serial;
QMutex *mutex;
explicit ComPort(QObject *parent = 0){
mutex = new QMutex();
serial = new QSerialPort();
connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError)));
}
~ComPort(){}
ComPort(ComPort const&) = delete;
ComPort& operator= (ComPort const&) = delete;
public:
static ComPort& get()
{
static ComPort instance;
return instance;
}
void open(){
if(serial->isOpen())
close();
serial->setPortName("COM2");
if (serial->open(QIODevice::ReadWrite)) {
if (serial->setBaudRate(QSerialPort::Baud115200)
&& serial->setFlowControl(QSerialPort::NoFlowControl)) {
qDebug() << "open";
} else {
qDebug() << QString(serial->errorString());
serial->close();
}
}
}
void close(){serial->close();}
QByteArray requestResponse(const QByteArray &data){
mutex->lock();
qDebug() << "-------------------------";
if(!serial->isOpen())
open();
int attempts = 1;
QByteArray readBuf;
while (attempts <= 3) {
if (serial->isWritable())
{
serial->write(data);
serial->waitForBytesWritten(3);
while (serial->waitForReadyRead(300)) { //(wait 300ms). Here, randomly breaks down
readBuf += serial->readAll();
if (readBuf.size() == 4){
close();
mutex->unlock();
return readBuf;
}
}
readBuf.clear();
close();
open();
attempts++;
}
else
{
qDebug() << "port is not written";
close();
mutex->unlock();
return 0;
}
}
close();
mutex->unlock();
return 0;
}
private slots:
void handleError(QSerialPort::SerialPortError error){
if (error == QSerialPort::ResourceError) {
close();
qDebug() << "Error! ";
}
}
};
#endif // DEVICEWORKER_H
/***deviceworker.cpp***/
#include "deviceworker.h"
FirstDevice::FirstDevice(QObject *parent):QObject(parent)
{
connect(this, SIGNAL(switchStateSig(int, bool)), this, SLOT(switchState(int, bool)));
connect(&readTimer, SIGNAL(timeout()), this, SLOT(getAllStates()));
}
bool FirstDevice::getAllStates()
{
QByteArray arr;
arr.resize(2);
arr[0] = 0xAB;
arr[1] = 0x01;
QByteArray response = ComPort::get().requestResponse(arr);
if(response[0] == arr[0])
{
emit remoteSignal(1);
return 1;
}
emit remoteSignal(0);
return 0;
}
bool FirstDevice::switchState(int id, bool state)
{
QByteArray arr;
arr.resize(2);
arr[0] = 0xAB;
arr[1] = 0x01;
QByteArray response = ComPort::get().requestResponse(arr);
if(response[0] == arr[0]) //ok
{
emit status(id, state, true);
return 1;
}
emit status(id, state, false);
return 0;
}
To copy to clipboard, switch view to plain text mode
#ifndef ALGORITHM_H
#define ALGORITHM_H
#include <QObject>
#include "deviceworker.h"
{
Q_OBJECT
private:
FirstDevice *device;
public:
Algorithm(FirstDevice *device_){
device = device_;
}
void run(){
device->open(3); //Here I must wait for an answer
//do something
device->close(3); //Here I must wait for an answer
}
};
#endif // ALGORITHM_H
#ifndef ALGORITHM_H
#define ALGORITHM_H
#include <QObject>
#include "deviceworker.h"
class Algorithm: public QObject
{
Q_OBJECT
private:
FirstDevice *device;
public:
Algorithm(FirstDevice *device_){
device = device_;
}
void run(){
device->open(3); //Here I must wait for an answer
//do something
device->close(3); //Here I must wait for an answer
}
};
#endif // ALGORITHM_H
To copy to clipboard, switch view to plain text mode
Bookmarks