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. Few emits in one slot
Forum Update on Monday, May 27th 2025

Few emits in one slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 395 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.
  • N Offline
    N Offline
    Narki1
    wrote on last edited by
    #1

    Hello,
    I don't understand one thing.I must use in aplication comunication with modbus and comparation of values in varables. In case of conditions "happens" events.For now I'm using thread for modbus comunication. Now I'm testing a drawt of main function.
    My problem is : I would like to send at least two emits wchich runs functions on modbus thread. Additionally main function is cyclic called by timer (I want to have fast refersh of values from modbus)
    Now part of code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "settings.h"
    #include <QStatusBar>
    #include <QDebug>
    #include<QString>
    #include <QSqlQueryModel>
    #include <QFile>
    #include <QThread>
    #include "modbus.h"
    MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),
        ui(new Ui::MainWindow)
    
    {
    
        ui->setupUi(this);
        timer_1=new QTimer(this);
        timer_1->setInterval(2000);
    //Threads
        QThread* thread = new QThread;
        modbus* Modbus = new modbus;
        Modbus->moveToThread(thread);
    
        connect(this, SIGNAL (cyl_1_FF()), Modbus, SLOT (Cylinder_1_FF()));
        connect(this, SIGNAL(Read_1()),Modbus, SLOT(Read_1_value()));
         thread->start();
          connect(timer_1,SIGNAL(timeout()),this,SLOT(Test_mode_1()));
    
    }
    void MainWindow::Test_mode_1(){
    //Main function
    emit Read_1();
    emit cyl_1_FF();
    }
    
    

    Problem is that: when is only one "emit" it's working. When are two "emits" - no one works.
    I've tried also connetion type" Qt::BlockingQueuedConnection" but also not works.

    VRoninV 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Before starting to add threading to your application, you should check whether the asynchronous nature of Qt is not already enough to manage the data coming from modbus.

      Then why do you need to have two different functions called one after the other ? Are you sure it's the correct design ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • N Offline
        N Offline
        Narki1
        wrote on last edited by Narki1
        #3

        Hello,

        Then why do you need to have two different functions called one after the other ? Are you sure it's the correct design ?

        Hello,
        This is an example for begin. Normally should be:

        emit Read_1();
        if( something) {
        emit cyl_1_FF();
        //other code
        }
        else{
        emit cyl_1_RW();
        //other code
        }
        

        I'm testing modbus communication using QSerialPort. I've heard when using separatelly thread that thread collect all "thinks to do" on stack and execute one by one. But I see that is not true. Those two signals are emited, but almost in the same time. This makes mess on QSerialPort. I must use some comunication switch.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well, your example shows that you seem to want to have a sequential blocking behaviour while using an asynchronous system. That doesn't seem right.

          By the way, why not use the QSerialBus module which support modbus ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • N Narki1

            Hello,
            I don't understand one thing.I must use in aplication comunication with modbus and comparation of values in varables. In case of conditions "happens" events.For now I'm using thread for modbus comunication. Now I'm testing a drawt of main function.
            My problem is : I would like to send at least two emits wchich runs functions on modbus thread. Additionally main function is cyclic called by timer (I want to have fast refersh of values from modbus)
            Now part of code:

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include "settings.h"
            #include <QStatusBar>
            #include <QDebug>
            #include<QString>
            #include <QSqlQueryModel>
            #include <QFile>
            #include <QThread>
            #include "modbus.h"
            MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),
                ui(new Ui::MainWindow)
            
            {
            
                ui->setupUi(this);
                timer_1=new QTimer(this);
                timer_1->setInterval(2000);
            //Threads
                QThread* thread = new QThread;
                modbus* Modbus = new modbus;
                Modbus->moveToThread(thread);
            
                connect(this, SIGNAL (cyl_1_FF()), Modbus, SLOT (Cylinder_1_FF()));
                connect(this, SIGNAL(Read_1()),Modbus, SLOT(Read_1_value()));
                 thread->start();
                  connect(timer_1,SIGNAL(timeout()),this,SLOT(Test_mode_1()));
            
            }
            void MainWindow::Test_mode_1(){
            //Main function
            emit Read_1();
            emit cyl_1_FF();
            }
            
            

            Problem is that: when is only one "emit" it's working. When are two "emits" - no one works.
            I've tried also connetion type" Qt::BlockingQueuedConnection" but also not works.

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @Narki1 said in Few emits in one slot:

            no one works.

            What does this means? it crashes? How are you debugging the problem?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0

            • Login

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