QSerialPort send few commands and use QTimer (sending every second)
-
Hello :)
I have a device to which I need to send few commands at a click of a button (start receiving data). Commands: STOP, RESET, SET parameters, START, READ data updating every second.
Data is received byemit readyRead()
.Simple example of implementation my code:
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); m_serialThread= new SerialThread; m_serialThread->start(QThread::NormalPriority); timConnect = new QTimer(this); timConnect->start(10); connect(timConnect, SIGNAL(timeout()), this, SLOT(OpenSerialPort())); // auto connect to device (I will omit this function implementation) connect(m_serialThread, SIGNAL(readyRead()), this, SLOT(readData())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::readData() { // Simple implementation reading data if(stop) { QByteArray data = m_serialThread->readAll(); stop = false; qDebug() << data; // how to make the code run further from on_pushButton_clicked()? } if(reset) { QByteArray data = m_serialThread->readAll(); reset = false; qDebug() << data; } if(set) { QByteArray data = m_serialThread->readAll(); set = false; qDebug() << data; } if(start) { QByteArray data = m_serialThread->readAll(); start=false; qDebug() << data; } if(counts) QByteArray data = m_serialThread->readAll(); BufferWithCounts.append(data); } void MainWindow::on_pushButton_clicked() { // Stop command static const char bufsStop[] = "\x01\x02\x00\x00\x00\x00\xAA\xAB"; // conditional commands in hexadecimal form QByteArray _bStop= QByteArray::fromRawData(bufsStop, sizeof(bufsStop) - 1); if(m_serialThread->isOpen() && _bStop.length() == 8) { stop = true; m_serialThread->write(_bStop); } // Stop command static const char bufReset[] = "\x01\x02\x03\x04\x05\x06\x07\x08";; // conditional commands in hexadecimal form QByteArray _bReset= QByteArray::fromRawData(bufReset, sizeof(bufReset) - 1); if(m_serialThread->isOpen() && _bReset.length() == 8) { reset= true; m_serialThread->write(_bReset); } ... // Command SET parametrs, Command START. // And at the end I need to read the data from the pulse recording device. At the same time updating the data every second // Command read data (counts) from my device static const char comCounts[] = "\x01\x02\x03\x04\x05\x06\x07\x08"; QByteArray _bCounts= QByteArray::fromRawData(comCounts, sizeof(comCounts) - 1); if(m_serialThread->isOpen() && _bCounts.length() == 8) { counts= true; m_serialThread->write(_bCounts); } // every second update, send in device, and add in buffer. }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; SerialThread *m_serialThread; bool stop = false; bool reset = false; bool set = false; bool start = false; bool counts = false; QByteArray BufferWithCounts; }; #endif // MAINWINDOW_H
Thank you in advance:)
-
Hello :)
I have a device to which I need to send few commands at a click of a button (start receiving data). Commands: STOP, RESET, SET parameters, START, READ data updating every second.
Data is received byemit readyRead()
.Simple example of implementation my code:
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); m_serialThread= new SerialThread; m_serialThread->start(QThread::NormalPriority); timConnect = new QTimer(this); timConnect->start(10); connect(timConnect, SIGNAL(timeout()), this, SLOT(OpenSerialPort())); // auto connect to device (I will omit this function implementation) connect(m_serialThread, SIGNAL(readyRead()), this, SLOT(readData())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::readData() { // Simple implementation reading data if(stop) { QByteArray data = m_serialThread->readAll(); stop = false; qDebug() << data; // how to make the code run further from on_pushButton_clicked()? } if(reset) { QByteArray data = m_serialThread->readAll(); reset = false; qDebug() << data; } if(set) { QByteArray data = m_serialThread->readAll(); set = false; qDebug() << data; } if(start) { QByteArray data = m_serialThread->readAll(); start=false; qDebug() << data; } if(counts) QByteArray data = m_serialThread->readAll(); BufferWithCounts.append(data); } void MainWindow::on_pushButton_clicked() { // Stop command static const char bufsStop[] = "\x01\x02\x00\x00\x00\x00\xAA\xAB"; // conditional commands in hexadecimal form QByteArray _bStop= QByteArray::fromRawData(bufsStop, sizeof(bufsStop) - 1); if(m_serialThread->isOpen() && _bStop.length() == 8) { stop = true; m_serialThread->write(_bStop); } // Stop command static const char bufReset[] = "\x01\x02\x03\x04\x05\x06\x07\x08";; // conditional commands in hexadecimal form QByteArray _bReset= QByteArray::fromRawData(bufReset, sizeof(bufReset) - 1); if(m_serialThread->isOpen() && _bReset.length() == 8) { reset= true; m_serialThread->write(_bReset); } ... // Command SET parametrs, Command START. // And at the end I need to read the data from the pulse recording device. At the same time updating the data every second // Command read data (counts) from my device static const char comCounts[] = "\x01\x02\x03\x04\x05\x06\x07\x08"; QByteArray _bCounts= QByteArray::fromRawData(comCounts, sizeof(comCounts) - 1); if(m_serialThread->isOpen() && _bCounts.length() == 8) { counts= true; m_serialThread->write(_bCounts); } // every second update, send in device, and add in buffer. }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; SerialThread *m_serialThread; bool stop = false; bool reset = false; bool set = false; bool start = false; bool counts = false; QByteArray BufferWithCounts; }; #endif // MAINWINDOW_H
Thank you in advance:)
@ialexpovod
Hello and welcome.What is your actual question?
Almost certainly you do not want to use any
QThread
. Newcomers seem to head straight for threads, which are easy to get wrong, and are not what they want the majority of the time. Qt's serial port handling is already asynchronous, use signals & slots to deal with it. This will probably be the answer to whatever you have in mind with your// how to make the code run further from on_pushButton_clicked()?