Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved custom modbus function code

    General and Desktop
    1
    1
    101
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      Lapsi last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • First post
        Last post