Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved UDP between Simulink and Qt

    General and Desktop
    4
    9
    2805
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Surizer
      Surizer last edited by A Former User

      Hey guys!! I am trying to send or/ stream variables from simulink to Qt . I tried setting up and UDP connection between simulink and Qt following few forums but nothing worked. Could anyone help to set up an UDP conncection between Simulink and Qt or if there is anyother way to stream the varaibles please enlighten me.

      Thank you

      ? 1 Reply Last reply Reply Quote 0
      • ?
        A Former User @Surizer last edited by

        @Surizer Hi! There is nothing such as a "UDP connection". What did you try so far (please show us some code)?

        1 Reply Last reply Reply Quote 0
        • Surizer
          Surizer last edited by

          I tried the following in QT and used "UDP SEND" block in simulink .I dono if i edited this code in QT in the best way .

          Source: http://www.bogotobogo.com/Qt/Qt5_QUdpSocket.php .

          I want to modify this is code in such a way it has to receive data from simulink.

          Modified Code:

          • #include "myudp.h"
            MyUDP::MyUDP(QObject *parent) :
            QObject(parent)
            {
            // create a QUDP socket
            socket = 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;
            Data.append("Hello from UDP");
            // 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;
            quint16 senderPort;
            // 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;
            }

          In the worst case if this code is of no use to send variables from simulink to QT for a real time application ,Could you let me know if there is anyother way?

          1 Reply Last reply Reply Quote 0
          • ?
            A Former User last edited by A Former User

            The code looks good to me. What exactly doesn't work? Does bind() work? Maybe the bind() is blocked by your OS's mandatory access control system (SELinux, AppArmor, Tomoyo, ...) Did you turn off your firewall?

            1 Reply Last reply Reply Quote 0
            • Surizer
              Surizer last edited by

              I have shared a screenshot . The problem is i dono if i set up the configurations properly to receive the data from the simulink.
              Simulink,QT,serial monitor

              ? 1 Reply Last reply Reply Quote 0
              • ?
                A Former User @Surizer last edited by

                @Surizer That looks ok. There are always two ports: In your own application you set the receiving port (25000). The sender (Simulink) uses a different port number for sending. The number of the receiving port is fixed because the sender must know where to send the packets to. But the sending port number doesn't matter to the receiver, so it's usually selected by random according to which port is currently unused on the system.

                1 Reply Last reply Reply Quote 0
                • Q
                  Q139 last edited by

                  Is simulink outside connection , maybe you have router port blocked

                  1 Reply Last reply Reply Quote 0
                  • Surizer
                    Surizer last edited by

                    What you said was right and then the problem was in sending the proper type of the variables. We sent uint8 70in simulink and received "F" in our application.

                    the_ 1 Reply Last reply Reply Quote 0
                    • the_
                      the_ @Surizer last edited by

                      @Surizer

                      We sent uint8 70in simulink and received "F" in our application.

                      Thats a correct value if you translate uint8 to char, as the character 'F' has a decimal value of 70 according to ASCII ;)

                      -- No support in PM --

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post