Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to stop writeDatagram ?



  • Hi Guys,
    i have some Problems with QT Network.
    So the Code works fine... but it doesn't stop... it sends all the time
    is there maybe a function how to stop this broadcast ?
    This is my Code:

    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);
                   }
    

  • Lifetime Qt Champion

    hi
    who calls broadcastDatagram ?
    Are you sure your code is not just calling it over and over ?


  • Lifetime Qt Champion

    Hi,

    Might be a silly suggestion but: don't call that function anymore ?



  • 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 :/


  • Lifetime Qt Champion

    Can you show the rest of the code ?



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

  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

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



  • 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 :)


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

    @BadHombre said in How to stop writeDatagram ?:

    QUdpSocket *udpSocket;

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



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


  • Lifetime Qt Champion

    @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 :)



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


  • Lifetime Qt Champion

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


Log in to reply