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

custom modbus function code



  • I need to add a custom Modbus function code in Qt. It doesn't have any parameters and as a result I expect to get a constant data-length packet.

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QObject *connector = new QObject();

    QModbusTcpClient *client = new QModbusTcpClient();
    client->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "127.0.0.1");
    client->setConnectionParameter(QModbusDevice::NetworkPortParameter, 8899);
    client->connectDevice();
    connector->connect(client, &QModbusClient::stateChanged, connector, [connector, client]()
    {
        if(client->state() == QModbusClient::ConnectedState)
        {
            auto cmd = QModbusPdu::FunctionCode(0x66);
            QModbusRequest request(cmd);
            QModbusReply* reply = client->sendRawRequest(request, 1);
    
            connector->connect(reply, &QModbusReply::finished, connector, [reply]()
            {
                qInfo() << "isFinished? " << reply->isFinished(); // true
                qInfo() << "Raw result fun code: " << reply->rawResult().functionCode(); // 0
                qInfo() << "Error? " << reply->errorString(); //""
                qInfo() << "Reply data size: " << reply->rawResult().dataSize(); // 0
                qInfo() << "Reply size: " << reply->rawResult().data().size(); // 0
                QByteArray data = reply->rawResult().data(); // empty
            });
        }
    });
    
    return a.exec();
    

    }

    The other side receives the packet correctly and sends the answer back. The sent answer looks like this:

    0x0000, 0x0000, 0x0007, // Modbus TCP header
    0x01, 0x66, // general Modbus header
    0x04, // the response size is 4 bytes
    0x0000, 0x0042 // 4 bytes of response

    But nothing is received in Qt (the reply finishes but there is nothing in it)

    I've tried this solution - https://bugreports.qt.io/browse/QTBUG-62192 but it doesn't work.


Log in to reply