QT UDP SEND DOUBLE



  • Hey guys i am new to QT , I am trying to send and receive datagram from simulink and QT. Successfully i was able to send variables from simulink to QT. Now am trying to send variables to simulink from QT. I am not sure how i am suppose to send a double variable from QT. It would be nice if someone can help me to send double type values .I have also attached the simulink side as a screenshot

    #include "myudp.h"
    double prev_a[3];
    
    MyUDP::MyUDP(QObject *parent) :
        QObject(parent)
        {
            socket = new QUdpSocket(this);
            socket->bind(QHostAddress::LocalHost, 25000);
            connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
        }
    /*    void MyUDP::HelloUDP() // instead of sending HelloUDP i want to send a double type variable.
        {
            QByteArray Data;
            Data.append("F");
    
            socket->writeDatagram(Data, QHostAddress::LocalHost, 26000);
        }
    */
    
    void MyUDP::readyRead()
    {
        double a[4];
    
        QByteArray buffer;
        buffer.resize(socket->pendingDatagramSize());
    
        QHostAddress sender;
        quint16 senderPort;
    
        socket->readDatagram(buffer.data(), buffer.size(),
                             &sender, &senderPort);
    
        memcpy(&a, buffer, 3*sizeof(double));
    
        if(a[0] != prev_a[0] || a[1] != prev_a[1] || a[2] != prev_a[2])
        {
            qDebug() << "\nMessage from: " << sender.toString();
            qDebug() << "Message port: " << senderPort;
            qDebug() << "X:" << a[0];
            qDebug() << "Y:" << a[1];
            qDebug() << "V:" << a[2];
        }
        else {
            qDebug() << "error";
    
        }
        prev_a[0] = a[0];
        prev_a[1] = a[1];
        prev_a[2] = a[2];
    
    }
    
    

  • Lifetime Qt Champion

    Hi,

    What about using QByteArray::number ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.