Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue reading from QSerialPort
Qt 6.12 LTS is out! See what's new in the release blog

Issue reading from QSerialPort

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 5.9k 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.
  • JonBJ JonB

    @Anna_64
    Don't understand your question. But as a general (doubtless unrelated) point:

    serial->write( rq);
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    

    I assume the intention here is that you write some data and then as result when the other end reads it it sends back some data for you to read? If that is so, I suggest you swap the order of your calls to:

    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    serial->write( rq);
    

    Your way is (at least theoretically) open to the reply being received immediately after the write(), before you have attached your signal slot handler. I think it's good general practice to attach your handlers before they can possibly get called, just in case....

    A Offline
    A Offline
    Anna_64
    wrote on last edited by
    #6

    @JonB ican write data to device but cannot read from the device using serial->readAll();

    JonBJ 1 Reply Last reply
    0
    • A Anna_64

      @JonB ican write data to device but cannot read from the device using serial->readAll();

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

      @Anna_64
      Well I don't have your device so I do not know whether it can send, whether it is sending, whether you are receiving, whether your code is correct, or what. Nor do I know what your code where you call serial->readAll(); looks like (where it is being called from etc.) So far you have only shown code which sends.

      If the code is just as you show, after your ba=serial->readAll(); what about at least qDebug() << ba.size() so we can at least see how many bytes were received? And if that turns out to be 0, don't forget:

      This function has no way of reporting errors; returning an empty QByteArray can mean either that no data was currently available for reading, or that an error occurred.

      You might find that https://doc.qt.io/qt-5/qiodevice.html#read or https://doc.qt.io/qt-5/qiodevice.html#errorString would then tell you if there was an error.

      A 2 Replies Last reply
      1
      • JonBJ JonB

        @Anna_64
        Well I don't have your device so I do not know whether it can send, whether it is sending, whether you are receiving, whether your code is correct, or what. Nor do I know what your code where you call serial->readAll(); looks like (where it is being called from etc.) So far you have only shown code which sends.

        If the code is just as you show, after your ba=serial->readAll(); what about at least qDebug() << ba.size() so we can at least see how many bytes were received? And if that turns out to be 0, don't forget:

        This function has no way of reporting errors; returning an empty QByteArray can mean either that no data was currently available for reading, or that an error occurred.

        You might find that https://doc.qt.io/qt-5/qiodevice.html#read or https://doc.qt.io/qt-5/qiodevice.html#errorString would then tell you if there was an error.

        A Offline
        A Offline
        Anna_64
        wrote on last edited by
        #8

        @JonB i can read also
        its like "\xAAU\t\x00\x00 \x00\x02\x00\x00\x03!"
        and lenght s 12
        now i have to take only 10th one that is 03 how to do that

        JonBJ 1 Reply Last reply
        0
        • JonBJ JonB

          @Anna_64
          Well I don't have your device so I do not know whether it can send, whether it is sending, whether you are receiving, whether your code is correct, or what. Nor do I know what your code where you call serial->readAll(); looks like (where it is being called from etc.) So far you have only shown code which sends.

          If the code is just as you show, after your ba=serial->readAll(); what about at least qDebug() << ba.size() so we can at least see how many bytes were received? And if that turns out to be 0, don't forget:

          This function has no way of reporting errors; returning an empty QByteArray can mean either that no data was currently available for reading, or that an error occurred.

          You might find that https://doc.qt.io/qt-5/qiodevice.html#read or https://doc.qt.io/qt-5/qiodevice.html#errorString would then tell you if there was an error.

          A Offline
          A Offline
          Anna_64
          wrote on last edited by
          #9

          @JonB its showing 12bytes

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

            Hi,

            I have forked your question into its own topic thread.

            Why are you creating a new QSerialPort object each time you call on_COM9_clicked ?

            Do you have a protocol to communicate with your device ? e.g. a start and stop sequence so you can separate frames you send and receive ?

            If you have one, then you can parse the received data once you know you have enough bytes to contain at least one frame.

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

            A 1 Reply Last reply
            2
            • A Anna_64

              @JonB i can read also
              its like "\xAAU\t\x00\x00 \x00\x02\x00\x00\x03!"
              and lenght s 12
              now i have to take only 10th one that is 03 how to do that

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

              @Anna_64 said in Issue reading from QSerialPort:

              now i have to take only 10th one that is 03 how to do that

              It seems a surprising question to ask, but if that's really what you want to know it would be ba[9].

              You should also always read, understand & act on what @SGaist asks/suggests.

              A 1 Reply Last reply
              0
              • JonBJ JonB

                @Anna_64 said in Issue reading from QSerialPort:

                now i have to take only 10th one that is 03 how to do that

                It seems a surprising question to ask, but if that's really what you want to know it would be ba[9].

                You should also always read, understand & act on what @SGaist asks/suggests.

                A Offline
                A Offline
                Anna_64
                wrote on last edited by
                #12

                @JonB how to split a bin file of size 1050 bytes into 64 chunks in qt

                JonBJ 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  I have forked your question into its own topic thread.

                  Why are you creating a new QSerialPort object each time you call on_COM9_clicked ?

                  Do you have a protocol to communicate with your device ? e.g. a start and stop sequence so you can separate frames you send and receive ?

                  If you have one, then you can parse the received data once you know you have enough bytes to contain at least one frame.

                  A Offline
                  A Offline
                  Anna_64
                  wrote on last edited by
                  #13

                  @SGaist i want to split a file which looks like
                  QFile file("C:\Users\Desktop\binfile_enc.bin");
                  this file is of size 1050.624 bytes
                  i want to split into 64 chunks please help

                  1 Reply Last reply
                  0
                  • A Anna_64

                    @JonB how to split a bin file of size 1050 bytes into 64 chunks in qt

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

                    @Anna_64

                    @JonB how to split a bin file of size 1050 bytes into 64 chunks in qt

                    This has nothing to do with Qt. You would obviously split it into chunks of 1050 / 64 bytes.

                    A 1 Reply Last reply
                    2
                    • JonBJ JonB

                      @Anna_64

                      @JonB how to split a bin file of size 1050 bytes into 64 chunks in qt

                      This has nothing to do with Qt. You would obviously split it into chunks of 1050 / 64 bytes.

                      A Offline
                      A Offline
                      Anna_64
                      wrote on last edited by
                      #15

                      @JonB its a stupid answer

                      JonBJ 1 Reply Last reply
                      -5
                      • A Anna_64

                        @JonB its a stupid answer

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

                        @Anna_64

                        @JonB its a stupid answer

                        Thank you so much. It's an exact answer to the question as posed by you. And follows on from the time I spent trying to answer your earlier questions. I haven't insulted you so far in your questions, thanks for insulting me. You are rude. I'm done with you, find out how people will respond to you, given that this support forum provides voluntary help....

                        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
                        • Unsolved