简体   繁体   English

使用 modbus 模拟器在 java 中编写一个程序来读取保持寄存器值

[英]write a program in java using modbus simulator for reading holding register values

I want to take real-time data using modbus tcp/ip simulator for filling of a tank that uses port no 502.我想使用modbus tcp/ip模拟器获取实时数据,以填充使用端口号 502 的坦克。

How can I write a code in java to get the holding register value from the simulator and also I want to control the values of it?如何在java中编写代码以从模拟器获取保持寄存器值,并且我想控制它的值?

If you use a Modbus library like this one most of the work is already done for you.如果您使用像这样Modbus 库,那么大部分工作已经为您完成。

ModbusTcpMasterConfig config = new ModbusTcpMasterConfig.Builder("localhost").build();
ModbusTcpMaster master = new ModbusTcpMaster(config);

CompletableFuture<ReadHoldingRegistersResponse> future =
        master.sendRequest(new ReadHoldingRegistersRequest(0, 10), 0);

future.thenAccept(response -> {
    System.out.println("Response: " + ByteBufUtil.hexDump(response.getRegisters()));

    ReferenceCountUtil.release(response);
});
import java.io.* ;
import java.net.* ;
import java.util.*;


public class Modcli {

  public static void main(String argv[]) {
    if (argv.length < 4) {
      System.out.println("usage: java test3 dns_name unit reg_no num_regs");
      System.out.println("eg java test3 aswales8.modicon.com 5 0 10");
      return;
    }
    try {
      String ip_adrs = argv[0];
      int unit = Integer.parseInt(argv[1]);
      int reg_no = Integer.parseInt(argv[2]);
      int num_regs = Integer.parseInt(argv[3]);
      System.out.println("ip_adrs = "+ip_adrs+" unit = "+unit+" reg_no = "+
      reg_no+" num_regs = "+num_regs);

      // set up socket
      Socket es = new Socket(ip_adrs,502);
      OutputStream os= es.getOutputStream();
      FilterInputStream is = new BufferedInputStream(es.getInputStream());
      byte obuf[] = new byte[261];
      byte ibuf[] = new byte[261];
      int c = 0;
      int i;

      // build request of form 0 0 0 0 0 6 ui 3 rr rr nn nn
      for (i=0;i<5;i++) obuf[i] = 0;
      obuf[5] = 6;
      obuf[6] = (byte)unit;
      obuf[7] = 3;
      obuf[8] = (byte)(reg_no >> 8);
      obuf[9] = (byte)(reg_no & 0xff);
      obuf[10] = (byte)(num_regs >> 8);
      obuf[11] = (byte)(num_regs & 0xff);

      // send request
      os.write(obuf, 0, 12);

      // read response
      i = is.read(ibuf, 0, 261);
      if (i<9) {
        if (i==0) {
          System.out.println("unexpected close of connection at remote end");
        } else {
          System.out.println("response was too short - "+i+" chars");
        }
      } else if (0 != (ibuf[7] & 0x80)) {
        System.out.println("MODBUS exception response - type "+ibuf[8]);
      } else if (i != (9+2*num_regs)) {
        System.out.println("incorrect response size is "+i+

            " expected"+(9+2*num_regs));
      } else {
        for (i=0;i<=2;i++) {
          int w = (ibuf[0+i+i]<<8) + ibuf[1+i+i];
          System.out.println("word "+i+" = "+w);
        }
        for (i=3;i<=5;i++) {
          int w = (ibuf[i+3]) ;
          System.out.println("word "+i+" = "+w);
        }
        for (i=0;i<num_regs;i++) {
          int w = (ibuf[9+i+i]<<8) + ibuf[10+i+i];
          System.out.println("word "+i+" = "+w);
        }

      }

      // close down
      es.close();
    } catch (Exception e) {
      System.out.println("exception :"+e);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM