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. Single line receive data from Serial Port
Forum Updated to NodeBB v4.3 + New Features

Single line receive data from Serial Port

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 1.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.
  • S Offline
    S Offline
    Shivam Sharma
    wrote on last edited by VRonin
    #1

    Hi guys. Currently successfully being able to send data from PC to serial port. Also can send data from serial port to PC but when i send the data it goes in column. Eg. if i send 'hey', it goes as:
    h
    e
    y.

    Can't i read this in one line? Need help.
    The code for the receive part is:

    void MainWindow::serialReceived()
    {
        QByteArray serialData;
        serialData = serial->readLine();
        QString data = QString( serialData ).remove("\r").remove("\n");
        serialData.clear();
        ui->receiver->append(data);
    }
    

    PFA of my screen.
    Screenshot from 2020-01-23 12-38-46.png image url)

    jsulmJ aha_1980A 2 Replies Last reply
    0
    • S Shivam Sharma

      Hi guys. Currently successfully being able to send data from PC to serial port. Also can send data from serial port to PC but when i send the data it goes in column. Eg. if i send 'hey', it goes as:
      h
      e
      y.

      Can't i read this in one line? Need help.
      The code for the receive part is:

      void MainWindow::serialReceived()
      {
          QByteArray serialData;
          serialData = serial->readLine();
          QString data = QString( serialData ).remove("\r").remove("\n");
          serialData.clear();
          ui->receiver->append(data);
      }
      

      PFA of my screen.
      Screenshot from 2020-01-23 12-38-46.png image url)

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

      @Shivam-Sharma Simply read the data into a buffer and when you got everything (do you have a protocol?) then append it to ui->receiver.
      Also, what type is ui->receiver?

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

      S 1 Reply Last reply
      3
      • S Shivam Sharma

        Hi guys. Currently successfully being able to send data from PC to serial port. Also can send data from serial port to PC but when i send the data it goes in column. Eg. if i send 'hey', it goes as:
        h
        e
        y.

        Can't i read this in one line? Need help.
        The code for the receive part is:

        void MainWindow::serialReceived()
        {
            QByteArray serialData;
            serialData = serial->readLine();
            QString data = QString( serialData ).remove("\r").remove("\n");
            serialData.clear();
            ui->receiver->append(data);
        }
        

        PFA of my screen.
        Screenshot from 2020-01-23 12-38-46.png image url)

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Shivam-Sharma said in Single line receive data from Serial Port:

        QByteArray serialData;
        serialData = serial->readLine();
        QString data = QString( serialData ).remove("\r").remove("\n");
        serialData.clear();
        ui->receiver->append(data);

        Please try this:

        if (serial->canReadLine()) {
          const QByteArray serialData = serial->readLine().trimmed();
          ui->receiver->append(serialData);
        }
        

        Calling readLine() without canReadLine() before may give unexpected results.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • jsulmJ jsulm

          @Shivam-Sharma Simply read the data into a buffer and when you got everything (do you have a protocol?) then append it to ui->receiver.
          Also, what type is ui->receiver?

          S Offline
          S Offline
          Shivam Sharma
          wrote on last edited by
          #4

          Nope, not working.

          KroMignonK JonBJ 2 Replies Last reply
          0
          • S Shivam Sharma

            Nope, not working.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @Shivam-Sharma What did not work? The code proposed by @aha_1980 will only show a line in you GUI if you hit "Enter" on your keyboard.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            3
            • S Shivam Sharma

              Nope, not working.

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

              @Shivam-Sharma
              As @KroMignon has just said. You saying "Nope, not working." doesn't help us much. Does not compile? Does not return anything? Does not return a whole line? Returns wrong characters? Whole program crashes?

              If canReadLine() returns true then readLine() should return the first line. Unless you're claiming the Qt methods don't work as documented.

              S 1 Reply Last reply
              2
              • S Offline
                S Offline
                Shivam Sharma
                wrote on last edited by
                #7

                Also, i think I've figured out that if I copy a text 'hello' all together then it would transfer the data as it is but if on gtk+ i give the input one by one as hey, it prints as
                h
                e
                y.

                I'm guessing this is some inbuilt issue.

                KroMignonK 1 Reply Last reply
                0
                • S Shivam Sharma

                  Also, i think I've figured out that if I copy a text 'hello' all together then it would transfer the data as it is but if on gtk+ i give the input one by one as hey, it prints as
                  h
                  e
                  y.

                  I'm guessing this is some inbuilt issue.

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #8

                  @Shivam-Sharma said in Single line receive data from Serial Port:

                  I'm guessing this is some inbuilt issue.

                  Can you tell us which version of Qt you are using? Just be sure there no confusion, I mean Qt Kit not QtCreator.

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  S 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Shivam-Sharma
                    As @KroMignon has just said. You saying "Nope, not working." doesn't help us much. Does not compile? Does not return anything? Does not return a whole line? Returns wrong characters? Whole program crashes?

                    If canReadLine() returns true then readLine() should return the first line. Unless you're claiming the Qt methods don't work as documented.

                    S Offline
                    S Offline
                    Shivam Sharma
                    wrote on last edited by
                    #9

                    @JonB Sorry for not being precise bu ya, i mean readline() also doesn't execute the whole thing. Anyways i guess this problem should be solved when the PC would be getting data from any sensor, no?

                    JonBJ 1 Reply Last reply
                    0
                    • S Shivam Sharma

                      @JonB Sorry for not being precise bu ya, i mean readline() also doesn't execute the whole thing. Anyways i guess this problem should be solved when the PC would be getting data from any sensor, no?

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

                      @Shivam-Sharma
                      If (and only if) you are using the canReadline() code and you are saying readLine() then returns just one letter (or indeed anything) when you type one letter on the device but do not hit "Enter", or if you type multiple letters and hit "Enter" at the end but it returns just one letter, then something strange is going on....

                      1 Reply Last reply
                      0
                      • KroMignonK KroMignon

                        @Shivam-Sharma said in Single line receive data from Serial Port:

                        I'm guessing this is some inbuilt issue.

                        Can you tell us which version of Qt you are using? Just be sure there no confusion, I mean Qt Kit not QtCreator.

                        S Offline
                        S Offline
                        Shivam Sharma
                        wrote on last edited by
                        #11

                        @KroMignon Desktop Qt 5.12.6 GCC 64Bit

                        Pablo J. RoginaP 1 Reply Last reply
                        0
                        • S Shivam Sharma

                          @KroMignon Desktop Qt 5.12.6 GCC 64Bit

                          Pablo J. RoginaP Offline
                          Pablo J. RoginaP Offline
                          Pablo J. Rogina
                          wrote on last edited by
                          #12

                          @Shivam-Sharma

                          but if on gtk+ i give the input one by one as

                          you were told in your other post related to the subject (BTW you may want to close it, since you created this new post already) to go and use some other terminal program (i.e. minicom, moserial, etc.) to test your Qt app.

                          In any case, what happens if you connect your "gtk+" to some other terminal program?
                          the data you receive is a character per line as you see in your Qt app? Checking that way you can identify how that "gtk+" app is behaving...

                          i guess this problem should be solved when the PC would be getting data from any sensor, no?

                          Well, that's not a good approach to assure the quality of your code...

                          What if the sensor behaves the same of your "gtk+" program?

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          1

                          • Login

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