PDA

View Full Version : Using QTcpSocket to receive binary data



felo188
22nd August 2011, 13:20
Hi,

I wolud like to connect via ethernet with microcontroller. At the beginning i try with an example broadcast receiver and only think i change was
QHostAddress address;
address.setAddress(QString("192.168.20.1"));
udpSocket->bind(address, QUdpSocket::ShareAddress);
Next i try use telnet (hyperterminal) to connect and send some data. I sets the same address like in qt, connect using: TCP/IP (Winsock) and port number 23. But the hyperterminal shows message window with Unable to connect.
And my first question is how to send some data (binary or string) from application like hyperterminal (maybe other) to gui in qt using ethernet (QNetwork). Maybe is some software allows me to put some data to chosen address.

Secondly i try to write my code but now using tcp/ip socket, not udp.
void MainWindow::receivData()
{
if (receivSocket->waitForConnected(3000))
{
qDebug() << "Connected";
}
QByteArray buffer = receivSocket->read();
bool ok;
int data = buffer.toInt(&ok,10);
ui->label->setText(QString::number(data));
}

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
receivSocket = new QTcpSocket(this);
connect(receivSocket,SIGNAL(readyRead()),this,SLOT (receivData()));
QHostAddress address;
address.setAddress(QString("192.168.20.1"));
receivSocket->connectToHost(address,QIODevice::ReadOnly);
}
and in .h
#include <QtNetwork/QTcpSocket>
...
private:
QTcpSocket *receivSocket;
private slots:
void receivData();
I try to compile it but are some errors. error: no matching function for call to 'QTcpSocket::read()' Follow http://doc.qt.nokia.com/latest/network-programming.html (this) QTcpSocket contains read(). My second question is what i should add/change in code if i want to receive some data.
Thanks in advance for any help.

ChrisW67
23rd August 2011, 01:50
Telnet uses TCP not UDP so trying to marry the two was never going to work.

QTcpSocket does not contain a read() method that takes no arguments, which what your compiler is telling you about your code.

felo188
23rd August 2011, 11:20
QTcpSocket does not contain a read() method that takes no arguments, which what your compiler is telling you about your code.Ok. I change it to correct form. But now when i'm debugging i gets errors:

\mainwindow.cpp:7: error: undefined reference to '_imp___ZN15QAbstractSocket16waitForConnectedEi'
\mainwindow.cpp:22: error: undefined reference to `_imp___ZN10QTcpSocketC1EP7QObject'
\mainwindow.cpp:24: error: undefined reference to `_imp___ZN15QAbstractSocket13connectToHostERK7QStr ingt6QFlagsIN9QIODevice12OpenModeFlagEE'
\mainwindow.cpp:24: error: undefined reference to `_imp___ZN15QAbstractSocket13connectToHostERK7QStr ingt6QFlagsIN9QIODevice12OpenModeFlagEE'
\mainwindow.cpp:24: error: undefined reference to `_imp___ZN15QAbstractSocket13connectToHostERK7QStr ingt6QFlagsIN9QIODevice12OpenModeFlagEE'
Is this related with header? And what about rest of code. Is he correct? Maybe i missing somethink?

nix
23rd August 2011, 12:21
Post your complete code (mainwindow.cpp & mainwindow.h) to be sure and have some advice, it seems to me you are missing
#include <QTcpSocket> or maybe
QT += network in your .pro, but without any reproductible code to try can't be too sure.


For your code, I don't understand why you are testing the connection's state in the slot connected to the readyRead() signal . If you want to be sure you are trully connected used waitForConnected() member or the connected() signal

felo188
23rd August 2011, 14:13
QT += network
Yes. It was it.
I change my code.
.cpp

#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(conne cted()));
connect(socket,SIGNAL(readyRead()),this,SLOT(ready Read()));
//connect(socket,SIGNAL(disconnected()),this,SLOT(di sconnected()));

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;
}

.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

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
QByteArray writeBuf = "...Data...";
socket->write(writeBuf);but it don't working)? Thanks in advance.

nix
23rd August 2011, 15:03
If you want to test your code in local (using only your computer) you have to do your own TCP server based on (QTcpServer) take a look to Fortune server example.

In your case the first thing you have to do is connecting. Without a connection you can't do anything in TCP socket. A socket is a bidirectionnal channel for communication data you wrote is send on the other side and data wrote by the other side is received by your app, that the basis.
So it's perfectly normal that you do not received what you sent.