zoukankan      html  css  js  c++  java
  • 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(" ");
        }
        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异步写入

  • 相关阅读:
    给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
    11
    实战 迁移学习 VGG19、ResNet50、InceptionV3 实践 猫狗大战 问题
    tx2系统备份与恢复
    如何在Ubuntu 18.04上安装和卸载TeamViewer
    bzoj 3732 Network (kruskal重构树)
    bzoj2152 聪聪可可 (树形dp)
    牛客 216D 消消乐 (二分图最小点覆盖)
    牛客 197E 01串
    Wannafly挑战赛23
  • 原文地址:https://www.cnblogs.com/myboat/p/11739257.html
Copyright © 2011-2022 走看看