Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort send few commands and use QTimer (sending every second)
Forum Updated to NodeBB v4.3 + New Features

QSerialPort send few commands and use QTimer (sending every second)

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 227 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ialexpovod
    wrote on last edited by
    #1

    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 by emit 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:)

    JonBJ 1 Reply Last reply
    0
    • I ialexpovod

      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 by emit 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:)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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()?

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved