Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QModbus long frame transmission error
Qt 6.11 is out! See what's new in the release blog

QModbus long frame transmission error

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 827 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.
  • H Offline
    H Offline
    Hy Tang
    wrote on last edited by Hy Tang
    #1

    I use a USB-to-RS485 converter, send request frame with write a small number of registers(such as 5 registers), it is ok, but with a large number of registers(such as 60 registers), it can not work well.

    Environment:
    Windows 7
    Qt 5.10.0
    MinGW 32bit

    Repetition:
    I use a spinBox to choose number of register, and a button to send Modbus request.

    void MainWindow::on_pushButtonSend_clicked()
    {
        int reg = ui->spinBoxReg->value();
        if (auto *reply = mClient->sendWriteRequest(QModbusDataUnit(QModbusDataUnit::HoldingRegisters, 0, reg), 1)) {
            if (!reply->isFinished()) {
                connect(reply, &QModbusReply::finished, this, [this, reply]() {
                    if (reply->error() == QModbusDevice::ProtocolError) {
                        statusBar()->showMessage(tr("Write response error: %1 (Modbus exception: 0x%2)")
                                                 .arg(reply->errorString()).arg(reply->rawResult().exceptionCode(), -1, 16));
                    } else if (reply->error() != QModbusDevice::NoError) {
                        statusBar()->showMessage(tr("Write response error: %1 (code: 0x%2)")
                                                 .arg(reply->errorString()).arg(reply->error(), -1, 16));
                    } else {
                        statusBar()->showMessage(tr("Write ok"));
                    }
                    reply->deleteLater();
                });
            } else {
                reply->deleteLater();
            }
        } else {
            ui->statusBar->showMessage(tr("Write error: ") + mClient->errorString());
        }
    }
    

    and the initialize:

        mClient = new QModbusRtuSerialMaster;
        mClient->setConnectionParameter(QModbusDevice::SerialPortNameParameter, "COM3");
        mClient->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, 9600);
        mClient->setConnectionParameter(QModbusDevice::SerialParityParameter, QSerialPort::NoParity);
        mClient->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, QSerialPort::Data8);
        mClient->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, QSerialPort::OneStop);
        mClient->setTimeout(3000);
        mClient->setNumberOfRetries(2);
        if (!mClient->connectDevice()) {
            QMessageBox::information(this, tr("err"), tr("can not open"));
            delete mClient;
            mClient = nullptr;
            ui->pushButtonSend->setEnabled(false);
        } else {
            ui->pushButtonSend->setEnabled(true);
        }
    

    the log info:
    error.png

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • H Hy Tang

      I use a USB-to-RS485 converter, send request frame with write a small number of registers(such as 5 registers), it is ok, but with a large number of registers(such as 60 registers), it can not work well.

      Environment:
      Windows 7
      Qt 5.10.0
      MinGW 32bit

      Repetition:
      I use a spinBox to choose number of register, and a button to send Modbus request.

      void MainWindow::on_pushButtonSend_clicked()
      {
          int reg = ui->spinBoxReg->value();
          if (auto *reply = mClient->sendWriteRequest(QModbusDataUnit(QModbusDataUnit::HoldingRegisters, 0, reg), 1)) {
              if (!reply->isFinished()) {
                  connect(reply, &QModbusReply::finished, this, [this, reply]() {
                      if (reply->error() == QModbusDevice::ProtocolError) {
                          statusBar()->showMessage(tr("Write response error: %1 (Modbus exception: 0x%2)")
                                                   .arg(reply->errorString()).arg(reply->rawResult().exceptionCode(), -1, 16));
                      } else if (reply->error() != QModbusDevice::NoError) {
                          statusBar()->showMessage(tr("Write response error: %1 (code: 0x%2)")
                                                   .arg(reply->errorString()).arg(reply->error(), -1, 16));
                      } else {
                          statusBar()->showMessage(tr("Write ok"));
                      }
                      reply->deleteLater();
                  });
              } else {
                  reply->deleteLater();
              }
          } else {
              ui->statusBar->showMessage(tr("Write error: ") + mClient->errorString());
          }
      }
      

      and the initialize:

          mClient = new QModbusRtuSerialMaster;
          mClient->setConnectionParameter(QModbusDevice::SerialPortNameParameter, "COM3");
          mClient->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, 9600);
          mClient->setConnectionParameter(QModbusDevice::SerialParityParameter, QSerialPort::NoParity);
          mClient->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, QSerialPort::Data8);
          mClient->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, QSerialPort::OneStop);
          mClient->setTimeout(3000);
          mClient->setNumberOfRetries(2);
          if (!mClient->connectDevice()) {
              QMessageBox::information(this, tr("err"), tr("can not open"));
              delete mClient;
              mClient = nullptr;
              ui->pushButtonSend->setEnabled(false);
          } else {
              ui->pushButtonSend->setEnabled(true);
          }
      

      the log info:
      error.png

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

      @Hy-Tang You will need to provide more information if you want to get meaningful answer.
      "it can not work well" - this is not a problem description...

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

      H 1 Reply Last reply
      0
      • H Hy Tang

        I use a USB-to-RS485 converter, send request frame with write a small number of registers(such as 5 registers), it is ok, but with a large number of registers(such as 60 registers), it can not work well.

        Environment:
        Windows 7
        Qt 5.10.0
        MinGW 32bit

        Repetition:
        I use a spinBox to choose number of register, and a button to send Modbus request.

        void MainWindow::on_pushButtonSend_clicked()
        {
            int reg = ui->spinBoxReg->value();
            if (auto *reply = mClient->sendWriteRequest(QModbusDataUnit(QModbusDataUnit::HoldingRegisters, 0, reg), 1)) {
                if (!reply->isFinished()) {
                    connect(reply, &QModbusReply::finished, this, [this, reply]() {
                        if (reply->error() == QModbusDevice::ProtocolError) {
                            statusBar()->showMessage(tr("Write response error: %1 (Modbus exception: 0x%2)")
                                                     .arg(reply->errorString()).arg(reply->rawResult().exceptionCode(), -1, 16));
                        } else if (reply->error() != QModbusDevice::NoError) {
                            statusBar()->showMessage(tr("Write response error: %1 (code: 0x%2)")
                                                     .arg(reply->errorString()).arg(reply->error(), -1, 16));
                        } else {
                            statusBar()->showMessage(tr("Write ok"));
                        }
                        reply->deleteLater();
                    });
                } else {
                    reply->deleteLater();
                }
            } else {
                ui->statusBar->showMessage(tr("Write error: ") + mClient->errorString());
            }
        }
        

        and the initialize:

            mClient = new QModbusRtuSerialMaster;
            mClient->setConnectionParameter(QModbusDevice::SerialPortNameParameter, "COM3");
            mClient->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, 9600);
            mClient->setConnectionParameter(QModbusDevice::SerialParityParameter, QSerialPort::NoParity);
            mClient->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, QSerialPort::Data8);
            mClient->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, QSerialPort::OneStop);
            mClient->setTimeout(3000);
            mClient->setNumberOfRetries(2);
            if (!mClient->connectDevice()) {
                QMessageBox::information(this, tr("err"), tr("can not open"));
                delete mClient;
                mClient = nullptr;
                ui->pushButtonSend->setEnabled(false);
            } else {
                ui->pushButtonSend->setEnabled(true);
            }
        

        the log info:
        error.png

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Hy-Tang does your device even accept such long frames ? How does it handle, for example, access tries of invalid registers. Is the Modbus Exception implemented correctly ?

        are you really only sending 60 Registers ?

        MODBUS only allows to READ 125 and WRITE 123 Registers in one datagram. aka 256 byte are you sure you'r not exceeding this ?


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


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

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @Hy-Tang You will need to provide more information if you want to get meaningful answer.
          "it can not work well" - this is not a problem description...

          H Offline
          H Offline
          Hy Tang
          wrote on last edited by
          #4

          @jsulm I posted the problem in advance due to misoperation, and have added the details

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hy Tang
            wrote on last edited by
            #5

            @J-Hilk I use Modbus Slave (https://modbustools.com/modbus_slave.html) to test this question, in modbus slave's Communication Traffic window, it has receive the long frame, and send the response, but qt client has error information.

            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