OPC API 简介

————————————————
版权声明:本文为CSDN博主「lgbisha」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lgbisha/article/details/82898228

1. 列举某Server下的所有OPC连接

  ServerList serverList = new ServerList("10.1.5.123", "freud",
    "password", "");

  Collection<ClassDetails> classDetails = serverList
    .listServersWithDetails(new Category[] {
    Categories.OPCDAServer10, Categories.OPCDAServer20,
    Categories.OPCDAServer30 }, new Category[] {});

  for (ClassDetails cds : classDetails) {
    System.out.println(cds.getProgId() + "=" + cds.getDescription());
  }

 

2.列举连接下的所有Group和Item

  public static void main(String[] args) throws Exception {
    ConnectionInformation ci = new ConnectionInformation();
    ci.setHost("10.1.5.123");
    ci.setDomain("");
    ci.setUser("freud");
    ci.setPassword("password");
    ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");

    Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());

    server.connect();

    dumpTree(server.getTreeBrowser().browse(), 0);
    dumpFlat(server.getFlatBrowser());

    server.disconnect();
  }

  private static void dumpFlat(final FlatBrowser browser)
      throws IllegalArgumentException, UnknownHostException, JIException {
      for (String name : browser.browse()) {
        System.out.println(name);
      }
  }

  private static void dumpTree(final Branch branch, final int level) {

    for (final Leaf leaf : branch.getLeaves()) {
      dumpLeaf(leaf, level);
    }


    for (final Branch subBranch : branch.getBranches()) {
      dumpBranch(subBranch, level);
      dumpTree(subBranch, level + 1);
    }
  }

  private static String printTab(int level) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < level; i++) {
    sb.append("\t");
    }
    return sb.toString();
  }

  private static void dumpLeaf(final Leaf leaf, final int level) {
    System.out.println(printTab(level) + "Leaf: " + leaf.getName() + ":"
      + leaf.getItemId());
  }

  private static void dumpBranch(final Branch branch, final int level) {
    System.out.println(printTab(level) + "Branch: " + branch.getName());
  }

3.Item的同步查询

  public static void main(String[] args) throws Exception {

    ConnectionInformation ci = new ConnectionInformation();
    ci.setHost("10.1.5.123");
    ci.setDomain("");
    ci.setUser("freud");
    ci.setPassword("password");
    ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");

    Server server = new Server(ci,
    Executors.newSingleThreadScheduledExecutor());

    server.connect();

    Group group = server.addGroup();
    Item item = group.addItem("Random.Real5");

    Map<String, Item> items = group.addItems("Random.Real1",
      "Random.Real2", "Random.Real3", "Random.Real4");

    dumpItem(item);

    for (Entry<String, Item> temp : items.entrySet()) {
      dumpItem(temp.getValue());
    }

    server.dispose();
  }

  private static void dumpItem(Item item) throws JIException {
    System.out.println("[" + (++count) + "],ItemName:[" + item.getId()
      + "],value:" + item.read(false).getValue());
  }

  private static int count;


4.Item的异步查询

 

  private static final int PERIOD = 100;

  private static final int SLEEP = 2000;

  public static void main(String[] args) throws Exception {

  ConnectionInformation ci = new ConnectionInformation();
  ci.setHost("10.1.5.123");
  ci.setDomain("");
  ci.setUser("freud");
  ci.setPassword("password");
  ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");

  Server server = new Server(ci,
  Executors.newSingleThreadScheduledExecutor());

  server.connect();

  AccessBase access = new SyncAccess(server, PERIOD);

  access.addItem("Random.Real5", new DataCallback() {
  private int i;

  public void changed(Item item, ItemState itemstate) {
    System.out.println("[" + (++i) + "],ItemName:[" + item.getId()
      + "],value:" + itemstate.getValue());
    }
  });

  access.bind();
  Thread.sleep(SLEEP);
  access.unbind();
  server.dispose();
  }


5.Item的发布订阅查询


  private static final int PERIOD = 100;

  private static final int SLEEP = 2000;

  public static void main(String[] args) throws Exception {

    ConnectionInformation ci = new ConnectionInformation();
    ci.setHost("10.1.5.123");
    ci.setDomain("");
    ci.setUser("freud");
    ci.setPassword("password");
    ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");

    Server server = new Server(ci,
    Executors.newSingleThreadScheduledExecutor());

    server.connect();

    AccessBase access = new Async20Access(server, PERIOD, false);

    access.addItem("Random.Real5", new DataCallback() {

    private int count;

    public void changed(Item item, ItemState itemstate) {
      System.out.println("[" + (++count) + "],ItemName:["
      + item.getId() + "],value:" + itemstate.getValue());
      }
    });

    access.bind();
    Thread.sleep(SLEEP);
    access.unbind();
    server.dispose();
  }


6.自动重连Item异步读取


  private static final int PERIOD = 100;

  private static final int SLEEP = 2000;

  public static void main(String[] args) throws Exception {

    ConnectionInformation ci = new ConnectionInformation();
    ci.setHost("10.1.5.123");
    ci.setDomain("");
    ci.setUser("freud");
    ci.setPassword("password");
    ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");

    Server server = new Server(ci,
    Executors.newSingleThreadScheduledExecutor());

    AutoReconnectController controller = new AutoReconnectController(server);

    controller.connect();

    AccessBase access = new SyncAccess(server, PERIOD);

    access.addItem("Random.Real5", new DataCallback() {
    private int i;

    public void changed(Item item, ItemState itemstate) {
      System.out.println("[" + (++i) + "],ItemName:[" + item.getId()
        + "],value:" + itemstate.getValue());
      }
    });

    access.bind();
    Thread.sleep(SLEEP);
    access.unbind();
    controller.disconnect();
  }


7.Item同步写入


  public static void main(String[] args) throws Exception {

    ConnectionInformation ci = new ConnectionInformation();
    ci.setHost("10.1.5.123");
    ci.setDomain("");
    ci.setUser("freud");
    ci.setPassword("password");
    ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");

    Server server = new Server(ci,
    Executors.newSingleThreadScheduledExecutor());

    server.connect();

    Group group = server.addGroup();
    Item item = group.addItem("Square Waves.Real4");

    final Float[] integerData = new Float[] { 1202f, 1203f, 1204f };
    final JIArray array = new JIArray(integerData, false);
    final JIVariant value = new JIVariant(array);

    item.write(value);
    Thread.sleep(2000);

    dumpItem(item);

    server.dispose();

  }

  private static void dumpItem(Item item) throws JIException {
    System.out.println("[" + (++count) + "],ItemName:[" + item.getId()
      + "],value:" + item.read(true).getValue());
  }

private static int count;

8.Item异步写入

 

上一篇:OPC Utgard的数据访问方式


下一篇:opc 带有session