Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-71282

QModbusTcpClient::connectDevice() always returns true

    XMLWordPrintable

Details

    • Windows
    • fb7a0ef5b31cba52a745006bf9dca61101229de8 (qt/qtserialbus/5.12)

    Description

      The documentation says:

      Connects the device to the Modbus network. Returns true on success; otherwise false.

      As far as I understand it should return false if it cannot connect the device to modbus. Instead it always returns true even if the device is not available.

      Here a complete example to reproduce that behavior:

      main.cpp

      #include <QApplication>
      #include <QDebug>
      #include "modbusclient.h"
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          ModbusClient client;
          qDebug() << client.open("192.168.1.157", 502); // address of a non-existent device
      
          return a.exec();
      }
      
      

      modbusclient.h

      #ifndef MODBUSCLIENT_H
      #define MODBUSCLIENT_H
      
      #include <QObject>
      #include <QModbusClient>
      
      class ModbusClient : public QObject
      {
          Q_OBJECT
      
      public:
          explicit ModbusClient(QObject *parent = nullptr);
          bool open(QString host, int port);
      
      private:
          QModbusClient *_modbus;
      
      };
      
      #endif // MODBUSCLIENT_H
      

      modbusclient.cpp

      #include "modbusclient.h"
      #include <QModbusTcpClient>
      #include <QVariant>
      
      ModbusClient::ModbusClient(QObject *parent) : QObject(parent)
      {
          _modbus = nullptr;
      }
      
      bool ModbusClient::open(QString host, int port)
      {
          if (_modbus)
          {
              _modbus->disconnectDevice();
              delete _modbus;
          }
      
          _modbus = new QModbusTcpClient(this);
          _modbus->setConnectionParameter(QModbusDevice::NetworkPortParameter, port);
          _modbus->setConnectionParameter(QModbusDevice::NetworkAddressParameter, host);
      
          _modbus->setTimeout(250);
          _modbus->setNumberOfRetries(1);
      
          return _modbus->connectDevice();
      }
      

      Here the output:

      true

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ablasche Alex Blasche
            mark81 Marco Trapanese
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes