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. QModbusTcpClient - How to read input registers of device?

QModbusTcpClient - How to read input registers of device?

Scheduled Pinned Locked Moved Solved General and Desktop
qmodbusdevice
3 Posts 2 Posters 1.2k 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.
  • R Offline
    R Offline
    robro
    wrote on 8 Apr 2020, 16:40 last edited by robro 4 Aug 2020, 16:41
    #1

    Hello,

    I am trying to use modbus the first time.

    I got one modbus device delivering sensor values and this can be used with tcp.

    In python I already managed to query the device.
    All I needed for that were the ip-address, port, input register and number of bytes to read.

    However, in Qt I somehow cannot manage to get this running.

    I would like to read one byte from register 1002.
    I am also wondering what to enter in "sendReadRequest" as second argument. I tried different values without success.

    The following code delivers the following output:

        QVector<quint16> data(1); // One Byte
        QModbusDataUnit adu(QModbusDataUnit::InputRegisters, 1002, data); // Read one byte of register 1002
        QModbusReply *reply = device->sendReadRequest(adu, 3); // What is the second argument for? What do I need there?
        if(reply != nullptr) {
            qDebug() << "Sent read request, state is: " << device->state();
                    qDebug() << reply;
                    qDebug() << "Value 0: " << reply->result().value(0);
                    qDebug() << "Value count: " << reply->result().valueCount();
                    qDebug() << "Input registers:" << reply->result().InputRegisters;
        } else {
            qDebug() << "Send of ready request failed! Error is: " << device->state();
        }
    

    Output:
    Sent read request, state is: QModbusDevice::ConnectedState
    QModbusReply(0x17cce5d00c0)
    Value 0: 0
    Value count: 0
    Input registers: 3

    What am I doing wrong?

    Thank you very much!

    J 1 Reply Last reply 9 Apr 2020, 07:04
    0
    • R robro
      8 Apr 2020, 16:40

      Hello,

      I am trying to use modbus the first time.

      I got one modbus device delivering sensor values and this can be used with tcp.

      In python I already managed to query the device.
      All I needed for that were the ip-address, port, input register and number of bytes to read.

      However, in Qt I somehow cannot manage to get this running.

      I would like to read one byte from register 1002.
      I am also wondering what to enter in "sendReadRequest" as second argument. I tried different values without success.

      The following code delivers the following output:

          QVector<quint16> data(1); // One Byte
          QModbusDataUnit adu(QModbusDataUnit::InputRegisters, 1002, data); // Read one byte of register 1002
          QModbusReply *reply = device->sendReadRequest(adu, 3); // What is the second argument for? What do I need there?
          if(reply != nullptr) {
              qDebug() << "Sent read request, state is: " << device->state();
                      qDebug() << reply;
                      qDebug() << "Value 0: " << reply->result().value(0);
                      qDebug() << "Value count: " << reply->result().valueCount();
                      qDebug() << "Input registers:" << reply->result().InputRegisters;
          } else {
              qDebug() << "Send of ready request failed! Error is: " << device->state();
          }
      

      Output:
      Sent read request, state is: QModbusDevice::ConnectedState
      QModbusReply(0x17cce5d00c0)
      Value 0: 0
      Value count: 0
      Input registers: 3

      What am I doing wrong?

      Thank you very much!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 9 Apr 2020, 07:04 last edited by
      #2

      @robro said in QModbusTcpClient - How to read input registers of device?:

      What am I doing wrong?

      You're reading the reply just after sending request.
      You should do the reading in a slot connected to https://doc.qt.io/qt-5/qmodbusreply.html#finished

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

      1 Reply Last reply
      3
      • R Offline
        R Offline
        robro
        wrote on 10 Apr 2020, 10:13 last edited by
        #3

        Thank you very much!
        Using the "finished"-signal everything works fine :-)

        1 Reply Last reply
        0

        1/3

        8 Apr 2020, 16:40

        • Login

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