Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What difference between while(true) and QTimer?



  • I want to read something form ADC connected to raspberry pi

    value = analogRead  (PCF + 0) ;
    

    When I do this with while(true) in function ,I get more numbers than when do this with QTimer that connected to read function
    I start a timer in mainWindow

    connect(tm, SIGNAL(timeout()),this, SLOT(readFunction()));
         tm->start();
    

    why?
    I don't want to use while(true) in my code ,what should I do to ,read more numbers at a faster rate?


  • Lifetime Qt Champion

    Hi,

    Your timer runs at a fixed interval.

    An infinite loop runs as fast as it can (but will block the event loop).

    Either use a smaller interval for your timer or implement a thread that will use your infinite loop.



  • @SGaist When start timer without the interval.like:

    tm->start();
    

    Is the interval 0?if yes, how can set smaller interval?


  • Lifetime Qt Champion

    @isan
    Hi
    It cannot be smaller than 1 ms.
    tm->start(1);

    If you need it faster, a thread - as suggested is the way to go.

    https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/


  • Moderators

    @isan said in What difference between while(true) and QTimer?:

    When start timer without the interval.like:

    tm->start();
    

    Is the interval 0?

    The interval used here is the one reported by tm->interval(). http://doc.qt.io/qt-5/qtimer.html#start-1

    The default interval value is 0. You can change it by calling setInterval().

    @isan said in What difference between while(true) and QTimer?:

    When I do this with while(true) in function ,I get more numbers than when do this with QTimer that connected to read function
    I start a timer in mainWindow

    connect(tm, SIGNAL(timeout()),this, SLOT(readFunction()));
         tm->start();
    

    why?

    Because a timer with a 0 interval yields the CPU when your slot finishes running. This allows the CPU to do other things between reading your ADC. (see https://en.wikipedia.org/wiki/Yield_(multithreading) ).

    while(true) {...} does not yield unless you call sleep() inside the loop. There is a chance it will consume 100% of the CPU core and never let other threads use it.

    Also, there is a bit of overhead when using signals and slots (but I don't know how much). You can try to reduce the overhead by using the new signal-slot syntax (see http://doc.qt.io/qt-5/signalsandslots-syntaxes.html ):

    connect(tm, &QTimer::timeout, this, &MyClass::readFunction); // replace "MyClass" with your real class name
    

    I don't want to use while(true) in my code ,what should I do to ,read more numbers at a faster rate?

    What rate do you want?


  • Lifetime Qt Champion

    What rate do you want?

    and what do you want to do with the ADC samples?



  • @JKSH when read number with while(true) I get 120 numbers per second
    and with QTimer I get 40 numbers per second
    so for now ok to get 120 numbers persecond



  • @aha_1980 I want to draw a chart with numbers and when the numbers lost the chart is not valid enough
    I know if change i2c baud rate the sample rate is change ,but also its important how to read


  • Lifetime Qt Champion

    @isan
    Hi
    I think you will be most happy if you spend the extra time and
    make a tread to do the reading as it wont block main and will
    be as fast as while(true).



  • @mrjj said in What difference between while(true) and QTimer?:

    I think you will be most happy if you spend the extra time and
    make a tread to do the reading as it wont block main and will
    be as fast as while(true).

    I do this surely. tank you


  • Moderators

    @isan said in What difference between while(true) and QTimer?:

    @JKSH when read number with while(true) I get 120 numbers per second
    and with QTimer I get 40 numbers per second
    so for now ok to get 120 numbers persecond

    120 samples per second with while(true) is quite slow. A QTimer should be able to reach that rate, so I don't think that CPU yielding is the reason for the slowness.

    1. Can you show more code?
    2. What does tm->interval() return?
    3. What ADC is it?
    4. Where is the analogRead() function from?

    @isan said in What difference between while(true) and QTimer?:

    @aha_1980 I want to draw a chart with numbers

    Drawing a chart is very expensive. I agree with @mrjj -- you should see an improvement if you read the ADC in a dedicated thread and draw the chart in the GUI thread.



  • @JKSH I dont use tm->interval()

    ADC is PCF8591 with 100k scl

    I use wiringpi library ,that has <pcf8591.h>

    I try to use reading analog pin in thread tnx.

    #include "chart.h"
    #include <QtCharts/QAbstractAxis>
    #include <QtCharts/QSplineSeries>
    #include <QtCharts/QValueAxis>
    #include <QtCore/QDebug>
    #include <wiringPi.h>
    #include <pcf8591.h>
    #define PCF       120
    
    Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags):
        QChart(QChart::ChartTypeCartesian, parent, wFlags),
        m_series(0),
        m_axis(new QValueAxis),
        m_step(0),
        m_x(5),
        m_y(1)
    {
        QObject::connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
        m_timer.setInterval(1000);
    
        tm=new QTimer(this);
         connect(tm, SIGNAL(timeout()),this, SLOT(readFun()));
         tm->start();
        m_series = new QSplineSeries(this);
        QPen green(Qt::red);
        green.setWidth(3);
        m_series->setPen(green);
        m_series->append(m_x, m_y);
    
        addSeries(m_series);
        createDefaultAxes();
        setAxisX(m_axis, m_series);
        m_axis->setTickCount(5);
        axisX()->setRange(0, 10);
        axisY()->setRange(60,180);
        wiringPiSetup () ;
            // Setup pcf8591 on base pin 120, and address 0x48
         pcf8591Setup (PCF, 0x48) ;
    
        m_timer.start();
    }
    

Log in to reply