Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Why the Qt(4.8) serial port readind data from device in 2 part?
Qt 6.11 is out! See what's new in the release blog

Why the Qt(4.8) serial port readind data from device in 2 part?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
9 Posts 3 Posters 4.0k Views 2 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.
  • MhM93M Offline
    MhM93M Offline
    MhM93
    wrote on last edited by MhM93
    #1

    Hi
    this is my code : (I used qextserialport)

    SerialPort::SerialPort(QString port)
    {
        sp=new QextSerialPort;
        sp->setPortName(port);
        /*port->setBaudRate(BAUD9600);
        port->setFlowControl(FLOW_OFF);
        port->setParity(PAR_NONE);
        port->setDataBits(DATA_8);
        port->setStopBits(STOP_2);*/
        sp->setBaudRate(BAUD115200);
        sp->setDataBits(DATA_8);
        sp->setFlowControl(FLOW_OFF);
        sp->setParity(PAR_NONE);
        sp->setStopBits(STOP_1);
        sp->open(QIODevice::ReadWrite);
        connect(sp,SIGNAL(readyRead()),this,SLOT(ReadData()));
    
    }
    
    void SerialPort::ReadData()
    {
        QByteArray ba;
        ba.resize(512);
        ba=sp->readAll();
        emit DataRecieve(ba);
    }
    

    this code run on Ubuntu at first and show all the bytes which receive from device, but when I tested it in linux embedded device this code read data from serial device in 2 part. () there is no \n in my bytes.
    How can I solve this problem?

    H.Ghassami

    1 Reply Last reply
    0
    • K Offline
      K Offline
      karti gesar
      wrote on last edited by
      #2

      how much size of data u are reading

      1 Reply Last reply
      1
      • MhM93M Offline
        MhM93M Offline
        MhM93
        wrote on last edited by
        #3

        some times 48 bit:

        "aa552401040000000000000000000000"
        "0000000000002801"

        some time512 bit

        H.Ghassami

        1 Reply Last reply
        0
        • K Offline
          K Offline
          karti gesar
          wrote on last edited by
          #4

          void SerialPort::ReadData()
          {
          if (serial->bytesAvailable()>0||serial->waitForReadyRead(10000))
          {
          QByteArray ba;
          ba=serial->readAll();
          }
          }
          try this

          1 Reply Last reply
          1
          • MhM93M Offline
            MhM93M Offline
            MhM93
            wrote on last edited by
            #5

            No. there is no diffrent :

            4
            "aa5524010400000000000000"
            12
            "000000000000000000002801"
            

            for this code:

            void SerialPort::ReadData()
            {
                QByteArray ba;
                ba.resize(512);
                if(sp->bytesAvailable()>0 || sp->waitForReadyRead(20000))
                {
                qDebug()<<sp->bytesAvailable();
                ba=sp->readAll();
                qDebug()<<ba.toHex();
                emit DataRecieve(ba);
                }
            }
            

            H.Ghassami

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

              Hi
              There is never a guarantee that bytes sent on a
              serial line is received as one stream/read.

              Therefore it is often needed to include data markers in the input to
              know when all has been read or use fixed size blocks.

              So when readyRead signal is sent. you read what there is with readAll() and append to a buffer.

              if enough data is read OR data marker seen, then you emit DataRecieve().

              1 Reply Last reply
              1
              • MhM93M Offline
                MhM93M Offline
                MhM93
                wrote on last edited by MhM93
                #7

                @mrjj thanks for help. I think maybe I have mistake in my code. so often the serial port can not read all of the data? . My serial device always send check sum at end of data, so I should check the data that receive and if the checksum is not true again listen run port.readall and attach the second data to the first and if it is true then emit the data.
                Is it true?

                H.Ghassami

                mrjjM 1 Reply Last reply
                0
                • MhM93M MhM93

                  @mrjj thanks for help. I think maybe I have mistake in my code. so often the serial port can not read all of the data? . My serial device always send check sum at end of data, so I should check the data that receive and if the checksum is not true again listen run port.readall and attach the second data to the first and if it is true then emit the data.
                  Is it true?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @MhM93

                  hi
                  Its normal that not all data can be read in one go. Often seen when moving from
                  PC to small(er) board.

                  The checksum sounds right way to know when all data been read or to catch transmission errors.
                  And yes, the logic if like that. On readyRead, read what there is. Append to the running buffer ( a member variable). When checksum is ok, emit signal. This logic will work fine as long there is
                  no chance that another data is sent while we wait for the rest of the first one.

                  1 Reply Last reply
                  1
                  • MhM93M Offline
                    MhM93M Offline
                    MhM93
                    wrote on last edited by
                    #9

                    thanksss a lotttt :)

                    H.Ghassami

                    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