————————————————
版权声明:本文为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 API 简介的更多相关文章

  1. Web Api 简介

    ASP.NET Web API 简介  ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP. ...

  2. ZooKeeper系列4:ZooKeeper API简介及编程

    问题导读: 1.ZooKeeper API 共包含几个包? 2.如何使用ZooKeeper API 创建zookeeper应用程序? 1)ZooKeeper API 简介   ZooKeeper AP ...

  3. WebSocket API简介

    WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例如Chrome,Safari,Firefox,Opera,IE等等,对该协议支持最早的应该是chrome,从chr ...

  4. 构建简单的 C++ 服务组件,第 1 部分: 服务组件体系结构 C++ API 简介

    构建简单的 C++ 服务组件,第 1 部分: 服务组件体系结构 C++ API 简介 熟悉将用于 Apache Tuscany SCA for C++ 的 API.您将通过本文了解该 API 的主要组 ...

  5. Raphael Js矢量库API简介:

    Raphael Js矢量库API简介:Raphael Javascript 是一个 Javascript的矢量库. 2010年6月15日,著名的JavaScript库ExtJS与触摸屏代码库项目jQT ...

  6. 开放数据接口 API 简介与使用场景、调用方法

    此文章对开放数据接口 API 进行了功能介绍.使用场景介绍以及调用方法的说明,供用户在使用数据接口时参考之用. 在给大家分享的一系列软件开发视频课程中,以及在我们的社区微信群聊天中,都积极地鼓励大家开 ...

  7. Monkey脚本API简介

    一.API简介 LaunchActivity(pkg_name, cl_name):启动应用的Activity.参数:包名和启动的Activity. Tap(x, y, tapDuration): 模 ...

  8. web API简介(四):客户端储存之IndexedDB API

    概述 前篇:web API简介(三):客户端储存之Web Storage API 客户端储存从某一方面来说和动态网站差不多.动态网站是用服务端来储存数据,而客户端储存是用客户端来储存数据. Index ...

  9. web API简介(三):客户端储存之Web Storage API

    概述 前篇:web API简介(二):客户端储存之document.cookie API 客户端储存从某一方面来说和动态网站差不多.动态网站是用服务端来储存数据,而客户端储存是用客户端来储存数据. W ...

随机推荐

  1. JDK源码那些事儿之我眼中的HashMap

    源码部分从HashMap说起是因为笔者看了很多遍这个类的源码部分,同时感觉网上很多都是粗略的介绍,有些可能还不正确,最后只能自己看源码来验证理解,写下这篇文章一方面是为了促使自己能深入,另一方面也是给 ...

  2. MGR测试及搭建

    一.部署规划:本次布署使用单机多实例进行basedir: /usr/local/mysql端口号 数据目录 group_repplication通信端口3306 /data/mysql/mysql_3 ...

  3. re模块中的非贪婪匹配

    python的re模块中有贪婪匹配和非贪婪匹配之分,当使用*时会匹配零个或多个,使用+时会匹配一个或多个.当使用?在前边特殊符号前时会进行非贪婪匹配,匹配零个或者一个,今天主要讨论非贪婪匹配中存在的坑 ...

  4. C语言笔记 09_共用体&typedef&输入|输出

    共用体 共用体允许您在相同的内存位置存储不同的数据类型.您可以定义一个带有多成员的共用体,但是任何时候只能有一个成员带有值.共用体提供了一种使用相同的内存位置的有效方式. 定义共用体 为了定义共用体, ...

  5. dubbo配置文件

    <dubbo:service/> 服务配置,用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心.<dubbo:reference/> ...

  6. iis大文件上传

    IS出于安全考虑限制了大文件的上传,而网上百度到的大部分解决方法都是用一个管理员权限的记事本打开一个文件修改参数,但是我发现里面根本没有网上所说的那些参数,最后自己找到了修改发布文件的webconfi ...

  7. Friends (ZOJ - 3710)

    Problem Alice lives in the country where people like to make friends. The friendship is bidirectiona ...

  8. c 判断数字是否无限

    /* isinf example */ #include <stdio.h> /* printf */ #include <math.h> /* isinf, sqrt */ ...

  9. @Configuration,@ConfigurationProperties,@EnableConfigurationProperties

    @Configuration API: https://www.javadoc.io/doc/org.springframework/spring-context/5.0.7.RELEASE @Con ...

  10. tmux 入门踩坑记录

    软件安装 sudo apt-get install tmux 1. 分割左右窗口 ^b -> % 运行 tmux 新建一个 tmux 的会话(session),此时窗口唯一的变化是在底部会出现一 ...