#include "ecodiomanager.h"
#include "ui_ecodiomanager.h"
const char NULLPTR = 0;
const quint16 iNULLPTR = 0;
EcoDIOManager
::EcoDIOManager(QWidget *parent
) : ui(new Ui::EcoDIOManager)
{
ui->setupUi(this);
setWindowFlags(windowFlags() & ~Qt::WindowMinimizeButtonHint);
setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
this->ui->pushDisconnect->setShown(false);
this->connect(ui->pushConnect, SIGNAL(clicked()), this, SLOT(pre_transfer()));
this->connect(ui->pushDisconnect, SIGNAL(clicked()), this, SLOT(disconnecting()));
this
->connect
(&client,
SIGNAL(error
(QAbstractSocket::SocketError)),
this,
SLOT(connection_failed
()));
this->connect(&client, SIGNAL(connected()), this, SLOT(connection_established()));
}
EcoDIOManager::~EcoDIOManager()
{
client.abort();
delete this->ui;
}
void EcoDIOManager::enableObjects()
{
this->ui->txtbxIP->setEnabled(false);
this->ui->txtbxPort->setEnabled(false);
this->ui->txtbxOutput->setEnabled(true);
this->ui->pushConnect->setShown(false);
this->ui->pushDisconnect->setShown(true);
this->ui->lblDIO0->setEnabled(true);
this->ui->lblDIO1->setEnabled(true);
this->ui->lblDIO2->setEnabled(true);
this->ui->lblDIO3->setEnabled(true);
this->ui->chkDIO0->setEnabled(true);
this->ui->chkDIO1->setEnabled(true);
this->ui->chkDIO2->setEnabled(true);
this->ui->chkDIO3->setEnabled(true);
this->ui->comboDIO0->setEnabled(true);
this->ui->comboDIO1->setEnabled(true);
this->ui->comboDIO2->setEnabled(true);
this->ui->comboDIO3->setEnabled(true);
}
void EcoDIOManager::disableObjects()
{
this->ui->txtbxIP->setEnabled(true);
this->ui->txtbxPort->setEnabled(true);
this->ui->pushConnect->setShown(true);
this->ui->pushDisconnect->setShown(false);
this->ui->lblDIO0->setEnabled(false);
this->ui->lblDIO1->setEnabled(false);
this->ui->lblDIO2->setEnabled(false);
this->ui->lblDIO3->setEnabled(false);
this->ui->chkDIO0->setEnabled(false);
this->ui->chkDIO1->setEnabled(false);
this->ui->chkDIO2->setEnabled(false);
this->ui->chkDIO3->setEnabled(false);
this->ui->comboDIO0->setEnabled(false);
this->ui->comboDIO1->setEnabled(false);
this->ui->comboDIO2->setEnabled(false);
this->ui->comboDIO3->setEnabled(false);
this->ui->txtbxOutput->setEnabled(false);
}
void EcoDIOManager::pre_transfer()
{
QString device_address
= this
->ui
->txtbxIP
->text
();
quint16 device_port = this->ui->txtbxPort->text().toInt();
this->connectTo(device_address, device_port);
}
void EcoDIOManager
::connectTo(QString host, quint16 port_n
) {
client.connectToHost(addr, port_n);
}
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_failed()
{
QString strFailed
= "Failed to connect to ",
device_address = this->ui->txtbxIP->text();
this->ui->txtbxSuccess->setText(strFailed%device_address);
}
void EcoDIOManager::disconnecting()
{
client.abort();
this->ui->txtbxSuccess->clear();
this->ui->txtbxOutput->clear();
this->disableObjects();
}
void EcoDIOManager::write_dio(quint16 dio_n, char in_out, char on_off)
{
send_bytes(dio_n, in_out, on_off);
}
void EcoDIOManager::send_bytes(quint16 dio_n, char in_out, char on_off)
{
/*
* 1st byte: Command Number
* 2nd byte: Version
* 3rd byte: Only for response
* 4th byte: Data length
* 5th byte: DIO channel
* 6th byte: Input/Output (0 = Input, 1 = Output)
* 7th byte: On/Off (0 = On, 1 = Off)
*/
bytes.push_back(2);
bytes.push_back(2);
bytes.push_back(NULLPTR);
bytes.push_back(3);
bytes.push_back(dio_n);
bytes.push_back(in_out);
bytes.push_back(on_off);
client.write(bytes);
}
void EcoDIOManager::read_dio(quint16 dio_n)
{
read_bytes(dio_n);
}
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] && !bytes[7]);
}
void EcoDIOManager::on_chkDIO0_toggled(bool checked)
{
if(checked && this->ui->comboDIO0->currentText() == "Input") this->read_dio(iNULLPTR);
else if(checked && this->ui->comboDIO0->currentText() == "Output") this->write_dio(iNULLPTR, 1, NULLPTR);
else if((!checked) && this->ui->comboDIO0->currentText() == "Input") this->read_dio(iNULLPTR);
else if((!checked) && this->ui->comboDIO0->currentText() == "Output") this->write_dio(iNULLPTR, 1, 1);
}
void EcoDIOManager::on_chkDIO1_toggled(bool checked)
{
if(checked && this->ui->comboDIO1->currentText() == "Input") this->read_dio(1);
else if(checked && this->ui->comboDIO1->currentText() == "Output") this->write_dio(1, 1, NULLPTR);
else if((!checked) && this->ui->comboDIO1->currentText() == "Input") this->read_dio(1);
else if((!checked) && this->ui->comboDIO1->currentText() == "Output") this->write_dio(1, 1, 1);
}
void EcoDIOManager::on_chkDIO2_toggled(bool checked)
{
if(checked && this->ui->comboDIO2->currentText() == "Input") this->read_dio(2);
else if(checked && this->ui->comboDIO2->currentText() == "Output") this->write_dio(2, 1, NULLPTR);
else if((!checked) && this->ui->comboDIO2->currentText() == "Input") this->read_dio(2);
else if((!checked) && this->ui->comboDIO2->currentText() == "Output") this->write_dio(2, 1, 1);
}
void EcoDIOManager::on_chkDIO3_toggled(bool checked)
{
if(checked && this->ui->comboDIO3->currentText() == "Input") this->read_dio(3);
else if(checked && this->ui->comboDIO3->currentText() == "Output") this->write_dio(3, 1, NULLPTR);
else if((!checked) && this->ui->comboDIO3->currentText() == "Input") this->read_dio(3);
else if((!checked) && this->ui->comboDIO3->currentText() == "Output") this->write_dio(3, 1, 1);
}
void EcoDIOManager::on_pushClear_clicked()
{
this->ui->txtbxOutput->clear();
}
Bookmarks