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異步寫入

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM