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. serial- read -its not wait to get the response and recieve ""- why?
Forum Updated to NodeBB v4.3 + New Features

serial- read -its not wait to get the response and recieve ""- why?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 710 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 last edited by RuWex
    #1

    hi,
    Im doing application that supposed to read file and send line by line to the terminal.
    and before it continue to the next line it supposed to recieve reply.
    I did function and everything supposed to work well, but my problem is that when am doing that line:

    rep=m_serial->readAll();
    

    its not wait to recieve reply and rep =""......:(
    it is not wait to recoeve response...
    what can I add/ remove?

    the function that send the commands:

    void SendCommands::StartToSendCommand( QString fileName)//Ruth
    {
    
    
            QFile file(fileName);
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                return;
            QTextStream in(&file);
            QString line;
            m_ok=false;
            while (!in.atEnd()) {
    
                line = in.readLine();
                m_line=line;
    
                for(int i=0; i< line.length(); ++i)
                {
                    QString letter= (QString)line[i];
                    m_serial->write(letter.toLatin1());
                }
                GetReplyFromCamera(line);
    
                m_ok=false;
                m_i=0;
                QApplication::processEvents();
    
            }
        }
    
    }
    

    the function that recieve the reply:

    void SendCommands::GetReplyFromCamera(QString line)
    {
    
        QString res, com;
        QByteArray rep;
        for(int i=0; i<line.length()+1/*must be changed!!*/; ++i)
        {
            m_i++;
            rep=m_serial->readAll();    //here rep always will be "" its not wait for I will send response...
    
            }
            m_reply+=rep;
    
            if(line[i] >= 48&& line[i] <= 57)
            {
                if(m_ok==false)
                {
                    res=m_reply[m_i-1];
                    com=line[m_i-1];
                    m_index=m_i;
                    m_ok=true;
                }
            }
    
        }
    
        IsReplyRight(res, com);
        m_command="";
    
     }
    
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @RuWex said in serial- read -its not wait to get the response and recieve ""- why?:

      its not wait to recieve reply and rep =""......:(

      And therefore you have to use the signal readReady() and read out the bytes which are in the buffer. Then you have to parse them according to your protocol.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        @RuWex said in serial- read -its not wait to get the response and recieve ""- why?:

        its not wait to recieve reply and rep =""......:(

        And therefore you have to use the signal readReady() and read out the bytes which are in the buffer. Then you have to parse them according to your protocol.

        R Offline
        R Offline
        RuWex
        wrote on last edited by
        #3

        @Christian-Ehrlicher

        void SendCommands::GetReplyFromCamera(QString line)
        {
        
            QString res, com;
            QByteArray rep;
            for(int i=0; i<line.length()+1; ++i)
            {
                m_i++;
                m_serial->readyRead();
                rep=m_serial->readAll();    //here rep always will be "" its not wait for I will send response...
        
                }
                m_reply+=rep;
        
                if(line[i] >= 48&& line[i] <= 57)
                {
                    if(m_ok==false)
                    {
                        res=m_reply[m_i-1];
                        com=line[m_i-1];
                        m_index=m_i;
                        m_ok=true;
                    }
                }
        
            }
        
            IsReplyRight(res, com);
            m_command="";
        
         }
        

        like this?
        because its not work:(

        JonBJ 1 Reply Last reply
        0
        • R RuWex

          @Christian-Ehrlicher

          void SendCommands::GetReplyFromCamera(QString line)
          {
          
              QString res, com;
              QByteArray rep;
              for(int i=0; i<line.length()+1; ++i)
              {
                  m_i++;
                  m_serial->readyRead();
                  rep=m_serial->readAll();    //here rep always will be "" its not wait for I will send response...
          
                  }
                  m_reply+=rep;
          
                  if(line[i] >= 48&& line[i] <= 57)
                  {
                      if(m_ok==false)
                      {
                          res=m_reply[m_i-1];
                          com=line[m_i-1];
                          m_index=m_i;
                          m_ok=true;
                      }
                  }
          
              }
          
              IsReplyRight(res, com);
              m_command="";
          
           }
          

          like this?
          because its not work:(

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @RuWex
          No. What do you expect statement m_serial->readyRead(); to achieve?

          You need to read and understand that void QIODevice::readyRead() is a Qt signal, to which you need to attach a slot. And that slot needs to accumulate/append received bytes into some member variable buffer, which you then use use to access the bytes received when enough of them have been received/accumulated.

          If you do not understand Qt signals and slots you need to read about them first, as you won't get anywhere in Qt programming without a thorough grasp of these.

          R 1 Reply Last reply
          2
          • JonBJ JonB

            @RuWex
            No. What do you expect statement m_serial->readyRead(); to achieve?

            You need to read and understand that void QIODevice::readyRead() is a Qt signal, to which you need to attach a slot. And that slot needs to accumulate/append received bytes into some member variable buffer, which you then use use to access the bytes received when enough of them have been received/accumulated.

            If you do not understand Qt signals and slots you need to read about them first, as you won't get anywhere in Qt programming without a thorough grasp of these.

            R Offline
            R Offline
            RuWex
            wrote on last edited by
            #5

            @JonB hi,
            I am sorry, but Im new to qt and to the programming world...
            I read it: https://doc.qt.io/qt-6/qiodevice.html#readyRead
            but I still dont understand what I am supposed to do...\

            can you please make me a favor and Tell me a little more about it?

            JonBJ 1 Reply Last reply
            0
            • R RuWex

              @JonB hi,
              I am sorry, but Im new to qt and to the programming world...
              I read it: https://doc.qt.io/qt-6/qiodevice.html#readyRead
              but I still dont understand what I am supposed to do...\

              can you please make me a favor and Tell me a little more about it?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @RuWex
              You need to start by reading and understanding https://doc.qt.io/qt-6/signalsandslots.html.

              You might look at an example for readyRead signal, like https://doc.qt.io/qt-6.2/qtserialport-terminal-example.html. Note that there the MainWindow::readData() slot calls readAll() and then immediately deals with the data received (outputting it to the terminal in their case). In your case (I think, from what you are doing, but I don't know) you will need to append the freshly received bytes into a persistent buffer (member variable), so that you look through the buffer when you have received however many characters you expect/need.

              R 1 Reply Last reply
              4
              • JonBJ JonB

                @RuWex
                You need to start by reading and understanding https://doc.qt.io/qt-6/signalsandslots.html.

                You might look at an example for readyRead signal, like https://doc.qt.io/qt-6.2/qtserialport-terminal-example.html. Note that there the MainWindow::readData() slot calls readAll() and then immediately deals with the data received (outputting it to the terminal in their case). In your case (I think, from what you are doing, but I don't know) you will need to append the freshly received bytes into a persistent buffer (member variable), so that you look through the buffer when you have received however many characters you expect/need.

                R Offline
                R Offline
                RuWex
                wrote on last edited by
                #7

                @JonB I still haven't had time to do everything you wrote, but I did the main thing,
                and right now its not working:(

                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    m_ui(new Ui::MainWindow),
                    m_status(new QLabel),
                    m_console(new Console),
                    m_settings(new SettingsDialog),
                    m_serial(new QSerialPort(this))
                {
                .
                .
                .
                
                
                    connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData)
                .
                .
                .
                }
                

                readData:

                QByteArray MainWindow::readData()
                {
                    DBG;
                    const QByteArray data = m_serial->readAll();
                    sendC->GetReplyFromCamera(data);
                    m_console->putData(data);
                    return data;
                }
                

                GetReplyFromUsb:

                void SendCommands::GetReplyFromCamera(QString line)
                {
                
                    QString res, com;
                    QByteArray rep;
                    for(int i=0; i<line.length()+1; ++i)
                    {
                        m_i++;
                        m_serial->readyRead();
                        rep=m_serial->readAll();    //here rep always will be "" its not wait for I will send response...
                
                        }
                        m_reply+=rep;
                
                        if(line[i] >= 48&& line[i] <= 57)
                        {
                            if(m_ok==false)
                            {
                                res=m_reply[m_i-1];
                                com=line[m_i-1];
                                m_index=m_i;
                                m_ok=true;
                            }
                        }
                
                    }
                
                    IsReplyRight(res, com);
                    m_command="";
                
                 }
                

                But it still doesn't work :(
                Do you know why???

                JonBJ 1 Reply Last reply
                0
                • R RuWex

                  @JonB I still haven't had time to do everything you wrote, but I did the main thing,
                  and right now its not working:(

                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      m_ui(new Ui::MainWindow),
                      m_status(new QLabel),
                      m_console(new Console),
                      m_settings(new SettingsDialog),
                      m_serial(new QSerialPort(this))
                  {
                  .
                  .
                  .
                  
                  
                      connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData)
                  .
                  .
                  .
                  }
                  

                  readData:

                  QByteArray MainWindow::readData()
                  {
                      DBG;
                      const QByteArray data = m_serial->readAll();
                      sendC->GetReplyFromCamera(data);
                      m_console->putData(data);
                      return data;
                  }
                  

                  GetReplyFromUsb:

                  void SendCommands::GetReplyFromCamera(QString line)
                  {
                  
                      QString res, com;
                      QByteArray rep;
                      for(int i=0; i<line.length()+1; ++i)
                      {
                          m_i++;
                          m_serial->readyRead();
                          rep=m_serial->readAll();    //here rep always will be "" its not wait for I will send response...
                  
                          }
                          m_reply+=rep;
                  
                          if(line[i] >= 48&& line[i] <= 57)
                          {
                              if(m_ok==false)
                              {
                                  res=m_reply[m_i-1];
                                  com=line[m_i-1];
                                  m_index=m_i;
                                  m_ok=true;
                              }
                          }
                  
                      }
                  
                      IsReplyRight(res, com);
                      m_command="";
                  
                   }
                  

                  But it still doesn't work :(
                  Do you know why???

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @RuWex
                  Well done for connecting the readyRead signal to a MainWindow::readData() slot. But I'm afraid I don't think you have understood what you are doing, else you would have changed your GetReplyFromCamera(QString line) code to work from the bytes received. Instead you still have

                          m_serial->readyRead();
                          rep=m_serial->readAll();    //here rep always will be "" its not wait for I will send response...
                  

                  there, which makes no sense.

                  • Never call m_serial->readyRead(); yourself.
                  • Never call m_serial->readAll(); anywhere other then in the readyRead() slot (MainWindow::readData()).

                  Other than that, in principle if you want to pass the pass bytes received to GetReplyFromCamera() and process them immediately there then you have appropriate code. Note that you may receive the readyRead() signal and hence call GetReplyFromCamera() multiple times (there is no guarantee that all bytes sent from serial device will arrive in a single readyRead(), they could be split into more than one separate "packets"). If that is OK with your code/intention then it is fine. If you need to accumulate bytes received to do your work in one go (I don't know whether you do), then you have not done that part.

                  R 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @RuWex
                    Well done for connecting the readyRead signal to a MainWindow::readData() slot. But I'm afraid I don't think you have understood what you are doing, else you would have changed your GetReplyFromCamera(QString line) code to work from the bytes received. Instead you still have

                            m_serial->readyRead();
                            rep=m_serial->readAll();    //here rep always will be "" its not wait for I will send response...
                    

                    there, which makes no sense.

                    • Never call m_serial->readyRead(); yourself.
                    • Never call m_serial->readAll(); anywhere other then in the readyRead() slot (MainWindow::readData()).

                    Other than that, in principle if you want to pass the pass bytes received to GetReplyFromCamera() and process them immediately there then you have appropriate code. Note that you may receive the readyRead() signal and hence call GetReplyFromCamera() multiple times (there is no guarantee that all bytes sent from serial device will arrive in a single readyRead(), they could be split into more than one separate "packets"). If that is OK with your code/intention then it is fine. If you need to accumulate bytes received to do your work in one go (I don't know whether you do), then you have not done that part.

                    R Offline
                    R Offline
                    RuWex
                    wrote on last edited by
                    #9

                    @JonB my problem is that I have to use
                    rep=m_serial->readAll();
                    becuse I already have in mainWindow indlude to sendCommand page,
                    and if I will do include to mainWindow in sendCommands it will make me The circularity problem...
                    so what do you think I should do?

                    R jsulmJ 2 Replies Last reply
                    0
                    • R RuWex

                      @JonB my problem is that I have to use
                      rep=m_serial->readAll();
                      becuse I already have in mainWindow indlude to sendCommand page,
                      and if I will do include to mainWindow in sendCommands it will make me The circularity problem...
                      so what do you think I should do?

                      R Offline
                      R Offline
                      RuWex
                      wrote on last edited by
                      #10

                      @RuWex and other question,
                      how it should know that the whole reply recieve and continue with the code?

                      1 Reply Last reply
                      0
                      • R RuWex

                        @JonB my problem is that I have to use
                        rep=m_serial->readAll();
                        becuse I already have in mainWindow indlude to sendCommand page,
                        and if I will do include to mainWindow in sendCommands it will make me The circularity problem...
                        so what do you think I should do?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @RuWex said in serial- read -its not wait to get the response and recieve ""- why?:

                        my problem is that I have to use
                        rep=m_serial->readAll();

                        You're already doing that in MainWindow::readData()!
                        You can't simply get readAll() at arbitrary time and expect it to return something!
                        It will only return what is there (was received). So, only call it in MainWindow::readData().

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

                        1 Reply Last reply
                        2

                        • Login

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