如何使用UDP连接到我的pic32并从中获得答案

问题描述

我想通过Windows上的Qt连接到我的PIC32 UDP服务器。使用C中的测试程序,我可以连接并获得答案,但是使用Qt则不可能。我在做什么错了?

如您所见,我在一段时间内正在使用主动等待,并且我的广告位似乎没有被触发。也许您可以在这里看到我的错误?我讨厌主动等待。...

这是我在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;

    }
}

这是我在PIC32上的代码,如您所见,一旦收到消息,我就发送答案,这使我可以进行双向通信:

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

有什么主意吗?

解决方法

尝试使用waitForBytesWrittenwaitForReadyRead

// 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;

一种更好的选择是使用documentation of QUdpSocket

中所述的信号和时隙 ,

如果您打算将微处理器用作具有 UDP 的客户端,您需要目标机器的 MAC 地址,否则它将无法工作。这个问题我花了 4 个小时才弄明白。