Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. UDP reception
Qt 6.11 is out! See what's new in the release blog

UDP reception

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 4.0k Views
  • 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.
  • R reshu

    Hi.i am using QT widget application to receive udp packets from another PC. my souce is a labview VI. i am able to see the received data in debug window.but cannot see in in user interface. i tried text edit and list widget.but not working. this is my first QT pgm. plz help.here is my code. i first tried a local connection. then the data was dis[layed in list widget. i modified the same code for reception.

    udp1.png

    MainWindow::MainWind ow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    msocket = new QUdpSocket(this);
    

    msocket-> bind(QHostAddress("192.168.10.10"),55500);
    connect(msocket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    }

    void MainWindow:: readyRead()
    {
        QByteArray Buffer;
        Buffer.resize(msocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;
        msocket->readDatagram(Buffer.data(),Buffer.size(),& sender,& senderPort);
       qDebug() << " message from:" << sender.toString();
        qDebug() << " message port:" << senderPort;
        qDebug() << " message:"<<Buffer;
        mystr = QString::fromStdString((const char*)Buffer);
        ui->textEdit->setText(mystr);
       // ui->listWidget->addItem(QString(mystr));
    }
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by jsulm
    #3

    @reshu said in UDP reception:

    mystr = QString::fromStdString((const char*)Buffer);

    Also, I'm not sure what you're trying to achieve with this line?
    QByteArray is not const char*
    Why don't you simply use https://doc.qt.io/qt-5/qbytearray.html#toStdString ?
    And there is actually no need to conver to std string if you want to set text in QLineEdit as it expects a QString.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    R 1 Reply Last reply
    1
    • jsulmJ jsulm

      @reshu said in UDP reception:

      mystr = QString::fromStdString((const char*)Buffer);

      Also, I'm not sure what you're trying to achieve with this line?
      QByteArray is not const char*
      Why don't you simply use https://doc.qt.io/qt-5/qbytearray.html#toStdString ?
      And there is actually no need to conver to std string if you want to set text in QLineEdit as it expects a QString.

      R Offline
      R Offline
      reshu
      wrote on last edited by
      #4

      @jsulm
      I tried this too.
      ui->textEdit->append(QString(Buffer));
      ui->listWidget->addItem(QString(Buffer));
      in both cases i am getting display in local mode in both listwidget and textedit. when i give the udp data from another PC , data is not displayed whereas the data is there in debug window.

      jsulmJ 1 Reply Last reply
      0
      • R reshu

        @jsulm
        I tried this too.
        ui->textEdit->append(QString(Buffer));
        ui->listWidget->addItem(QString(Buffer));
        in both cases i am getting display in local mode in both listwidget and textedit. when i give the udp data from another PC , data is not displayed whereas the data is there in debug window.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #5

        @reshu said in UDP reception:

        ui->textEdit->append(QString(Buffer));

        ui->textEdit->append(QString::fromLatin1(Buffer));
        

        You did not answer this question: "Also what exact data do you get over UDP? Is it text or binary?"!
        What data do you send? Is it actually printable?
        Your qDebug output does not look like the data you're sending is printable...

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        R 1 Reply Last reply
        2
        • jsulmJ jsulm

          @reshu said in UDP reception:

          ui->textEdit->append(QString(Buffer));

          ui->textEdit->append(QString::fromLatin1(Buffer));
          

          You did not answer this question: "Also what exact data do you get over UDP? Is it text or binary?"!
          What data do you send? Is it actually printable?
          Your qDebug output does not look like the data you're sending is printable...

          R Offline
          R Offline
          reshu
          wrote on last edited by
          #6

          @jsulm
          udp_data.png
          this is my data which i am sending from another PC ,a labview VI. actually this data simulates the data from some sensors, counters etc updated in different fields. real time. Here i am using constant data in all fields.
          ui->listWidget->addItem(sender.toString()); this is working
          ui->txtedit->setText(sender.toString()); this is working

          QByteArray x;
          x="5";
          ui->listWidget->addItem(QByteArray(x)); this is also working

          ui->txtedit->append(QString::fromLatin1(Buffer)); is not working

          JonBJ 1 Reply Last reply
          0
          • R reshu

            @jsulm
            udp_data.png
            this is my data which i am sending from another PC ,a labview VI. actually this data simulates the data from some sensors, counters etc updated in different fields. real time. Here i am using constant data in all fields.
            ui->listWidget->addItem(sender.toString()); this is working
            ui->txtedit->setText(sender.toString()); this is working

            QByteArray x;
            x="5";
            ui->listWidget->addItem(QByteArray(x)); this is also working

            ui->txtedit->append(QString::fromLatin1(Buffer)); is not working

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #7

            @reshu
            As @jsulm asked you, your buffer data is binary bytes and not "actually printable", isn't it? So how would you expect it to be displayable in a QTextEdit? Everything is working, you need to do something about deciding how you want this binary data to be visibly displayed. For example, if you're expecting it to come out like the bottom pane you show, or like qDebug() chooses to display it, you're going to have write the code to do that, it doesn't happen by magic.

            R 1 Reply Last reply
            0
            • JonBJ JonB

              @reshu
              As @jsulm asked you, your buffer data is binary bytes and not "actually printable", isn't it? So how would you expect it to be displayable in a QTextEdit? Everything is working, you need to do something about deciding how you want this binary data to be visibly displayed. For example, if you're expecting it to come out like the bottom pane you show, or like qDebug() chooses to display it, you're going to have write the code to do that, it doesn't happen by magic.

              R Offline
              R Offline
              reshu
              wrote on last edited by
              #8

              @JonB
              Sorry.i am new to this. i thought i will be able to see the byte array in textedit as in labview. all the fields of the UDP data corresponds to some sensor data. i just wanted to see if all the data fields are updating correctly first. later i will use the data as i wish to do some other action. is there any other option by which i can see my data and verify it?? the data which i am getting in the debug window is also not exactly the same as the one i am not sending. some data is missing. sorry for wasting your time. let me try first.

              B 1 Reply Last reply
              0
              • R reshu

                @JonB
                Sorry.i am new to this. i thought i will be able to see the byte array in textedit as in labview. all the fields of the UDP data corresponds to some sensor data. i just wanted to see if all the data fields are updating correctly first. later i will use the data as i wish to do some other action. is there any other option by which i can see my data and verify it?? the data which i am getting in the debug window is also not exactly the same as the one i am not sending. some data is missing. sorry for wasting your time. let me try first.

                B Offline
                B Offline
                Bonnie
                wrote on last edited by
                #9

                @reshu You could use Buffer.toHex(' ')

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  reshu
                  wrote on last edited by
                  #10

                  @Bonnie
                  thanks.
                  .i tried it already and i am getting the data which i am sending displayed correctly in the debug window.
                  qDebug() << "total msg:"<< Buffer.toHex();
                  i tried like this . i mean without (' ') single quotes. will it make any change.

                  message from: "192.168.10.100"
                  message port: 55000
                  message: @ // this corresponds to a single byte (ascii value of 4th byte) //qDebug() << " message:"<< (Buffer.data()[3]);
                  total msg: "00015a400000000055aa000001000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

                  i want to convert each byte seperately to hex so that i can initiate some other action based on these byte values. can you give the solution for converting each byte of QBytearray to hex.

                  B 1 Reply Last reply
                  0
                  • R reshu

                    @Bonnie
                    thanks.
                    .i tried it already and i am getting the data which i am sending displayed correctly in the debug window.
                    qDebug() << "total msg:"<< Buffer.toHex();
                    i tried like this . i mean without (' ') single quotes. will it make any change.

                    message from: "192.168.10.100"
                    message port: 55000
                    message: @ // this corresponds to a single byte (ascii value of 4th byte) //qDebug() << " message:"<< (Buffer.data()[3]);
                    total msg: "00015a400000000055aa000001000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

                    i want to convert each byte seperately to hex so that i can initiate some other action based on these byte values. can you give the solution for converting each byte of QBytearray to hex.

                    B Offline
                    B Offline
                    Bonnie
                    wrote on last edited by Bonnie
                    #11

                    @reshu with a (' ') as parameter, it should be like "00 01 5a 40".
                    So if you want the hex text seperated byte by byte maybe you can try

                    Buffer.toHex(' ').split(' ');
                    

                    This will return a QList<QByteArray>, from which each one should be a hex of a byte.
                    And if you want a specific byte to hex, for example, the 4th (index=3), you can try

                    QByteArray(1, Buffer[3]).toHex();
                    

                    or

                    QByteArray().append(Buffer[3]).toHex();
                    
                    1 Reply Last reply
                    1
                    • R Offline
                      R Offline
                      reshu
                      wrote on last edited by
                      #12

                      @Bonnie
                      thank you so much. it worked perfectly. i tried all the three. split and append worked.
                      QByteArray(1, Buffer[3]).toHex(); i didn't get properly.
                      here is my output
                      message from: "192.168.10.100"
                      message port: 55000
                      message: @ // 3dr byte read directly from buffer
                      total msg: "00015a400000000055aa000001000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
                      total msg_dec: @
                      test: "00" /// output of QByteArray(1, Buffer[3]).toHex();
                      test1: "40" /// output of QByteArray().append(Buffer[3]).toHex();

                      test3: ("00", "01", "5a", "40", "00", "00", "00", "00", "55", "aa", "00", "00", "01", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "01", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00") //// output of Buffer.toHex(' ').split(' ');

                      thanks once again

                      B 1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        And what should be wrong here? 0x40 = '@' ...

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        R 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          And what should be wrong here? 0x40 = '@' ...

                          R Offline
                          R Offline
                          reshu
                          wrote on last edited by
                          #14

                          @Christian-Ehrlicher
                          I never said that it is wrong.
                          please refer to the previous msg
                          message: @ // this corresponds to a single byte (ascii value of 4th byte) //qDebug() << " message:"<< (Buffer.data()[3]);
                          i have to chk each byte of the data for some control action for which i need either decimal or hex value for easiness. its difficult to compare ascii values .that's it.

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • R reshu

                            @Bonnie
                            thank you so much. it worked perfectly. i tried all the three. split and append worked.
                            QByteArray(1, Buffer[3]).toHex(); i didn't get properly.
                            here is my output
                            message from: "192.168.10.100"
                            message port: 55000
                            message: @ // 3dr byte read directly from buffer
                            total msg: "00015a400000000055aa000001000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
                            total msg_dec: @
                            test: "00" /// output of QByteArray(1, Buffer[3]).toHex();
                            test1: "40" /// output of QByteArray().append(Buffer[3]).toHex();

                            test3: ("00", "01", "5a", "40", "00", "00", "00", "00", "55", "aa", "00", "00", "01", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "01", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00") //// output of Buffer.toHex(' ').split(' ');

                            thanks once again

                            B Offline
                            B Offline
                            Bonnie
                            wrote on last edited by
                            #15

                            @reshu The second one works on my machine, not sure why not working on yours.
                            But since you can use the third one, that doesn't matter.
                            If your problem is solved, please mark this topic as "solved".

                            R 1 Reply Last reply
                            0
                            • R reshu

                              @Christian-Ehrlicher
                              I never said that it is wrong.
                              please refer to the previous msg
                              message: @ // this corresponds to a single byte (ascii value of 4th byte) //qDebug() << " message:"<< (Buffer.data()[3]);
                              i have to chk each byte of the data for some control action for which i need either decimal or hex value for easiness. its difficult to compare ascii values .that's it.

                              Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by Christian Ehrlicher
                              #16

                              @reshu said in UDP reception:

                              its difficult to compare ascii values

                              Why? It's very basic C stuff ...

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              0
                              • B Bonnie

                                @reshu The second one works on my machine, not sure why not working on yours.
                                But since you can use the third one, that doesn't matter.
                                If your problem is solved, please mark this topic as "solved".

                                R Offline
                                R Offline
                                reshu
                                wrote on last edited by
                                #17

                                @Bonnie yeah its solved.thanks for the support.

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved