C++ (Qt)class Controller : public QObject{ Q_OBJECT Q_PROPERTY(double temperature READ temperature NOTIFY temperatureChanged) ....public: explicit Controller(QObject *parent = nullptr); Q_INVOKABLE void start(); // открывает серийный порт и запускает таймер Q_INVOKABLE void stop(); // останавливает таймер и закрывает порт double temperature() const; // возвращает m_temperature signals: void temperatureChanged(double temperature); private: void processIncomingResponse(); // парсит ответ от контроллера и дергает setTemperature() и т.п. void setTemperature(double temperature); // просто меняет значение m_temperature и емитит сигнал temperatureChanged double m_temperature = 0.0; // температура, полученная от ответа контроллера QSerialPort *m_uart = nullptr; // серийый порт QTimer *m_timer = nullptr; // таймер, который шлет запросы к контроллеру}
C++ (Qt)int main(int argc, char *argv[]){ ... Controller controller; view.rootContext()->setContextProperty(QLatin1String("controller"), &controller); ...
C++ (Qt)Text { text: "Temperature: " + controller.temperature}
Q_PROPERTY(quint8 fu READ fu NOTIFY fuChanged)
C++ (Qt)#ifndef GETDATA_H#define GETDATA_H#include <QObject>#include <QtSerialPort/QSerialPort>#include <QTimer> class Controller : public QObject{ Q_OBJECT Q_PROPERTY(int sp READ sp NOTIFY spChanged) // skorost koretki Q_PROPERTY(int th READ th NOTIFY thChanged) // oboroty motora Q_PROPERTY(int analog1 READ analog1 NOTIFY analog1Changed) // temperatura masla Q_PROPERTY(int analog2 READ analog2 NOTIFY analog2Changed) // temperatura motora Q_PROPERTY(int incoming READ incoming NOTIFY incomingChanged) // byte do kakoy tochki doshla koretka Q_PROPERTY(int lamps_group_1 READ lamps_group_1 NOTIFY lamps_group_1Changed) // avariynie lampy 1 8 bit - 8 lamp Q_PROPERTY(int lamps_group_2 READ lamps_group_2 NOTIFY lamps_group_2Changed) // avariynie lampy 2 8 bit - 8 lamp Q_PROPERTY(int mapADC READ mapADC NOTIFY mapADCChanged) // davlenie pnevmo nasosa Q_PROPERTY(int batADC READ batADC NOTIFY batADCChanged) // naprizhenie akkuma pri rabote ot akkuma Q_PROPERTY(int rpmVHR READ rpmVHR NOTIFY rpmVHRChanged) // scorost shpindelya Q_PROPERTY(int adjust0 READ adjust0 NOTIFY adjust0Changed) // rezervnie vhody Q_PROPERTY(int adjust1 READ adjust1 NOTIFY adjust1Changed) Q_PROPERTY(int adjust2 READ adjust2 NOTIFY adjust2Changed) Q_PROPERTY(int adjust3 READ adjust3 NOTIFY adjust3Changed) public: explicit Controller(QObject *parent = nullptr); Q_INVOKABLE void start(); // открывает серийный порт и запускает таймер int sp() const; int th() const; int analog1() const; int analog2() const; int incoming() const; int lamps_group_1() const; int lamps_group_2() const; int mapADC() const; int batADC() const; int rpmVHR() const; int adjust0() const; int adjust1() const; int adjust2() const; signals: void spChanget(int sp); void thCChanget(int th); void analog1Changed(int analog1); void analog2Changed(int analog2); void incomingChanged(int incoming); void lamps_group_1Changed(int lamps_group_1); void lamps_group_2Changed(int lamps_group_2); void mapADCChanged(int mapADC); void batADCChanged(int batADC); void rpmVHRChanged(int rpmVHR); void adjust0Changed(int adjust0); void adjust1Changed(int adjust1); void adjust2Changed(int adjust2); void adjust3Changed(int adjust3); private:void parsIn(); void setSp(int sp);void setTh(int th);void setAnalog1(int analog1);void setAnalog2(int analog2);void setIncoming(int incoming);void setLamps_group1(int lamps_group_1);void setLamps_group2(int lamps_group_2);void setMapADC(int mapADC);void setBatADC(int batADC);void setRpmHRV(int rpmVHR);void setAdjust0(int adjust0);void setAdjust1(int adjust1);void setAdjust2(int adjust2);void setAdjust3(int adjust3); int m_sp = 0;QSerialPort *m_uart = nullptr;QTimer *m_timer = nullptr; };#endif // GETDATA_H
C++ (Qt)#include "mainwindow.h"#include "ui_mainwindow.h"#include "receiver.h"#include <QMessageBox>#include <QPixmap>#include <QPainter>#include <QMatrix> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); initSerialPort(m_serialPort, consComPortName); connect(&m_recvTimer, SIGNAL(timeout()), this, SLOT(getData())); m_recvTimer.start(50);} MainWindow::~MainWindow(){ delete ui;} void MainWindow::getData() { // prepare tx buffer STxPacket txPkt; txPkt.req = 'A'; //declare rx buffer const SRxPacket* rx_ptr = NULL; rxBuff.clear(); //calculated data SViewPacket view; m_serialPort.write((const char*)&txPkt, sizeof(txPkt)); rxBuff.append(m_serialPort.readAll()); // check packet size if (rxBuff.size() >= constPacketLen) { rx_ptr = (SRxPacket*)rxBuff.data(); if (CalcRxPacket(rx_ptr, &view)) { } } rxBuff.clear(); }
C++ (Qt)Controller::Controller(QObject *parent) : QObject(parent) , m_uart(new QSerialPort(this)) , m_timer(new QTimer(this)){ connect(m_uart, &QSerialPort::readyRead, this, &Controller::processResponse); m_uart->setPortName(...); m_uart->setBaudRate(...); ... connect(m_timer, &QTimer::timeout, this, &Controller::sendRequest); m_timer->setInterval(500);} void Controller::start(){ if (!m_uart->open(ReadWrite)) return; m_timer->start();} void Controller::stop(){ m_timer->stop(); m_uart->close();} int Controller::th() const{ return m_th;} void Controller::setTh(int th){ if (m_th == th) return; m_th = th; emit thChanged(m_th);} void Controller::processResponse(){ for (;;) { const auto bytesAvailable = m_uart->bytesAvailable(); if (bytesAvailable == 0) return; // Читаем из порта и парсим все что нужно, извлекаем все значения. const int newTh = ...; // распарсили из принятых данных setTh(newTh); // Также по аналогии и для других сигналов/пропертей. ... ... }} void Controller::sendRequest(){ // Готовим запрос в порт const QByteArray request = ....; m_uart->write(request);}
C++ (Qt)void Controller::setTh(int th){ if (m_th == th) return; m_th = th; emit thChanged(m_th);}
C++ (Qt) connect(m_uart, &QSerialPort::readyRead, this, &Controller:: processIncomingResponse); m_uart->setPortName("/dev/ttyACM0"); m_uart->setBaudRate(QSerialPort::Baud19200); m_uart->setDataBits(QSerialPort::Data8); m_uart->setParity(QSerialPort::NoParity); m_uart->setStopBits(QSerialPort::OneStop); m_uart->setFlowControl(QSerialPort::NoFlowControl); m_uart->open(QSerialPort::ReadWrite);
C++ (Qt) for (;;) { const auto bytesAvailable = m_uart->bytesAvailable(); if (bytesAvailable == 0) return;
C++ (Qt) char pt = 'A'; m_uart->putChar(pt);