Originally Posted by
nix
QT += network
QT += network
To copy to clipboard, switch view to plain text mode
Yes. It was it.
I change my code.
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTcpSocket>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
text->setText("");
text->setGeometry(20,20,400,150);
Test();
}
void MainWindow::Test()
{
connect(socket,SIGNAL(connected()),this,SLOT(connected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
//connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
text->setText("Conecting...");
socket->connectToHost("169.254.19.60",23);
if (!socket->waitForConnected(5000))
{
text->setText("Error: "+socket->errorString());
}
}
void MainWindow::connected()
{
text->setText("Connected");
}
void MainWindow::readyRead()
{
text->setText("Reading...\n"+buf);
}
//void MainWindow::disconnected()
//{
// text->setText("Disconnected");
//}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTcpSocket>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
text = new QLabel("",this);
text->setText("");
text->setGeometry(20,20,400,150);
Test();
}
void MainWindow::Test()
{
socket = new QTcpSocket(this);
connect(socket,SIGNAL(connected()),this,SLOT(connected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
//connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
text->setText("Conecting...");
socket->connectToHost("169.254.19.60",23);
if (!socket->waitForConnected(5000))
{
text->setText("Error: "+socket->errorString());
}
}
void MainWindow::connected()
{
text->setText("Connected");
}
void MainWindow::readyRead()
{
QByteArray buf = socket->readAll();
text->setText("Reading...\n"+buf);
}
//void MainWindow::disconnected()
//{
// text->setText("Disconnected");
//}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QLabel>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void Test();
private:
Ui::MainWindow *ui;
public slots:
void connected();
// void disconnected();
void readyRead();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QLabel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void Test();
private:
Ui::MainWindow *ui;
QTcpSocket *socket;
QLabel *text;
public slots:
void connected();
// void disconnected();
void readyRead();
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
Now i'm getting error with timed out because i don't connect anything to HostAddress in connectToHost but i'm wondering if i connect to my PC microcontroller and send data from it to PC via ethernet will i get data i was send? I want to ask is my code correct to receive binary data? May i send to myself some data and read it using my program (if yes what to add, i try
socket->write(writeBuf);
QByteArray writeBuf = "...Data...";
socket->write(writeBuf);
To copy to clipboard, switch view to plain text mode
but it don't working)? Thanks in advance.
Bookmarks