首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >J2mod从保持寄存器读取不同的值

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

Stack Overflow用户
提问于 2019-07-20 21:27:12
回答 1查看 418关注 0票数 0

我已经通过使用j2mod成功地连接到我的modbusRTU,当我试图从保持40050到40054的寄存器读取数值时,我总是得到28826,28828,28830,28832,28834。我不知道这个值是什么,请帮帮忙。

代码语言:javascript
运行
复制
 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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-29 01:30:38

正如评论中所讨论的,您的问题被证明是非常容易解决的:您只是使用了错误的偏移量作为您想要读取的Modbus地址。

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

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

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

代码语言:javascript
运行
复制
int ref = 49; //the reference, where to start reading from
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57125201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档