Hello everybody. I have been strugling with something since several weeks already, as I still havent understood entyrely the behaviour of UDP socket programming and threads.
I must create a server that reads the pending datagram from the client (it is a robot from KUKA). However I need to reply this package within 11 ms with a previously made string taken from an xml file. The package repplied must mirror the timstamp received in the message from the client. So far, so good (at least that is what I think). Using several tutorial, I finally manage to do that code. The problem arises when I try either to read the package received from the client or to write something different in the datagram to send to the client. Whenever I write a line of code that slows down the program a bit, the communication is broken and the robot disconnects as it did not have an answer within that time.
I have tried to use Qthread in different ways, but it seems that I havent understood it at all. What ever I do ends up in stopping the robot or crashing my program.
Here is what I have so far. I mean the "working implementation".
myudp.h
#ifndef MYUDP_H
#define MYUDP_H
#include <QObject>
#include <QUdpSocket>
#include <QtXml>
#include <QTextStream>
#include <QDebug>
#include <mythread.h>
{
Q_OBJECT
quint16 senderPort;
public:
explicit MyUDP
(QObject *parent
= 0);
void CopyIPOC();
signals:
void started();
public slots:
void readPendingDatagram();
void SendUDP();
void writeReceivedData();
private:
};
#endif // MYUDP_H
#ifndef MYUDP_H
#define MYUDP_H
#include <QObject>
#include <QUdpSocket>
#include <QtXml>
#include <QTextStream>
#include <QDebug>
#include <mythread.h>
class MyUDP : public QObject
{
Q_OBJECT
QByteArray IPOCnumber;
QString SendXML;
QByteArray SendXML_b;
QHostAddress sender;
quint16 senderPort;
QByteArray Buffer,dataReceived;
QMutex mtxSender,mtxReceiver;
public:
explicit MyUDP(QObject *parent = 0);
void readFile(QString file);
void CopyIPOC();
signals:
void started();
public slots:
void readPendingDatagram();
void SendUDP();
void writeReceivedData();
private:
QUdpSocket *socket;
};
#endif // MYUDP_H
To copy to clipboard, switch view to plain text mode
my udp.cpp
#include "myudp.h"
#include <thread>
{
// QHostAddress receiver;
// receiver = "192.168.1.11"; //Kuka computer 192.198.1.101
SendXML_b.clear();
connect(socket, SIGNAL(readyRead()),this,SLOT(readPendingDatagram()));
qDebug() <<"Socket binded";
}
void MyUDP::readPendingDatagram(){
mtxReceiver.lock();
Buffer.resize(socket->pendingDatagramSize());
socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);//collects information
//emit started();
mtxReceiver.unlock();
CopyIPOC();
SendUDP();
}
void MyUDP
::readFile(QString file){ SendXML = file;
}
void MyUDP::CopyIPOC(){
//Reading IPOC positions
int beginsAt = Buffer.indexOf("<IPOC>") + 6;
int stopsAt = Buffer.indexOf("</IPOC>");
//Copying IPOC
IPOCnumber = Buffer.mid(beginsAt,stopsAt-beginsAt);
//finding where to insert
SendXML_b.append(SendXML);
beginsAt = SendXML_b.indexOf("<IPOC>") + 6;
stopsAt = SendXML_b.indexOf("</IPOC>");
SendXML_b.remove(beginsAt,stopsAt-beginsAt);
SendXML_b.insert(beginsAt,IPOCnumber);
}
void MyUDP::SendUDP(){
// qDebug() << "Message senT: " << SendXML_b;
socket->writeDatagram(SendXML_b, sender, senderPort);
SendXML_b.clear();
}
void MyUDP::writeReceivedData(){
mtxReceiver.lock();
qDebug()<< Buffer;
mtxReceiver.unlock();
}
#include "myudp.h"
#include <thread>
MyUDP::MyUDP(QObject *parent) : QObject(parent)
{
// QHostAddress receiver;
// receiver = "192.168.1.11"; //Kuka computer 192.198.1.101
SendXML_b.clear();
socket = new QUdpSocket(this);
socket->bind(QHostAddress("192.168.1.11"),6050);
connect(socket, SIGNAL(readyRead()),this,SLOT(readPendingDatagram()));
qDebug() <<"Socket binded";
}
void MyUDP::readPendingDatagram(){
mtxReceiver.lock();
Buffer.resize(socket->pendingDatagramSize());
socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);//collects information
//emit started();
mtxReceiver.unlock();
CopyIPOC();
SendUDP();
}
void MyUDP::readFile(QString file){
SendXML = file;
}
void MyUDP::CopyIPOC(){
//Reading IPOC positions
int beginsAt = Buffer.indexOf("<IPOC>") + 6;
int stopsAt = Buffer.indexOf("</IPOC>");
//Copying IPOC
IPOCnumber = Buffer.mid(beginsAt,stopsAt-beginsAt);
//finding where to insert
SendXML_b.append(SendXML);
beginsAt = SendXML_b.indexOf("<IPOC>") + 6;
stopsAt = SendXML_b.indexOf("</IPOC>");
SendXML_b.remove(beginsAt,stopsAt-beginsAt);
SendXML_b.insert(beginsAt,IPOCnumber);
}
void MyUDP::SendUDP(){
// qDebug() << "Message senT: " << SendXML_b;
socket->writeDatagram(SendXML_b, sender, senderPort);
SendXML_b.clear();
}
void MyUDP::writeReceivedData(){
mtxReceiver.lock();
qDebug()<< Buffer;
mtxReceiver.unlock();
}
To copy to clipboard, switch view to plain text mode
and finally main .cpp
#include <QCoreApplication>
#include "myudp.h"
#include "thread"
int main(int argc, char *argv[])
{
// Open a file for reading
QFile file("C:/Users/admin/Desktop/clein/Example/RealtimeEthernet/Server_app/ExternalData.xml");
{
qDebug() << "Failed to open the file for reading.";
}
else
{
SendXML = in.readAll();
file.close();
}
qDebug() << "File in External XML:\n"<<SendXML;
// QByteArray ExternalFile = document.toByteArray(0);
MyUDP Server; //receives by UDP
Server.readFile(SendXML);
qDebug() <<"Program started: ";
return a.exec();
}
#include <QCoreApplication>
#include "myudp.h"
#include "thread"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString SendXML;
// Open a file for reading
QFile file("C:/Users/admin/Desktop/clein/Example/RealtimeEthernet/Server_app/ExternalData.xml");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "Failed to open the file for reading.";
}
else
{
QTextStream in(&file);
SendXML = in.readAll();
file.close();
}
qDebug() << "File in External XML:\n"<<SendXML;
// QByteArray ExternalFile = document.toByteArray(0);
MyUDP Server; //receives by UDP
Server.readFile(SendXML);
qDebug() <<"Program started: ";
return a.exec();
}
To copy to clipboard, switch view to plain text mode
I would really appreaciate it if someone can help me to solve my problem. Thank you!
Bookmarks