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. QML SerialPort: using readAll or readLine but receive multi lines

QML SerialPort: using readAll or readLine but receive multi lines

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
12 Posts 5 Posters 927 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.
  • T Offline
    T Offline
    taind
    wrote on last edited by taind
    #1

    I want to received data only 1 line/1time.Thank in advance
    open serial:

    void SerialPort:: openSerialPort (QString comName, int baudRate) {
         ...
         connect (serial, SIGNAL (readyRead()), this, SLOT (readFromSerialPort()));
    }
    

    read from serial funtion

    void SerialPort::readFromSerialPort() {
    \\      if (serial->canReadLine())
    \\     {
                 QByteArray data = QByteArray (serial->readAll());
                 emit getData (data.toHex());
     \\     }
    }
    

    ...
    qml file:

    Connections {
         target:  MySerial
         onGetData: {
              textArea.append(data)
         }
    }
    

    this is result (3 lines):
    z3790340735211_f31e0379b09263d5eabcf2e345aa3731.jpg

    jsulmJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      readAll reads all the data. Did you try with read(..) or readLine(..) with maxsize ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1
      • T Offline
        T Offline
        taind
        wrote on last edited by
        #3

        I already tried with read and readLine but same result

        1 Reply Last reply
        0
        • T taind

          I want to received data only 1 line/1time.Thank in advance
          open serial:

          void SerialPort:: openSerialPort (QString comName, int baudRate) {
               ...
               connect (serial, SIGNAL (readyRead()), this, SLOT (readFromSerialPort()));
          }
          

          read from serial funtion

          void SerialPort::readFromSerialPort() {
          \\      if (serial->canReadLine())
          \\     {
                       QByteArray data = QByteArray (serial->readAll());
                       emit getData (data.toHex());
           \\     }
          }
          

          ...
          qml file:

          Connections {
               target:  MySerial
               onGetData: {
                    textArea.append(data)
               }
          }
          

          this is result (3 lines):
          z3790340735211_f31e0379b09263d5eabcf2e345aa3731.jpg

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

          @taind It should work with canReadline and readLine. How is the data sent? What data is actually sent? You convert it to hex - is it possible that you're sending binary data?

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

          1 Reply Last reply
          0
          • T Offline
            T Offline
            taind
            wrote on last edited by
            #5

            @jsulm i received from XCTU software and I don't know why "serial->canReadLine()" this case is always "false"

            jsulmJ J.HilkJ 2 Replies Last reply
            0
            • T taind

              @jsulm i received from XCTU software and I don't know why "serial->canReadLine()" this case is always "false"

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

              @taind said in QML SerialPort: using readAll or readLine but receive multi lines:

              XCTU software

              I don't know what it is and what data it is sending. If it sends binary data I would not expect to be able to read it correctly line by line. You really should first clarify what type of data you actually get (binary? text?).

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

              1 Reply Last reply
              1
              • T taind

                @jsulm i received from XCTU software and I don't know why "serial->canReadLine()" this case is always "false"

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

                @taind said in QML SerialPort: using readAll or readLine but receive multi lines:

                @jsulm i received from XCTU software and I don't know why "serial->canReadLine()" this case is always "false"

                probably because your frame doesn't end on 0x0A ? the last one seems to be 0xFD


                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
                • T Offline
                  T Offline
                  taind
                  wrote on last edited by
                  #8

                  @jsulm if I don't convert toHex().. data return text type when readLine from serial

                  jsulmJ 1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    taind
                    wrote on last edited by
                    #9

                    @J-Hilk Thank you for answer!
                    I think so. Some frame finish with "checksum" . Maybe need a intermediary var to handle them.

                    JonBJ 1 Reply Last reply
                    0
                    • T taind

                      @jsulm if I don't convert toHex().. data return text type when readLine from serial

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

                      @taind said in QML SerialPort: using readAll or readLine but receive multi lines:

                      data return text type when readLine from serial

                      That doesn't mean that the other side is sending text...

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

                      T 1 Reply Last reply
                      0
                      • T taind

                        @J-Hilk Thank you for answer!
                        I think so. Some frame finish with "checksum" . Maybe need a intermediary var to handle them.

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

                        @taind
                        Since you do not seem to have any idea what bytes are being sent to you, before trying to "read lines" you should really just use readAll() and (convert the bytes to hex if you wish) look at what is being actually being sent before proceeding to code.

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @taind said in QML SerialPort: using readAll or readLine but receive multi lines:

                          data return text type when readLine from serial

                          That doesn't mean that the other side is sending text...

                          T Offline
                          T Offline
                          taind
                          wrote on last edited by
                          #12

                          @jsulm Thank you so much for supporting!
                          I tried with Hercules or Terminal v.1.9 software. XCTU software only respones when receive hex and return text or hex. But probably my problem because of XCTU received/ sent type data

                          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