J2mod 从保持寄存器读取不同的值

J2mod reading different value from holding register

我已经使用 j2mod 成功连接到我的 modbusRTU,当我尝试从保持寄存器读取 40050 到 40054 范围内的值时,我总是得到 28826、28828、28830、28832、28834。我不知道这个值是多少,请大家帮忙

 ModbusSerialTransaction trans = null;
       ReadMultipleRegistersRequest req = null;
       ReadMultipleRegistersResponse res = null;

       int unitid = 1; //the unit identifier we will be talking to
       int ref = 40050; //the reference, where to start reading from
       int count = 5; //the count of IR's to read
       int repeat = 1; //a loop for repeating the transaction

       //4. Open the connection
       try {

           //5. Prepare a request
           req = new ReadMultipleRegistersRequest(ref, count);
           req.setUnitID(unitid);
           req.setHeadless();

           //6. Prepare a transaction
           trans = new ModbusSerialTransaction(con);
           trans.setRequest(req);

           int k = 0;
           do {
               trans.setTransDelayMS(50);
               trans.execute();
               res = (ReadMultipleRegistersResponse) trans.getResponse();
               for (int n = 0; n < res.getWordCount(); n++) {
                   System.out.println("Word " + n + "=" + res.getRegisterValue(n));
               }
               k++;
           } while (k < repeat);
           con.close();
           //8. Close the connection
       } catch (ModbusException ex) {
          System.out.print(ex);
       }

please check this code.

正如评论中所讨论的那样,您的问题很容易解决:您只是为要读取的 Modbus 地址使用了错误的偏移量。

Modbus 保持寄存器按照惯例从 40000 开始编号,但对于大多数库而言,不考虑此偏移量。

因此要读取某个设备的 Modbus 映射中称为 40011 的寄存器,您必须使用索引号 10。

按照同样的逻辑,要读取寄存器 40050,您只需更改代码中的地址行:

int ref = 49; //the reference, where to start reading from