Results 1 to 20 of 20

Thread: Checking QByteArray for certain values

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

    Default Checking QByteArray for certain values

    Hey all!

    I've been messing around like mad with my code, and it seems I can't get it right.

    Basically, I have a QByteArray that contains the values that I have read from my device. The results are usually like this:

    01 02 00 03 0x 0y 0z, where X is the DIO number, Y the Input or Output, and Z whether the DIO is turned on or off.

    I have to check these values (Y and Z), to see whether it's on Input (read) or Output (write), and whether it's turned on (0) or off (1), so that when I start the application, the checkboxes are either checked or unchecked dependent on the values that have been read.

    Here are the relevant code pieces:

    Using setChecked() for the checkboxes once clicked on the connect button, which contains the functions to then check whether the DIOs are on or off.
    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->read_bytes(0));
    9. this->ui->chkDIO1->setChecked(this->read_bytes(1));
    10. this->ui->chkDIO2->setChecked(this->read_bytes(2));
    11. this->ui->chkDIO3->setChecked(this->read_bytes(3));
    12. }
    To copy to clipboard, switch view to plain text mode 

    This is the function for checking the status (writing to the device which then responds with not 5 bytes, but 7 (6th is Input/Output, and 7th is On/Off).

    Qt Code:
    1. bool 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.  
    12. bytes.push_back(1);
    13. bytes.push_back(2);
    14. bytes.push_back(NULLPTR);
    15. bytes.push_back(1);
    16. bytes.push_back(dio_n);
    17.  
    18. client.write(bytes);
    19.  
    20. client.read(7);
    21.  
    22. return bytes[6] == '00';
    23. }
    To copy to clipboard, switch view to plain text mode 

    I want to return true or false dependent on the status with the return, but I've tried everything I had in mind, but couldn't fix it.

    E.g.:

    Qt Code:
    1. return (bytes[8] == '0' & bytes[9] == '0');
    2.  
    3. return (bytes.mid(8,2) == '00');
    To copy to clipboard, switch view to plain text mode 

    Etc.

    Any help would be greatly appreciated. =)

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Checking QByteArray for certain values

    I'm not sure I understand your problem, it doesn't seem realy clear to me.
    However, it seems to me that you want look if a byte is 0 and to do that
    Qt Code:
    1. bytes[8] == '0'
    To copy to clipboard, switch view to plain text mode 
    is wrong (you are actually testing 0 == 0x30) try
    Qt Code:
    1. bytes[8] == '\0'
    To copy to clipboard, switch view to plain text mode 
    instead.

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

    To test for 0x00 and 0x01 (or any other value)
    Qt Code:
    1. if(!bytes[8] && !bytes[9])
    To copy to clipboard, switch view to plain text mode 
    will work (only when both are 0x00 you'll get true).

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

    -Kyr0s- (25th October 2011)

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

    Hey,

    Thanks to you both for the swift replies. Much appreciated.

    Unfortunately neither suggestions worked. Your suggestion, Spitfire, somehow set all checkboxes to true. Not sure why.

    Sorry for confusing you, Nix. I'll try to explain better.

    Basically, I have a device which can be written to, and read from. Each of these have their own functions (write_bytes and read_bytes).

    My intention is to check the state of the LEDs (Digital I/Os) by writing to the device, which then in turn, delivers a response. Unlike the writing, which has 5 bytes, the response has 7. These extra two bytes are the ones that return the state of being Input (which means you can only read) or Output (which means only writing), and whether the LEDs are turned on or off.

    So, for instance, LED 4(number 3) are on and on output, the response would be (used .toHex() to see the results in the textbox):

    Qt Code:
    1. 01 02 00 03 03 (LED number) 01 (output) 00 (on)
    To copy to clipboard, switch view to plain text mode 

    So, when I start the application, I want it to check what the status of the checkboxes are (on or off) right away, so when the user presses connect, checkbox 4 is checked (using this example) and the rest turned off.

    Here's the entire code if needed:

    Qt Code:
    1. #include "ecodiomanager.h"
    2. #include "ui_ecodiomanager.h"
    3.  
    4. const char NULLPTR = 0;
    5. const quint16 iNULLPTR = 0;
    6.  
    7. EcoDIOManager::EcoDIOManager(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::EcoDIOManager)
    10. {
    11. ui->setupUi(this);
    12. setWindowFlags(windowFlags() & ~Qt::WindowMinimizeButtonHint);
    13. setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
    14.  
    15. this->ui->pushDisconnect->setShown(false);
    16.  
    17. this->connect(ui->pushConnect, SIGNAL(clicked()), this, SLOT(pre_transfer()));
    18. this->connect(ui->pushDisconnect, SIGNAL(clicked()), this, SLOT(disconnecting()));
    19. this->connect(&client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connection_failed()));
    20. this->connect(&client, SIGNAL(connected()), this, SLOT(connection_established()));
    21. }
    22.  
    23. EcoDIOManager::~EcoDIOManager()
    24. {
    25. client.abort();
    26. delete this->ui;
    27. }
    28.  
    29. void EcoDIOManager::enableObjects()
    30. {
    31. this->ui->txtbxIP->setEnabled(false);
    32. this->ui->txtbxPort->setEnabled(false);
    33. this->ui->txtbxOutput->setEnabled(true);
    34. this->ui->pushConnect->setShown(false);
    35. this->ui->pushDisconnect->setShown(true);
    36. this->ui->lblDIO0->setEnabled(true);
    37. this->ui->lblDIO1->setEnabled(true);
    38. this->ui->lblDIO2->setEnabled(true);
    39. this->ui->lblDIO3->setEnabled(true);
    40. this->ui->chkDIO0->setEnabled(true);
    41. this->ui->chkDIO1->setEnabled(true);
    42. this->ui->chkDIO2->setEnabled(true);
    43. this->ui->chkDIO3->setEnabled(true);
    44. this->ui->comboDIO0->setEnabled(true);
    45. this->ui->comboDIO1->setEnabled(true);
    46. this->ui->comboDIO2->setEnabled(true);
    47. this->ui->comboDIO3->setEnabled(true);
    48. }
    49.  
    50. void EcoDIOManager::disableObjects()
    51. {
    52. this->ui->txtbxIP->setEnabled(true);
    53. this->ui->txtbxPort->setEnabled(true);
    54. this->ui->pushConnect->setShown(true);
    55. this->ui->pushDisconnect->setShown(false);
    56. this->ui->lblDIO0->setEnabled(false);
    57. this->ui->lblDIO1->setEnabled(false);
    58. this->ui->lblDIO2->setEnabled(false);
    59. this->ui->lblDIO3->setEnabled(false);
    60. this->ui->chkDIO0->setEnabled(false);
    61. this->ui->chkDIO1->setEnabled(false);
    62. this->ui->chkDIO2->setEnabled(false);
    63. this->ui->chkDIO3->setEnabled(false);
    64. this->ui->comboDIO0->setEnabled(false);
    65. this->ui->comboDIO1->setEnabled(false);
    66. this->ui->comboDIO2->setEnabled(false);
    67. this->ui->comboDIO3->setEnabled(false);
    68. this->ui->txtbxOutput->setEnabled(false);
    69. }
    70.  
    71. void EcoDIOManager::pre_transfer()
    72. {
    73. QString device_address = this->ui->txtbxIP->text();
    74. quint16 device_port = this->ui->txtbxPort->text().toInt();
    75.  
    76. this->connectTo(device_address, device_port);
    77. }
    78.  
    79. void EcoDIOManager::connectTo(QString host, quint16 port_n)
    80. {
    81. QHostAddress addr(host);
    82.  
    83. client.connectToHost(addr, port_n);
    84. }
    85.  
    86. void EcoDIOManager::connection_established()
    87. {
    88. QString strSuccess = "Successfully connected to ",
    89. device_address = this->ui->txtbxIP->text();
    90.  
    91. this->enableObjects();
    92. this->ui->txtbxSuccess->setText(strSuccess%device_address);
    93. this->ui->chkDIO0->setChecked(this->read_bytes(0));
    94. this->ui->chkDIO1->setChecked(this->read_bytes(1));
    95. this->ui->chkDIO2->setChecked(this->read_bytes(2));
    96. this->ui->chkDIO3->setChecked(this->read_bytes(3));
    97. }
    98.  
    99. void EcoDIOManager::connection_failed()
    100. {
    101. QString strFailed = "Failed to connect to ",
    102. device_address = this->ui->txtbxIP->text();
    103.  
    104. this->ui->txtbxSuccess->setText(strFailed%device_address);
    105. }
    106.  
    107. void EcoDIOManager::disconnecting()
    108. {
    109. client.abort();
    110.  
    111. this->ui->txtbxSuccess->clear();
    112. this->ui->txtbxOutput->clear();
    113. this->disableObjects();
    114. }
    115.  
    116. void EcoDIOManager::write_dio(quint16 dio_n, char in_out, char on_off)
    117. {
    118. send_bytes(dio_n, in_out, on_off);
    119. }
    120.  
    121. void EcoDIOManager::send_bytes(quint16 dio_n, char in_out, char on_off)
    122. {
    123. /*
    124.   * 1st byte: Command Number
    125.   * 2nd byte: Version
    126.   * 3rd byte: Only for response
    127.   * 4th byte: Data length
    128.   * 5th byte: DIO channel
    129.   * 6th byte: Input/Output (0 = Input, 1 = Output)
    130.   * 7th byte: On/Off (0 = On, 1 = Off)
    131.   */
    132. QByteArray bytes;
    133.  
    134. bytes.push_back(2);
    135. bytes.push_back(2);
    136. bytes.push_back(NULLPTR);
    137. bytes.push_back(3);
    138. bytes.push_back(dio_n);
    139. bytes.push_back(in_out);
    140. bytes.push_back(on_off);
    141.  
    142. client.write(bytes);
    143. }
    144.  
    145. void EcoDIOManager::read_dio(quint16 dio_n)
    146. {
    147. read_bytes(dio_n);
    148. }
    149.  
    150. bool EcoDIOManager::read_bytes(quint16 dio_n)
    151. {
    152. /* *******************************
    153.   * 1st byte: Command number, 1
    154.   * 2nd byte: Version, 2
    155.   * 3rd byte: Response only, any
    156.   * 4th byte: Data length, 1
    157.   * 5th byte: DIO channel
    158.   ********************************/
    159. QByteArray bytes;
    160.  
    161. bytes.push_back(1);
    162. bytes.push_back(2);
    163. bytes.push_back(NULLPTR);
    164. bytes.push_back(1);
    165. bytes.push_back(dio_n);
    166.  
    167. client.write(bytes);
    168.  
    169. client.read(7);
    170.  
    171. return (!bytes[6] && !bytes[7]);
    172. }
    173.  
    174. void EcoDIOManager::on_chkDIO0_toggled(bool checked)
    175. {
    176. if(checked && this->ui->comboDIO0->currentText() == "Input") this->read_dio(iNULLPTR);
    177. else if(checked && this->ui->comboDIO0->currentText() == "Output") this->write_dio(iNULLPTR, 1, NULLPTR);
    178. else if((!checked) && this->ui->comboDIO0->currentText() == "Input") this->read_dio(iNULLPTR);
    179. else if((!checked) && this->ui->comboDIO0->currentText() == "Output") this->write_dio(iNULLPTR, 1, 1);
    180. }
    181.  
    182. void EcoDIOManager::on_chkDIO1_toggled(bool checked)
    183. {
    184. if(checked && this->ui->comboDIO1->currentText() == "Input") this->read_dio(1);
    185. else if(checked && this->ui->comboDIO1->currentText() == "Output") this->write_dio(1, 1, NULLPTR);
    186. else if((!checked) && this->ui->comboDIO1->currentText() == "Input") this->read_dio(1);
    187. else if((!checked) && this->ui->comboDIO1->currentText() == "Output") this->write_dio(1, 1, 1);
    188. }
    189.  
    190. void EcoDIOManager::on_chkDIO2_toggled(bool checked)
    191. {
    192. if(checked && this->ui->comboDIO2->currentText() == "Input") this->read_dio(2);
    193. else if(checked && this->ui->comboDIO2->currentText() == "Output") this->write_dio(2, 1, NULLPTR);
    194. else if((!checked) && this->ui->comboDIO2->currentText() == "Input") this->read_dio(2);
    195. else if((!checked) && this->ui->comboDIO2->currentText() == "Output") this->write_dio(2, 1, 1);
    196. }
    197.  
    198. void EcoDIOManager::on_chkDIO3_toggled(bool checked)
    199. {
    200. if(checked && this->ui->comboDIO3->currentText() == "Input") this->read_dio(3);
    201. else if(checked && this->ui->comboDIO3->currentText() == "Output") this->write_dio(3, 1, NULLPTR);
    202. else if((!checked) && this->ui->comboDIO3->currentText() == "Input") this->read_dio(3);
    203. else if((!checked) && this->ui->comboDIO3->currentText() == "Output") this->write_dio(3, 1, 1);
    204. }
    205.  
    206. void EcoDIOManager::on_pushClear_clicked()
    207. {
    208. this->ui->txtbxOutput->clear();
    209. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by -Kyr0s-; 25th October 2011 at 11:46.

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

    Qt Code:
    1. if(bytes[6])
    2. {
    3. // LED off
    4. }
    5. else
    6. {
    7. // LED on
    8. }
    To copy to clipboard, switch view to plain text mode 
    Also when you read 7 bytes index of last byte is 6 not 7, that's why all checkboxes are set to true.

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

    -Kyr0s- (25th October 2011)

  8. #6
    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 see, indeed. Thanks for reminding me. =)

    And unfortunately, that didn't work either. I went into debug mode and added bytes[6] to my Watch List, and it appears that it constantly has "<no such value>". The checkboxes, even with index 6, are all set to true still, too.
    Attached Images Attached Images

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

    when you're reading some unasigned value you're getting some memory garbage, most likely it will be set to something else than 0x00 that's why it returns true.

    When you look at your code you have a 5 bytes you write to the client, then you read 7 bytes, but the read fucntion do not assing return value to anything. shouldn't it be
    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.  
    11. bytes = client.read(7);
    To copy to clipboard, switch view to plain text mode 
    ?

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

    -Kyr0s- (25th October 2011)

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

    Thanks for the quick reply once more.

    Good call. I didn't assign it to the bytes variable at all. I've changed it, but that does not change the case however; all checkboxes are still either checked or unchecked (as in, all of them), and the Watch List shows <no such value> still, regardless of what index I use (tried index 0, 1, 3, 6, etc.) :/

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

    I think you're reading from the device wrong or something of that sort.

    Look at what your read() function returns (assign it to new bytearray and print).
    Make sure that you're geting what you expect and follow from there.

    the if(bytes[x]) will do what you want as long as you check correct and valid byte.

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

    -Kyr0s- (25th October 2011)

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

    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

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

    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 

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

    -Kyr0s- (25th October 2011)

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

    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?

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

    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.

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

    -Kyr0s- (31st October 2011)

  20. #14
    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

    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.

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

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

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

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

    -Kyr0s- (31st October 2011)

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

  25. #18
    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!

  26. #19
    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.

  27. #20
    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, 07:44
  2. Replies: 1
    Last Post: 22nd June 2011, 09:12
  3. Checking QByteArray
    By camol in forum Qt Programming
    Replies: 3
    Last Post: 18th March 2011, 14:07
  4. Extracting int values from QByteArray
    By Tottish in forum Newbie
    Replies: 4
    Last Post: 7th April 2010, 11:41
  5. Replies: 9
    Last Post: 25th July 2009, 14: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.