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. need ideas: using threads for can sending?
Qt 6.11 is out! See what's new in the release blog

need ideas: using threads for can sending?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.7k Views 3 Watching
  • 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.
  • H Offline
    H Offline
    haxxe
    wrote on last edited by
    #1

    hi,
    i have a function which sends 1700 can frames per sec. (34 frames after every 20ms).
    but there is one problem: the GUI gets kinda laggy/unresponsive(on raspberry pi) because the GUI thread is working on it?
    i already tried out threads with qfuture and qconcurrent, but my function is pretty messy and the compiler complains about this:

    error: invalid use of non-static member function

    so, do i really need threads or is there another, good way to keep the gui fast and keep my function running?
    maybe i also need threads because i want to receive frames, too.
    what do you think?
    greetings

    Venkatesh VV 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Yes, if you have a heavy function in the main thread.
      If it stays long in there, you are strangulating the event loop.
      (the exec() u seen in main.cpp)

      to solve this, its a good idea to use a worker object and run in a thread.
      https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

      • maybe i also need threads because i want to receive frames, too.
        Yes, as while sending, it will not be able to read anything otherwise.

      • error: invalid use of non-static member function
        Hard to guess at without the code.
        It most likely involves something u flagged as static.
        Like a static function.

      1 Reply Last reply
      3
      • H haxxe

        hi,
        i have a function which sends 1700 can frames per sec. (34 frames after every 20ms).
        but there is one problem: the GUI gets kinda laggy/unresponsive(on raspberry pi) because the GUI thread is working on it?
        i already tried out threads with qfuture and qconcurrent, but my function is pretty messy and the compiler complains about this:

        error: invalid use of non-static member function

        so, do i really need threads or is there another, good way to keep the gui fast and keep my function running?
        maybe i also need threads because i want to receive frames, too.
        what do you think?
        greetings

        Venkatesh VV Offline
        Venkatesh VV Offline
        Venkatesh V
        wrote on last edited by
        #3

        @haxxe
        Hi,

        create one more thread object(Worker Thread) and whatever the functionality regarding sending the frame is assign to this thread. dont use UI Thread for other activity like sending the huge data.

        1 Reply Last reply
        4
        • H Offline
          H Offline
          haxxe
          wrote on last edited by haxxe
          #4

          ok thanks i'll check the links.
          i've read very often, that one shouldnt use qthread, because qtconcurrent seems to be better?
          but i'll try it with qthread.

          EDIT: ok, i got the basics running.
          what now? do i have to copy my functions over to the worker class and call it within them?
          for example i do have this function:

          void MainWindow::pushSingleDataToSlave()
          {
              QByteArray payload ="";
              QCanBusFrame frame;
              unsigned int frameID = 0;
              int led_nr = ui->spinBox_ledNr->value();
              if(led_nr >= LEDs_TOATL_COUNT)
                  return;
          
              // generate frame id
              frameID = generateFrameId(	ID_OP_GRAY_SCALE_DATA, led_nr);
          
              payload.append((uint8_t)(ledGrayscale[led_nr]&0x00FF));
              payload.append((uint8_t)((ledGrayscale[led_nr]&0xFF00)>>8));
          
              frame.setFrameId(frameID);
              can->writeFrame(frame);
          }
          

          in mainWindow, how do i put this on a thread?

          EDIT: is it just enough to make an object of MainWindow?

          void Worker::process(){
              //allocate ressources using new here
              qDebug("Hello World!");
              MainWindow ui;
              ui.initCanBus();
              ui.pushSingleDataToSlave()
              emit finished();
          }
          
          Venkatesh VV 1 Reply Last reply
          0
          • H haxxe

            ok thanks i'll check the links.
            i've read very often, that one shouldnt use qthread, because qtconcurrent seems to be better?
            but i'll try it with qthread.

            EDIT: ok, i got the basics running.
            what now? do i have to copy my functions over to the worker class and call it within them?
            for example i do have this function:

            void MainWindow::pushSingleDataToSlave()
            {
                QByteArray payload ="";
                QCanBusFrame frame;
                unsigned int frameID = 0;
                int led_nr = ui->spinBox_ledNr->value();
                if(led_nr >= LEDs_TOATL_COUNT)
                    return;
            
                // generate frame id
                frameID = generateFrameId(	ID_OP_GRAY_SCALE_DATA, led_nr);
            
                payload.append((uint8_t)(ledGrayscale[led_nr]&0x00FF));
                payload.append((uint8_t)((ledGrayscale[led_nr]&0xFF00)>>8));
            
                frame.setFrameId(frameID);
                can->writeFrame(frame);
            }
            

            in mainWindow, how do i put this on a thread?

            EDIT: is it just enough to make an object of MainWindow?

            void Worker::process(){
                //allocate ressources using new here
                qDebug("Hello World!");
                MainWindow ui;
                ui.initCanBus();
                ui.pushSingleDataToSlave()
                emit finished();
            }
            
            Venkatesh VV Offline
            Venkatesh VV Offline
            Venkatesh V
            wrote on last edited by
            #5

            @haxxe

            Hi,
            Create one class inherit from QThread,

            //WorkerThread.h
            Class WorkerThread :public Qthread
            {

            public:
            void run();
            void sendFrame();
            };

            //WorkerThread.cpp
            WorkerThread::run()
            {
            sendFrame();
            }
            WorkerThread::sendFrame()
            {
            //sending Functionality here
            }

            //in mainWindow Class

            WorkerThread *wThread = new WorkerThread;
            wThread->start(); // it calls run method

            1 Reply Last reply
            2
            • H Offline
              H Offline
              haxxe
              wrote on last edited by
              #6

              thanks! my function is kinda komplex but ill try it soon :)

              1 Reply Last reply
              0
              • aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by aha_1980
                #7

                Hi @haxxe,

                you're really sending 1700 CAN frames per second? Which bitrate are you using? And how is which CAN adapter connected? (AFAIK Raspberry don't have CAN onboard...)

                In your send function, your creating a payload but do not assign it to the CAN frame, is this correct?

                Also, you can simplify your function like this:

                void MainWindow::pushSingleDataToSlave()
                {
                    int led_nr = ui->spinBox_ledNr->value();
                    if (led_nr >= LEDs_TOATL_COUNT)
                        return;
                
                    // generate frame id
                    quint32 frameID = generateFrameId(	ID_OP_GRAY_SCALE_DATA, led_nr);
                
                    QByteArray payload;
                    payload.append((uint8_t)(ledGrayscale[led_nr]&0x00FF));
                    payload.append((uint8_t)((ledGrayscale[led_nr]&0xFF00)>>8));
                
                    const QCanBusFrame frame(frameID, payload);
                    can->writeFrame(frame);
                }
                

                Qt has to stay free or it will die.

                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