Qt Forum

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

    Unsolved QModbusDevice connect/disconnect

    General and Desktop
    qmodbusdevice qserialport
    2
    3
    2326
    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.
    • J.Hilk
      J.Hilk Moderators last edited by

      Hello everyone,

      I'm useing the QModbusDevice class to exchange data over a serial bus with an external device.

      So far everything works fine, but the programm needs to be as fool proof as possible, so I need to catch wrong parameters, reconnects, etc.

      When I call void QModbusDevice::disconnectDevice(), the state QModbusClient does not change from QModbusDevice::ClosingState to QModbusDevice::UnconnectedState and therefore does not accept new parameters and does not allow a reconnect.

      I was able to circumvent this by re-initializating the QModbusClient. But there has to be a better way.

      The question:
      What is the right way to disconnect a QModbusClient?

      Here, the 2 functions I wrote, that handle the Connect/Disconnet:

      void Modbus::connectDevice(bool connect)
      {
          qDebug() << "connectDevice" << connect << mDevice->state();
      
          if(connect){
              switch (mDevice->state()) {
              
              case QModbusDevice::ClosingState:
              case QModbusDevice::ConnectedState:
              case QModbusDevice::ConnectingState:
                  mDevice->deleteLater();
                  mDevice = new QModbusRtuSerialMaster(this);
                  setupSignalSlots();
                  QTimer::singleShot(100,this, [=]{setConnectParamter(m_port,m_paramter);});
                  break;
              
              case QModbusDevice::UnconnectedState:
              default:
                  if(!mDevice->connectDevice()){
                      emit signalModbusError("Verbindungsfehler: "+mDevice->errorString());
                      mDevice->disconnectDevice();
                  }
                  break;
              }
          }else{
              mDevice->disconnectDevice();
          }
      }
      
      void Modbus::setConnectParamter(QString port, QList<int> Paramter)
      {
          qDebug() << "setConnectParamter" << port << Paramter;
          if(!mDevice){
              qDebug() << "Error setConnectParamter";
              emit signalModbusError("Modbus not initialised!");
              return;
          }
          if(Paramter.size() >= 7){
              m_paramter = Paramter; m_port = port;
              mDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter,port);
              mDevice->setConnectionParameter(QModbusDevice::SerialParityParameter, Paramter[0]);
              mDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,Paramter[1]);
              mDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,Paramter[2]);
              mDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,Paramter[3]);
              mDevice->setTimeout(Paramter[4]);
              mDevice->setNumberOfRetries(Paramter[5]);
              deviceID = Paramter[6];
              connectDevice(true);
          }else{
              qDebug() << " Error setConnectParamter, Parameter mismatch";
          }
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

      Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply Reply Quote 0
      • T
        tangxinzhou last edited by

        Excuse , do you have the answer to this question? I encountered the same problem.

        J.Hilk 1 Reply Last reply Reply Quote 0
        • J.Hilk
          J.Hilk Moderators @tangxinzhou last edited by

          @tangxinzhou I'm afraid I do not have a solid solutions. I'm running a workaround now.

          Let me post it for you:

          void connectDevice(bool connect)
          {
              if(connect){
                  switch (mDevice->state()) {
          
                  case QModbusDevice::ClosingState:
                  case QModbusDevice::ConnectedState:
                  case QModbusDevice::ConnectingState:
                      mDevice->deleteLater();
                      mDevice = new QModbusRtuSerialMaster(this);
                      setupSignalSlots();
                      QTimer::singleShot(100,this, [=]{setConnectParamter(m_port,m_paramter);});
                      break;
          
                  case QModbusDevice::UnconnectedState:
                  default:
                      if(!mDevice->connectDevice()){
                          emit signalModbusError(mDevice->errorString());
                          mDevice->disconnectDevice();
                      }
                      break;
                  }
              }else{
                  stopPoll();
                  mDevice->disconnectDevice();
                  QTimer::singleShot(200,this,[=]{checkDisconnected();});
              }
          }
          
          void checkDisconnected()
          {
              if(mDevice->state() != QModbusDevice::UnconnectedState){
                  mDevice->deleteLater();
                  mDevice = new QModbusRtuSerialMaster(this);
                  setupSignalSlots();
              }
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

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