赞
踩
解压安装包傻瓜式安装就可以了,不赘述......
右键点击kepserver的图标选怎opc ua配置,点击进入之后显示为:
第一步设置本机通讯opc地址(也就是通过java程序也就是本地服务ip为:127.0.0.1)的安全策略推荐设置成无,博主设置成其它选项折腾很久未果,设置如图:
到此kepserver的设置结束了,配置完重新初始化一下设置就可以了。
- @Configuration
- @Slf4j
- public class OpcClient {
-
- @Value("${opc.endpointUrl}")
- private String endpointUrl;
-
- @Value("${opc.username}")
- private String username;
-
- @Value("${opc.password}")
- private String password;
-
- @Value("${opc.applicationName}")
- private String applicationName;
-
- @Value("${opc.applicationUri}")
- private String applicationUri;
-
- static {
- Security.addProvider(new BouncyCastleProvider());
- }
-
- @Bean("OpcUaClient")
- public OpcUaClient createClient() throws Exception {
-
- //安全策略 None、Basic256、Basic128Rsa15、Basic256Sha256
- SecurityPolicy securityPolicy = SecurityPolicy.None;
-
- List<EndpointDescription> endpoints = DiscoveryClient.getEndpoints(endpointUrl).get();
-
- EndpointDescription endpoint = endpoints.stream()
- .filter(e -> e.getSecurityPolicyUri().equals(securityPolicy.getUri()))
- .findFirst()
- .orElseThrow(() -> new Exception("没有连接上端点"));
-
- log.info("使用端点: {} [{}/{}]", endpoint.getEndpointUrl(), securityPolicy, endpoint.getSecurityMode());
-
- OpcUaClientConfig config = OpcUaClientConfig.builder()
- .setApplicationName(LocalizedText.english(applicationName))
- .setApplicationUri(applicationUri)
- .setEndpoint(endpoint)
- //AnonymousProvider (匿名方式)UsernameProvider(账户密码)
- //.setIdentityProvider(new AnonymousProvider())
- .setIdentityProvider(new UsernameProvider(username, password))
- .setRequestTimeout(uint(5000))
- .build();
- OpcUaClient opcUaClient = OpcUaClient.create(config);
- opcUaClient.connect().get();
- return opcUaClient;
- }
- }

- @Slf4j
- @Component
- public class OpcUaUtil {
-
- @Autowired
- private OpcUaClient client;
-
- /**
- * 写入数据到opc服务器
- *
- * @param namespaceIndex
- * @param identifier
- * @param value
- * @throws Exception
- */
- public void write(int namespaceIndex, String identifier, Object value) throws Exception {
- NodeId nodeId = new NodeId(namespaceIndex, identifier);
- Variant v = new Variant(value);
- DataValue dv = new DataValue(v, null, null);
- CompletableFuture<StatusCode> status = client.writeValue(nodeId, dv);
- StatusCode statusCode = status.get();
- boolean good = statusCode.isGood();
- if (!good)
- throw new BusinessException("输入写入失败,请检查系统运行情况");
- }
-
- /**
- * 从opc服务器读数据
- *
- * @param namespaceIndex
- * @param identifier
- * @return
- * @throws Exception
- */
- public Object read(int namespaceIndex, String identifier) throws Exception {
- UaVariableNode node = client.getAddressSpace().getVariableNode(new NodeId(namespaceIndex, identifier));
- DataValue value = node.readValue();
- Object data = value.getValue().getValue();
- return data;
- }
-
- /**
- * 获取对应的节点
- *
- * @param namespaceIndex
- * @param identifier
- * @return
- * @throws UaException
- */
- public List<ExpandedNodeId> browseNode(int namespaceIndex, String identifier) throws Exception {
- List<ReferenceDescription> referenceDescriptions = client.getAddressSpace().browse(new NodeId(namespaceIndex, identifier));
- List<ExpandedNodeId> listNode = new ArrayList<>();
- for (ReferenceDescription referenceDescription : referenceDescriptions) {
- listNode.add(referenceDescription.getNodeId());
- }
- return listNode;
- }

- @Test
- public void redWriteOpcTest() throws Exception {
- System.out.println(opcUaUtil.read(2, "欧姆龙.设备 1.测试"));
- // System.out.println(opcUaUtil.read(2, "欧姆龙.设备 1.测试1"));
- // opcUaUtil.write(2,"欧姆龙.设备 1.测试",(short)888);//西门子opc-ua.设备1.PLC.Memory.测i试
- // System.out.println(opcUaUtil.read(2, "西门子opc-ua.设备 1.PLC.Memory.测试"));
- }
避坑处:kepserver的namespace都为 2
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。