Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Using 485 for QmodbusServer library
QtWS25 Last Chance

Using 485 for QmodbusServer library

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
qmodbusdevicemodbusrs485
7 Posts 4 Posters 1.1k 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.
  • C Offline
    C Offline
    Chai18
    wrote on last edited by
    #1

    Hello Everyone,

    I am using the QModbusServer library for implementation of slave device. I have tested this library and receive signal from the master device. However there is an issue regarding 485. Since it is a half duplex protocol, we need to switch the mode using GPIO pin.
    Using the sample application which is given here . I am using imx6ul series embedded device with a custom Yocto build .
    I am receiving correct signal from the master, I am not able to respond because I am not able to switch 485 into transmit mode.
    How do I modify this code for 485 communication.
    Regards,

    jsulmJ 1 Reply Last reply
    0
    • C Chai18

      Hello Everyone,

      I am using the QModbusServer library for implementation of slave device. I have tested this library and receive signal from the master device. However there is an issue regarding 485. Since it is a half duplex protocol, we need to switch the mode using GPIO pin.
      Using the sample application which is given here . I am using imx6ul series embedded device with a custom Yocto build .
      I am receiving correct signal from the master, I am not able to respond because I am not able to switch 485 into transmit mode.
      How do I modify this code for 485 communication.
      Regards,

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Chai18 said in Using 485 for QmodbusServer library:

      because I am not able to switch 485 into transmit mode

      Why not? What happens?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Chai18 said in Using 485 for QmodbusServer library:

        because I am not able to switch 485 into transmit mode

        Why not? What happens?

        C Offline
        C Offline
        Chai18
        wrote on last edited by
        #3

        @jsulm where in the code do I switch the GPIO pin?

        jsulmJ 1 Reply Last reply
        0
        • C Chai18

          @jsulm where in the code do I switch the GPIO pin?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Chai18 said in Using 485 for QmodbusServer library:

          where in the code do I switch the GPIO pin?

          I would say before you respond

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Chai18 said in Using 485 for QmodbusServer library:

            where in the code do I switch the GPIO pin?

            I would say before you respond

            C Offline
            C Offline
            Chai18
            wrote on last edited by
            #5

            @jsulm Ok, let me rephrase it . QMobusRtuSerial is handling low level serial communication. The class is emitting a signal whenever it receives a frame. Is there a way to modify this behaviour.
            In a nutshell where exactly in the code do I put my GPIO pin switch code.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by
              #6

              Maybe you can override QModbusServer::processRequest() and do all work there.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AYJF
                wrote on last edited by AYJF
                #7

                Hi @Chai18 Did you find any solution ? I have a similar issue but in the client side (QModbusClient) , this is my function to write data to the holding registers , but I'm all the time getting the response timeout, and master is actually handling incoming requests and send appropriate responses automatically.

                void Serial::writeData(int start, const QList<quint16> &holdingRegisters) {
                  if (!modbusDevice)
                    return;
                
                  startAddress = start;
                  numberOfEntries = holdingRegisters.size();
                
                  // Switch to Transmit Mode
                  switchGPIO->setValue(true); //  enables TX
                
                  QModbusDataUnit writeUnit = writeRequest();
                  QModbusDataUnit::RegisterType table = writeUnit.registerType();
                  for (qsizetype i = 0, total = writeUnit.valueCount(); i < total; ++i) {
                    const auto addr = i + writeUnit.startAddress();
                    if (table == QModbusDataUnit::Coils)
                      writeUnit.setValue(i, m_coils[addr]);
                    else
                      writeUnit.setValue(i, holdingRegisters[addr]);
                  }
                
                  if (auto *reply = modbusDevice->sendWriteRequest(writeUnit, serverAddress)) {
                    if (!reply->isFinished()) {
                      QModbusDevice::connect(
                          reply, &QModbusReply::finished, this, [this, reply]() {
                            // Switch back to Receive Mode
                            usleep(1000);                // Small delay before switching
                            switchGPIO->setValue(false); // Enable RX mode
                
                            const auto error = reply->error();
                            if (error == QModbusDevice::ProtocolError) {
                              qDebug() << "Write response error:" << reply->errorString()
                                       << "(Modbus exception:"
                                       << reply->rawResult().exceptionCode() << ")";
                            } else if (error != QModbusDevice::NoError) {
                              qDebug() << "Write response error:" << reply->errorString()
                                       << "(code: 0x" << QString::number(error, 16) << ")";
                            }
                            reply->deleteLater();
                          });
                    } else {
                      qDebug() << "Write error: " << reply->errorString();
                      reply->deleteLater();
                    }
                  } else {
                    qDebug() << "Write error: " << modbusDevice->errorString();
                    switchGPIO->setValue(false); // Ensure RX mode is enabled on failure
                  }
                }
                

                Thanks in advance

                1 Reply Last reply
                0

                • Login

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