UDP between Simulink and Qt



  • 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


  • Moderators

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



  • 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?


  • Moderators

    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?



  • 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


  • Moderators

    @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.



  • Is simulink outside connection , maybe you have router port blocked



  • 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.



  • @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 ;)


Log in to reply
 

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