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

Modbus RTU client: send failed error



  • Here the output of my program:

    State changed -> QModbusDevice::State(ConnectingState)
    State changed -> QModbusDevice::State(ConnectedState)
    
    (RTU client) Sent Serial PDU: 0x0303fe0001 ((null):0, (null))
    >> "Tx: 0x010303fe0001e5be"
     (RTU client) Sent Serial ADU: 0x010303fe0001e5be ((null):0, (null))
     (RTU client) Send failed: 0x0303fe0001 ((null):0, (null))
    Error "Request timeout." 
    << "Rx: 0x0103020000b844"
    (RTU client) Response buffer: "0103020000b844" ((null):0, (null))
    (RTU client) Received ADU: "0103020000b844" ((null):0, (null))
    (RTU client) Cannot match response with open request, ignoring ((null):0, (null))
    

    Here my code:

        QModbusClient *_modbus;
    
        bool ModbusMaster::open()
        {
            _modbus = new QModbusRtuSerialMaster(this);
    
            connect(_modbus, &QModbusClient::stateChanged, this, &ModbusMaster::state_changed);
            connect(_modbus, &QModbusClient::errorOccurred, this, &ModbusMaster::error_occurred);
            _modbus->setConnectionParameter(QModbusDevice::SerialPortNameParameter, _port);
    
            _retry = RTU_RETRY; // 0
            _timeout = RTU_TIMEOUT;  // 1000
    
            _modbus->setTimeout(_timeout);
            _modbus->setNumberOfRetries(_retry);
    
            return _modbus->connectDevice();
    }
    
        bool ModbusMaster::read(QModbusDataUnit::RegisterType type, int startAddress, quint16 count)
        {
            if (!_modbus) return false;
            if (_modbus->state() != QModbusDevice::ConnectedState) return false;
    
            QModbusDataUnit req(type, startAddress, count);    
            if (auto *reply = _modbus->sendReadRequest(req, _id))
            {
                if (!reply->isFinished()) connect(reply, &QModbusReply::finished, this, &ModbusMaster::readReady);
                else delete reply;
                return true;
            }
            return false;
        }
    
    
        void ModbusMaster::readReady()
        {
            auto reply = qobject_cast<QModbusReply *>(sender());
            if (!reply) return;
            reply->deleteLater();
    
            if (reply->error() == QModbusDevice::NoError)
            {
                const QModbusDataUnit unit = reply->result();
                // do something
            }
            else if (reply->error() == QModbusDevice::ProtocolError)
            {
                QStringList msg = QStringList() << "Error protocol" << reply->errorString() << QString::number(reply->rawResult().exceptionCode()) << QString::number(reply->result().startAddress());
                emit errorOccurred(msg.join(", "));
            }
            else emit errorOccurred(reply->errorString());
        }
    

    What does may cause a failure during send?
    Of course the tty port is available and I can write to it.


Log in to reply