Running two interval timers on QT



  • Hello to all,
    Thanks for the "helping brains" here in this forum.
    Now I have a problem using Qt-Creator to develop a program which uses two timers.
    Here the problem:
    I want to read data from a 868Mhz receiver.
    One timer is to read on bit from the device in a 200µs interval in a sequence of ca. 100 bit.
    The second one is to make a delay of 10 minutes for the next sequence.
    Now I found this program which would solve this problem. I make it with this command on the Raspberry: Compile with: gcc -Wall -o timer1 -lrt timer1.c
    But it cannot be used by Qt-Creator. [0_1565542414226_main.cpp](Uploading 100%) It cannot find the two functions timer_create and timer_settime

    #include <QCoreApplication>
    
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #include <signal.h>
    
    timer_t Timerid1;
    timer_t Timerid2;
    int count1 = 0;
    int count2 = 0;
    
    
    
    
    /* Timer erzeugen und starten
     * Timerid: die zurueckgegebene ID des Timers
     * sek: Wartezeit Sekundenanteil
     * msek: Wartezeit Millisekundenanteil
     */
    void start_timer(timer_t *Timerid, int sek, int msek)
      {
      struct itimerspec timer;
    
      timer.it_value.tv_sec = sek;
      timer.it_value.tv_nsec = msek * 1000000;
      timer.it_interval.tv_sec = sek;
      timer.it_interval.tv_nsec = msek * 1000000;
    
      timer_create (CLOCK_REALTIME, NULL, Timerid);
      timer_settime (*Timerid, 0, &timer, NULL);
      printf("Timer gestartet, ID: 0x%lx\n", (long) *Timerid);
      }
    
    /* Anhalten eines durch Timerid identifizierten Timers
     * durch Setzen aller Zeiten auf 0
     */
    void stop_timer(timer_t *Timerid)
      {
      struct itimerspec timer;
    
      timer.it_value.tv_sec = 0;
      timer.it_value.tv_nsec = 0;
      timer.it_interval.tv_sec = 0;
      timer.it_interval.tv_nsec = 0;
    
      timer_settime (*Timerid, 0, &timer, NULL);
      }
    
    /* Signalhandler für alle Timer
     * Unterscheidung der Timer anhand von tidp
     */
    void timer_callback(int sig, siginfo_t *si, void *uc)
      {
      timer_t *tidp;
    
      tidp = &si->si_value.sival_ptr;
      printf("Signal: %d, ID: %p ", sig, tidp);
      if (tidp == Timerid1)
        printf(", Count 1: %d\n",count1++);
      if (tidp == Timerid2)
        printf(", Count 2: %d\n",count2++);
      }
    
    int main(int argc, char *argv[])
    {
        struct sigaction sa;
    
        /* callback-Handler installieren */
        memset(&sa, 0, sizeof (sa));
        sa.sa_flags = SA_SIGINFO;
        sa.sa_sigaction = timer_callback;
        sigemptyset(&sa.sa_mask);
        if (sigaction(SIGALRM, &sa, NULL) == -1)
          perror("sigaction");
    
        /* timer starten */
        start_timer(&Timerid1,1,0);
        start_timer(&Timerid2,0,500);
    
        /* Programm macht irgendwas */
        QCoreApplication a(argc, argv);
    
        while(count1 <= 5);
    
        /* Fertig, Timer stoppen */
        stop_timer(&Timerid2);
        stop_timer(&Timerid1);
    
        return a.exec();
    }
    
    

    when I try to compile this program I get two errors:
    /home/kurt/Qt-Projecte/ZweiTimer/main.cpp:32: error: undefined reference to timer_create' /home/kurt/Qt-Projecte/ZweiTimer/main.cpp:33: error: undefined reference totimer_settime'
    I have not found any solution.
    If somebody has an idea whats wrong please help me.
    Kurt



  • Hello, here I'm again!
    I think this is a problem using the timer function.
    I tried to make it with QTimer it works but it can handle only milli seconds. But I need an interval of 200 micro seconds. The second interval is 15 minutes.
    So I still need a solution using POSIX timer. And here is my problem.
    My configuration is :
    UBUNTU 18 running as Hyper-v machine on Windows 10
    QT-Creator:
    0_1565604212648_Screenshot from 2019-08-12 12-01-17.png

    It was installed using this Description:
    installation guide in german
    It seems there is something missing.
    Please could somebody give me a tip
    Thanks a lot in advance.
    Kurt



  • @k-str

    The "problem" is not with Qt creator, but the actual Qt libs you are using for your application.

    Qt creator is an IDE (Integrated Development Environment) which happens to be based on Qt libs as well. Creator 4.9.2 is using Qt 5.12.4, but you may use any other Qt libs version as installed on your machine. The version may be higher or lower or the exactly the same. However, those are strictly separated.

    Under projects on the left pane in creator you will find Build & run, whzere you can see the actual version used for compilation. Also lower you will find an icon with the project. When you click on this you see the actual version being used and if it is release or debug mode.

    Finally to your question. AFAIK QTimer is not meant for what you are trying to do. As you found out it has only granularity of 1 ms, but you cannot rely on this. https://doc.qt.io/qt-5/qtimer.html#accuracy-and-timer-resolution
    Timer events might be blocked during other events running in the event loop. Timer events will be executed when they are encountered in the event loop.



  • Hi @koahnig ,
    thanks for your answer.
    I checked some on my configs.
    It seems I have on my raspberry Qt version 5.7.1
    on the UBUNTU toolchain I have Qt version 5.12.3
    This means I have to install QT-Creator and the toolchain completely new?
    Kurt



  • @k-str

    Not necessarily. It depends on your intentions. This might be perfectly fine. However, I tend to have same versions everywhere. IMHO otherwise one gets confused.

    When you use the Ubuntu install to do a cross-compile for RPi, it is recommended to have the same kit/toolchain for both. I do not know if there is a place to get RPi pre-compiled versions. You might have to cross-compile a newer Qt lib version for RPi or the other way around you need to downgrade on Ubuntu to Qt 5.7.1.



  • @koahnig
    Thanks for your answer.
    I think I install everything completely new.
    This means I have to install the following Items:

    • QT-Creator
    • qt-everywhere
    • a rpi image raspbian stretch which fits to qt-everywhere

    What are the actual versions of this items?
    Kurt



  • @k-str

    It depends how you have installed the components before. The easiest is IMHO the online installer found on https://www.qt.io/download e.g. the open-source version on the right.

    Potentially you have used in the past and you have already maintenance tool available. This is typically updating for the newest Qt creator anyway.

    When you have already a prebuilt rpi Qt lib version you can try to match this version by installing it on your linux desktop. Otherwise you can download the linux open source archive and build the rpi Qt lib version based on https://wiki.qt.io/Raspberry_Pi_Beginners_Guide . There are also other tutorials available based on different Qt versions. I expect that a fairly new tutorial would work also with latest Qt lib code.

    As indicated above I suggest to install the same version on linux desktop and use it also for rpi. Typically it is a matter of taste what people prefer. However, using Qt 5.12.4 seem to be good choice because it is relatively new and has long-term support.



  • @koahnig ,
    hello, thanks for your tip.
    It*s quiet confusing, because whatever you do you run in some problems.
    I followed the beginners guide described here but I runned into some problems:

    • the mount command
    sudo mount -o loop,offset=62914560 2015-05-05-raspbian-wheezy.img /mnt/rasp-pi-rootfs
    

    does not work

    • has to be something like this
    sudo mount -v -o offset=62914560 -t ext4 2015-05-05-raspbian-wheezy.img /mnt/rasp-pi-rootfs
    

    finaly I'm stuck on this point

    sudo apt-get install ia32-libs
    

    ther is no way to install lib32bz2-1.0
    maybe this causes this failure
    You don't seem to have 'make' or 'gmake' inyour PATH
    when I run this command

    ./configure -opengl es2 -device linux-rasp-pi-g++ -device-option CROSS_COMPILE=~/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf- -sysroot /mnt/rasp-pi-rootfs -opensource -confirm-license -optimized-qmake -reduce-exports -release -make libs -prefix /usr/local/qt5pi -hostprefix /usr/local/qt5pi
    

    the command

    sudo ./fixQualifiedLibraryPaths /mnt/rasp-pi-rootfs/ ~/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc
    

    reports usage ./cmd targt-rootfs
    So what can I do?



  • @k-str

    I am not really a linux guy. Therefore I am struggling as well.

    However, can you run everywhere on your Ubuntu machine

    make -v
    

    or

    gmake -v
    

    otherwise you may to install either command. You need for configure and the cross-compilation anyhow.



  • @koahnig
    the outputs are :

    make -v
    
    Command 'make' not found, but can be installed with:
    
    sudo apt install make      
    sudo apt install make-guileth
    

    and

    gmake -v
    
    Command 'gmake' not found, but there are 14 similar ones.
    

    This means I must install make?



  • @k-str

    Yes, try to install either make or gmake. Not sure if there is a difference, but you need one.



  • @koahnig
    Thanks for your answer!
    I have a question about the versions of gcc linearo and raspbian. It's quiet confusing.
    Is it correct, that the version of gcc-linearo and gcc of the raspbian has to be equal?
    gcc-4.7-linearo has version 7.4.0
    gcc on 2019-04-08-raspbian-stretch has version 8.3
    gcc on 2018-11-13-raspbian-stretch has version 6.3



  • @k-str said in Running two interval timers on QT:

    @koahnig
    Thanks for your answer!
    I have a question about the versions of gcc linearo and raspbian. It's quiet confusing.
    Is it correct, that the version of gcc-linearo and gcc of the raspbian has to be equal?
    gcc-4.7-linearo has version 7.4.0
    gcc on 2019-04-08-raspbian-stretch has version 8.3
    gcc on 2018-11-13-raspbian-stretch has version 6.3

    Not necessarily. However, there are a couple of possible issues.

    The different versions of gcc have to be compatible on object level, when you want to mix object files resp libraries. AFAIK are most gcc compilers compatible on object level, but I know there some which are not. In general I would recommend staying with the same version everywhere, at least where you intend to mix.

    The other thing are Qt libs. If you are using pre-compiled versions as delivered with an OS, it would be better to have same versions ( see recommendation above). Not sure if there are already Qt dynamic libs included with raspbian. If so, you have also a possible version issue with those libs. Some time it works without issue, but personally I recommend staying with same versions.

    The linaro compiler is on desktop for cross-compilation. The other two are on RPi. If you intend to cross-compile on desktop, you would need to use always the same compiler there. If you intend to compile on RPi, try to get everything on RPi and do compilation there. That will make live easier.

    When you do all on desktop, you have to cross-compile Qt libs and deploy the libraries for Qt also to RPi. For this you may use also a pretty old compiler as long as it support the required C++ standards.



  • @koahnig ,
    thanks a lot for your tips.
    I installed qt again .
    using an other guide
    It worked almost with some changes.
    The timer problem I solved by adding -lrt to the make.
    The problem using the assignemend :

    tidp = &si->si_value.sival_ptr;
    

    I solved in this "old style" way;

    titp = (void**)si->si_value.sival_ptr;
    

    It works now.


Log in to reply