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
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    with this code, i will lose some packets no ?
    No. All data will be saved (accumulated) in internal buffer of class in background. So, you can read data in any time (even without readyRead() signal, e.g. by a signal from QTimer or by button clicked and so on).

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

    AUDI_ENG (6th 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
    Finally You saw the problem First question : can you change the controller software ? Second : the data in the package are binary (bytes in the range 00 - FF) ?
    Yes, I can change the software in ther sender (Assembly langage) and the data in the package are binary ( 00 - FF) but the data can't be over ~ 400(int) = 190 'H'
    I'm thinking to send 2 bytes in the begininning of each package : for exemple A1 - A2 - A3 - A4 .. A16 instead of 01 - 02 - 03 -04 ... 16


    Added after 10 minutes:


    Quote Originally Posted by kuzulis View Post
    No. All data will be saved (accumulated) in internal buffer of class in background. So, you can read data in any time (even without readyRead() signal, e.g. by a signal from QTimer or by button clicked and so on).
    Hi,

    I'm testing the code, its work perfectly. So, I have 9 bytes after each cycle, but the beginning of the package is not the ID, it's the last byte :
    for example :
    byte 8__byte ID__ byte 1__ byte 2__ byte 3__ byte 4__ byte 5__ byte 6__ byte 7
    17______01______0D_____ 09_____ 17_____ 08_____ 09_____ 08_____ 08
    17______02______0D_____ 09_____ 17_____ 08_____ 09_____ 08_____ 08
    17______03______0D_____ 09_____ 17_____ 08_____ 09_____ 08_____ 08
    17______04______0D_____ 09_____ 17_____ 08_____ 09_____ 08_____ 08
    .................................................. .................................................. .............;

    The problem is related to the detection of the beginning of package ?
    Last edited by AUDI_ENG; 7th July 2014 at 08:59.

  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

    And this is good idea - create a frame with marker.
    According to me it would look like this : AA AA id_0 id_1 d_0 d_1 d_2 d_3 d_4 d_5 d_6 d_7 d_8

  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

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

    Quote Originally Posted by kuzulis View Post
    Qt Code:
    1. Foo::readyRead()
    2. {
    3. static const int expectedPacketSize = 9; // your 9 bytes
    4. if (port->bytesAvailable() < expectedPacketSize)
    5. return; // do nothing
    6. QByteArray data = port->read(expectedPacketSize);
    7.  
    8. // do domething processing of your packet
    9. }
    To copy to clipboard, switch view to plain text mode 

    Of course, I'm skip the parsing and synchronization of incoming packets from an incoming data stream.

    the code works perfectly but I like to understand it deeply, can you explain me how it works, for exemple how it detects 9 bytes without losing data

    Thank you


    Added after 4 minutes:


    Quote Originally Posted by Lesiok View Post
    And this is good idea - create a frame with marker.
    According to me it would look like this : AA AA id_0 id_1 d_0 d_1 d_2 d_3 d_4 d_5 d_6 d_7 d_8
    Thank you,

    but why I want to repeat id_0 id_1 twice ? and you meaning that AA AA , id_0 8bytes , id_1 8bytes , id_2 8bytes...... ??
    Last edited by AUDI_ENG; 7th July 2014 at 09:15.

  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

    Quote Originally Posted by AUDI_ENG View Post
    Thank you,
    but why I want to repeat id_0 id_1 twice ? and you meaning that AA AA , id_0 8bytes , id_1 8bytes , id_2 8bytes...... ??
    If id is one byte send only one of course. I thought that id is 2 bytes.
    And NO, I mean : AA AA id_0 8 bytes, AA AA id_1 8 bytes.... Every packet starts with 2 bytes marker AA AA.

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

    AUDI_ENG (7th July 2014)

  9. #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
    If id is one byte send only one of course. I thought that id is 2 bytes.
    And NO, I mean : AA AA id_0 8 bytes, AA AA id_1 8 bytes.... Every packet starts with 2 bytes marker AA AA.
    Ok, i will try this : AA AA id_0 8 bytes , AA AA id_1 8 bytes ...
    in Qt application I will change things in the code no when i read data?
    Last edited by AUDI_ENG; 7th July 2014 at 09:42.

  10. #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

    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.

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

    AUDI_ENG (7th July 2014)

  12. #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

    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

  13. #9
    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 

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

    AUDI_ENG (7th July 2014)

  15. #10
    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.

  16. #11
    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.

  17. #12
    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.

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.