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. Issue reading from QSerialPort
Qt 6.11 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.5k 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.
  • A Offline
    A Offline
    Anna_64
    wrote on last edited by
    #1

    i want to read a qbyte array how to do that
    my array look like
    QByteArray rq;
    if
    ( serial->open(QIODevice::ReadWrite)==true){
    qDebug()<<"connected";

        rq.resize(7);
        rq[0] = 0xAA;
        rq[1] = 0x55;
        rq[2] = 0x07;
        rq[3] = 0x20;
        rq[4] = 0x00;
        rq[5] = 0x00;
        rq[6] = 0xBB;
    

    }else
    {
    qDebug()<<"Not connected";
    }
    serial->write( rq);
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    }
    this bytedata has version number in it i want to find the version number in the rq[7] am sending and want to write it

    JonBJ jsulmJ 2 Replies Last reply
    0
    • A Anna_64

      i want to read a qbyte array how to do that
      my array look like
      QByteArray rq;
      if
      ( serial->open(QIODevice::ReadWrite)==true){
      qDebug()<<"connected";

          rq.resize(7);
          rq[0] = 0xAA;
          rq[1] = 0x55;
          rq[2] = 0x07;
          rq[3] = 0x20;
          rq[4] = 0x00;
          rq[5] = 0x00;
          rq[6] = 0xBB;
      

      }else
      {
      qDebug()<<"Not connected";
      }
      serial->write( rq);
      connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
      }
      this bytedata has version number in it i want to find the version number in the rq[7] am sending and want to write it

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

      @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 2 Replies Last reply
      5
      • A Anna_64

        i want to read a qbyte array how to do that
        my array look like
        QByteArray rq;
        if
        ( serial->open(QIODevice::ReadWrite)==true){
        qDebug()<<"connected";

            rq.resize(7);
            rq[0] = 0xAA;
            rq[1] = 0x55;
            rq[2] = 0x07;
            rq[3] = 0x20;
            rq[4] = 0x00;
            rq[5] = 0x00;
            rq[6] = 0xBB;
        

        }else
        {
        qDebug()<<"Not connected";
        }
        serial->write( rq);
        connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
        }
        this bytedata has version number in it i want to find the version number in the rq[7] am sending and want to write it

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

        @Anna_64

        1. Your question is not related to this thread as you're not using QDataStream, you should open your own thread
        2. Your question is not clear: "i want to find the version number in the rq[7] am sending and want to write it" - write it to where, and if it is already in rq[7] then what is the problem in finding it?

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

        1 Reply Last reply
        3
        • 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
          #4

          @JonB QSerialPort *serial;
          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          }
          MainWindow::~MainWindow()
          {
          delete ui;
          serial->close();
          }
          void MainWindow::serialReceived()
          {
          QByteArray ba;
          ba=serial->readAll();
          //qDebug()<<
          }
          void MainWindow::on_Connect_clicked()
          {
          QFile file("C:\Users\pavt\Desktop\binfile_enc.bin");
          if (!file.open(QIODevice::ReadOnly)) return;
          QByteArray iContents = file.readAll();
          ushort c3 = 0xFF;
          c3 = (unsigned char) iContents.at(2) | (unsigned char) iContents.at(3) << 8;
          qDebug()<<file.size();
          }
          void MainWindow::on_COM9_clicked()
          {
          serial = new QSerialPort(this);
          connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
          serial->setPortName("COM9");
          serial->setBaudRate(QSerialPort::Baud9600);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::NoParity);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setFlowControl(QSerialPort::NoFlowControl);
          QByteArray rq;
          if
          ( serial->open(QIODevice::ReadWrite)==true){
          qDebug()<<"connected";

              rq.resize(10);
              rq[0] = 0xAA;
              rq[1] = 0x55;
              rq[2] = 0x07;
              rq[3] = 0x00;
              rq[4] = 0x00;
              rq[5] = 0x20;
              rq[6] = 0x00;
              rq[7] = 0x02;
              rq[8] = 0x00;
              rq[9] = 0xBB;
          

          }else
          {
          qDebug()<<"Not connected";
          }
          serial->write( rq);
          }
          this s the data am sending to a device and it writes this data into a device now i want to read the data from the device because rq[8] contains my version number i want to fetch that version number inside a button click so when i click button named version i should display my version data which is now written in device

          JonBJ 1 Reply Last reply
          0
          • A Anna_64

            @JonB QSerialPort *serial;
            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);
            }
            MainWindow::~MainWindow()
            {
            delete ui;
            serial->close();
            }
            void MainWindow::serialReceived()
            {
            QByteArray ba;
            ba=serial->readAll();
            //qDebug()<<
            }
            void MainWindow::on_Connect_clicked()
            {
            QFile file("C:\Users\pavt\Desktop\binfile_enc.bin");
            if (!file.open(QIODevice::ReadOnly)) return;
            QByteArray iContents = file.readAll();
            ushort c3 = 0xFF;
            c3 = (unsigned char) iContents.at(2) | (unsigned char) iContents.at(3) << 8;
            qDebug()<<file.size();
            }
            void MainWindow::on_COM9_clicked()
            {
            serial = new QSerialPort(this);
            connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
            serial->setPortName("COM9");
            serial->setBaudRate(QSerialPort::Baud9600);
            serial->setDataBits(QSerialPort::Data8);
            serial->setParity(QSerialPort::NoParity);
            serial->setStopBits(QSerialPort::OneStop);
            serial->setFlowControl(QSerialPort::NoFlowControl);
            QByteArray rq;
            if
            ( serial->open(QIODevice::ReadWrite)==true){
            qDebug()<<"connected";

                rq.resize(10);
                rq[0] = 0xAA;
                rq[1] = 0x55;
                rq[2] = 0x07;
                rq[3] = 0x00;
                rq[4] = 0x00;
                rq[5] = 0x20;
                rq[6] = 0x00;
                rq[7] = 0x02;
                rq[8] = 0x00;
                rq[9] = 0xBB;
            

            }else
            {
            qDebug()<<"Not connected";
            }
            serial->write( rq);
            }
            this s the data am sending to a device and it writes this data into a device now i want to read the data from the device because rq[8] contains my version number i want to fetch that version number inside a button click so when i click button named version i should display my version data which is now written in device

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

            @Anna_64
            So, in some shape or form, the receiving end needs to receive the 10 bytes (e.g. perhaps with similar connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived())); and call readAll() in the slot each time data is received, you may find that going to https://doc.qt.io/qt-5/qdatastream.html and searching for "Using Read Transactions" helps you for this, or you can do the necessary code yourself). And then just pick out response_buffer[8] if that's what you're interested in. I don't see there's anything else to say.

            1 Reply Last reply
            0
            • 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 Extensions
                                  • Unsolved