#include "etclient.h"
#include "ui_etclient.h"
#include "about.h"
ETClient
::ETClient(QWidget *parent
) : ui(new Ui::ETClient)
{
this->ui->setupUi(this);
this->connect(ui->menuAbout,SIGNAL(triggered()),this,SLOT(show_about()));
this->connect(ui->pushConnect,SIGNAL(clicked()),this,SLOT(pre_begin()));
this->connect(ui->pushDisconnect,SIGNAL(clicked()),this,SLOT(close_session()));
this
->connect
(&client,
SIGNAL(error
(QAbstractSocket::SocketError)),
this,
SLOT(connection_invalid
()));
this->connect(&client,SIGNAL(connected()),this,SLOT(start_transfer()));
this->ui->pushDisconnect->setShown(false);
}
ETClient::~ETClient()
{
client.close();
delete(this->ui);
}
void ETClient::show_about()
{
About *about = new About();
about->show();
}
void ETClient::pre_begin()
{
QString device_addr
= this
->ui
->txtbxIP
->text
();
int device_port = this->ui->txtbxPort->text().toInt();
this->begin(device_addr,device_port);
}
void ETClient
::begin(QString host, quint16 port_n
) {
client.connectToHost(addr,port_n);
}
void ETClient::start_transfer()
{
QString conSucc
= "Connected successfully to: ";
QString device_addr
= this
->ui
->txtbxIP
->text
();
this->ui->txtbxIP->setText(conSucc%device_addr);
this->ui->txtbxIP->setEnabled(false);
this->ui->txtbxPort->setEnabled(false);
this->ui->pushConnect->setShown(false);
this->ui->pushDisconnect->setShown(true);
this->ui->chkDIO0->setEnabled(true);
this->ui->chkDIO1->setEnabled(true);
this->ui->chkDIO2->setEnabled(true);
this->ui->chkDIO3->setEnabled(true);
this->on_pushSendData_clicked();
}
void ETClient::connection_invalid()
{
QString strInvalid
= "Failed to connect. ";
QString device_addr
= this
->ui
->txtbxIP
->text
();
this->ui->txtbxIP->setText("Invalid IP.");
this->ui->txtbxIP->isActiveWindow() == true;
}
void ETClient::close_session()
{
QString device_addr
= this
->ui
->txtbxIP
->text
();
client.abort();
this->ui->txtbxIP->setEnabled(true);
this->ui->txtbxPort->setEnabled(true);
this->ui->pushDisconnect->setShown(false);
this->ui->pushConnect->setShown(true);
this->ui->chkDIO0->setEnabled(false);
this->ui->chkDIO1->setEnabled(false);
this->ui->chkDIO2->setEnabled(false);
this->ui->chkDIO3->setEnabled(false);
}
void ETClient::on_pushSendData_clicked()
{
bool dio0 = this->ui->chkDIO0->isChecked();
bool dio1 = this->ui->chkDIO1->isChecked();
bool dio2 = this->ui->chkDIO2->isChecked();
bool dio3 = this->ui->chkDIO3->isChecked();
if(dio0 == true) this->dio0_enabled();
if(dio1 == true) this->dio1_enabled();
if(dio2 == true) this->dio2_enabled();
if(dio3 == true) this->dio3_enabled();
else;
}
/*
*
* DIO management functions (DIO 0,1,2,3 ENABLED & DISABLED)
*
*/
void ETClient::dio0_enabled()
{
int nullptr = 0;
bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(nullptr); // DIO number (0 = 1ste, 1 = 2de, 2 = 3de, 3 = 4de)
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(nullptr); // signal (0 = aan, 1 = uit)
client.write(bytes);
}
void ETClient::dio1_enabled()
{
int nullptr = 0;
bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(1); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(nullptr); // signal (0, 1)
client.write(bytes);
}
void ETClient::dio2_enabled()
{
int nullptr = 0;
bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(2); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(nullptr); // signal (0, 1)
client.write(bytes);
}
void ETClient::dio3_enabled()
{
int nullptr = 0;
bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(3); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(nullptr); // signal (0, 1)
client.write(bytes);
}
void ETClient::dio0_disabled()
{
int nullptr = 0;
bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(nullptr); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(1); // signal (0, 1)
client.write(bytes);
}
void ETClient::dio1_disabled()
{
int nullptr = 0;
bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(1); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(1); // signal (0, 1)
client.write(bytes);
}
void ETClient::dio2_disabled()
{
int nullptr = 0;
bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(2); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(1); // signal (0, 1)
client.write(bytes);
}
void ETClient::dio3_disabled()
{
int nullptr = 0;
bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(3); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(1); // signal (0, 1)
client.write(bytes);
}
Bookmarks