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. QSerialPort's read buffer is always empty while running another func.
Forum Updated to NodeBB v4.3 + New Features

QSerialPort's read buffer is always empty while running another func.

Scheduled Pinned Locked Moved Unsolved General and Desktop
40 Posts 6 Posters 13.3k Views 1 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.
  • G Offline
    G Offline
    Gokhan
    wrote on last edited by Gokhan
    #22

    That is a part of the user manual. I don't know how to use it like you said? However, it's normally running if use the waitforfinish function like I have mentioned above.

    The usage is : 
        img_cvt  -i  inputfilename  -f  format
    
             format is as follow:
                0 : ARGB1555 [default]
                1 : L1
                2 : L4
                3 : L8
                4 : RGB332
                5 : ARGB2
                6 : ARGB4
                7 : RGB565
                8 : PALETTEED [FT80X only]    
                9 : L2 [FT81X only]
    
    Example : 
        img_cvt  -i  lenaface40.png  -f   8
    
    
    jsulmJ 1 Reply Last reply
    0
    • G Gokhan

      That is a part of the user manual. I don't know how to use it like you said? However, it's normally running if use the waitforfinish function like I have mentioned above.

      The usage is : 
          img_cvt  -i  inputfilename  -f  format
      
               format is as follow:
                  0 : ARGB1555 [default]
                  1 : L1
                  2 : L4
                  3 : L8
                  4 : RGB332
                  5 : ARGB2
                  6 : ARGB4
                  7 : RGB565
                  8 : PALETTEED [FT80X only]    
                  9 : L2 [FT81X only]
      
      Example : 
          img_cvt  -i  lenaface40.png  -f   8
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #23

      @Gokhan
      Just pass each parameter as one string in the string list (as shown here http://doc.qt.io/qt-5/qprocess.html):

      arguments <<"/k"<<"img_cvt"<<"-i"<<QString(%1).arg(img_down[0].image_with_ext)<<"-f"<<"0";
      

      Also you should connect http://doc.qt.io/qt-5/qprocess.html#errorOccurred signal to a slot to check whether something went wrong.

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

      1 Reply Last reply
      2
      • G Offline
        G Offline
        Gokhan
        wrote on last edited by Gokhan
        #24

        @jsulm I just changed my code like you said and connected the signals that are like below in the constructor. The issue isn't solved. ErrorOccurred don't give an error while running.

            proc = new QProcess ();
            connect(proc, & QProcess::errorOccurred, proc,[=](QProcess::ProcessError error){qDebug() <<"Process had an error:" <<  error; });
            connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)),
                        this, SLOT(finishProcess(int, QProcess::ExitStatus)) );
        
        jsulmJ 1 Reply Last reply
        0
        • G Gokhan

          @jsulm I just changed my code like you said and connected the signals that are like below in the constructor. The issue isn't solved. ErrorOccurred don't give an error while running.

              proc = new QProcess ();
              connect(proc, & QProcess::errorOccurred, proc,[=](QProcess::ProcessError error){qDebug() <<"Process had an error:" <<  error; });
              connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)),
                          this, SLOT(finishProcess(int, QProcess::ExitStatus)) );
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #25

          @Gokhan Connect http://doc.qt.io/qt-5/qprocess.html#readyReadStandardError and http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput to slots and print out what http://doc.qt.io/qt-5/qprocess.html#readAllStandardError and http://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput return. Maybe you will then see what is going on.

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Gokhan
            wrote on last edited by Gokhan
            #26

            @jsulm There is no any readAllStandardError and you can see the output of readAllStandardOutput below. Everything shows normal.

            readyReadStandardOutput "image conversion utility for FT8XX V0.7\r\nconvert complete!\r\n"
            readyReadStandardOutput "\r\nD:I\\conv>"
            
            1 Reply Last reply
            0
            • G Offline
              G Offline
              Gokhan
              wrote on last edited by
              #27

              @jsulm @J-Hilk and @SGaist

              I'm trying that it, but I get an error that is "no matching function for call to 'image_down_dialog::connect(QProcess*&, <unresolved overloaded function type>, image_down_dialog*, void (image_down_dialog::*)(int, QProcess::ExitStatus))' " Why do I get this?
              ^
              connect(proc, &QProcess::finished, this, &image_down_dialog::finishProcess);

              J.HilkJ 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #28

                How did you declare proc?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • G Gokhan

                  @jsulm @J-Hilk and @SGaist

                  I'm trying that it, but I get an error that is "no matching function for call to 'image_down_dialog::connect(QProcess*&, <unresolved overloaded function type>, image_down_dialog*, void (image_down_dialog::*)(int, QProcess::ExitStatus))' " Why do I get this?
                  ^
                  connect(proc, &QProcess::finished, this, &image_down_dialog::finishProcess);

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #29

                  @Gokhan
                  Sadly, Signal finished is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast:

                  connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), [=](int exitCode, QProcess::ExitStatus exitStatus){ /* ... */ });
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    Gokhan
                    wrote on last edited by Gokhan
                    #30

                    @J-Hilk It should be connected below to compile successfully, so it's static cast. I did it and it can compile and connect now without an issue. However, I haven't solved this problem yet, it isn't still giving the finished event. Can it be a bug with qprocess ? Should I write a bug report?
                    QObject::connect(proc, (void (QProcess::*)(int,QProcess::ExitStatus))&QProcess::finished, this, &image_down_dialog::finished);

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #31

                      You should rather use qOverload. In the case you can't, use a static_cast like @J-Hilk shown, not a C-Style cast.

                      As for never finishing, maybe because cmd.exe is still running. You are passing the /k parameter to cmd.exe which AFAIK means run the command and return to the CMD prompt. Shouldn't you be using /C ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • G Offline
                        G Offline
                        Gokhan
                        wrote on last edited by
                        #32

                        @SGaist thank you. The problem was solved when changed "/k" with "/c". However, the problem which I have mentioned in the title hasn't been solved yet. I'm still missing some data during communication with MCU through the serial port. The missing data are around 3K while sending 300K.

                        Also, I able to read max 512 byte in the readyRead signal, Is it writing to its read buffer during making another process like filling a table? If yes, how can I read the all buffer without missing any data?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #33

                          Do you have any protocol established for the communication ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            Gokhan
                            wrote on last edited by
                            #34

                            @SGaist I have just solved this problem. it was caused by debugging mode. After it was realesed and run .exe, it normally ran, until the application window is moved. Yes, I have a new problem :). It's lagging and missing data during moving or resizing the application window. I just tried to up to real time priority for windows on task manager, but it's still missing.

                            J.HilkJ 1 Reply Last reply
                            0
                            • G Gokhan

                              @SGaist I have just solved this problem. it was caused by debugging mode. After it was realesed and run .exe, it normally ran, until the application window is moved. Yes, I have a new problem :). It's lagging and missing data during moving or resizing the application window. I just tried to up to real time priority for windows on task manager, but it's still missing.

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #35

                              @Gokhan
                              how about moving your QSerialPort stuff into int's own thread? Should make things a bit smoother as in its not blocked when your main thread is busy with ui-stuff.


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                Gokhan
                                wrote on last edited by
                                #36

                                @J-Hilk I have already connected to a function and read it in this function. I don't make anything other this, it only reads the serial port and converts the data. Until moving the window, I tried it for around an hour, it never misses data. Actually, not only moving, it also has this problem during two-clicking the window's border.

                                J.HilkJ 1 Reply Last reply
                                0
                                • G Gokhan

                                  @J-Hilk I have already connected to a function and read it in this function. I don't make anything other this, it only reads the serial port and converts the data. Until moving the window, I tried it for around an hour, it never misses data. Actually, not only moving, it also has this problem during two-clicking the window's border.

                                  J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by J.Hilk
                                  #37

                                  @Gokhan
                                  never the less you should seperate your QSerialPort interaction from your GUI-Stuff. If a rather empty GUI-Resize causes you to lose data, imagine what will happen if your programm eventually becomes more complex.

                                  From your description its is critical that no informations are lost. So make a workerclass for your SerialPort interaction and run it in its own thread. Eventually raise that thread priority too.

                                  And you should be fine. Its easy enough to do with qt.

                                  Here a link that will help:


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    Gokhan
                                    wrote on last edited by Gokhan
                                    #38

                                    @J-Hilk OK. I have to use a Qthread and read buffer with it. So, how should I read the buffer? Which registers do I check before reading? How do I able to read periodically? For example, I'm using the following codes, but it can't connect the serial port.
                                    The process function is the same with the link.

                                        QThread* thread = new QThread;
                                        serialPort.moveToThread(thread);
                                        connect(&serialPort, SIGNAL (error(QString)), this, SLOT (errorString(QString)));
                                        connect(thread, SIGNAL (started()), &serialPort, SLOT (process()));
                                        connect(&serialPort, SIGNAL (finished()), thread, SLOT (quit()));
                                        connect(&serialPort, SIGNAL (finished()), &serialPort, SLOT (deleteLater()));
                                        connect(thread, SIGNAL (finished()), thread, SLOT (deleteLater()));
                                        thread->start();
                                    
                                    void serial_port::process()
                                    {
                                        for(;;)
                                        {
                                            if(serial->isOpen())
                                            {
                                                msleep(100);
                                                if (serial->waitForReadyRead(100))
                                                {
                                                    // read request
                                                    newdata.append(serial->readAll());
                                                    while (serial->waitForReadyRead(10))
                                                        newdata.append(serial->readAll());
                                                }
                                            }
                                        }
                                    }
                                    
                                    J.HilkJ 1 Reply Last reply
                                    0
                                    • G Gokhan

                                      @J-Hilk OK. I have to use a Qthread and read buffer with it. So, how should I read the buffer? Which registers do I check before reading? How do I able to read periodically? For example, I'm using the following codes, but it can't connect the serial port.
                                      The process function is the same with the link.

                                          QThread* thread = new QThread;
                                          serialPort.moveToThread(thread);
                                          connect(&serialPort, SIGNAL (error(QString)), this, SLOT (errorString(QString)));
                                          connect(thread, SIGNAL (started()), &serialPort, SLOT (process()));
                                          connect(&serialPort, SIGNAL (finished()), thread, SLOT (quit()));
                                          connect(&serialPort, SIGNAL (finished()), &serialPort, SLOT (deleteLater()));
                                          connect(thread, SIGNAL (finished()), thread, SLOT (deleteLater()));
                                          thread->start();
                                      
                                      void serial_port::process()
                                      {
                                          for(;;)
                                          {
                                              if(serial->isOpen())
                                              {
                                                  msleep(100);
                                                  if (serial->waitForReadyRead(100))
                                                  {
                                                      // read request
                                                      newdata.append(serial->readAll());
                                                      while (serial->waitForReadyRead(10))
                                                          newdata.append(serial->readAll());
                                                  }
                                              }
                                          }
                                      }
                                      
                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #39

                                      @Gokhan

                                      ok,

                                      same basic info.
                                      The constructor, of your serial port class, should be empty. Everything necessery should be initialized after the thread is started. That includes creating the instance of the QSerialPort as well as all connections:

                                      //serialPort class
                                      
                                      void serialPort::init(){
                                          serial = new QSerialPort();
                                          connect(serial, &QSerialPort::readyRead, this, serialPort::newData);
                                      
                                      }
                                      
                                      void serialPort::newData{
                                         QByteArray bArray = serial->readAll();
                                         //do Stuff
                                      ....
                                         emit finishedData(data);
                                      }
                                      

                                      QSerialPort should emit the readyRead Signal when ever it has new data to be processed. Just connect to signal to a slot and handle the data there. -> The Thread will be more or less idle - only running its event loop- but still react as soon as new data is available.

                                      the thread handling in your MainClass seems good enough:

                                      QThread* thread = new QThread;
                                      serialPort *mySerialPort = new serialPort();
                                      mySerialPort -> moveToThread(thread);
                                          
                                      connect(mySerialPort , SIGNAL (error(QString)), this, SLOT (errorString(QString)));
                                      connect(mySerialPort , SIGNAL (finishedData(QVariant)), this, SLOT (displayData(QVariant)));
                                      connect(thread, SIGNAL (started()), mySerialPort , SLOT (init()));
                                      connect(mySerialPort , SIGNAL (finished()), thread, SLOT (quit()));
                                      connect(mySerialPort , SIGNAL (finished()), &serialPort, SLOT (deleteLater()));
                                      connect(thread, SIGNAL (finished()), thread, SLOT (deleteLater()));
                                      thread->start();
                                      

                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        Gokhan
                                        wrote on last edited by
                                        #40

                                        @J-Hilk I have just changed like you said, but it hasn't been solved again. It continues to miss some data during resizing or moving windows.

                                        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