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. my application doesnt work:( can you help me to understand why?
Forum Updated to NodeBB v4.3 + New Features

my application doesnt work:( can you help me to understand why?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++commandusbterminal
14 Posts 3 Posters 1.2k 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.
  • R Offline
    R Offline
    RuWex
    wrote on 3 Nov 2022, 10:22 last edited by
    #1

    I write application that send commands t camera by USB
    the terminal work well and everithing amazing:)
    but--
    in my application there is option also to choose file with commands and my appication should read the command and send them to the usb- to the camera.
    and then I read them char by char and use my function I uses always to send to the terminal, but now its not work.
    I will show you my file, and be happy for your help:)

    SendCommand.cpp:

    SendCommands::SendCommands()
    {
        additionalTest= new DoAdditionalTest();
        timer= new QTimer();
    }
    
    
    
    
    
    void SendCommands::StartToSendCommand(/*QPlainTextEdit *textEdit,*/ QString fileName)//Ruth
    {
    
        bool ans=additionalTest->CountsTheTimeRemaining(fileName);
        if (ans)
        {
            QFile file(fileName);
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                return;
    
    
            QTextStream in(&file);
            QString line;
            while (!in.atEnd()) {
    
                line = in.readLine();
                if (line==""||line[0]=="#")
                    continue;
               
                for(int i=0; i< line.length(); ++i)
                {
    
                    QString letter= (QString)line[i];
                    MainSendCommand mainSend(letter.toLatin1());
                    mainSend.ActivationMainWindow();
                    QThread::sleep(1);
    
                }
                timer->start(GWaitingTimeBetweenCommand);
                QApplication::processEvents();
    
            }
    
        }
        else {
            return;
        }
    
    }
    

    mainSendCommand.cpp:

    MainSendCommand::MainSendCommand(/* QPlainTextEdit *textEdit,*/QString fileName)
    {
        sendCommand= new SendCommands();
        mainwind= new MainWindow();
        /*this->textEdit= textEdit;*/
        this->fileName= fileName;
    
    }
    MainSendCommand::MainSendCommand(QByteArray file)
    {
        sendCommand= new SendCommands();
        mainwind= new MainWindow();
        this->file= file;
    
    }
    MainSendCommand::MainSendCommand()
    {
        sendCommand= new SendCommands();
        mainwind= new MainWindow();
    
    
    }
    
    void MainSendCommand::ActivationSendCommand()
    {
        sendCommand->StartToSendCommand(/*textEdit,*/fileName );
       // mainwind->ChangeStatus("Send Commands");
    }
    
    void MainSendCommand::ActivationMainWindow()
    {
        mainwind->writeData(file);
       // mainwind->ChangeStatus("Send Commands");
    
    }
    
    

    mainWindow.cpp:

    void MainWindow::writeData(const QByteArray &data)
    {
        DBG;
        m_serial->write(data);
    }
    

    ??
    do tou have any idea?
    thank:)

    J 1 Reply Last reply 3 Nov 2022, 11:03
    0
    • R RuWex
      3 Nov 2022, 10:22

      I write application that send commands t camera by USB
      the terminal work well and everithing amazing:)
      but--
      in my application there is option also to choose file with commands and my appication should read the command and send them to the usb- to the camera.
      and then I read them char by char and use my function I uses always to send to the terminal, but now its not work.
      I will show you my file, and be happy for your help:)

      SendCommand.cpp:

      SendCommands::SendCommands()
      {
          additionalTest= new DoAdditionalTest();
          timer= new QTimer();
      }
      
      
      
      
      
      void SendCommands::StartToSendCommand(/*QPlainTextEdit *textEdit,*/ QString fileName)//Ruth
      {
      
          bool ans=additionalTest->CountsTheTimeRemaining(fileName);
          if (ans)
          {
              QFile file(fileName);
              if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                  return;
      
      
              QTextStream in(&file);
              QString line;
              while (!in.atEnd()) {
      
                  line = in.readLine();
                  if (line==""||line[0]=="#")
                      continue;
                 
                  for(int i=0; i< line.length(); ++i)
                  {
      
                      QString letter= (QString)line[i];
                      MainSendCommand mainSend(letter.toLatin1());
                      mainSend.ActivationMainWindow();
                      QThread::sleep(1);
      
                  }
                  timer->start(GWaitingTimeBetweenCommand);
                  QApplication::processEvents();
      
              }
      
          }
          else {
              return;
          }
      
      }
      

      mainSendCommand.cpp:

      MainSendCommand::MainSendCommand(/* QPlainTextEdit *textEdit,*/QString fileName)
      {
          sendCommand= new SendCommands();
          mainwind= new MainWindow();
          /*this->textEdit= textEdit;*/
          this->fileName= fileName;
      
      }
      MainSendCommand::MainSendCommand(QByteArray file)
      {
          sendCommand= new SendCommands();
          mainwind= new MainWindow();
          this->file= file;
      
      }
      MainSendCommand::MainSendCommand()
      {
          sendCommand= new SendCommands();
          mainwind= new MainWindow();
      
      
      }
      
      void MainSendCommand::ActivationSendCommand()
      {
          sendCommand->StartToSendCommand(/*textEdit,*/fileName );
         // mainwind->ChangeStatus("Send Commands");
      }
      
      void MainSendCommand::ActivationMainWindow()
      {
          mainwind->writeData(file);
         // mainwind->ChangeStatus("Send Commands");
      
      }
      
      

      mainWindow.cpp:

      void MainWindow::writeData(const QByteArray &data)
      {
          DBG;
          m_serial->write(data);
      }
      

      ??
      do tou have any idea?
      thank:)

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 3 Nov 2022, 11:03 last edited by
      #2

      @RuWex said in my application doesnt work:( can you help me to understand why?:

      do tou have any idea?

      This is quite messed up code to be honest.
      What is MainWindow? If it is QMainWindow - why do you create one each time you call MainSendCommand::MainSendCommand() or MainSendCommand::MainSendCommand(QByteArray file)?! And are you aware that you have memory leaks all over the place?

      You also forgot to say which part exactly does not work...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RuWex
        wrote on 3 Nov 2022, 11:15 last edited by
        #3

        @jsulm
        I did it because of a problem I had in my application- its Unfortunately, consumed
        this line in sendCommand.cpp doesnt work:

         MainSendCommand mainSend(letter.toLatin1());
         mainSend.ActivationMainWindow();
        

        I just want to remind what I have in mainSendCommand.cpp:

        MainSendCommand::MainSendCommand(QByteArray file)
        {
            sendCommand= new SendCommands();
            mainwind= new MainWindow();
            this->file= file;
        
        }
        void MainSendCommand::ActivationMainWindow()
        {
            mainwind->writeData(file);
        
        }
        
        J 1 Reply Last reply 3 Nov 2022, 11:17
        0
        • R RuWex
          3 Nov 2022, 11:15

          @jsulm
          I did it because of a problem I had in my application- its Unfortunately, consumed
          this line in sendCommand.cpp doesnt work:

           MainSendCommand mainSend(letter.toLatin1());
           mainSend.ActivationMainWindow();
          

          I just want to remind what I have in mainSendCommand.cpp:

          MainSendCommand::MainSendCommand(QByteArray file)
          {
              sendCommand= new SendCommands();
              mainwind= new MainWindow();
              this->file= file;
          
          }
          void MainSendCommand::ActivationMainWindow()
          {
              mainwind->writeData(file);
          
          }
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 3 Nov 2022, 11:17 last edited by
          #4

          @RuWex said in my application doesnt work:( can you help me to understand why?:

          this line in sendCommand.cpp doesnt work

          There are two lines.
          And each of this lines involves other code. Please tell us what exactly does not work, I'm not a debugger...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply 3 Nov 2022, 11:27
          0
          • J jsulm
            3 Nov 2022, 11:17

            @RuWex said in my application doesnt work:( can you help me to understand why?:

            this line in sendCommand.cpp doesnt work

            There are two lines.
            And each of this lines involves other code. Please tell us what exactly does not work, I'm not a debugger...

            R Offline
            R Offline
            RuWex
            wrote on 3 Nov 2022, 11:27 last edited by
            #5

            @jsulm @jsulm
            mainwind->writeData(file);

            its not sent it to terminal and writedata()- this function work(I use it in other situation and it workes well)

            J 1 Reply Last reply 3 Nov 2022, 11:42
            0
            • R RuWex
              3 Nov 2022, 11:27

              @jsulm @jsulm
              mainwind->writeData(file);

              its not sent it to terminal and writedata()- this function work(I use it in other situation and it workes well)

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 3 Nov 2022, 11:42 last edited by
              #6

              @RuWex What about adding error handling? A proper application should have it.
              For example: m_serial->write(data) has a return value with a meaning, you simply ignore it. And there is also https://doc.qt.io/qt-5/qserialport.html#error-prop
              Then you're blocking Qt event loop with your QThread::sleep(1), this can also break things.

              Why do you send these strings character by character instead whole string at once?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • R Offline
                R Offline
                RuWex
                wrote on 3 Nov 2022, 11:45 last edited by
                #7

                @jsulm
                -Why do you send these strings character by character instead whole string at once?

                because I have to make timer between any char,
                the camera need to get the command character by character with waiting between

                J 1 Reply Last reply 3 Nov 2022, 12:28
                0
                • R RuWex
                  3 Nov 2022, 11:45

                  @jsulm
                  -Why do you send these strings character by character instead whole string at once?

                  because I have to make timer between any char,
                  the camera need to get the command character by character with waiting between

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 3 Nov 2022, 12:28 last edited by
                  #8

                  @RuWex said in my application doesnt work:( can you help me to understand why?:

                  the camera need to get the command character by character with waiting between

                  Then you should rework your code. You should use a QTimer and on each timeout send next character. With your current approach you're blocking the event loop.
                  And don't forget to add error handling...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply 3 Nov 2022, 12:41
                  2
                  • J jsulm
                    3 Nov 2022, 12:28

                    @RuWex said in my application doesnt work:( can you help me to understand why?:

                    the camera need to get the command character by character with waiting between

                    Then you should rework your code. You should use a QTimer and on each timeout send next character. With your current approach you're blocking the event loop.
                    And don't forget to add error handling...

                    R Offline
                    R Offline
                    RuWex
                    wrote on 3 Nov 2022, 12:41 last edited by
                    #9

                    @jsulm
                    I did it,
                    before I load the code I took away all unrelevant line:

                    void SendCommands::StartToSendCommand( QString fileName)//Ruth
                    {
                    
                        bool ans=additionalTest->CountsTheTimeRemaining(fileName);
                        if (ans)
                        {
                            QFile file(fileName);
                            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                                return;
                    
                    
                            QTextStream in(&file);
                            QString line;
                            while (!in.atEnd()) {
                    
                                line = in.readLine();
                                if (line==""||line[0]=="#")
                                    continue;
                       
                                for(int i=0; i< line.length(); ++i)
                                {
                                    QString letter= (QString)line[i];
                                    MainSendCommand mainSend(letter.toLatin1());
                                    mainSend.ActivationMainWindow();
                                    QThread::sleep(1);
                    
                                }
                                qDebug()<<endl;
                                timer->start(GWaitingTimeBetweenCommand);
                                QApplication::processEvents();
                                QThread::sleep(2);
                    
                            }
                    
                        }
                        else {
                            return;
                        }
                    
                    }
                    
                    J J 2 Replies Last reply 3 Nov 2022, 12:44
                    0
                    • R RuWex
                      3 Nov 2022, 12:41

                      @jsulm
                      I did it,
                      before I load the code I took away all unrelevant line:

                      void SendCommands::StartToSendCommand( QString fileName)//Ruth
                      {
                      
                          bool ans=additionalTest->CountsTheTimeRemaining(fileName);
                          if (ans)
                          {
                              QFile file(fileName);
                              if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                                  return;
                      
                      
                              QTextStream in(&file);
                              QString line;
                              while (!in.atEnd()) {
                      
                                  line = in.readLine();
                                  if (line==""||line[0]=="#")
                                      continue;
                         
                                  for(int i=0; i< line.length(); ++i)
                                  {
                                      QString letter= (QString)line[i];
                                      MainSendCommand mainSend(letter.toLatin1());
                                      mainSend.ActivationMainWindow();
                                      QThread::sleep(1);
                      
                                  }
                                  qDebug()<<endl;
                                  timer->start(GWaitingTimeBetweenCommand);
                                  QApplication::processEvents();
                                  QThread::sleep(2);
                      
                              }
                      
                          }
                          else {
                              return;
                          }
                      
                      }
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 3 Nov 2022, 12:44 last edited by
                      #10

                      @RuWex said in my application doesnt work:( can you help me to understand why?:

                      I did it,

                      What?
                      QTimer to send characters? No, you did not!
                      You start a timer after sending all the characters in the for loop...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • R RuWex
                        3 Nov 2022, 12:41

                        @jsulm
                        I did it,
                        before I load the code I took away all unrelevant line:

                        void SendCommands::StartToSendCommand( QString fileName)//Ruth
                        {
                        
                            bool ans=additionalTest->CountsTheTimeRemaining(fileName);
                            if (ans)
                            {
                                QFile file(fileName);
                                if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                                    return;
                        
                        
                                QTextStream in(&file);
                                QString line;
                                while (!in.atEnd()) {
                        
                                    line = in.readLine();
                                    if (line==""||line[0]=="#")
                                        continue;
                           
                                    for(int i=0; i< line.length(); ++i)
                                    {
                                        QString letter= (QString)line[i];
                                        MainSendCommand mainSend(letter.toLatin1());
                                        mainSend.ActivationMainWindow();
                                        QThread::sleep(1);
                        
                                    }
                                    qDebug()<<endl;
                                    timer->start(GWaitingTimeBetweenCommand);
                                    QApplication::processEvents();
                                    QThread::sleep(2);
                        
                                }
                        
                            }
                            else {
                                return;
                            }
                        
                        }
                        
                        J Offline
                        J Offline
                        JonB
                        wrote on 3 Nov 2022, 12:46 last edited by
                        #11

                        @RuWex said in my application doesnt work:( can you help me to understand why?:

                                QThread::sleep(1);
                                QApplication::processEvents();
                                QThread::sleep(2);
                        

                        And in addition to @jsulm none of the above lines should be in your code any longer (when you get the timer right).

                        1 Reply Last reply
                        1
                        • R Offline
                          R Offline
                          RuWex
                          wrote on 3 Nov 2022, 12:59 last edited by
                          #12

                          @RuWex said in my application doesnt work:( can you help me to understand why?:

                          timer->start(GWaitingTimeBetweenCommand);

                          I did it in the constructor:

                          SendCommands::SendCommands()
                          {
                              additionalTest= new DoAdditionalTest();
                              timer= new QTimer();
                          }
                          

                          void SendCommands::StartToSendCommand( QString fileName)//Ruth
                          {

                          bool ans=additionalTest->CountsTheTimeRemaining(fileName);
                          if (ans)
                          {
                              QFile file(fileName);
                              if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                                  return;
                          
                          
                              QTextStream in(&file);
                              QString line;
                              while (!in.atEnd()) {
                          
                                  line = in.readLine();
                                  if (line==""||line[0]=="#")
                                      continue;
                          
                                  for(int i=0; i< line.length(); ++i)
                                  {
                                      QString letter= (QString)line[i];
                                      MainSendCommand mainSend(letter.toLatin1());
                                      mainSend.ActivationMainWindow();
                                      QThread::sleep(1);
                          
                                  }
                                  qDebug()<<endl;
                                  **~~timer->start(GWaitingTimeBetweenCommand);~~**
                                  QApplication::processEvents();
                                  QThread::sleep(2);
                          
                              }
                          
                          }
                          else {
                              return;
                          }
                          

                          }

                          R 1 Reply Last reply 3 Nov 2022, 13:01
                          0
                          • R RuWex
                            3 Nov 2022, 12:59

                            @RuWex said in my application doesnt work:( can you help me to understand why?:

                            timer->start(GWaitingTimeBetweenCommand);

                            I did it in the constructor:

                            SendCommands::SendCommands()
                            {
                                additionalTest= new DoAdditionalTest();
                                timer= new QTimer();
                            }
                            

                            void SendCommands::StartToSendCommand( QString fileName)//Ruth
                            {

                            bool ans=additionalTest->CountsTheTimeRemaining(fileName);
                            if (ans)
                            {
                                QFile file(fileName);
                                if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                                    return;
                            
                            
                                QTextStream in(&file);
                                QString line;
                                while (!in.atEnd()) {
                            
                                    line = in.readLine();
                                    if (line==""||line[0]=="#")
                                        continue;
                            
                                    for(int i=0; i< line.length(); ++i)
                                    {
                                        QString letter= (QString)line[i];
                                        MainSendCommand mainSend(letter.toLatin1());
                                        mainSend.ActivationMainWindow();
                                        QThread::sleep(1);
                            
                                    }
                                    qDebug()<<endl;
                                    **~~timer->start(GWaitingTimeBetweenCommand);~~**
                                    QApplication::processEvents();
                                    QThread::sleep(2);
                            
                                }
                            
                            }
                            else {
                                return;
                            }
                            

                            }

                            R Offline
                            R Offline
                            RuWex
                            wrote on 3 Nov 2022, 13:01 last edited by RuWex 11 Mar 2022, 13:02
                            #13

                            @jsulm @JonB I dont understand what is my problem
                            can you explain me how to change the code and where?

                            J 1 Reply Last reply 3 Nov 2022, 13:10
                            0
                            • R RuWex
                              3 Nov 2022, 13:01

                              @jsulm @JonB I dont understand what is my problem
                              can you explain me how to change the code and where?

                              J Offline
                              J Offline
                              JonB
                              wrote on 3 Nov 2022, 13:10 last edited by JonB 11 Mar 2022, 13:11
                              #14

                              @RuWex
                              Please look at the QTimer documentation, and the examples there. This does not work by "inserting a delay" into the code where it is called. Rather, it sets off a timer which keeps running, and you supply a "slot"/"callback" which will be invoked when the timer expires. It is in that function/lambda that you do whatever work you wish to perform each time the repeating or singleshot timer expires, e.g. in your case maybe it sends the next character.

                              1 Reply Last reply
                              1

                              6/14

                              3 Nov 2022, 11:42

                              • Login

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