#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QThread>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QObject>
#include <QDebug>
#include <QTime>
#include "globalne.h"
{
Q_OBJECT
public:
explicit TcpClient
(QObject *parent
= 0);
~TcpClient();
void run();
bool StartConnection();
void zakoncz_polaczenie_z_e1000();
signals:
void connectionError();
public slots:
void readyRead();
void disconnected();
void connected();
// void bytesWritten (qint64 bytes);
private:
// CanMon canmnon_parent;
// QTcpSocket socket;
// QStringList lista;
bool ponawiaj;
};
#endif // TCPCLIENT_H
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QThread>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QObject>
#include <QDebug>
#include <QTime>
#include "globalne.h"
class TcpClient : public QThread
{
Q_OBJECT
public:
explicit TcpClient(QObject *parent = 0);
~TcpClient();
void run();
bool StartConnection();
void zakoncz_polaczenie_z_e1000();
signals:
void dataReady(QByteArray, QTime);
void connectionError();
public slots:
void readyRead();
void disconnected();
void connected();
void stateChanged(QAbstractSocket::SocketState);
void displayError(QAbstractSocket::SocketError);
// void bytesWritten (qint64 bytes);
private:
// CanMon canmnon_parent;
// QTcpSocket socket;
QTcpSocket *socket;
QTime time;
// QStringList lista;
bool ponawiaj;
};
#endif // TCPCLIENT_H
To copy to clipboard, switch view to plain text mode
//#include "tcpclient.h"
//TcpClient::TcpClient(QObject *parent) :
// QObject(parent)
//{
//}
#include <QStringList>
#include "tcpclient.h"
#include <QFile>
#include <QTextStream>
TcpClient
::TcpClient(QObject *parent
) :{
socket = NULL;
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState");
ponawiaj = true;
}
TcpClient::~TcpClient()
{
// qDebug() << "p";
disconnect(socket,SIGNAL(connected()), this, SLOT(connected()));
disconnect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
disconnect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
// disconnect(socket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(stateChanged(qSockState)));
socket->close();
qDebug() << "socket open: " << socket->isOpen();
delete socket;
}
{
qDebug() << "Socket state: " << st;
switch(st)
{
qDebug() << "sock open: " << socket->isOpen();
break;
}
}
{
qDebug() << "Socket error: " << socket->errorString();
qDebug() << "Socket error: " << se;
switch(se)
{
StartConnection();
break;
qDebug() << "Network error: " << socket->errorString();
break;
}
// this->StartConnection();
}
bool TcpClient::StartConnection()
{
#define z_pliku 0
#if z_pliku
QFile mFile
("iphosta.cfg");
{
qDebug() << mFile.errorString();
qDebug() << "nie otworzono pliku... próba połączenia z localhost";
socket->connectToHost("localhost",4010);
}
else
{
mFile.close();
qDebug() << "Connecting to: " << host;
socket->connectToHost(host,4010);
}
#else
// QString host = "192.168.1.48";
qDebug() << "Connecting to: " << host;
socket->connectToHost(host,4010);
#endif
if(socket->waitForConnected(5000))
{
qDebug() << "Connected: " << socket->isOpen();
}
else
{
qDebug() << "Not connected: " << socket->isOpen();
socket->close();
}
qDebug() << "sock open: " << socket->isOpen();
return socket->isOpen();
}
void TcpClient::zakoncz_polaczenie_z_e1000()
{
int result;
if (socket->state())
{
result
= socket
->write
(QByteArray::fromHex(QString("e1fc00000000000000000000000000000000000000000000000000f7e000e2").
toAscii()));
qDebug() << "nadano koniec polaczenia: " << result;
this->msleep(500);
qDebug() << "minely 2 sekundy";
}
ponawiaj = false;
}
void TcpClient::run()
{
qDebug() << "socket: " << socket;
if(!socket)
qDebug() << "socket: " << socket;
connect(socket,SIGNAL(connected()), this, SLOT(connected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
connect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
// error(QAbstractSocket::SocketError)
// SIGNAL(stateChanged(qSockState)),this,SLOT(tcStateChanged(qSockState)));
this->StartConnection();
this->exec();
}
void TcpClient::connected()
{
qDebug() << "connection established: " << socket->isOpen();
}
void TcpClient::disconnected()
{
qDebug() << " Disconnected";
}
void TcpClient::readyRead()
{
// qDebug() << "Redy read ";
if (socket->bytesAvailable() > 0)
{
// qDebug() << "dostepne bajty: " << socket->bytesAvailable();
data = socket->readAll();
// qDebug() << "odebrano: " << data.toHex();
time.start();
// qDebug() << "TIME: " << time.toString() << "." << time.msec();
emit dataReady(data, time);
}
}
//#include "tcpclient.h"
//TcpClient::TcpClient(QObject *parent) :
// QObject(parent)
//{
//}
#include <QStringList>
#include "tcpclient.h"
#include <QFile>
#include <QTextStream>
TcpClient::TcpClient(QObject *parent) :
QThread(parent)
{
socket = NULL;
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState");
ponawiaj = true;
}
TcpClient::~TcpClient()
{
// qDebug() << "p";
disconnect(socket,SIGNAL(connected()), this, SLOT(connected()));
disconnect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
disconnect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
// disconnect(socket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(stateChanged(qSockState)));
socket->close();
qDebug() << "socket open: " << socket->isOpen();
delete socket;
}
void TcpClient::stateChanged(QAbstractSocket::SocketState st)
{
qDebug() << "Socket state: " << st;
switch(st)
{
case QAbstractSocket::UnconnectedState:
qDebug() << "sock open: " << socket->isOpen();
break;
}
}
void TcpClient::displayError(QAbstractSocket::SocketError se)
{
qDebug() << "Socket error: " << socket->errorString();
qDebug() << "Socket error: " << se;
switch(se)
{
case QAbstractSocket::SocketTimeoutError:
StartConnection();
break;
case QAbstractSocket::NetworkError:
qDebug() << "Network error: " << socket->errorString();
break;
}
// this->StartConnection();
}
bool TcpClient::StartConnection()
{
#define z_pliku 0
#if z_pliku
QFile mFile("iphosta.cfg");
if (!mFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << mFile.errorString();
qDebug() << "nie otworzono pliku... próba połączenia z localhost";
socket->connectToHost("localhost",4010);
}
else
{
QTextStream in(&mFile);
QString host = in.readAll();
mFile.close();
qDebug() << "Connecting to: " << host;
socket->connectToHost(host,4010);
}
#else
QString host = "192.168.1.5";
// QString host = "192.168.1.48";
qDebug() << "Connecting to: " << host;
socket->connectToHost(host,4010);
#endif
if(socket->waitForConnected(5000))
{
qDebug() << "Connected: " << socket->isOpen();
}
else
{
qDebug() << "Not connected: " << socket->isOpen();
socket->close();
}
qDebug() << "sock open: " << socket->isOpen();
return socket->isOpen();
}
void TcpClient::zakoncz_polaczenie_z_e1000()
{
int result;
if (socket->state())
{
result = socket->write(QByteArray::fromHex(QString("e1fc00000000000000000000000000000000000000000000000000f7e000e2").toAscii()));
qDebug() << "nadano koniec polaczenia: " << result;
this->msleep(500);
qDebug() << "minely 2 sekundy";
}
ponawiaj = false;
}
void TcpClient::run()
{
qDebug() << "socket: " << socket;
if(!socket)
socket = new QTcpSocket();
qDebug() << "socket: " << socket;
connect(socket,SIGNAL(connected()), this, SLOT(connected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
connect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(socket,SIGNAL( stateChanged(QAbstractSocket::SocketState)),this, SLOT(stateChanged(QAbstractSocket::SocketState)));
// error(QAbstractSocket::SocketError)
// SIGNAL(stateChanged(qSockState)),this,SLOT(tcStateChanged(qSockState)));
this->StartConnection();
this->exec();
}
void TcpClient::connected()
{
qDebug() << "connection established: " << socket->isOpen();
}
void TcpClient::disconnected()
{
qDebug() << " Disconnected";
}
void TcpClient::readyRead()
{
QByteArray data;
// qDebug() << "Redy read ";
if (socket->bytesAvailable() > 0)
{
// qDebug() << "dostepne bajty: " << socket->bytesAvailable();
data = socket->readAll();
// qDebug() << "odebrano: " << data.toHex();
time.start();
// qDebug() << "TIME: " << time.toString() << "." << time.msec();
emit dataReady(data, time);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks