Search code examples
qtsocketsudppic32

How to connect using UDP to my pic32 and get an answer from it


I want to connet with Qt on windows to my PIC32 UDP server. With a test program in C, I can connect and get the answer, but with Qt it is impossible. What am I doing wrong ?

As you can see I use an ACTIVE WAITING with the while, and my slot doesn't seems to be triggered. Maybe you can see my mistake here ? I hate active waiting....

Here is my code on Qt :

void MuTweezer::run()
{    

    QHostAddress sender;
    quint16 senderPort;
    QByteArray datagram;
    qint64 pendingBytes;
    int cpt = 0;
    
    // Message to send
    QByteArray message("Hello that's charly");

    // m_imuRcvSocket.bind(10000); why is it for in a client side !?

    // SEEMS to NOT BE TRIGGERED
    connect(&m_imuRcvSocket, SIGNAL(readyRead()), this, SLOT(recu()));

    while(m_isRunning && cpt++ < 10)
    {
        // send the initial message
        qint64 bytesSent= m_imuRcvSocket.writeDatagram(message, QHostAddress("192.168.0.15"), 10000);

        cout << "Bytes sent : " << bytesSent << endl;

        // We wait the answer from the server
        while(!m_imuRcvSocket.hasPendingDatagrams());

        // If there is no datagram available, this function returns -1
        if((pendingBytes = m_imuRcvSocket.pendingDatagramSize()) == -1)
            continue;

        datagram.resize(pendingBytes);
        m_imuRcvSocket.readDatagram(datagram.data(), datagram.size(),
                                        &sender, &senderPort);

        cout << "================="
             << "\nMessage from <" << sender.toString().toStdString().substr(7) << "> on port " << senderPort
             << "\nString : " << datagram.data()
             << "\nSize: " << pendingBytes << " Bytes (characters)\n"
             << "=================" <<
        endl;

    }
}

Here is my code on the PIC32, as you can see, once I receive a message, I send the answer, it allows me to make a bidirectionnal communication :

if(!UDPIsOpened(mySocket)){
    DBPRINTF("Socket CLOSED");
    continue; // go back to loop beginning
}

DBPRINTF("Socket OPEN");

if(!(lengthToGet = UDPIsGetReady(mySocket)))
    continue;

// get the string
// UDPGetArray : returns the number of bytes successfully read from the UDP buffer.
if((lengthWeGot = UDPGetArray(message, lengthToGet)))
    UDPDiscard(); // Discards any remaining RX data from a UDP socket.

/* Modifie it and send it back */
if(UDPIsPutReady(mySocket)){
    message[20]= 'Z';
    message[21]= 'i';
    message[22]= 'b';
    message[23]= 'o';

    UDPPutArray(message, lengthWeGot);
    UDPFlush();
}

Any idea ?


Solution

  • Try to use waitForBytesWritten and waitForReadyRead:

    // to receive datagrams, the socket needs to be bound to an address and port
    m_imuRcvSocket.bind();
    
    // send the initial message
    QByteArray message("Hi it's Charly");
    
    qint64 bytesSent= m_imuRcvSocket.writeDatagram(message,
                                                   QHostAddress("200.0.0.3"),
                                                   10000);
    
    bool datagramWritten = m_imuRcvSocket.waitForBytesWritten();
    // add code to check datagramWritten
    
    datagram.resize(50); // random size for testing
    
    bool datagramReady = m_imuRcvSocket.waitForReadyRead() && m_imuRcvSocket.hasPendingDatagrams();
    // add code to check datagramReady
    
    m_imuRcvSocket.readDatagram(datagram.data(),
                                datagram.size(),
                                &sender,
                                &senderPort);
    
    cout << "================="
         << "\nMessage from <" << sender << "> on port " << senderPort
         << "\nString : " << datagram
         << "\nSize: " << pendingBytes << " Bytes (characters)\n"
         << "=================" <<
    endl;
    

    A better alternative would be to use signals and slots as described in the documentation of QUdpSocket