Results 1 to 5 of 5

Thread: QByteArray container and Timer syncronization

  1. #1
    Join Date
    Nov 2010
    Posts
    100
    Qt products
    Qt3 Qt4
    Thanks
    38
    Thanked 1 Time in 1 Post

    Default QByteArray container and Timer syncronization

    Hello every one. i have a problem which i am not able to come out from, been trying to get a solution for it past 2 weeks.. I am actually retrieving data from rs232 port, and doing some bit shifting and aligning, after which i am searching for a Start of the Frame 03b702200000 and then trying to save exactly 2944 chars in every row of the data base.. cause 2944 chars is one master frame in this protocol. Now my problem is after the first 3 rows in the data base either data is repeated from the first row or new data is not saved into the data base. i am calling this function every 30 milli seconds once the Acquire button is clicked on the data base.. Someone pls tell me what is the mistake i am doing here. cause the first 2 rows have the exact data after the 3 rd frame is saved into the db again data from the first row is saved in it , instead of the next new frame data to be saved into the 4th row.. I will be very thank full, i don't have any other help for me other then qt center and forum,
    Qt Code:
    1. void datareceiver::myexport1()
    2. {
    3. qint64 numBytes ;
    4. qint64 i;
    5. const int blockLength = 2944;
    6. char buff[4096];
    7. QByteArray result;
    8. quint16 r;
    9. QString covdat;
    10. if(port->bytesAvailable())
    11. {
    12. inscnt++;
    13. numBytes = port->bytesAvailable();
    14. if(numBytes%2 == 0)
    15. numBytes = numBytes;
    16. else
    17. numBytes = numBytes-1;
    18. if(numBytes > sizeof(buff))
    19. numBytes = sizeof(buff);
    20. i = port->read(buff,numBytes);
    21. QByteArray data = (QByteArray::fromRawData(buff,numBytes)); // retreving data on RS232 terminal
    22. quint16 r;
    23. //****************** Bit Wise Shift Left Logic and ORing of two Bytes *********************/
    24.  
    25. for (int i = 0; i < data.size(); i+=2)
    26. {
    27. // r = ((stfram.at(i) << 5) |(stfram.at(i+1)));
    28. r = (((quint16) data.at(i) << 5) |(data.at(i+1)));
    29. char *ptrR = (char*) &r;
    30. result.append(ptrR[1]);
    31. result.append(ptrR[0]);
    32. }
    33. oldat.append(result.toHex()); // QByteArray oldat,result,stfrm ;
    34.  
    35. //****************** Logic to search for First frame ID and save to db from there*********************/
    36. if(datrow ==0)
    37.  
    38. {
    39. QByteArray pattern("03b702200000");
    40. QByteArrayMatcher matcher(pattern);
    41. pos = matcher.indexIn(oldat, pos);
    42. datrow++;
    43. }
    44. for(np = pos; np<oldat.size(); np++)
    45. {
    46. stfram.append(oldat.at(np));
    47. pos++;
    48. }
    49.  
    50. //************************* Cheacking for the Start of Frame "03b702200000" if true then insert 2944 chars every row******************//
    51. if((stfram.startsWith("03b702200000")))
    52. {
    53. QSqlQuery qry;
    54. qry.prepare("insert into Bulkdb (Time_Interval, ChannelData) values (?, ?)");
    55. while ((stfram.length() > blockLength)) {
    56. qry.bindValue(0, rcount++);
    57. qry.bindValue(1, stfram.left(blockLength));
    58. qry.exec();
    59. stfram = stfram.mid(blockLength);
    60.  
    61. }
    62. qDebug()<< stfram;
    63. }
    64. }
    65. // QByteArray oldat,result,stfrm ; Do i have to clear all these containers everytime this function is called sir, i am not doing it right now , since the data is continuous these containers will have data appended into them till the program runs.
    66. }
    To copy to clipboard, switch view to plain text mode 
    Is this a timer syncronization problem or am i not clearing the QByteArray Containers properly..
    thank you very much hope to see some one help me.
    Last edited by nagabathula; 8th January 2011 at 10:11.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QByteArray container and Timer syncronization

    Your code is very unreadable, try simplifying it first. Lines #13-21 can be rewritten as:
    Qt Code:
    1. qint64 numBytes = bytesAvailable();
    2. QByteArray data = port->read(numBytes % 2 ? numBytes : numBytes-1);
    To copy to clipboard, switch view to plain text mode 
    I don't even know what #23-33 do so I won't try simplifying this.

    Lines #36-43 are some attempt to synchronize frames although in my opinion you shouldn't be needing this - is there any data between the end of one frame and the beginning of the next one? I don't even know what oldat is...

    Lines #44-48 should be:
    Qt Code:
    1. strfram.append(oldat.mid(pos));
    2. pos = oldat.size(); // I don't get this one completely
    To copy to clipboard, switch view to plain text mode 

    And then there is "something" which looks like dumping some data to a database. What do all the previous calculations have to do with it - I have no idea.

    In general your whole code should look more or less like this:
    Qt Code:
    1. QByteArray buffer; // "global" buffer
    2. void datareceiver::myexport1() {
    3. buffer.append(port->readAll());
    4. int pos = 0;
    5. QByteArray pattern("03b702200000");
    6. QByteArrayMatcher matcher(pattern);
    7. forever {
    8. int newPos = matcher.indexIn(buffer, pos);
    9. if(newPos==-1) break; // no more frames detected
    10. const int frameStart = newPos+pattern.size();
    11.  
    12. QByteArray frameData = buffer.mid(frameStart, 2944);
    13. if(frameData.size()!=2944) break; // incomplete frame
    14. insertIntoDatabase(frameData);
    15. pos = frameStart+2944;
    16. }
    17. // sync buffer:
    18. if(pos>0) buffer.remove(0, pos); // if a frame was found, remove all the processed frames
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 8th January 2011 at 09:00.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    nagabathula (8th January 2011)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QByteArray container and Timer syncronization

    Here are a number of ways your data can get broken in your code:
    • The first data received does not contain a frame header (the sender is asynchronous after all)
    • A frame header falls across a buffer boundary.
    • The first byte you receive is an odd byte. This will break your processing at line 28 and mangle the data following. You should look for the sequence of bytes that make up your frame header before you process them: this will allow you to synchronise with sets of 2944 bytes.
    • You receive up to 4096 bytes into your buffer (buff), process all of the bytes, potentially save some of them, and then discard the remainder when the routine exits (and buff[] is discarded). The receive buffer needs to persist between calls to this routine.
    • If datrow is zero (where is that set) then you look for "03b702200000" and assume it is found. What happens if it is not found? Does datrow ever get reset? Does the header ever appear again?

    There are probably many more. You lost me at line 50 with some stuff about covdat.

    I can see bits of code cobbled together from the various other times you have asked questions about this problem. You really need to break this problem into individual bits that you understand and can test are working before you try to put them together into a whole and process live data.

    Just an afterthought: Once every 30 milliseconds you expect to have 2944 bytes over an RS232 interface. That's quite a bit rate: 2944 bytes * 8 bits/byte / 0.03 secs = 785067 bits/sec. About 7x the typical 115400 bits per sec port. Are you sure about this?


    Edit: Look closely at Wysota's great simplification of the problem.

    Wysota: There's a long history of questions on this same problem. Most of the code above is poorly understood cut-and-paste jobs from other threads, including the mysterious bit mangling in lines 22 to 33.
    Last edited by ChrisW67; 8th January 2011 at 09:04.

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

    nagabathula (8th January 2011)

  6. #4
    Join Date
    Nov 2010
    Posts
    100
    Qt products
    Qt3 Qt4
    Thanks
    38
    Thanked 1 Time in 1 Post

    Default Re: QByteArray container and Timer syncronization

    hello sir thanks for the reply..

    In line number 23-33 i am actually bit shifting every second byte by 5 times and ORing it with the byte before it and appending the Bit shifted and ORed data into oldat.

    From line number 36 - 48, i am looking for the start of the frame 03b702200000 in oldat and appending the data from there to stfram.

    is there any data between the end of one frame and the beginning of the next one
    No sir there won't be any data the frame would start with 03b702200000 end with 03b70220001f it will have 640 channels data in each Master frame, After the start of the frame 03b702200000 there is 20 channels of data after which the frame id is incremented 03b702200001and so on till 03b70220001f so i will have 32 frames in one master frame with the total of 640 channels data in it..

    From line number 53 - 68
    the last part of the code just checking if the data starts with 03b702200000 and if the length is greater then 2944 chars then save every 2944 chars into each row, cause after every 2944 chars a new master frame starts. I thought i will process all the data i.e aligning and capturing data from the start of frame and then save the data into the db.

    ChrisW sir tried helping me a lot before with this problem, but i cud't get the final result, all this looks very new to me. I understand what each part of the code does but i think i am going wrong with the flow of the program or something. but i am still programming like how i did for my collage programs.. I need to learn more stuff. I actually tested every part of the code in a different test program and then used it in the main program. I will analyze what you and Chris sir have told me and try getting the result.. I am sorry Chris Sir i have no where else to go,tats why i had to come back with this problem again on the forum. i am working late till one every night no break for Christmas or the new year also working everyday past month.. and still i have no result I am not going in the right way i guess. I will try what wysota sir and you have told me now..

    If datrow is zero (where is that set) then you look for "03b702200000" and assume it is found. What happens if it is not found? Does datrow ever get reset? Does the header ever appear again?
    Yes sir i set datrow=0; in the constructor of the program, i even did that i tried clearing all the containers and set datrow=0; in the else statement if the start of Frame is not found but when that happened data never showed up again the control stayed in the else itself.


    thanks a lot
    Last edited by nagabathula; 8th January 2011 at 10:59.

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QByteArray container and Timer syncronization

    Quote Originally Posted by nagabathula View Post
    In line number 23-33 i am actually bit shifting every second byte by 5 times and ORing it with the byte before it and appending the Bit shifted and ORed data into oldat.
    Ok but what for? You are only doing it with some of the data. And then you are converting things to text which I something I totally don't understand.

    From line number 36 - 48, i am looking for the start of the frame 03b702200000 in oldat and appending the data from there to stfram.
    Why?

    No sir there won't be any data the frame would start with 03b702200000 end with 03b70220001f it will have 640 channels data in each Master frame, After the start of the frame 03b702200000 there is 20 channels of data after which the frame id is incremented 03b702200001and so on till 03b70220001f so i will have 32 frames in one master frame with the total of 640 channels data in it..
    According to me this magic number is a synchronisation sequence. Once you have it, you should never be needing it again unless you lose synchronisation with the data stream.

    From line number 53 - 68
    the last part of the code just checking if the data starts with 03b702200000 and if the length is greater then 2944 chars then save every 2944 chars into each row,
    What if it is not greater than 2944 bytes? What happens then?

    ChrisW sir tried helping me a lot before with this problem, but i cud't get the final result, all this looks very new to me. I understand what each part of the code does but i think i am going wrong with the flow of the program or something.
    Your main problem is calling this code with a timer. You should be doing that when new data arrives and not periodically.

    I need to learn more stuff.
    Oh yes, you sure do. And it's high time you do it. It seems the problem is way over your head.


    A small suggestion - start by break your design into functions. Make each function do one thing, test it (one function at a time) and then when you know each function behaves correctly, assemble your main method where you will call those functions/methods in sequence to perform the complete task. Furthermore you have completely no error checking, you just assume it will work which is a very bad approach.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    nagabathula (17th January 2011)

Similar Threads

  1. Replies: 9
    Last Post: 25th July 2009, 13:27
  2. Container plugins
    By Benne Gesserit in forum Qt Tools
    Replies: 4
    Last Post: 20th November 2008, 23:43
  3. Scrollbar in the container
    By anju123 in forum Qt Programming
    Replies: 6
    Last Post: 21st July 2008, 07:44
  4. Quick ? about qSort(Container & container)
    By JimDaniel in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2007, 11:20
  5. Container Extensions
    By mariok in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 11:06

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.