hello community,
i am new in this forum and in qt I need your help to finish my program.
i am writing a c++ code with the IDE Qt for my project.
i have two separately classes, that communicate on updsocket. One class(SendObjects) send data on port 50885 and the second class(receiveObjects) receive this data on port 50885. The data to send are of type struct. In both classes i have declared a structure type, called mDataChannels with 6 elements: Channel_Nr, AC_Volt, DC_Volt,etc...
Now on my socket with port 50885 from class "SendObjects", i want to send simultaneously 40 objects of typ structure(mDataChannels) and then receive this all objects(or all elements of this) on the other class names "receiveObjects".
i tried to write a code with and without QVector but it doesn't work fine. I need help to improve my code. i don't have error but i cannot read something. I don't too know if my class "SendObjects" or my class "receiveObjects" really work fine. I have added a new QDataStream Operator. may be it is wrong.
SendObject.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QTimer>
#include <QDateTime>
#include <vector>
#include <QString>
#include <QDebug>
#include <QVector>
namespace Ui {
class MainWindow;
}
struct mDataChannels
{
float mAC_Volt;
float mDC_Volt;
float mAC_Current;
float mDC_Current;
float mmPotential;
};
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
friend QDataStream
& operator <<
(QDataStream &stream,
const QVector<mDataChannels>
&_DataTest
);
private slots:
void sendTestData();
private:
Ui::MainWindow *ui;
QVector < mDataChannels > _DataTest, temp;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QTimer>
#include <QDateTime>
#include <vector>
#include <QString>
#include <QDebug>
#include <QVector>
namespace Ui {
class MainWindow;
}
struct mDataChannels
{
QString mmyChannel_Nr;
float mAC_Volt;
float mDC_Volt;
float mAC_Current;
float mDC_Current;
float mmPotential;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
friend QDataStream& operator << (QDataStream &stream, const QVector<mDataChannels> &_DataTest);
private slots:
void sendTestData();
private:
Ui::MainWindow *ui;
QUdpSocket *testUdpSocket;
QTimer *testTimer;
QVector < mDataChannels > _DataTest, temp;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
,
SendObject.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
_DataTest.resize(40);
temp.resize(40);
for(int i = 0; i < 40; i++)
{
_DataTest
[i
].
mmyChannel_Nr = tr
("Kanal ") +QString::number(i
+ 1);
_DataTest[i].mAC_Volt = 150.320 + (float)i;
_DataTest[i].mDC_Volt = 80.67 + (float)i;
_DataTest[i].mAC_Current = 5.0 + (float)i/10;
_DataTest[i].mDC_Current = 3.0 + (float)i/10;
_DataTest[i].mmPotential = 6.0 + (float)i/2;
}
connect(testTimer, SIGNAL(timeout()), this, SLOT(sendTestData()));
testTimer->start(2 * 1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
QDataStream
& operator <<
(QDataStream &stream, QVector<mDataChannels>
&_DataTest
){
_DataTest.resize(40);
stream << dataTime;
for(int i = 0; i < 40; i++)
stream <<
(QString)_DataTest
[i
].
mmyChannel_Nr << static_cast<float>
(_DataTest
[i
].
mAC_Volt) << static_cast<float>(_DataTest[i].mDC_Volt) << static_cast<float>(_DataTest[i].mAC_Current)
<< static_cast<float>(_DataTest[i].mDC_Current) << static_cast<float>(_DataTest[i].mmPotential);
return stream;
}
void MainWindow::sendTestData()
{
for(int i = 0; i < 40; i++)
stream << _DataTest[i].mmyChannel_Nr << _DataTest[i].mAC_Volt
<< _DataTest[i].mDC_Volt << _DataTest[i].mAC_Current
<< _DataTest[i].mDC_Current << _DataTest[i].mmPotential;
testUdpSocket
->writeDatagram
(myTestSendData,
QHostAddress::LocalHost,
50885);
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
testUdpSocket = new QUdpSocket(this);
_DataTest.resize(40);
temp.resize(40);
for(int i = 0; i < 40; i++)
{
_DataTest[i].mmyChannel_Nr = tr("Kanal ") +QString::number(i + 1);
_DataTest[i].mAC_Volt = 150.320 + (float)i;
_DataTest[i].mDC_Volt = 80.67 + (float)i;
_DataTest[i].mAC_Current = 5.0 + (float)i/10;
_DataTest[i].mDC_Current = 3.0 + (float)i/10;
_DataTest[i].mmPotential = 6.0 + (float)i/2;
}
testTimer = new QTimer(this);
connect(testTimer, SIGNAL(timeout()), this, SLOT(sendTestData()));
testTimer->start(2 * 1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
QDataStream& operator << (QDataStream &stream, QVector<mDataChannels> &_DataTest)
{
_DataTest.resize(40);
QDateTime dataTime;
stream << dataTime;
for(int i = 0; i < 40; i++)
stream << (QString)_DataTest[i].mmyChannel_Nr << static_cast<float>(_DataTest[i].mAC_Volt)
<< static_cast<float>(_DataTest[i].mDC_Volt) << static_cast<float>(_DataTest[i].mAC_Current)
<< static_cast<float>(_DataTest[i].mDC_Current) << static_cast<float>(_DataTest[i].mmPotential);
return stream;
}
void MainWindow::sendTestData()
{
QByteArray myTestSendData;
QDataStream stream(&myTestSendData, QIODevice::WriteOnly);
stream.setVersion(QDataStream::Qt_5_9);
stream << QDateTime::currentDateTime();
for(int i = 0; i < 40; i++)
stream << _DataTest[i].mmyChannel_Nr << _DataTest[i].mAC_Volt
<< _DataTest[i].mDC_Volt << _DataTest[i].mAC_Current
<< _DataTest[i].mDC_Current << _DataTest[i].mmPotential;
testUdpSocket->writeDatagram(myTestSendData, QHostAddress::LocalHost, 50885);
}
To copy to clipboard, switch view to plain text mode
receiveObjects.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QTimer>
#include <QDebug>
#include <QDateTime>
namespace Ui {
class MainWindow;
}
struct DataChannels
{
float AC_Volt;
float DC_Volt;
float AC_Current;
float DC_Current;
float mPotential;
};
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
friend QDataStream
& operator >>
(QDataStream &stream,
QVector <DataChannels>
&receivePacket
);
private slots:
void readAllValue();
private:
Ui::MainWindow *ui;
QVector < DataChannels > receivePacket;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QTimer>
#include <QDebug>
#include <QDateTime>
namespace Ui {
class MainWindow;
}
struct DataChannels
{
QString myChannel_Nr;
float AC_Volt;
float DC_Volt;
float AC_Current;
float DC_Current;
float mPotential;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
friend QDataStream& operator >> (QDataStream &stream, QVector <DataChannels> &receivePacket);
private slots:
void readAllValue();
private:
Ui::MainWindow *ui;
QUdpSocket *readSocket;
QVector < DataChannels > receivePacket;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
,
receiveObjects.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
receivePacket.resize(40);
connect(readSocket, SIGNAL(readyRead()), this, SLOT(readAllValue()));
}
MainWindow::~MainWindow()
{
delete ui;
}
QDataStream
& operator >>
(QDataStream &stream, QVector<DataChannels>
&receivePacket
){
quint32 tempInt;
receivePacket.resize(40);
stream >> mDateTime;
for(int i = 0; i < 40; i++)
{
stream >> tempString; receivePacket[i].myChannel_Nr = tempString;
stream >> tempInt; receivePacket[i].AC_Volt = tempInt;
stream >> tempInt; receivePacket[i].DC_Volt = tempInt;
stream >> tempInt; receivePacket[i].AC_Current = tempInt;
stream >> tempInt; receivePacket[i].DC_Current = tempInt;
stream >> tempInt; receivePacket[i].mPotential = tempInt;
}
return stream;
}
void MainWindow::readAllValue()
{
do
{
mReceiveDatagram.resize(int(readSocket->pendingDatagramSize()));
readSocket->readDatagram(mReceiveDatagram.data(), mReceiveDatagram.size());
} while(readSocket->hasPendingDatagrams());
stream >> dateTime;
for(int i = 0; i< 40; i++)
{
stream >> receivePacket[i].myChannel_Nr >> receivePacket[i].AC_Volt >> receivePacket[i].DC_Volt
>> receivePacket[i].AC_Current >> receivePacket[i].DC_Current >> receivePacket[i].mPotential;
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
readSocket = new QUdpSocket(this);
readSocket->bind(QHostAddress::LocalHost, 50885);
receivePacket.resize(40);
connect(readSocket, SIGNAL(readyRead()), this, SLOT(readAllValue()));
}
MainWindow::~MainWindow()
{
delete ui;
}
QDataStream& operator >>(QDataStream &stream, QVector<DataChannels> &receivePacket)
{
QDateTime mDateTime;
quint32 tempInt;
QString tempString;
receivePacket.resize(40);
stream >> mDateTime;
for(int i = 0; i < 40; i++)
{
stream >> tempString; receivePacket[i].myChannel_Nr = tempString;
stream >> tempInt; receivePacket[i].AC_Volt = tempInt;
stream >> tempInt; receivePacket[i].DC_Volt = tempInt;
stream >> tempInt; receivePacket[i].AC_Current = tempInt;
stream >> tempInt; receivePacket[i].DC_Current = tempInt;
stream >> tempInt; receivePacket[i].mPotential = tempInt;
}
return stream;
}
void MainWindow::readAllValue()
{
QByteArray mReceiveDatagram;
do
{
mReceiveDatagram.resize(int(readSocket->pendingDatagramSize()));
readSocket->readDatagram(mReceiveDatagram.data(), mReceiveDatagram.size());
} while(readSocket->hasPendingDatagrams());
QDateTime dateTime;
QDataStream stream(&mReceiveDatagram, QIODevice::ReadOnly);
stream.setVersion(QDataStream::Qt_5_9);
stream >> dateTime;
for(int i = 0; i< 40; i++)
{
stream >> receivePacket[i].myChannel_Nr >> receivePacket[i].AC_Volt >> receivePacket[i].DC_Volt
>> receivePacket[i].AC_Current >> receivePacket[i].DC_Current >> receivePacket[i].mPotential;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks