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. How to stop writeDatagram ?
Qt 6.11 is out! See what's new in the release blog

How to stop writeDatagram ?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 6.3k Views 2 Watching
  • 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.
  • B Offline
    B Offline
    BadHombre
    wrote on last edited by BadHombre
    #4

    normally i just call it ones : this is the original Code... i am clicking a button on a controller (A)

    if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A)
                   {
                       //qDebug() << "Now broadcasting datagram";
                       QByteArray datagram = QByteArray::number(0) + QByteArray::number(1) + QByteArray::number(0);
                       udpSocket->writeDatagram(datagram.data(), datagram.size(),
                                                QHostAddress::Broadcast, 45454);
               
                       qDebug() << "hostadress"<< QHostAddress::Broadcast;
                       Player1->Vibrate(65535, 0);
                   }
    

    if i do this it seems that it enter into a loop and doesn't stop to send the datagram :/

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #5

      Can you show the rest of the code ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BadHombre
        wrote on last edited by BadHombre
        #6

        so i have 4 cpp files... So the problem is that if i am clicking A the program stops working ...
        the network code part i took from this QT example :
        http://doc.qt.io/qt-5/qtnetwork-broadcastsender-example.html
        So i run the example program and in this program it doesn't stop to send..
        so i thought that this is what cause the problem in my program... because first the mainwindow gui stops working and then the whole program stops.
        Isn't there a function like stop broadcast or sth else :D

        main :

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            MainWindow w;
            w.show();
            mythread mthread1;
            mthread1.start();
        
            return a.exec();
        }
        
        

        and then the thread.cpp

        #include "mythread.h"
        #include <QtCore>
        #include <QDebug>
        #include "mainwindow.h"
        #include <CXBOXController.h>
        #include <iostream>
        #include "ui_mainwindow.h"
        #include <QtNetwork>
        
        
        
        
        mythread::mythread()
        {
        
        }
        
        void mythread::run()
        {
        CXBOXController* Player1;
            qDebug() << "running";
        
            Player1 = new CXBOXController(1);
        
               std::cout << "Instructions:\n";
               std::cout << "[A] Vibrate Left Only\n";
               std::cout << "[B] Vibrate Right Only\n";
               std::cout << "[X] Vibrate Both\n";
               std::cout << "[Y] Vibrate Neither\n";
               std::cout << "[BACK] Exit\n";
        
               while(true)
               {
                   if(Player1->IsConnected())
                   {
                       if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A)
                       {
                           //qDebug() << "Now broadcasting datagram";
                           QByteArray datagram = QByteArray::number(0) + QByteArray::number(1) + QByteArray::number(0);
                           udpSocket->writeDatagram(datagram.data(), datagram.size(),
                                                    QHostAddress::Broadcast, 45454);
                       
                           qDebug() << "hostadress"<< QHostAddress::Broadcast;
                           Player1->Vibrate(65535, 0);
                       }
                       if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER )
                       {
                          qDebug() << Player1->GetState().Gamepad.sThumbLX;
        
                       }
        
        
                       if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B)
                       {
                           Player1->Vibrate(0, 65535);
                       }
        
                       if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_X)
                       {
                           Player1->Vibrate(65535, 65535);
                       }
        
                       if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_Y)
                       {
                           Player1->Vibrate();
                       }
        
                       if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_BACK)
                       {
                           break;
                       }
                   }
                   else
                   {
                       std::cout << "\n\tERROR! PLAYER 1 - XBOX 360 Controller Not Found!\n";
                       std::cout << "Press Any Key To Exit.";
                       std::cin.get();
                       break;
                   }
               }
        
               delete(Player1);
        
        
        }
        
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #7

          You should debug your infinite loop. As long as you have Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A returning true then you'll have datagram sent.

          On a side note, you might be interested by the QtGamePad module.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @BadHombre said in How to stop writeDatagram ?:

            CXBOXController

            Is that from here ?
            https://www.codeproject.com/Articles/26949/Xbox-Controller-Input-in-C-with-XInput

            That is close to 10 years old :)
            Are you sure
            Player1->GetState().Gamepad.wButtons returns the actual state and not just
            the value for some other field ?
            I would put in a
            std::cout << "GetState: =" << (Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A);
            and check it actually still works.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BadHombre
              wrote on last edited by BadHombre
              #9

              i debuged it now and the gamepad control works so it jumps into the if clause of A and then stops working at

               udpSocket->writeDatagram(datagram.data(), datagram.size(),QHostAddress::Broadcast, 45454);
              

              after clicking 3 time F10... but its strange because in the example it works fine

              I am not using the QT Gamepad module because it seems that there is a bug ... it didn't recognize my gamepad :/
              @mrjj yes that's the link :)

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #10

                hi
                it stays inside udpSocket->writeDatagram or in what way stops working ?
                The socket is new´ed and ready ?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BadHombre
                  wrote on last edited by
                  #11

                  yes the socket is new ..
                  this is the header file :

                  #ifndef SENDER_H
                  #define SENDER_H
                  
                  #include <QWidget>
                  
                  class QDialogButtonBox;
                  class QLabel;
                  class QPushButton;
                  class QTimer;
                  class QUdpSocket;
                  
                  class Sender : public QWidget
                  {
                      Q_OBJECT
                  
                  public:
                      Sender(QWidget *parent = 0);
                  
                  private slots:
                      void startBroadcasting();
                      void broadcastDatagram();
                  
                  private:
                      QLabel *statusLabel;
                      QPushButton *startButton;
                      QPushButton *quitButton;
                      QDialogButtonBox *buttonBox;
                      QUdpSocket *udpSocket;
                      QTimer *timer;
                      int messageNo;
                  };
                  
                  #endif
                  
                  

                  a window pop up wir .exe doesn't work anymore

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #12

                    @BadHombre said in How to stop writeDatagram ?:

                    QUdpSocket *udpSocket;

                    and you have/do
                    udpSocket = new QUdpSocket ;
                    somewhere ?

                    1 Reply Last reply
                    2
                    • B Offline
                      B Offline
                      BadHombre
                      wrote on last edited by
                      #13

                      oh i forgot it :S it works now ...
                      sry ... stupid error

                      mrjjM 1 Reply Last reply
                      0
                      • B BadHombre

                        oh i forgot it :S it works now ...
                        sry ... stupid error

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @BadHombre
                        Ok, so it was a dangling pointer :)
                        Well its easy error to make.
                        Even pretty experienced i sometimes copy from .h file
                        like
                        QUdpSocket *udpSocket;
                        and do
                        QUdpSocket *udpSocket = new QUdpSocket ;
                        Which makes a new variable and do not new the one in .h

                        so stupid error will always be made :)

                        1 Reply Last reply
                        3
                        • B Offline
                          B Offline
                          BadHombre
                          wrote on last edited by BadHombre
                          #15

                          Thank you :) One last question ... if i run it with the debugger it works fine ... so it enter only 1 time into the if clause and send it ... but if i run the program and click on a on the gamepad it enter sth like 30 times ... so it sends 30 commands... so it seems that its to fast when i start the program normal ... it's like i am pressing the button to long :S How could i fix that ? mybe with a time and an if ? or is there a better way ?

                          mrjjM 1 Reply Last reply
                          0
                          • B BadHombre

                            Thank you :) One last question ... if i run it with the debugger it works fine ... so it enter only 1 time into the if clause and send it ... but if i run the program and click on a on the gamepad it enter sth like 30 times ... so it sends 30 commands... so it seems that its to fast when i start the program normal ... it's like i am pressing the button to long :S How could i fix that ? mybe with a time and an if ? or is there a better way ?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            @BadHombre
                            It sounds like thread is too fast so that the XInput is not yet updated.
                            so this
                            if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A)
                            is true for 30 times before it sort of discovered that you are not holding the button?

                            you could use a timer and let it call a function like the run()
                            (not while using run() for a thread of course)

                            Then you can set the speed. (of the timer and hence how fast it calls)

                            There is also
                            QThread::msleep(1000);
                            to slow it down but timer is better design.
                            However for testing you can try that.

                            1 Reply Last reply
                            1

                            • Login

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