2023-09-08 15:24:52 +02:00
|
|
|
// udpclient.cpp
|
|
|
|
|
2023-09-08 14:42:37 +02:00
|
|
|
#include "udpclient.h"
|
2023-09-16 12:13:04 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
quint16 serverPort = 1901; // Port number for the UDP server
|
2023-09-08 14:42:37 +02:00
|
|
|
|
|
|
|
UdpClient::UdpClient(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2023-09-16 12:13:04 +02:00
|
|
|
|
2023-09-08 15:24:52 +02:00
|
|
|
// Create a QHostAddress for the server's IP address
|
2023-09-16 12:13:04 +02:00
|
|
|
QHostAddress serverAddress("127.0.0.1");
|
2023-09-08 15:24:52 +02:00
|
|
|
|
|
|
|
// Bind the UDP socket to a specific port for receiving data (replace with your desired port)
|
2023-09-16 12:13:04 +02:00
|
|
|
udpSocket.bind(QHostAddress::Any, serverPort); // Replace 12345 with your desired port
|
2023-09-08 14:42:37 +02:00
|
|
|
|
2023-09-08 15:24:52 +02:00
|
|
|
// Connect the UDP socket's readyRead signal to the receiveData slot
|
|
|
|
connect(&udpSocket, &QUdpSocket::readyRead, this, &UdpClient::receiveData);
|
|
|
|
|
|
|
|
// Set the server's address and port for sending data
|
2023-09-16 12:13:04 +02:00
|
|
|
udpSocket.connectToHost(serverAddress, serverPort); // Replace 12345 with the server's port
|
|
|
|
|
2023-09-08 14:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UdpClient::startClient()
|
|
|
|
{
|
2023-09-08 15:24:52 +02:00
|
|
|
// Start any client functionality here
|
|
|
|
// This method can be used to initialize the client if needed.
|
|
|
|
qDebug() << "UDP Client is listening for data...";
|
2023-09-16 12:13:04 +02:00
|
|
|
messageCount = 0;
|
2023-09-08 15:24:52 +02:00
|
|
|
}
|
2023-09-08 14:42:37 +02:00
|
|
|
|
2023-09-08 15:24:52 +02:00
|
|
|
void UdpClient::receiveData()
|
|
|
|
{
|
|
|
|
// Process pending datagrams
|
|
|
|
processPendingDatagrams();
|
2023-09-08 14:42:37 +02:00
|
|
|
}
|
|
|
|
|
2023-09-08 15:24:52 +02:00
|
|
|
|
|
|
|
|
2023-09-08 14:42:37 +02:00
|
|
|
void UdpClient::processPendingDatagrams()
|
|
|
|
{
|
|
|
|
while (udpSocket.hasPendingDatagrams()) {
|
|
|
|
QByteArray datagram;
|
|
|
|
datagram.resize(udpSocket.pendingDatagramSize());
|
|
|
|
QHostAddress sender;
|
|
|
|
quint16 senderPort;
|
2023-09-16 12:13:04 +02:00
|
|
|
QTime currentTime = QTime::currentTime();
|
|
|
|
|
2023-09-08 14:42:37 +02:00
|
|
|
|
2023-09-16 12:13:04 +02:00
|
|
|
if(udpSocket.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort)){
|
|
|
|
// std::cerr << "found data" << std::endl;
|
|
|
|
|
|
|
|
}
|
2023-09-08 14:42:37 +02:00
|
|
|
|
|
|
|
// Parse and display the received data
|
|
|
|
QString receivedData = QString::fromUtf8(datagram);
|
2023-09-16 12:13:04 +02:00
|
|
|
// std::cout << "Received Data: " << receivedData.toStdString() << std::endl;
|
2023-09-08 14:42:37 +02:00
|
|
|
QStringList dataList = receivedData.split(',');
|
2023-09-16 12:13:04 +02:00
|
|
|
|
|
|
|
if (dataList.size() == 9) {
|
|
|
|
|
2023-09-08 14:42:37 +02:00
|
|
|
double intensity = dataList[0].toDouble();
|
|
|
|
double position = dataList[1].toDouble();
|
|
|
|
double focus = dataList[2].toDouble();
|
2023-09-16 12:13:04 +02:00
|
|
|
double rSquare = dataList[3].toDouble();
|
|
|
|
int msecTime = dataList[4].toInt();
|
|
|
|
|
|
|
|
// qDebug() << "Received data - Intensity: " << intensity << " Position:" << position << " Focus:" << focus << " rsquare:" << rSquare << " msec:" << msecTime ;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
qDebug() << "Data of unexpected size" << dataList.size() ;
|
|
|
|
}
|
|
|
|
messageCount++;
|
|
|
|
if (messageCount==1) {
|
|
|
|
startTime = currentTime.msecsSinceStartOfDay();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if 1000 messages have been received
|
|
|
|
if (messageCount % 1000 == 0 && messageCount >1) {
|
|
|
|
QTime currentTime = QTime::currentTime();
|
|
|
|
elapsedTimeInmSeconds = currentTime.msecsSinceStartOfDay();
|
|
|
|
elapsedTimeInmSeconds-= startTime;
|
|
|
|
|
|
|
|
// Calculate the rate in messages per second
|
|
|
|
// double messageRate = static_cast<double>(messageCount) / elapsedTimeInmSeconds;
|
|
|
|
|
|
|
|
// qInfo() << "Messages received: " << messageCount;
|
|
|
|
qInfo() << "Message rate (msec/1k): " << elapsedTimeInmSeconds;;// << 1000/(elapsedTimeInmSeconds/1000) << "kHz" ;
|
|
|
|
|
2023-09-08 14:42:37 +02:00
|
|
|
|
2023-09-16 12:13:04 +02:00
|
|
|
// Reset the counters
|
|
|
|
messageCount = 0;
|
|
|
|
startTime = currentTime.msecsSinceStartOfDay();
|
2023-09-08 14:42:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|