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:
this->ui->chkDIO0->setChecked(this->isOn(0));
this->ui->chkDIO1->setChecked(this->isOn(1));
this->ui->chkDIO2->setChecked(this->isOn(2));
this->ui->chkDIO3->setChecked(this->isOn(3));
this->ui->chkDIO0->setChecked(this->isOn(0));
this->ui->chkDIO1->setChecked(this->isOn(1));
this->ui->chkDIO2->setChecked(this->isOn(2));
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.
QByteArray DIOMoxaBoard
::read_bytes(quint16 dio_n
) {
/* *******************************
* 1st byte: Command number, 1
* 2nd byte: Version, 2
* 3rd byte: Response only, any
* 4th byte: Data length, 1
* 5th byte: DIO channel
********************************/
bytes.push_back(1);
bytes.push_back(2);
bytes.push_back(_NULL);
bytes.push_back(1);
bytes.push_back(dio_n);
client.write(bytes); // successfully writes 5 bytes if it doesn't access isOn()
return client.read(7).toHex(); //successfully reads 7 bytes if it doesn't access isOn()
}
bool DIOMoxaBoard::isOn(quint16 dio_n)
{
bool isOn = false;
const char ON = 0x00;
const int switchIndex = 6;
QByteArray readBytes
= this
->read_bytes
(dio_n
);
//readBytes remains ""
if(readBytes.size() > switchIndex && readBytes[switchIndex] == ON)
isOn = true;
else
isOn = false;
return isOn;
}
QByteArray DIOMoxaBoard::read_bytes(quint16 dio_n)
{
/* *******************************
* 1st byte: Command number, 1
* 2nd byte: Version, 2
* 3rd byte: Response only, any
* 4th byte: Data length, 1
* 5th byte: DIO channel
********************************/
QByteArray bytes;
bytes.push_back(1);
bytes.push_back(2);
bytes.push_back(_NULL);
bytes.push_back(1);
bytes.push_back(dio_n);
client.write(bytes); // successfully writes 5 bytes if it doesn't access isOn()
return client.read(7).toHex(); //successfully reads 7 bytes if it doesn't access isOn()
}
bool DIOMoxaBoard::isOn(quint16 dio_n)
{
bool isOn = false;
const char ON = 0x00;
const int switchIndex = 6;
QByteArray readBytes = this->read_bytes(dio_n); //readBytes remains ""
if(readBytes.size() > switchIndex && readBytes[switchIndex] == ON)
isOn = true;
else
isOn = false;
return isOn;
}
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!
Bookmarks