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. QTimer in QThread ..???
Forum Updated to NodeBB v4.3 + New Features

QTimer in QThread ..???

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.6k 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.
  • FranckynosF Offline
    FranckynosF Offline
    Franckynos
    wrote on last edited by
    #1

    Hi,

    If I write this :

    void StepperMotor::moveR(int step)
    {
        qDebug() << this->thread()->currentThread();
        QTimer t;
        t.setInterval(1000);
        int posReach = send(GAP,1,0,0) + step;
        send(MVP, 1, step, 0);
        t.start();
        while (posReach != send(GAP,1,0,0,false) && t.remainingTime()>=0);
        qDebug() << "END "<< QTime::currentTime() << t.remainingTime();
    }
    

    Launch by a QtConcurrent, the timer "t.remainingTime() = 0 at once.

    And no pause are done so if I wrote several time :
    moveR(100);
    moveR(-1100);
    moveR(150);

    These 3 function are done in the same time ~3ms between 2 functions.

    I don't know how wait the end of the first moveR to launch the second. (Without hanging GUI)

    Someone can help me ?
    thx

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Lineaxe
      wrote on last edited by Lineaxe
      #2

      I think start up a separate thread. Inside it you can emit a signal to the GUI thread which can be picked up by a slot that you create in the GUI thread. This slot can give you lots of feedback information. I emit a signal just for diagnosing code in the thread. It can be inserted anywhere in the thread code. It simply supplies a Qstring to the GUI slot . You can use csv (comma separated values) for a simple way of holding data of different types. You can show them in a Lineedit ,Textedit or even fill up combobox's , and so on.
      This way you can test methods ...Also, you might even look into trying something along this pseudo code . Do loops work great in these situations.
      Do
      {
      your code to moveR
      {
      while ( your condition to test end of moveR)

      // you don't have to worry about blocking GUI in separate thread

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

        https://forum.qt.io/topic/64682/function-step-by-setp/4

        1 Reply Last reply
        1
        • FranckynosF Offline
          FranckynosF Offline
          Franckynos
          wrote on last edited by Franckynos
          #4

          If i put moveR in thread my timer dosen't work

          t.start(5000);
              do{
                  qDebug() << t.remainingTime();
              }while (posReach != send(GAP,1,0,0,false) && t.remainingTime()>=0);
          

          Outside thread:
          Timer =
          1000
          1000
          1000
          999
          999
          999
          999
          998
          998
          998
          997

          Inside thread :
          Timer =
          -177908879

          and ended the function.

          In my condition while :

          while (posReach != send(GAP,1,0,0,false) && t.remainingTime()>=0);
          

          I call send function :

          int StepperMotor::send(int commande, int param, int value, int motor_bank, bool verbose)
          {
              UCHAR Address, Status;
              int	Value, Timeout;
              Address=0;
              Status=0;
              Value=0;
              Timeout=GetTickCount();
              SendCmd(hRS232, 1, commande, param, motor_bank, value);
              while(GetResult(hRS232, &Address, &Status, &Value)==TMCL_RESULT_NOT_READY && abs(GetTickCount()-Timeout)<1000);
              if(Address == 0)
              {
                  portIsOpen = false;
                  emit errorPort("Envoi impossible");
                  if(!closeCOMmanual)
                      tryToConnect();
                  return -1;
              }
              emit reponse(Value, Status, Address);
              if(verbose)
                  qDebug() << QString("[%4] Result: Address=%1, Status=%2, Value=%3\n").arg(Address).arg(Status).arg(Value).arg(GetTickCount()-Timeout);
              return Value;
          }
          
          
          /******/
          void StepperMotor::SendCmd(HANDLE Handle, UCHAR Address, UCHAR Command, UCHAR Type, UCHAR Motor, INT Value)
          {
              UCHAR TxBuffer[9];
              DWORD BytesWritten;
              int i;
          
              TxBuffer[0]=Address;
              TxBuffer[1]=Command;
              TxBuffer[2]=Type;
              TxBuffer[3]=Motor;
              TxBuffer[4]=Value >> 24;
              TxBuffer[5]=Value >> 16;
              TxBuffer[6]=Value >> 8;
              TxBuffer[7]=Value & 0xff;
              TxBuffer[8]=0;
              for(i=0; i<8; i++)
                  TxBuffer[8]+=TxBuffer[i];
          
              //Send the datagram
              cptRequest++;
              WriteFile(Handle, TxBuffer, 9, &BytesWritten, NULL);
          }
          
          UCHAR StepperMotor::GetResult(HANDLE Handle, UCHAR *Address, UCHAR *Status, int *Value)
          {
              UCHAR RxBuffer[9], Checksum;
              DWORD Errors, BytesRead;
              COMSTAT ComStat;
              int i;
          
              //Check if enough bytes can be read
              ClearCommError(Handle, &Errors, &ComStat);
              if(ComStat.cbInQue>8)
              {
                  cptRequest++;
                  //Receive
                  ReadFile(Handle, RxBuffer, 9, &BytesRead, NULL);
          
                  Checksum=0;
                  for(i=0; i<8; i++)
                      Checksum+=RxBuffer[i];
          
                  if(Checksum!=RxBuffer[8]) return TMCL_RESULT_CHECKSUM_ERROR;
          
                  *Address=RxBuffer[0];
                  *Status=RxBuffer[2];
                  *Value=(RxBuffer[4] << 24) | (RxBuffer[5] << 16) | (RxBuffer[6] << 8) | RxBuffer[7];
              } else return TMCL_RESULT_NOT_READY;
          
              return TMCL_RESULT_OK;
          }
          
          1 Reply Last reply
          0
          • kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @Franckynos

            If i put moveR in thread my timer dosen't work

            Your timer can't work when you block the event loop, since a timer relies on the event loop to dispatch the timer events.

            Read and abide by the Qt Code of Conduct

            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