PDA

View Full Version : Qt UDP network accessing to camera connected by LAN



RadioKong
15th January 2019, 08:51
Hello, I am trying to get data from a LAN-connected infrared camera.
I don't know how to receive packets from the external network.
Here's my code.



MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
// create a QUDP socket
socket = new QUdpSocket(this);
socket_cam = new QUdpSocket(this);
// The most common way to use QUdpSocket class is
// to bind to an address and port using bind()
// bool QAbstractSocket::bind(const QHostAddress & address,
// quint16 port = 0, BindMode mode = DefaultForPlatform)
socket->bind(QHostAddress::LocalHost, 1234);

connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}

void MyUDP::HelloUDP()
{
QByteArray Data;

QHostAddress camera_loc = QHostAddress("192.168.10.197");
quint16 cameraPort = 32197;

qint64 deg_num = socket->readDatagram(Data.data() ,964, &camera_loc, &cameraPort);
qDebug() << "Deg_num: " << deg_num; //this returns -1. Therefore, it seems it can't read any data from camera_loc.
// Sends the datagram datagram
// to the host address and at port.
// qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
// const QHostAddress & host, quint16 port)
socket->writeDatagram(Data, QHostAddress::LocalHost, 1234);
}

void MyUDP::readyRead()
{
// when data comes in
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());

QHostAddress sender = QHostAddress("192.168.10.197");
quint16 senderPort = 32197;

// qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize,
// QHostAddress * address = 0, quint16 * port = 0)
// Receives a datagram no larger than maxSize bytes and stores it in data.
// The sender's host address and port is stored in *address and *port
// (unless the pointers are 0).

socket->readDatagram(buffer.data(), buffer.size(),
&sender, &senderPort);
qDebug() << "Message from: " << sender.toString();
qDebug() << "Message port: " << senderPort;
qDebug() << "Message: " << buffer;
}


I changed a little from Qt5 Tutorial QUdpSocket(https://www.bogotobogo.com/Qt/Qt5_QUdpSocket.php)
As I've seen on Wireshark, packets arrived correctly as I expected.
However, my code doesn't work as I expected. (readDatagram on camera_loc returns (-1))
According to other threads, we don't need to connect them because UDP communication doesn't need to make connections.
What I want to make with this code is as follow.

(0. Save data from the camera (192.168.10.197) on a variable using readDatagram) I am not sure this is a really necessary process...:o
1. Write data to buffer as written in this code (using writeDatagram function).

I could not find solutions even if I struggled. :)
I thought it would be easy but it wasn't ...
Any advice will be very grateful because I am a newbie to qt and UDP network.

Thanks in advance. ;)

RadioKong

anda_skoa
16th January 2019, 21:08
Does the readyRead() slot get called?
Which value is returned by pendingDatagramSize()?

The readDatagram() call in HelloUDP() passes a null pointer as the receive buffer. Probably not what you want.

Cheers,
_