Results 1 to 20 of 20

Thread: Checking QByteArray for certain values

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Checking QByteArray for certain values

    What happens if you call client.read(7) twice in a row?
    Do you get the same output twice?

    Instead of returning the output straight away create temporary variable and return that variable and see what happens.
    IMHO it should make no difference.

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

    -Kyr0s- (31st October 2011)

  3. #2
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    13
    Thanks
    7
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Checking QByteArray for certain values

    I've tried what you suggested, but to no avail.

    Qt Code:
    1. QByteArray bytes;
    2.  
    3. bytes.push_back(1);
    4. bytes.push_back(2);
    5. bytes.push_back(NULLPTR);
    6. bytes.push_back(1);
    7. bytes.push_back(dio_n);
    8.  
    9. client.write(bytes);
    10. buffer = client.read(7);
    11. this->ui->txtbxOutput->append(buffer.toHex());
    12.  
    13. return buffer;
    To copy to clipboard, switch view to plain text mode 

    Buffer is a QByteArray.

    Also, maybe worth noting, is that the textbox won't show any output, until I click one of the checboxes. Oddly enough, when the application starts, it does go through read_bytes() and thus, is supposed to print out the string. It doesn't do that, only after having clicked a checkbox it would, whilst it goes through the same function as start-up.

    I don't know why it doesn't immediately parse the output in the textbox, as it's pretty much the very same functions..

  4. #3
    Join Date
    Oct 2011
    Location
    Netherlands
    Posts
    13
    Thanks
    7
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Checking QByteArray for certain values

    Hello,

    I've decided to redo the application to see where it goes wrong. It seems everything works fine; it writes and reads the appropriate number of bytes. Reading is supposed to send 5 bytes, and receive 7 in return.

    However, once it accesses the isOn() method, these bytes turn into 20.

    I suspect it's due to this:

    Qt Code:
    1. this->ui->chkDIO0->setChecked(this->isOn(0));
    2. this->ui->chkDIO1->setChecked(this->isOn(1));
    3. this->ui->chkDIO2->setChecked(this->isOn(2));
    4. this->ui->chkDIO3->setChecked(this->isOn(3));
    To copy to clipboard, switch view to plain text mode 

    Each time it goes through reading bytes, it sends 5 bytes, so 5x4 is 20. I guess that makes sense, but even if it didn't, readBytes in isOn() remains to be an empty string.

    Qt Code:
    1. QByteArray DIOMoxaBoard::read_bytes(quint16 dio_n)
    2. {
    3. /* *******************************
    4.   * 1st byte: Command number, 1
    5.   * 2nd byte: Version, 2
    6.   * 3rd byte: Response only, any
    7.   * 4th byte: Data length, 1
    8.   * 5th byte: DIO channel
    9.   ********************************/
    10.  
    11. QByteArray bytes;
    12.  
    13. bytes.push_back(1);
    14. bytes.push_back(2);
    15. bytes.push_back(_NULL);
    16. bytes.push_back(1);
    17. bytes.push_back(dio_n);
    18.  
    19. client.write(bytes); // successfully writes 5 bytes if it doesn't access isOn()
    20.  
    21. return client.read(7).toHex(); //successfully reads 7 bytes if it doesn't access isOn()
    22. }
    23.  
    24. bool DIOMoxaBoard::isOn(quint16 dio_n)
    25. {
    26. bool isOn = false;
    27. const char ON = 0x00;
    28. const int switchIndex = 6;
    29.  
    30. QByteArray readBytes = this->read_bytes(dio_n); //readBytes remains ""
    31.  
    32. if(readBytes.size() > switchIndex && readBytes[switchIndex] == ON)
    33. isOn = true;
    34. else
    35. isOn = false;
    36.  
    37. return isOn;
    38. }
    To copy to clipboard, switch view to plain text mode 

    Any idea how to avoid sending 20 bytes due to calling isOn() four times for each checkbox (I've tried pause/sleep already), as well as being able to pass on the buffer values to the new variable readBytes, instead of having it remain as an empty string?


    Thanks in advance!

  5. #4
    Join Date
    Nov 2011
    Posts
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Checking QByteArray for certain values

    Hi

    I am no expert but here are some of my thoughts:
    You shouldn't use client.read() right after client.write(). Getting data over the network is asynchronous so you need to give it time to arrive.
    If I were to guess, you are using QAbstractSocket as your client. If so, you should use its signal readyRead() to know when to read the answer.
    This would explain why your program prints out your string only after you click a checkbox. At first pass of read_bytes() you don't have anything to read() as the answer have not arrived yet.
    To set your checkboxes you will need to implement some queue, for example: isOn(0); readyRead()->setchecked()->isOn(1); readyRead()->setChecked()->isOn(2); and so on.

    Hope it helps.

  6. #5
    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: Checking QByteArray for certain values

    In line 21 You are converting binary data to hex (that is to say hexadecimal character notation). So method read_bytes() returns more when 2 x 7 bytes. Next in line 32 You compare this character to binary value ON. This have no sense.

Similar Threads

  1. How to set values in QByteArray?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 19th July 2011, 06:44
  2. Replies: 1
    Last Post: 22nd June 2011, 08:12
  3. Checking QByteArray
    By camol in forum Qt Programming
    Replies: 3
    Last Post: 18th March 2011, 13:07
  4. Extracting int values from QByteArray
    By Tottish in forum Newbie
    Replies: 4
    Last Post: 7th April 2010, 10:41
  5. Replies: 9
    Last Post: 25th July 2009, 13:27

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.