连接信息一般是固定的,可以写成常量
public class OpcConnection { private static final ConnectionInformation CI = new ConnectionInformation(); private OpcConnection() { } static { CI.setHost("127.0.0.1"); CI.setUser("OPCUser"); CI.setPassword("111111"); CI.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); } public static ConnectionInformation getInfo() { return CI; } }读取多个值方法的使用:
private static final List<String> ITEMS = Arrays.asList("通道 1.设备 1.TAG1", "通道 1.设备 1.TAG2"); private final Server server = new Server(OpcConnection.getInfo(), Executors.newSingleThreadScheduledExecutor()); public List<String> getData() { List<String> res = new ArrayList<>(); try { server.connect(); Group group = server.addGroup(); res = OpcUtil.readValues(group, ITEMS); } catch (UnknownHostException | JIException | AlreadyConnectedException | NotConnectedException | DuplicateGroupException e) { e.printStackTrace(); } finally { server.disconnect(); } return res; } 参考:https://www.cnblogs.com/ioufev/articles/9894452.html
https://www.cnblogs.com/ioufev/p/9928971.html
https://www.cnblogs.com/ioufev/p/9929170.html
https://www.hifreud.com/2014/12/27/opc-4-client-invoke-use-utgard
三、注意事项 1、kepware频率设置kepware 中可以对设备指定几种扫描模式,扫描速率最高可设置到 10ms
如果选择“遵循标记指定的扫描速率”,也可以单独对标记设置扫描速率
kepware 客户端也可以设置速率,不过这里的速率只是客户端的,真实的采集速率还是要根据服务端来定
一个通道里尽量放一个设备,因为 kepware 通道内是单线程(kepware 客服说的),如果放多个设备会影响扫描速率,造成数据刷新变慢
3、DCOM 配置