Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. custom modbus function code

custom modbus function code

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 234 Views
  • 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 Offline
    L Offline
    Lapsi
    wrote on 9 Feb 2021, 09:41 last edited by
    #1

    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
    0

    1/1

    9 Feb 2021, 09:41

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved