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. QSerialPort data missing while reading
Forum Updated to NodeBB v4.3 + New Features

QSerialPort data missing while reading

Scheduled Pinned Locked Moved Unsolved General and Desktop
32 Posts 8 Posters 13.4k Views 3 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.
  • B Offline
    B Offline
    biomed12
    wrote on last edited by biomed12
    #17

    @mostefa
    Yes, I have this trouble definitely. I will try and share the results.

    M 1 Reply Last reply
    0
    • B biomed12

      @mostefa
      Yes, I have this trouble definitely. I will try and share the results.

      M Offline
      M Offline
      mostefa
      wrote on last edited by
      #18

      @biomed12 said in QSerialPort data missing while reading:

      Yes, I have this trouble definitely. I will try and share the results.

      Ok keep us informed ;)

      B 1 Reply Last reply
      0
      • M mostefa

        @biomed12 said in QSerialPort data missing while reading:

        Yes, I have this trouble definitely. I will try and share the results.

        Ok keep us informed ;)

        B Offline
        B Offline
        biomed12
        wrote on last edited by
        #19

        @mostefa said in QSerialPort data missing while reading:

        @biomed12 said in QSerialPort data missing while reading:

        Yes, I have this trouble definitely. I will try and share the results.

        Ok keep us informed ;)

        ooppss.. Qbyte array is getting larger always.

        M 1 Reply Last reply
        0
        • B biomed12

          @mostefa said in QSerialPort data missing while reading:

          @biomed12 said in QSerialPort data missing while reading:

          Yes, I have this trouble definitely. I will try and share the results.

          Ok keep us informed ;)

          ooppss.. Qbyte array is getting larger always.

          M Offline
          M Offline
          mostefa
          wrote on last edited by
          #20

          @biomed12 said in QSerialPort data missing while reading:

          @mostefa said in QSerialPort data missing while reading:

          @biomed12 said in QSerialPort data missing while reading:

          Yes, I have this trouble definitely. I will try and share the results.

          Ok keep us informed ;)

          ooppss.. Qbyte array is getting larger always.

          Getting larger than what?

          I don't understand could you provide more details please?

          B 1 Reply Last reply
          0
          • M mostefa

            @biomed12 said in QSerialPort data missing while reading:

            @mostefa said in QSerialPort data missing while reading:

            @biomed12 said in QSerialPort data missing while reading:

            Yes, I have this trouble definitely. I will try and share the results.

            Ok keep us informed ;)

            ooppss.. Qbyte array is getting larger always.

            Getting larger than what?

            I don't understand could you provide more details please?

            B Offline
            B Offline
            biomed12
            wrote on last edited by
            #21

            @mostefa said in QSerialPort data missing while reading:

            @biomed12 said in QSerialPort data missing while reading:

            @mostefa said in QSerialPort data missing while reading:

            @biomed12 said in QSerialPort data missing while reading:

            Yes, I have this trouble definitely. I will try and share the results.

            Ok keep us informed ;)

            ooppss.. Qbyte array is getting larger always.

            Getting larger than what?

            I don't understand could you provide more details please?

            like
            axx
            axxxaxa
            axaxaxxxx

            I am trying now this algorithm, what is your opinion?

            QByteArray serialData;
            union
            {
                unsigned char bytes[2];
                unsigned int intPart;
            
            }serData;
            //! [7]
            void MainWindow::readData()
            {
                serialData.append(serial->readAll());
                if(serialData.length()<4 || serialData[0] != 'a' || serialData[3] != 'b')
                {
                    return;
                }
            
                serData.bytes[0] = serialData[1];
                serData.bytes[1] = serialData[2];
                qDebug()<<QString::number(serData.intPart);
                serData.intPart = 0;
                //qDebug()<<QString::number(data.length());
                console->append(QString::fromLatin1(serialData));
                serialData.clear();
            }
            
            M 1 Reply Last reply
            0
            • B biomed12

              @mostefa said in QSerialPort data missing while reading:

              @biomed12 said in QSerialPort data missing while reading:

              @mostefa said in QSerialPort data missing while reading:

              @biomed12 said in QSerialPort data missing while reading:

              Yes, I have this trouble definitely. I will try and share the results.

              Ok keep us informed ;)

              ooppss.. Qbyte array is getting larger always.

              Getting larger than what?

              I don't understand could you provide more details please?

              like
              axx
              axxxaxa
              axaxaxxxx

              I am trying now this algorithm, what is your opinion?

              QByteArray serialData;
              union
              {
                  unsigned char bytes[2];
                  unsigned int intPart;
              
              }serData;
              //! [7]
              void MainWindow::readData()
              {
                  serialData.append(serial->readAll());
                  if(serialData.length()<4 || serialData[0] != 'a' || serialData[3] != 'b')
                  {
                      return;
                  }
              
                  serData.bytes[0] = serialData[1];
                  serData.bytes[1] = serialData[2];
                  qDebug()<<QString::number(serData.intPart);
                  serData.intPart = 0;
                  //qDebug()<<QString::number(data.length());
                  console->append(QString::fromLatin1(serialData));
                  serialData.clear();
              }
              
              M Offline
              M Offline
              mostefa
              wrote on last edited by mostefa
              #22

              @biomed12 said in QSerialPort data missing while reading:

              @mostefa said in QSerialPort data missing while reading:

              @biomed12 said in QSerialPort data missing while reading:

              @mostefa said in QSerialPort data missing while reading:

              @biomed12 said in QSerialPort data missing while reading:

              Yes, I have this trouble definitely. I will try and share the results.

              Ok keep us informed ;)

              ooppss.. Qbyte array is getting larger always.

              Getting larger than what?

              I don't understand could you provide more details please?

              like
              axx
              axxxaxa
              axaxaxxxx

              I am trying now this algorithm, what is your opinion?

              QByteArray serialData;
              union
              {
                  unsigned char bytes[2];
                  unsigned int intPart;
              
              }serData;
              //! [7]
              void MainWindow::readData()
              {
                  serialData.append(serial->readAll());
                  if(serialData.length()<4 || serialData[0] != 'a' || serialData[3] != 'b')
                  {
                      return;
                  }
              
                  serData.bytes[0] = serialData[1];
                  serData.bytes[1] = serialData[2];
                  qDebug()<<QString::number(serData.intPart);
                  serData.intPart = 0;
                  //qDebug()<<QString::number(data.length());
                  console->append(QString::fromLatin1(serialData));
                  serialData.clear();
              }
              

              I am not able to understand all what you are doing

              Why don't we start with simple things:

              So

              1. When you readData is CALLED ? , is it a SLOT ? , is it connected to readyRead signal?

              2. What is the output of this code?

              For example we keep only this part of the code :

              
              // i am trying only to keepthe basic lines for reading serial port
              QByteArray serialData;
              union
              {
                  unsigned char bytes[2];
                  unsigned int intPart;
              
              }serData;
              //! [7]
              void MainWindow::readData()
              {
                  serialData.append(serial->readAll());
                  qDebug() << "receivedthat for the moment = " << serialData;
              }
              
              B 1 Reply Last reply
              0
              • M mostefa

                @biomed12 said in QSerialPort data missing while reading:

                @mostefa said in QSerialPort data missing while reading:

                @biomed12 said in QSerialPort data missing while reading:

                @mostefa said in QSerialPort data missing while reading:

                @biomed12 said in QSerialPort data missing while reading:

                Yes, I have this trouble definitely. I will try and share the results.

                Ok keep us informed ;)

                ooppss.. Qbyte array is getting larger always.

                Getting larger than what?

                I don't understand could you provide more details please?

                like
                axx
                axxxaxa
                axaxaxxxx

                I am trying now this algorithm, what is your opinion?

                QByteArray serialData;
                union
                {
                    unsigned char bytes[2];
                    unsigned int intPart;
                
                }serData;
                //! [7]
                void MainWindow::readData()
                {
                    serialData.append(serial->readAll());
                    if(serialData.length()<4 || serialData[0] != 'a' || serialData[3] != 'b')
                    {
                        return;
                    }
                
                    serData.bytes[0] = serialData[1];
                    serData.bytes[1] = serialData[2];
                    qDebug()<<QString::number(serData.intPart);
                    serData.intPart = 0;
                    //qDebug()<<QString::number(data.length());
                    console->append(QString::fromLatin1(serialData));
                    serialData.clear();
                }
                

                I am not able to understand all what you are doing

                Why don't we start with simple things:

                So

                1. When you readData is CALLED ? , is it a SLOT ? , is it connected to readyRead signal?

                2. What is the output of this code?

                For example we keep only this part of the code :

                
                // i am trying only to keepthe basic lines for reading serial port
                QByteArray serialData;
                union
                {
                    unsigned char bytes[2];
                    unsigned int intPart;
                
                }serData;
                //! [7]
                void MainWindow::readData()
                {
                    serialData.append(serial->readAll());
                    qDebug() << "receivedthat for the moment = " << serialData;
                }
                
                B Offline
                B Offline
                biomed12
                wrote on last edited by
                #23

                @mostefa said in QSerialPort data missing while reading:

                @biomed12 said in QSerialPort data missing while reading:

                @mostefa said in QSerialPort data missing while reading:

                @biomed12 said in QSerialPort data missing while reading:

                @mostefa said in QSerialPort data missing while reading:

                @biomed12 said in QSerialPort data missing while reading:

                Yes, I have this trouble definitely. I will try and share the results.

                Ok keep us informed ;)

                ooppss.. Qbyte array is getting larger always.

                Getting larger than what?

                I don't understand could you provide more details please?

                like
                axx
                axxxaxa
                axaxaxxxx

                I am trying now this algorithm, what is your opinion?

                QByteArray serialData;
                union
                {
                    unsigned char bytes[2];
                    unsigned int intPart;
                
                }serData;
                //! [7]
                void MainWindow::readData()
                {
                    serialData.append(serial->readAll());
                    if(serialData.length()<4 || serialData[0] != 'a' || serialData[3] != 'b')
                    {
                        return;
                    }
                
                    serData.bytes[0] = serialData[1];
                    serData.bytes[1] = serialData[2];
                    qDebug()<<QString::number(serData.intPart);
                    serData.intPart = 0;
                    //qDebug()<<QString::number(data.length());
                    console->append(QString::fromLatin1(serialData));
                    serialData.clear();
                }
                

                I am not able to understand all what you are doing

                Why don't we start with simple things:

                So

                1. When you readData is CALLED ? , is it a SLOT ? , is it connected to readyRead signal?

                2. What is the output of this code?

                For example we keep only this part of the code :

                
                // i am trying only to keepthe basic lines for reading serial port
                QByteArray serialData;
                union
                {
                    unsigned char bytes[2];
                    unsigned int intPart;
                
                }serData;
                //! [7]
                void MainWindow::readData()
                {
                    serialData.append(serial->readAll());
                    qDebug() << "receivedthat for the moment = " << serialData;
                }
                

                Guy,

                1. Of course the signal slot mechanism such you mention.
                2. The code is always append received data to buffer. But, I need just four bytes. My microcontroller send datas like that:

                starts with 'a' char and, 2 data bytes which include what i want, and terminates with 'b'

                for example; "affb", i need the ff data.

                So, your codes output like that:

                affb
                affbaffb
                affbaffbaffb

                because you are appending new datas consistently the buffer.

                M 1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kuzulis
                  Qt Champions 2020
                  wrote on last edited by kuzulis
                  #24
                  Foo::onReadyRead()
                  {
                      for (;;) {
                          if (serial->bytesAvailable() <  4)
                              return; 
                         const QByteArray peeked = serial-> peek(4);
                         QDataStream ds(peeked);
                         qint8 a;
                         qint8 b;
                         qint16 data;
                         ds >> a >> data >> b;   
                         if (a == 'a' && b == 'b') {
                             // all good, data variable contains ADC value
                             serial->read(4); // dummy read whole frame
                         } else {
                             serial->read(1);  // try to synchronise
                         }
                      }  //end for
                  }
                  
                  B 1 Reply Last reply
                  2
                  • B biomed12

                    @mostefa said in QSerialPort data missing while reading:

                    @biomed12 said in QSerialPort data missing while reading:

                    @mostefa said in QSerialPort data missing while reading:

                    @biomed12 said in QSerialPort data missing while reading:

                    @mostefa said in QSerialPort data missing while reading:

                    @biomed12 said in QSerialPort data missing while reading:

                    Yes, I have this trouble definitely. I will try and share the results.

                    Ok keep us informed ;)

                    ooppss.. Qbyte array is getting larger always.

                    Getting larger than what?

                    I don't understand could you provide more details please?

                    like
                    axx
                    axxxaxa
                    axaxaxxxx

                    I am trying now this algorithm, what is your opinion?

                    QByteArray serialData;
                    union
                    {
                        unsigned char bytes[2];
                        unsigned int intPart;
                    
                    }serData;
                    //! [7]
                    void MainWindow::readData()
                    {
                        serialData.append(serial->readAll());
                        if(serialData.length()<4 || serialData[0] != 'a' || serialData[3] != 'b')
                        {
                            return;
                        }
                    
                        serData.bytes[0] = serialData[1];
                        serData.bytes[1] = serialData[2];
                        qDebug()<<QString::number(serData.intPart);
                        serData.intPart = 0;
                        //qDebug()<<QString::number(data.length());
                        console->append(QString::fromLatin1(serialData));
                        serialData.clear();
                    }
                    

                    I am not able to understand all what you are doing

                    Why don't we start with simple things:

                    So

                    1. When you readData is CALLED ? , is it a SLOT ? , is it connected to readyRead signal?

                    2. What is the output of this code?

                    For example we keep only this part of the code :

                    
                    // i am trying only to keepthe basic lines for reading serial port
                    QByteArray serialData;
                    union
                    {
                        unsigned char bytes[2];
                        unsigned int intPart;
                    
                    }serData;
                    //! [7]
                    void MainWindow::readData()
                    {
                        serialData.append(serial->readAll());
                        qDebug() << "receivedthat for the moment = " << serialData;
                    }
                    

                    Guy,

                    1. Of course the signal slot mechanism such you mention.
                    2. The code is always append received data to buffer. But, I need just four bytes. My microcontroller send datas like that:

                    starts with 'a' char and, 2 data bytes which include what i want, and terminates with 'b'

                    for example; "affb", i need the ff data.

                    So, your codes output like that:

                    affb
                    affbaffb
                    affbaffbaffb

                    because you are appending new datas consistently the buffer.

                    M Offline
                    M Offline
                    mostefa
                    wrote on last edited by mostefa
                    #25

                    @biomed12 said in QSerialPort data missing while reading:

                    @mostefa said in QSerialPort data missing while reading:

                    @biomed12 said in QSerialPort data missing while reading:

                    @mostefa said in QSerialPort data missing while reading:

                    @biomed12 said in QSerialPort data missing while reading:

                    @mostefa said in QSerialPort data missing while reading:

                    @biomed12 said in QSerialPort data missing while reading:

                    Yes, I have this trouble definitely. I will try and share the results.

                    Ok keep us informed ;)

                    ooppss.. Qbyte array is getting larger always.

                    Getting larger than what?

                    I don't understand could you provide more details please?

                    like
                    axx
                    axxxaxa
                    axaxaxxxx

                    I am trying now this algorithm, what is your opinion?

                    QByteArray serialData;
                    union
                    {
                        unsigned char bytes[2];
                        unsigned int intPart;
                    
                    }serData;
                    //! [7]
                    void MainWindow::readData()
                    {
                        serialData.append(serial->readAll());
                        if(serialData.length()<4 || serialData[0] != 'a' || serialData[3] != 'b')
                        {
                            return;
                        }
                    
                        serData.bytes[0] = serialData[1];
                        serData.bytes[1] = serialData[2];
                        qDebug()<<QString::number(serData.intPart);
                        serData.intPart = 0;
                        //qDebug()<<QString::number(data.length());
                        console->append(QString::fromLatin1(serialData));
                        serialData.clear();
                    }
                    

                    I am not able to understand all what you are doing

                    Why don't we start with simple things:

                    So

                    1. When you readData is CALLED ? , is it a SLOT ? , is it connected to readyRead signal?

                    2. What is the output of this code?

                    For example we keep only this part of the code :

                    
                    // i am trying only to keepthe basic lines for reading serial port
                    QByteArray serialData;
                    union
                    {
                        unsigned char bytes[2];
                        unsigned int intPart;
                    
                    }serData;
                    //! [7]
                    void MainWindow::readData()
                    {
                        serialData.append(serial->readAll());
                        qDebug() << "receivedthat for the moment = " << serialData;
                    }
                    

                    Guy,

                    1. Of course the signal slot mechanism such you mention.
                    2. The code is always append received data to buffer. But, I need just four bytes. My microcontroller send datas like that:

                    starts with 'a' char and, 2 data bytes which include what i want, and terminates with 'b'

                    for example; "affb", i need the ff data.

                    So, your codes output like that:

                    affb
                    affbaffb
                    affbaffbaffb

                    because you are appending new datas consistently the buffer.

                    Ok , it's clear for now

                    QByteArray dataToTreat;
                    void MainWindow::readData()
                    {
                        serialData.append(serial->readAll());
                    
                        if(serialData.startWith('a')
                        {
                    	if(serialData.length() > 4)
                    	{
                    		 dataToTreat = serialData.left(4);
                    		serialData.remove(0,4);
                    	}
                        }
                        else
                        {
                               // Not a normal case
                         }
                    
                    // DO WHAT YOU WANT
                        serData.bytes[0] = serialData[1];
                        serData.bytes[1] = serialData[2];
                        qDebug()<<QString::number(serData.intPart);
                        serData.intPart = 0;
                        //qDebug()<<QString::number(data.length());
                        console->append(QString::fromLatin1(serialData));
                        serialData.clear();
                    }
                    
                    B 1 Reply Last reply
                    0
                    • M mostefa

                      @biomed12 said in QSerialPort data missing while reading:

                      @mostefa said in QSerialPort data missing while reading:

                      @biomed12 said in QSerialPort data missing while reading:

                      @mostefa said in QSerialPort data missing while reading:

                      @biomed12 said in QSerialPort data missing while reading:

                      @mostefa said in QSerialPort data missing while reading:

                      @biomed12 said in QSerialPort data missing while reading:

                      Yes, I have this trouble definitely. I will try and share the results.

                      Ok keep us informed ;)

                      ooppss.. Qbyte array is getting larger always.

                      Getting larger than what?

                      I don't understand could you provide more details please?

                      like
                      axx
                      axxxaxa
                      axaxaxxxx

                      I am trying now this algorithm, what is your opinion?

                      QByteArray serialData;
                      union
                      {
                          unsigned char bytes[2];
                          unsigned int intPart;
                      
                      }serData;
                      //! [7]
                      void MainWindow::readData()
                      {
                          serialData.append(serial->readAll());
                          if(serialData.length()<4 || serialData[0] != 'a' || serialData[3] != 'b')
                          {
                              return;
                          }
                      
                          serData.bytes[0] = serialData[1];
                          serData.bytes[1] = serialData[2];
                          qDebug()<<QString::number(serData.intPart);
                          serData.intPart = 0;
                          //qDebug()<<QString::number(data.length());
                          console->append(QString::fromLatin1(serialData));
                          serialData.clear();
                      }
                      

                      I am not able to understand all what you are doing

                      Why don't we start with simple things:

                      So

                      1. When you readData is CALLED ? , is it a SLOT ? , is it connected to readyRead signal?

                      2. What is the output of this code?

                      For example we keep only this part of the code :

                      
                      // i am trying only to keepthe basic lines for reading serial port
                      QByteArray serialData;
                      union
                      {
                          unsigned char bytes[2];
                          unsigned int intPart;
                      
                      }serData;
                      //! [7]
                      void MainWindow::readData()
                      {
                          serialData.append(serial->readAll());
                          qDebug() << "receivedthat for the moment = " << serialData;
                      }
                      

                      Guy,

                      1. Of course the signal slot mechanism such you mention.
                      2. The code is always append received data to buffer. But, I need just four bytes. My microcontroller send datas like that:

                      starts with 'a' char and, 2 data bytes which include what i want, and terminates with 'b'

                      for example; "affb", i need the ff data.

                      So, your codes output like that:

                      affb
                      affbaffb
                      affbaffbaffb

                      because you are appending new datas consistently the buffer.

                      Ok , it's clear for now

                      QByteArray dataToTreat;
                      void MainWindow::readData()
                      {
                          serialData.append(serial->readAll());
                      
                          if(serialData.startWith('a')
                          {
                      	if(serialData.length() > 4)
                      	{
                      		 dataToTreat = serialData.left(4);
                      		serialData.remove(0,4);
                      	}
                          }
                          else
                          {
                                 // Not a normal case
                           }
                      
                      // DO WHAT YOU WANT
                          serData.bytes[0] = serialData[1];
                          serData.bytes[1] = serialData[2];
                          qDebug()<<QString::number(serData.intPart);
                          serData.intPart = 0;
                          //qDebug()<<QString::number(data.length());
                          console->append(QString::fromLatin1(serialData));
                          serialData.clear();
                      }
                      
                      B Offline
                      B Offline
                      biomed12
                      wrote on last edited by
                      #26

                      @mostefa

                      I want to get the second and third byte of data to in another union type TO MAKE it unsigned int like 4095.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mostefa
                        wrote on last edited by mostefa
                        #27

                        even with the last code i proposed?
                        Or you can try to use the code of @kuzulis , this can be the best way to achieve what you want

                        B 1 Reply Last reply
                        0
                        • M mostefa

                          even with the last code i proposed?
                          Or you can try to use the code of @kuzulis , this can be the best way to achieve what you want

                          B Offline
                          B Offline
                          biomed12
                          wrote on last edited by
                          #28

                          @mostefa said in QSerialPort data missing while reading:

                          even with the last code i proposed?
                          Or you can try to use the code of @kuzulis , this can be the best way to achieve what you want

                          baü
                          baü
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baþ
                          baþ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ
                          baÿ

                          Again. always framing error.

                          M 1 Reply Last reply
                          0
                          • K kuzulis
                            Foo::onReadyRead()
                            {
                                for (;;) {
                                    if (serial->bytesAvailable() <  4)
                                        return; 
                                   const QByteArray peeked = serial-> peek(4);
                                   QDataStream ds(peeked);
                                   qint8 a;
                                   qint8 b;
                                   qint16 data;
                                   ds >> a >> data >> b;   
                                   if (a == 'a' && b == 'b') {
                                       // all good, data variable contains ADC value
                                       serial->read(4); // dummy read whole frame
                                   } else {
                                       serial->read(1);  // try to synchronise
                                   }
                                }  //end for
                            }
                            
                            B Offline
                            B Offline
                            biomed12
                            wrote on last edited by
                            #29

                            @kuzulis said in QSerialPort data missing while reading:

                            //your code here
                            ```Foo:onReadyRead()
                            {
                                for (;;) {
                                    if (serial->bytesAvailable() <  4)
                                        return; 
                                   QByteArray peeked = serial-> peek(4);
                                   QDataStream ds(peeked);
                                   qint8 a;
                                   qint8 b;
                                   qint16 data;
                                   ds >> a >> data >> b;   
                                   if (a == 'a' && b == 'b') {
                                       // all good, data variable contains ADC value
                                       serial-> read(4); // dummy read whole frame
                                   } else {
                                       serial-> read(1);  // try 
                                   }
                              }  //end for
                            }
                            

                            how to leave from for(;;)?

                            K 1 Reply Last reply
                            0
                            • B biomed12

                              @mostefa said in QSerialPort data missing while reading:

                              even with the last code i proposed?
                              Or you can try to use the code of @kuzulis , this can be the best way to achieve what you want

                              baü
                              baü
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baþ
                              baþ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ

                              Again. always framing error.

                              M Offline
                              M Offline
                              mostefa
                              wrote on last edited by mostefa
                              #30

                              @biomed12 said in QSerialPort data missing while reading:

                              @mostefa said in QSerialPort data missing while reading:

                              even with the last code i proposed?
                              Or you can try to use the code of @kuzulis , this can be the best way to achieve what you want

                              baü
                              baü
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baþ
                              baþ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ
                              baÿ

                              Again. always framing error.

                              baÿ

                              Looks like ba is correctly in hex , but ÿ is in ascii

                              The hex code equivalent to ÿ is ff

                              http://www.rapidtables.com/convert/number/ascii-to-hex.htm

                              this will lead actually to baffbaffbaff... and i think that this is what you need?

                              So there is a conversion problem in your code( or maybe just a display problem)

                              B 1 Reply Last reply
                              0
                              • B biomed12

                                @kuzulis said in QSerialPort data missing while reading:

                                //your code here
                                ```Foo:onReadyRead()
                                {
                                    for (;;) {
                                        if (serial->bytesAvailable() <  4)
                                            return; 
                                       QByteArray peeked = serial-> peek(4);
                                       QDataStream ds(peeked);
                                       qint8 a;
                                       qint8 b;
                                       qint16 data;
                                       ds >> a >> data >> b;   
                                       if (a == 'a' && b == 'b') {
                                           // all good, data variable contains ADC value
                                           serial-> read(4); // dummy read whole frame
                                       } else {
                                           serial-> read(1);  // try 
                                       }
                                  }  //end for
                                }
                                

                                how to leave from for(;;)?

                                K Offline
                                K Offline
                                kuzulis
                                Qt Champions 2020
                                wrote on last edited by
                                #31

                                @biomed12 said in QSerialPort data missing while reading:

                                how to leave from for(;;)?

                                if (serial->bytesAvailable() <  4)
                                            return;
                                

                                @biomed12 said in QSerialPort data missing while reading:

                                Again. always framing error.

                                Are you sure, your setting:

                                serial->setStopBits(QSerialPort::TwoStop);
                                

                                were applied?

                                @biomed12 said in QSerialPort data missing while reading:

                                baÿ
                                baÿ
                                baÿ

                                Try to output the data variable instead...

                                PS1: QSerialPort is a buffered class and nothing missed.

                                PS2:

                                serial->setReadBufferSize(4);
                                

                                need to remove it if you do not know what for..

                                1 Reply Last reply
                                2
                                • M mostefa

                                  @biomed12 said in QSerialPort data missing while reading:

                                  @mostefa said in QSerialPort data missing while reading:

                                  even with the last code i proposed?
                                  Or you can try to use the code of @kuzulis , this can be the best way to achieve what you want

                                  baü
                                  baü
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baþ
                                  baþ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ
                                  baÿ

                                  Again. always framing error.

                                  baÿ

                                  Looks like ba is correctly in hex , but ÿ is in ascii

                                  The hex code equivalent to ÿ is ff

                                  http://www.rapidtables.com/convert/number/ascii-to-hex.htm

                                  this will lead actually to baffbaffbaff... and i think that this is what you need?

                                  So there is a conversion problem in your code( or maybe just a display problem)

                                  B Offline
                                  B Offline
                                  biomed12
                                  wrote on last edited by
                                  #32

                                  @mostefa

                                  I solved framing errror problem with this algorithm:

                                  Device send datas like:

                                  sendData[0] = (char)'a';
                                  sendData[1] = (char)'k';
                                  sendData[2] = adcValue.bytes[0];
                                  sendData[3] = (adcValue.bytes[1] & 15);
                                  

                                  Read data algorithm:

                                  union{
                                      unsigned char bytes[2];
                                      unsigned int intPart;
                                      unsigned char *ptr;
                                  }serData;
                                  
                                  void MedicalSoftware::serialReadData()
                                  {
                                  
                                      if(serial->bytesAvailable()<4)
                                      {
                                          return;
                                      }
                                  
                                      QByteArray serialInData = serial->readAll();
                                  
                                      //my algorithm
                                      if(serialInData[0] == 'a' && serialInData[1] == 'k')
                                      {
                                          serData.bytes[0] = serialInData[2];
                                          serData.bytes[1] = serialInData[3];
                                  
                                      }else if(serialInData[2] == 'a' && serialInData[3] == 'k')
                                      {
                                          serData.bytes[0] = serialInData[0];
                                          serData.bytes[1] = serialInData[1];
                                      }
                                      else if(serialInData[1] == 'a' && serialInData[2] == 'k')
                                      {
                                          serial->read(1);
                                          return;
                                      }else if(serialInData[0] == 'k' && serialInData[3] == 'a')
                                      {
                                          serData.bytes[0] = serialInData[1];
                                          serData.bytes[1] = serialInData[2];
                                  
                                      }
                                    
                                      if(plotDataButtonStatus == true)
                                      {
                                          plotMainGraph(serData.intPart);
                                      }
                                      serData.intPart = 0;
                                  }
                                  

                                  I have another problem now:) but it is another topic. Thanks

                                  my new problem if you wonder:

                                  http://stackoverflow.com/questions/42742033/qcustomplot-huge-amount-of-data-plotting

                                  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