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. Qt slow when reading from serialPort
Forum Updated to NodeBB v4.3 + New Features

Qt slow when reading from serialPort

Scheduled Pinned Locked Moved Solved General and Desktop
qserialport
2 Posts 2 Posters 1.4k 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.
  • M Offline
    M Offline
    Massaimara98
    wrote on 27 Feb 2016, 14:57 last edited by Massaimara98
    #1

    whenever i do , my qt code always get slow compared c#. I dont expect any performance between c# and qt framework but at least dont late read from serialport.
    here two example , one of them writting at C# and the other is Qt.
    c# code (this code isnt belong to me)

    byte[] pkg2 = new byte[3]; // just console output
               while ((1 == 1) && (Buffer.Count > 0))
               {
                   // packet header available and one packet available
                   while ((Buffer.Count > 0) && (Buffer[0] != 0xa5)) Buffer.RemoveAt(0);
    
                   Debug.WriteLine(Buffer.Count); // // just console output
                   if ((Buffer.Count >= 17) && (Buffer[0] == 0xa5))
                   {
                       pkg2[0] = Buffer[0];
    
                       // read packet
                       byte[] pkg = new byte[17];
                       Olimexino328_packet pk = new Olimexino328_packet();
    
                       for (int i = 0; i < 17; i++) {
                           pkg[i] = Buffer[0];
                           Buffer.RemoveAt(0);
                       }
    
                       if ((pkg[0] == 0xa5) && (pkg[1] == 0x5a))
                       {
                           pk.sync0 = pkg[0];
                           pk.sync1 = pkg[1];
                           pk.version = pkg[2];
                           pk.count = pkg[3];
                           pk.d1 = (UInt16)(pkg[5] + (pkg[4] << 8));
                           pk.d2 = (UInt16)(pkg[7] + (pkg[6] << 8));
                           pk.d3 = (UInt16)(pkg[9] + (pkg[8] << 8));
                           pk.d4 = (UInt16)(pkg[11] + (pkg[10] << 8));
                           pk.d5 = (UInt16)(pkg[13] + (pkg[12] << 8));
                           pk.d6 = (UInt16)(pkg[15] + (pkg[14] << 8));
                           //Debug.WriteLine(pk.d1);
        
                           pk.switches = pkg[16];
    
                           rtfv1.Pop(pk.d1);
                           rtfv2.Pop(pk.d2);
                           rtfv3.Pop(pk.d3);
                           rtfv4.Pop(pk.d4);
                           rtfv5.Pop(pk.d5);
                           rtfv6.Pop(pk.d6);
                       }
                       else
                       {
                           Debug.WriteLine("Invalid packet received!");
                       }
                   }
                   else
                   {
                       break;
                   }
    
               }
    
    

    and its my Qt Code

    
    void myRecord::readData()
    {
        int const_a5=165;
        int const_5a=90;
        Buffer.append(serialPort->readAll());
        while ((Buffer.size()> 0) && (Buffer.at(0) != char(const_a5)))
        {
            Buffer.remove(0,1);
    
        }
        qDebug() << Buffer.size();
        if ((Buffer.size() >= 17) && (Buffer.at(0) == char(const_a5)))
    {
    
            char pkg[17];
            Olimexino328_packet pk ;
    
            for (int i = 0; i < 17; i++) {
                pkg[i] = Buffer.at(0);
                Buffer.remove(0,1);
            }
            if ((pkg[0] == (char)const_a5) && (pkg[1] == (char)const_5a))
            {
                pk.sync0 = pkg[0];
                pk.sync1 = pkg[1];
                pk.version = pkg[2];
                pk.count = pkg[3];
                pk.d1 = (quint16)(pkg[5] + (pkg[4] << 8));
                pk.d2 = (quint16)(pkg[7] + (pkg[6] << 8));
                pk.d3 = (quint16)(pkg[9] + (pkg[8] << 8));
                pk.d4 = (quint16)(pkg[11] + (pkg[10] << 8));
                pk.d5 = (quint16)(pkg[13] + (pkg[12] << 8));
                pk.d6 = (quint16)(pkg[15] + (pkg[14] << 8));
    
                pk.switches = pkg[16];
    
                emit incomingData(pk.d1);
    
                                  }
              }
        }
    

    ofc this (myRecord::readData) connect to signal readyRead().
    There are two code just trying to read from arduino and arduino sending just 17 byte.
    but how did i understood my Qt code is slow ? From output...
    when i seeking Buffer.size() , at Qt , size increasing without any stopping , just Qt does not keep up to remove, on the other hand at c#, number sometimes get bigger sometimes thinner but i can understand at least c# keep up to remove data

    Anyway, maybe my code isnt just good as i think, maybe tell me someone say what's wrong with my code ?
    Thank you...

    ------UPDATED-------
    i finally find my solution for problem.
    Solution is , everybody says there is common mistake do something in readyRead() so i created new method and send data to it.
    like that :

    void myRecord::readData()
    {
    if (serialPort->isReadable())
    processData(serialPort->readAll());

    }
    void myRecord::processData(QByteArray &Buffer)
    {
    int const_a5=165;
    int const_5a=90;
    while ((Buffer.size()> 0) && (Buffer.at(0) != char(const_a5)))
    {
    Buffer.remove(0,1);

    }
    if ((Buffer.size() >= 17) && (Buffer.at(0) == char(const_a5)))
    

    {

        char pkg[17];
        Olimexino328_packet pk ;
    
        for (int i = 0; i < 17; i++) {
            pkg[i] = Buffer.at(0);
            Buffer.remove(0,1);
        }
        if ((pkg[0] == (char)const_a5) && (pkg[1] == (char)const_5a))
        {
            pk.sync0 = pkg[0];
            pk.sync1 = pkg[1];
            pk.version = pkg[2];
            pk.count = pkg[3];
            pk.d1 = (quint16)(pkg[5] + (pkg[4] << 8));
            pk.d2 = (quint16)(pkg[7] + (pkg[6] << 8));
            pk.d3 = (quint16)(pkg[9] + (pkg[8] << 8));
            pk.d4 = (quint16)(pkg[11] + (pkg[10] << 8));
            pk.d5 = (quint16)(pkg[13] + (pkg[12] << 8));
            pk.d6 = (quint16)(pkg[15] + (pkg[14] << 8));
    
            pk.switches = pkg[16];
            if (pk.d1 < 60000)
            emit incomingData(pk.d1);
    
          }
      }
    
        }
    

    So that's it ..

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IPlayGenji6
      wrote on 4 May 2023, 01:44 last edited by IPlayGenji6 5 Apr 2023, 01:45
      #2
      This post is deleted!
      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