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
    Oct 2011
    Location
    Netherlands
    Posts
    13
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows
    Thanks
    7

    Default Re: Checking QByteArray for certain values

    Unless I completely misunderstand the QBA, I receive precisely what I expected. I've done as you adviced, and the following printed out in my output textbox:

    Qt Code:
    1. 01020003000100
    2. 01020003000101
    To copy to clipboard, switch view to plain text mode 

    I wrote to the DIO (output) first to turn it on, and then read it (input) to see the values in the textbox. The first line is DIO turned on, the second is off.

    So, assuming the returned value doesn't include anything beside that in a secretive manner, the reading works as it is supposed to. :S

    For debug purposes, I disabled the function calls for the other 3 checkboxes, it is still the same as before.

    Attachment 7035
    Attached Images Attached Images

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

    Default Re: Checking QByteArray for certain values

    Try that:
    Qt Code:
    1. void EcoDIOManager::connection_established()
    2. {
    3. QString strSuccess = "Successfully connected to ",
    4. device_address = this->ui->txtbxIP->text();
    5.  
    6. this->enableObjects();
    7. this->ui->txtbxSuccess->setText(strSuccess%device_address);
    8. this->ui->chkDIO0->setChecked(this->isOn(0));
    9. this->ui->chkDIO1->setChecked(this->isOn(1));
    10. this->ui->chkDIO2->setChecked(this->isOn(2));
    11. this->ui->chkDIO3->setChecked(this->isOn(3));
    12. }
    13.  
    14. QByteArray EcoDIOManager::read_bytes(quint16 dio_n)
    15. {
    16. QByteArray bytes;
    17. bytes.push_back(1);
    18. bytes.push_back(2);
    19. bytes.push_back(NULLPTR);
    20. bytes.push_back(1);
    21. bytes.push_back(dio_n);
    22.  
    23. client.write(bytes);
    24.  
    25. return client.read(7);
    26. }
    27.  
    28. bool EcoDIOManager::isOn( quint16 dio_n )
    29. {
    30. bool isOn = false;
    31. const char ON = 0x00;
    32. const int switchIndex = 6;
    33.  
    34. QByteArray readBytes = this.read_bytes(dio_n);
    35.  
    36. if(readBytes.size() > switchIndex && readBytes[switchIndex] == ON)
    37. isOn = true;
    38.  
    39. return isOn;
    40. }
    To copy to clipboard, switch view to plain text mode 

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

    -Kyr0s- (25th October 2011)

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

    Default Re: Checking QByteArray for certain values

    Thanks for taking your time to assist me, once again much appreciated.

    Seems that it still is refusing to work. I've put a breakpoint in bool EcoDIOManager::isOn(quint16 dio_n), and the following were the results once it returned at the end:

    Qt Code:
    1. ON: 0 '\0'
    2. dio_n: 0
    3. isOn: false
    4. readBytes: ""
    5. switchIndex: 6
    6. this: @0x28fe48
    To copy to clipboard, switch view to plain text mode 

    I guess readBytes isn't supposed to be an empty string?

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

    Default Re: Checking QByteArray for certain values

    No it should not

    To make sure that everything else works just your example values into the array and see if checkbox is set correctly.
    then investigate around that place, this array should contain exacly what you expect to see.

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

    -Kyr0s- (31st October 2011)

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

    Default Re: Checking QByteArray for certain values

    Hey again,

    I've been pulling my hair out for these past few days, but it seems I just can't find the problem.

    readBytes is still an empty string after this bit:

    Qt Code:
    1. bool EcoDIOManager::isOn(quint16 dio_n)
    2. {
    3. bool isOn = false;
    4. const char ON = 0x00;
    5. const int switchIndex = 6;
    6.  
    7. QByteArray readBytes = this->read_bytes(dio_n);
    8.  
    9. if(readBytes.size() > switchIndex && readBytes[switchIndex] == ON)
    10. {
    11. isOn = true;
    12. }
    13. else
    14. {
    15. isOn = false;
    16. }
    17.  
    18. return isOn;
    19. }
    To copy to clipboard, switch view to plain text mode 

    I suspected that the problem lies with the call of the read_bytes(dio_n) function.

    Qt Code:
    1. QByteArray EcoDIOManager::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. QByteArray bytes;
    11. bytes.push_back(1);
    12. bytes.push_back(2);
    13. bytes.push_back(NULLPTR);
    14. bytes.push_back(1);
    15. bytes.push_back(dio_n);
    16.  
    17. client.write(bytes);
    18.  
    19. this->ui->txtbxOutput->append(client.read(7).toHex());
    20.  
    21. return client.read(7);
    22. }
    To copy to clipboard, switch view to plain text mode 

    I've also tried return client.read(7).toHex();.

    The function on itself parses the values (the string of bytes) properly in the textbox (I can see the values I need without any issues), yet it cannot be used in EcoDIOManager::isOn().

    Any suggestions as to why this might occur at all? It's driving me crazy. I've nearly not had as many problems figuring out how to write as opposed to apply a process to reading.

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

    Default Re: Checking QByteArray for certain values

    Never mind, false alarm with this post. Wasn't called twice. >_<
    Last edited by -Kyr0s-; 31st October 2011 at 12:54.

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

    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.

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

    -Kyr0s- (31st October 2011)

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

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

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

    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!

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

    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.

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

    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
  •  
Qt is a trademark of The Qt Company.