Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt Quick QML Read Data over Serial Port and Show on GUI
QtWS25 Last Chance

Qt Quick QML Read Data over Serial Port and Show on GUI

Scheduled Pinned Locked Moved QML and Qt Quick
22 Posts 4 Posters 23.0k 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.
  • M Offline
    M Offline
    matinzk
    wrote on last edited by
    #1

    Hi everyone.

    How can I read data from a serial port on Linux with Qt Quick and show the results on the GUI with QML?

    Any help is much appreciated.

    M.

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

      Hi,

      Only with QtQuick ? You can't. However you can use QSerialPort for that on the C++ side for the serial port communication and then QtQuick for the UI.

      Hope it helps

      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
      1
      • M Offline
        M Offline
        matinzk
        wrote on last edited by
        #3

        That's exactly what I'd like to do, but haven't been able to find any examples.

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

          There's no example of that case precisely, but you can take a look at the Extending QML with C++ chapters from Qt's documentation, the bases are the same.

          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
          • M Offline
            M Offline
            matinzk
            wrote on last edited by
            #5

            Code has been deleted as we have abandoned it.

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

              Hi,

              So with QSerialPort they get data only once ? From a quick look, their code is doing that, get the data once, set it in your QML code and start the application. There's even a memory leak.

              Do you have access to the code they wrote for QSerialPort ?

              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
              • M Offline
                M Offline
                matinzk
                wrote on last edited by
                #7

                That's all the code they've written. They didn't use QSerialPort ta all but used ioinput stuff in C++ I believe.

                We asked them to get data from serial port and keep changing the GUI. However their code only does this once. They claim that with Qt you can only get the data once, when application starts, and not possible to keep receiving data and keep changing the GUI.

                We have told them that Qt is a powerful tool that's able to do but they're arguing otherwise.

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  valliammal
                  wrote on last edited by
                  #8

                  I am the contractor who written this code.

                  what this while(1) is doing. Is it not continuously looping and reading the data. It is a loop, and it is continously reading. it is not 1 time read.

                  while (1) {
                  char *data = GetNextLine(fp);
                  printf( " %s , %d\n", data, strlen(data));

                  ya, I had not cleared the buffers yet. But that is this customer kept asking for this data to be refreshed on the GUI UML continously.

                  For that , it needs to have additional triggers, like atleast a button, so that it will do the signal and read.

                  or it needs to do the serial port reading from QML. What the customer gave as requirement is , read from serial port and display . ya, ie done.
                  QT is taking the last data read and display in QML.
                  For refreshing QML atleast He should tell, there should be an additional button object. So that we press that and it continously refresh.
                  As I kept working on that, I had not yet deleted the memory allocated for the buffers. I kept helping him , explaining him. But he could not understand.

                  Now you tell me , after the app.exec, control goes in that GUI and never returns back to the program. Then without a signal and slot or without any check, How it will send message to the QT GUI?.

                  The QSerial port of the customer returns canRead() function to be false all the time. So I used C++ method to read the code.

                  Please explain if this canread() function comes as false, then no read can be performed. what needs to be done.

                  I am expert in my ways. So I used C++ to do this. Now about QT GUI refresh with the data for QML from the indefiniteloop, what I need to do?. without a button / or any other signal handlers?

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

                    The first while loop will run exactly once since at the end you either assert or call return app.exec().

                    There's no need for any infinite loop nor additional triggers. Use the asynchronous technique aka signals and slots. The terminal example from the qtserialport module shows how to do it.

                    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
                    1
                    • V Offline
                      V Offline
                      valliammal
                      wrote on last edited by
                      #10

                      It is running indefinitely and it is printing the output in the console log by keep reading and displaying. the major issue with QTSerial port is
                      it is getting recognized. But it is returning
                      canReadable as false.

                      ie the problem I have in that.

                      you are asking to use this way.

                      @
                      QSerialPort port;

                       // ... serial port initialization here
                      
                       // Connect signals/slots
                       connect(&port, SIGNAL(readyRead()), this, SLOT(readData()));
                      
                       port.open();
                      
                       // Start a new message loop on this thread
                       exec();
                      

                      @

                      But the following things in QTSerial port is returning false.

                      http://doc.qt.io/qt-5/qtserialport-terminal-example.html
                      and
                      http://qt-project.org/forums/viewthread/33090
                      http://qt.apidoc.info/5.2.0/qtserialport/examples.html
                      http://qt-project.org/wiki/QtSerialPort

                      He has problem with his Serial port way, I had corrected that for my work even 4 things on that and made the data to come by normal cpp reading.

                      Please let me know what to do for this?

                      @
                      #include <QtCore/QCoreApplication>
                      #include <QtCore/QDebug>

                      #include <QtSerialPort/QSerialPort>
                      #include <QtSerialPort/QSerialPortInfo>
                       
                      QT_USE_NAMESPACE
                       
                      int main(int argc, char *argv[]&#41;
                      {
                          QCoreApplication a(argc, argv);
                       
                          // Example use QSerialPortInfo
                          foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
                              qDebug() << "Name        : " << info.portName();
                              qDebug() << "Description : " << info.description();
                              qDebug() << "Manufacturer: " << info.manufacturer();
                       
                              // Example use QSerialPort
                              QSerialPort serial;
                              serial.setPort(info);
                              if (serial.open(QIODevice::ReadWrite))
                                  serial.close();
                          }
                         
                          return a.exec&#40;&#41;;
                      }
                      

                      @

                      This is taken from http://qt-project.org/wiki/QtSerialPort

                      This is the code. It is working upto recognization and give the corresponding manufacturer and the port number etc.
                      but the QSerialPort::canReadLine() also returning false.

                      had been referenced for this.

                      https://qt.gitorious.org/qt/qtserialport/source/286db284107b4bb343d97c46736555b70979bfcd:src/serialport/qserialport.cpp#L187-193

                      This is the source code of QtSerialPort had been referenced.,
                      CanReadLine() function is returning a false as the value.,

                      serial.open(QIODevice::ReadWrite) also returning false.

                      the QT Serial port device recognization had been done.

                      what needs to be done for this?. if it comes with false as value and not reading it.

                      thanks,
                      regards,
                      MVS

                      [edit: added missing coding tags @ SGaist]

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

                        Did you check what error you got since open failed ?

                        canReadLine will only return true if you received a line of data on your serial port.

                        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
                        • M Offline
                          M Offline
                          matinzk
                          wrote on last edited by
                          #12

                          The terminal example works fine on our Linux machine. Hence the port works absolutely fine. What we've noticed is that with the Qt terminal example the port initialises fine and it works even if the device that outputs the serial data is disconnected and reconnected. With the pure C++ code that the contractor has provided this is very glitch and manual. We need to go to Linux terminal and set the budrate every time off it to work.

                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            valliammal
                            wrote on last edited by
                            #13

                            So finally He agreed , He needs to set some values to make it to work.

                            " We need to go to Linux terminal and set the budrate every time off it to work. "

                            So this is his fault. And I kept trying with QTSerialPort several time and it had returned with CanReadWrite() as false.

                            I had provided the right soln and I know it is right. His serial port is not getting value with CanReadWrite() as true. He is now telling this.

                            In linux terminal port, He is setting some baud rate to make it to work.
                            I had verified , for corrections, 4 times on his port and made the normal way to work. Now for this , He is telling

                            this is not his fault. CanReadWrite() as true only QT Serial Port will work.

                            If QTSerialPort is working fine, then CanReadWrite() will return value as true.
                            ie the proof. So why it is returned as false?. whether he again claims that His serial port baud rate or his on off of device, or no data had come?.

                            we work by hrs. we are not infront of the machine and checking and doing it. When it returns false as value, I am sure it had not worked. He is now taking more time than needed.

                            As Expert always , I gives best soln. I had checked this and told him. Now I am really doubt to work with him again.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              matinzk
                              wrote on last edited by
                              #14

                              [quote author="valliammal" date="1418522987"]So finally He agreed , He needs to set some values to make it to work.

                              " We need to go to Linux terminal and set the budrate every time off it to work. "

                              So this is his fault. And I kept trying with QTSerialPort several time and it had returned with CanReadWrite() as false.

                              I had provided the right soln and I know it is right. His serial port is not getting value with CanReadWrite() as true. He is now telling this.

                              In linux terminal port, He is setting some baud rate to make it to work.
                              I had verified , for corrections, 4 times on his port and made the normal way to work. Now for this , He is telling

                              this is not his fault. CanReadWrite() as true only QT Serial Port will work.

                              If QTSerialPort is working fine, then CanReadWrite() will return value as true.
                              ie the proof. So why it is returned as false?. whether he again claims that His serial port baud rate or his on off of device, or no data had come?.

                              we work by hrs. we are not infront of the machine and checking and doing it. When it returns false as value, I am sure it had not worked. He is now taking more time than needed.

                              As Expert always , I gives best soln. I had checked this and told him. Now I am really doubt to work with him again.[/quote]

                              You're mixing things up.

                              The Qt terminal example uses the QtSerialPort library, and works fine on our machine. This means that it initialises the serial port correctly and hence reads it fine. Therefore there is nothing wrong with the actual serial port.

                              Your code needs this command to run on Linux terminal in order for it to work:
                              stty -F /dev/ttyUSB0 115200

                              This makes things very manual!

                              If the code that you tried with QtSerialPort library returns false with CanReadWrite(), there must be something wrong with your code and not with the actual serial port. As explained above Qt terminal example works fine and it does makes used of the QtSerialPort library.

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                matinzk
                                wrote on last edited by
                                #15

                                Hi guys, we have managed to use QtSerialPort libraries to achieve what we need.

                                Our serial read code runs in a separate thread in a forever to receive the data and update the GUI.

                                Here is the serial code. Would you kindly have a look and make sure it is good to go?

                                Our data that arrives looks like this:-
                                234|654\n

                                @QString elem0;
                                QString elem1;

                                forever {

                                        if (serialPort.waitForReadyRead(1500))  //set the timeout time for the port
                                        {
                                
                                            serialPort.flush();      /make sure there is no old data every time we want to read
                                      
                                            QByteArray data;
                                            QStringList myArrayOfNums;
                                
                                                      if (serialPort.canReadLine())   //If we can read the data then read it
                                                      {
                                                               data = serialPort.readLine();  //Read line of data as Byte Array
                                        
                                                               QString myData(data);     //Convert the received the data to String
                                                               myArrayOfNums = myData.split("|");   //Split the data as it's delimited and put into a String List
                                
                                                               //USE THE FINAL DATA
                                                               elem0 = myArrayOfNums[0].simplified(); //simplified removes any white spaces/characters
                                                               elem1 = myArrayOfNums[1].simplified();
                                                               
                                                               qDebug() << elem0;
                                                               qDebug() << elem1;
                                                        }
                                
                                
                                        }@
                                

                                One thing we've noticed is that when waitForReadyRead() is set to for example waitForReadyRead(30), so only 30 millisecs, then waitForReadyRead() returns false quite alot. Does anyone know the reason for that?

                                Additionally if we monitor the data that arrives into the Qt application, every often canReadLine() also returns false. Is there a way to make sure it always returns true so receiving data can become smoother and loss of data eliminated?

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

                                  Why not it in asynchronous mode and use readyRead() + readAll() ?

                                  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
                                  1
                                  • M Offline
                                    M Offline
                                    matinzk
                                    wrote on last edited by
                                    #17

                                    With readAll() some data still gets missing.

                                    Is it best to send data to the app with a "\n" at the end or with 2 different identifiers at the beginning and end?

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      matinzk
                                      wrote on last edited by
                                      #18

                                      Actually most of the issues are sorted.

                                      Still not using readyRead() though. However I have connected our microcontroller to our Raspberry Pi via the UART (Rx/Tx pins) with a level shifter.

                                      The Qt app cannot read anything from the /dev/ttyAMA0. It all works fine if the microcontroller is connected to the Raspberry Pi via USB.

                                      Is this normal?

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

                                        Depends, do you have the rights to read/write from /dev/ttyAMA0 ?

                                        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
                                        • M Offline
                                          M Offline
                                          matinzk
                                          wrote on last edited by
                                          #20

                                          Yes there are permissions.

                                          I can send data if I connect the rx/tx pins of the RPi to a FTDI board and send data from a terminal program from a PC.

                                          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