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

QUdpSocket Not sending datagrams properly



  • Hi all, so I have a program for interfacing with an embedded device, and it when my "Enable" function is run, it's supposed to send a UDP packet, then run 3 subroutines that each send a UDP packet. The first time the routine is run, the enable routine sends it's packet, it does call the three functions and according to my debugger, it's sending the packets, but they do not show up. Wireshark has verified this. If it runs again, the packets are all sent properly, but for some reason, the first time it is run, it doesn't work correctly. Code below:

    @
    void main_funcs::sys_enable()
    {
    if (dialog.size() > 4096) string_strip();
    QQuickItem* object = myDialog->rootObject();
    QObject textarea = object->findChild<QObject>("textareaz");
    QByteArray datagram;
    datagram.resize(4);
    datagram.clear();
    dialog.prepend(separator);
    if (!IP_is_set) .
    {
    dialog.prepend("\r IP Address Not Set. Please Set IP Address to continue. \r");
    textarea->setProperty("text", dialog);
    clr_chkbox();
    return;
    }
    if (!enabled && !inhibited)
    {
    datagram[0] = 0xFF;
    datagram[1] = 0xF0;
    dialog.prepend("Enable Code: 0xFFF0 Loaded.\r");
    textarea->setProperty("text", dialog);
    if (left_inv)
    {
    datagram[2] = 0xFF;
    dialog.prepend("Left Inverter Code: 0xFF00 Loaded. \r");
    textarea->setProperty("text", dialog);
    }
    else
    {
    datagram[2] = 0x00;
    }
    if (right_inv)
    {
    datagram[3] = 0xFF;
    dialog.prepend("Right Inverter Code: 0x00FF Loaded. \r");
    textarea->setProperty("text", dialog);
    }
    else
    {
    datagram[3] = 0x00;
    }
    if (!left_inv && !right_inv)
    {
    datagram[2] = 0xFF;
    datagram[3] = 0xFF;
    udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress(QString(newIP)), device_Port);
    qDebug() << datagram.toHex().toUpper();
    enabled = true;
    qDebug() << "(System Enable Both) System Enabled Status:" << enabled;
    sys_volts_changed();
    sys_current_changed();
    sys_power_changed();
    dialog.prepend("Full System Enable. \r");
    textarea->setProperty("text", dialog);
    datagram.clear();
    clr_chkbox();
    return;
    }
    udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress(QString(newIP)), device_Port);
    qDebug() << datagram.toHex().toUpper();
    dialog.prepend("Data Sent to Device. \r");
    textarea->setProperty("text", dialog);
    enabled = true;
    qDebug() << "(System Enable) System Enabled Status:" << enabled;
    sys_volts_changed();
    sys_current_changed();
    sys_power_changed();
    clr_chkbox();
    datagram.clear();
    return;
    }
    else if (inhibited)
    {
    dialog.prepend("System Inhibited - Cannot Enable.\r");
    textarea->setProperty("text", dialog);
    return;
    }
    }@

    Can anyone shed some light on what would cause something like this to happen? As I said before, all my qDebug() statements are outputting and correctly so, but this one's got me baffled. Thanks all!



  • Couldn't fit the sub-program on the original post, too large. Each of the three functions called is basically a copy of the others:
    @void main_funcs::sys_volts_changed()
    {
    qDebug() << "(Voltage Adjust) System Enabled Status:" << enabled;
    QQuickItem* object = myDialog->rootObject();
    QObject textarea = object->findChild<QObject>("textareaz");
    QObject v_spin = object->findChild<QObject>("v_pct");
    dialog.prepend(separator);
    if (!IP_is_set)
    {
    dialog.prepend("\r IP Address Not Set. Please Set IP Address to continue. \r");
    textarea->setProperty("text", dialog);
    clr_chkbox();
    return;
    }
    if (!enabled)
    {
    dialog.prepend("System Not Enabled. Values Will Not Be Sent to Device. \r");
    textarea->setProperty("text", dialog);
    return;
    }
    double vpct = v_spin->property("value").toDouble();
    qDebug() << "(Double) Data converted: " << vpct;
    quint8 vpct8 = static_cast<int>(vpct);
    qDebug() << "(8-bit) Data converted: " << vpct8;
    QByteArray datagram;
    datagram.resize(6);
    datagram.clear();
    datagram[0] = 0xFD;
    datagram[1] = 0xAC;
    datagram[2] = 0xDA;
    datagram[3] = 0xC1;
    datagram[4] = 0x00;
    datagram[5] = vpct8;
    udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress(QString(newIP)), device_Port);
    qDebug() << "Data Sent: " << datagram.toHex().toUpper();
    dialog.prepend("System Voltage Change Sent to System. \r");
    textarea->setProperty("text", dialog);
    datagram.clear();
    clr_chkbox();
    return;
    }@



  • I've also tried implementing a 50ms (Sleep(50);) delay but that didn't help.


Log in to reply