Page 1 of 2 12 LastLast
Results 1 to 20 of 22

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

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

    Smile Number of bytes receives not the same after each cycle

    Hi,

    I want to receive 9 bytes after each cycle
    Im using the QSerialPort to comunicate between a reaspberry pi and an other device (microcontroller)

    I write a code

    I have perfectly 9 bytes after each cycle but not in the same QbyteArray :

    for example :

    nb data read = 9 bytes = "011109101809090913"
    nb data read = 8 bytes = "0209091018090909"
    nb data read = 1 bytes = "13"
    nb data read = 8 bytes = "0309091018090908"
    nb data read = 1 bytes = "13"
    nb data read = 9 bytes = "04110910170909090f"
    nb data read = 9 bytes = "050909101809090913"
    nb data read = 9 bytes = "061509101809090813"
    nb data read = 8 bytes = "0711091018080909"
    nb data read = 10 bytes = "1308110910170908090f"

    I want to receive exaclty 9 bytes :

    If u see the example , you can remark that each byte have an ID(1,2,3....) and 8 other bytes
    and when i have 8 bytes, the next byte is the end of the later (8 bytes) ..

    i have this code

    Qt Code:
    1. StopAff->setEnabled(true);
    2. QByteArray bytes;
    3. bytes = PortReception->readAll();
    4. qDebug() <<"nb data read = " << bytes.size() << " bytes = "<< bytes.toHex();
    To copy to clipboard, switch view to plain text mode 

    How I can resolve the problem
    Last edited by AUDI_ENG; 4th July 2014 at 15:41.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    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

    This is normal symptom for each data stream device (serial port, TCP socket etc.). 9 characters in packet is your private assumption. Data stream device do not know about it. You should read stream of data, analyze it and divide for logical packets.
    By the way your protocol is very weak. How do you know that for example 01 is the number of the next packet and not its content ?

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

    AUDI_ENG (5th July 2014)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Work with a local buffer.

    All data that comes in is appended to the buffer.
    Then you loop over the combined buffer, take chunks of 9 bytes until that is no longer possible.

    Cheers,
    _

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

    AUDI_ENG (5th July 2014)

  6. #4
    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

    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.

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

    AUDI_ENG (5th July 2014)

  8. #5
    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
    This is normal symptom for each data stream device (serial port, TCP socket etc.). 9 characters in packet is your private assumption. Data stream device do not know about it. You should read stream of data, analyze it and divide for logical packets.
    By the way your protocol is very weak. How do you know that for example 01 is the number of the next packet and not its content ?
    Thank you for your reply,

    my protocol is :
    _ID________ |______ Data______
    01_______________8 bytes_____ (always)
    02_______________8 bytes_____
    03_______________8 bytes_____
    04_______________8 bytes_____
    05_______________8 bytes_____
    06_______________8 bytes_____
    ..

    Quote Originally Posted by anda_skoa View Post
    Work with a local buffer.

    All data that comes in is appended to the buffer.
    Then you loop over the combined buffer, take chunks of 9 bytes until that is no longer possible.

    Cheers,
    _
    thanks for your reply

    what do you mean about local buffer ?

    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.


    Thanks for your reply

    I will try this monday,
    with this code, i will lose some packets no ?
    Last edited by AUDI_ENG; 5th July 2014 at 12:01.

  9. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    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 for your reply,

    my protocol is :
    _ID________ |______ Data______
    01_______________8 bytes_____ (always)
    02_______________8 bytes_____
    03_______________8 bytes_____
    04_______________8 bytes_____
    05_______________8 bytes_____
    06_______________8 bytes_____
    ..
    OK, I'm asking once more : How do you know that for example 01 is the packet ID and not its content ?
    Is the sender sends the data all the time or only at your request?

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

    AUDI_ENG (6th July 2014)

  11. #7
    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
    OK, I'm asking once more : How do you know that for example 01 is the packet ID and not its content ?
    Is the sender sends the data all the time or only at your request?

    OK, I'm asking once more : How do you know that for example 01 is the packet ID and not its content ?:
    __________________________________________________ ___________________________________
    because I have 9 bytes : one for ID and 8 for bytes , and I have a point of depart : when I click on start button : I receive data



    Is the sender sends the data all the time or only at your request? :
    __________________________________________________ ____
    I have my application Qt in the raspberry and i receive data from microcontroller

    I have 2 buttons : one for start and one for for stop
    when i click start, the port serial is openning, and data (packets of 9 bytes) is receiving from µC
    I will display this packets on a table ..


    You are right, the processing of data in the sender is infinitely, so when i click on the start button, i receive data but i don't know where is the begenning of the packet, !
    its an other problem , thank for your tip, but you have any idea about resolving this ?
    Last edited by AUDI_ENG; 5th July 2014 at 17:51.

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

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

    AUDI_ENG (6th July 2014)

  14. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    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
    You are right, the processing of data in the sender is infinitely, so when i click on the start button, i receive data but i don't know where is the begenning of the packet, !
    its an other problem , thank for your tip, but you have any idea about resolving this ?
    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) ?

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

    AUDI_ENG (7th July 2014)

  16. #10
    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 09:59.

  17. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    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

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

    AUDI_ENG (7th July 2014)

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

  20. #13
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    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.

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

    AUDI_ENG (7th July 2014)

  22. #14
    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 10:42.

  23. #15
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    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.

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

    AUDI_ENG (7th July 2014)

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

  26. #17
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    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 

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

    AUDI_ENG (7th July 2014)

  28. #18
    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 15:23.

  29. #19
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    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.

  30. #20
    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 10:51.

Similar Threads

  1. Replies: 4
    Last Post: 23rd October 2012, 09:40
  2. Qt EventLoop Duty Cycle (Blocking vs Spinning)
    By bob2oneil in forum Qt Programming
    Replies: 5
    Last Post: 20th July 2011, 23:06
  3. Replies: 2
    Last Post: 9th June 2010, 17:08
  4. MainWindow::resizeEvent - Infinite Cycle
    By metRo_ in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2010, 00: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, 18: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.