Results 1 to 20 of 22

Thread: Number of bytes receives not the same after each cycle

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Number of bytes receives not the same after each cycle

    As we said earlier : collect data into the buffer and then analyze them searching for frame start tag (bytes AA AA). Remember that the frame is 11 bytes no 9.

  2. The following user says thank you to Lesiok for this useful post:

    AUDI_ENG (7th July 2014)

  3. #2
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Number of bytes receives not the same after each cycle

    Quote Originally Posted by Lesiok View Post
    As we said earlier : collect data into the buffer and then analyze them searching for frame start tag (bytes AA AA). Remember that the frame is 11 bytes no 9.
    I used this code :

    Foo::readyRead(){

    static const int expectedPacketSize = 11; // your 11 bytes
    if (port->bytesAvailable() < expectedPacketSize)
    return; // do nothing

    QByteArray data = port->read(expectedPacketSize); // do domething processing of your packet
    }

    so I have 11 bytes in the data buffer but how I can detect the start of the packet , in the same time when I read data ?
    I can't analyse my packet for detecting data after reading, and if I collect all data in the same buffer, its very heavy for application

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Number of bytes receives not the same after each cycle

    You should declar variable dataBuffer in class Foo and then do something like this :
    Qt Code:
    1. Foo::readyRead(){
    2. int frameLen = 11;
    3. dataBuffer.append(port->readAll());
    4. if( dataBuffer.size() < frameLen )
    5. return;// data to short
    6. int indexOfFrame = dataBuffer.indexOf("\xAA\xAA");
    7. if( indexOfFrame < 0)
    8. return;// frame marker not found
    9. QByteArray oneFrame = dataBuffer.mid(indexOfFrame,frameLen);// we have one frame, do something with this
    10. dataBuffer = dataBuffer.right(indexOfFrame,frameLen);//remove processed data from the buffer
    11. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Lesiok for this useful post:

    AUDI_ENG (7th July 2014)

  6. #4
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face Re: Number of bytes receives not the same after each cycle

    I'm writing this code before your code, because with yours I'm losing data

    Qt Code:
    1. if(Port->bytesAvailable() < lenFram){
    2.  
    3. return;
    4. }
    5. dataBuffer.append(port->read(lenFram)); // I change your line to this
    To copy to clipboard, switch view to plain text mode 

    When I'm adding this before your code I have :

    bytesreceives = "0aaaaa0c1209080a090908"
    index TAG = 1
    nb data read = 10 bytes = "aaaa0c1209080a090908"

    bytesreceives = "0aaaaa0d0a09090a090909"
    index TAG = 1
    nb data read = 10 bytes = "aaaa0d0a09090a090909"

    bytesreceives = "0aaaaa0e0e09080a080909"
    index TAG = 1
    nb data read = 10 bytes = "aaaa0e0e09080a080909"

    _________________________________________________

    with your code (with readAll() ) : I have this :

    nb data read = 11 bytes = "aaaa070a09080a0908090a"
    nb data read = 11 bytes = "aaaa080a08090a0909080a"
    nb data read = 11 bytes = "aaaa0a1709090a0809090a"
    nb data read = 11 bytes = "08090aaaaa0d0a08090a09" --> AA is not in the beginning
    nb data read = 11 bytes = "09080aaaaa0f1709090a08" --> AA is not in the beginning
    nb data read = 11 bytes = "09080aaaaa020a08090a09" --> AA is not in the beginning
    nb data read = 11 bytes = "aaaa031209090a0908090a"
    nb data read = 11 bytes = "aaaa041209090a0908090a" --> ID = 4
    nb data read = 11 bytes = "08090aaaaa070e08090a09" --> AA is not in the beginning (ID = 7) ---> I miss data with ID = 5 and 6
    nb data read = 11 bytes = "08090aaaaa091209080a09" --> AA is not in the beginning
    nb data read = 11 bytes = "09090aaaaa0b0a09080a09" --> AA is not in the beginning



    I have still the problem with the beginning of frames, sometimes, it is AAAA and sometimes other bytes of frames
    Last edited by AUDI_ENG; 7th July 2014 at 14:23.

  7. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Number of bytes receives not the same after each cycle

    Between lines 8 and 9 of my code should be :
    Qt Code:
    1. if( (dataBuffer.size() - indexOfFrame) < frameLen )
    2. return; // frame is not complete
    To copy to clipboard, switch view to plain text mode 
    Line 10 sholud be :
    Qt Code:
    1. dataBuffer = dataBuffer.right(indexOfFrame+frameLen);//remove processed data from the buffer
    To copy to clipboard, switch view to plain text mode 
    I have not tested my code. This is just an example of how something should look.

  8. #6
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Number of bytes receives not the same after each cycle

    Quote Originally Posted by Lesiok View Post
    Between lines 8 and 9 of my code should be :
    Qt Code:
    1. if( (dataBuffer.size() - indexOfFrame) < frameLen )
    2. return; // frame is not complete
    To copy to clipboard, switch view to plain text mode 
    Line 10 sholud be :
    Qt Code:
    1. dataBuffer = dataBuffer.right(indexOfFrame+frameLen);//remove processed data from the buffer
    To copy to clipboard, switch view to plain text mode 
    I have not tested my code. This is just an example of how something should look.
    I have always 11 bytes and the beginning AAAA (working perfectly)
    but I have still the problem about losing data, its normal to losing data (because somtimes the nombre of bytes availables is more then 22 for exemple --> so I lost one frame) ??
    Last edited by AUDI_ENG; 8th July 2014 at 09:51.

  9. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Number of bytes receives not the same after each cycle

    The amount of data in the buffer depends on how quickly you process them. Remember that the serial port operates completely asynchronously and it can happen that during the processing of one frame to the computer reaches a few more.

  10. The following user says thank you to Lesiok for this useful post:

    AUDI_ENG (8th July 2014)

  11. #8
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Number of bytes receives not the same after each cycle

    Thank you so much for your replies, for your patience, for your ideas and your important information
    I like your motivation to help all beginners
    Thank you again
    Last edited by AUDI_ENG; 8th July 2014 at 10:09.

Similar Threads

  1. Replies: 4
    Last Post: 23rd October 2012, 08:40
  2. Qt EventLoop Duty Cycle (Blocking vs Spinning)
    By bob2oneil in forum Qt Programming
    Replies: 5
    Last Post: 20th July 2011, 22:06
  3. Replies: 2
    Last Post: 9th June 2010, 16:08
  4. MainWindow::resizeEvent - Infinite Cycle
    By metRo_ in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2010, 23:33
  5. How use Model view with a tree wich have Cycle ?
    By weepdoo in forum Qt Programming
    Replies: 2
    Last Post: 9th December 2008, 17:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.