custom modbus function code
Unsolved
General and Desktop
-
wrote on 9 Feb 2021, 09:41 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 responseBut 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/1