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.
void EcoDIOManager::connection_established()
{
QString strSuccess
= "Successfully connected to ",
device_address = this->ui->txtbxIP->text();
this->enableObjects();
this->ui->txtbxSuccess->setText(strSuccess%device_address);
this->ui->chkDIO0->setChecked(this->read_bytes(0));
this->ui->chkDIO1->setChecked(this->read_bytes(1));
this->ui->chkDIO2->setChecked(this->read_bytes(2));
this->ui->chkDIO3->setChecked(this->read_bytes(3));
}
void EcoDIOManager::connection_established()
{
QString strSuccess = "Successfully connected to ",
device_address = this->ui->txtbxIP->text();
this->enableObjects();
this->ui->txtbxSuccess->setText(strSuccess%device_address);
this->ui->chkDIO0->setChecked(this->read_bytes(0));
this->ui->chkDIO1->setChecked(this->read_bytes(1));
this->ui->chkDIO2->setChecked(this->read_bytes(2));
this->ui->chkDIO3->setChecked(this->read_bytes(3));
}
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).
bool EcoDIOManager::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(NULLPTR);
bytes.push_back(1);
bytes.push_back(dio_n);
client.write(bytes);
client.read(7);
return bytes[6] == '00';
}
bool EcoDIOManager::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(NULLPTR);
bytes.push_back(1);
bytes.push_back(dio_n);
client.write(bytes);
client.read(7);
return bytes[6] == '00';
}
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.:
return (bytes[8] == '0' & bytes[9] == '0');
return (bytes.mid(8,2) == '00');
return (bytes[8] == '0' & bytes[9] == '0');
return (bytes.mid(8,2) == '00');
To copy to clipboard, switch view to plain text mode
Etc.
Any help would be greatly appreciated. =)
Bookmarks