Eclipse Milo (버전 0.1.5)를 테스트하여 TwinCAT PLC의 OPC UA 서버와 클라이언트로 통신합니다. 내 Java 프로그램은 일종의 미들웨어 역할을합니다. TwinCAT OPC UA 서버에서 구조를 읽고 JSON에 값을 입력하여 다른 서버로 보냅니다. - bVar1 (부울)
- fVar2 (플로트)
Picture of Struct in TwinCAT디코드 바이너리 ExtensionObject
I이 경우
stSimpleStruct (PLC의 구조 definiend)
:
는 시험 I는 두 변수와 PLC에 샘플 구조체를 만들어 노드를 읽으면 값은 ByteString으로 인코딩 된 ExtensionObject입니다. 구조 변수 (bVar1 및 fVar2)의 값을 읽고 JSON 객체에 넣어야합니다. 결과는 다음과 같습니다.
{
“bVar1” : false,
“fVar2” : 0
}
위의 구조는 그 예입니다. 읽을 구조는 런타임에만 알려져 있습니다. 구조 변수의 값에 액세스하려면 어떻게 이진 ExtensionObject를 디코딩 할 수 있습니까?
// Get endpoints
String endpointURL = "opc.tcp://172.20.1.1:4840";
EndpointDescription[] endpoints = UaTcpStackClient.getEndpoints(endpointURL).get();
logger.info("Available endpoint:");
for (EndpointDescription endpoint : endpoints)
{
logger.info("{} Security: {}", endpoint.getEndpointUrl(), endpoint.getSecurityPolicyUri());
}
// Chose endpoint
SecurityPolicy securityPolicy = SecurityPolicy.None;
EndpointDescription endpoint = Arrays.stream(endpoints)
.filter(e -> e.getSecurityPolicyUri().equals(securityPolicy.getSecurityPolicyUri()))
.findFirst()
.orElseThrow(() -> new Exception("No desired endpoints returned"));
logger.info("Using endpoint: {} [{}]", endpoint.getEndpointUrl(), SecurityPolicy.None);
// Create client config
OpcUaClientConfig config = OpcUaClientConfig.builder()
.setApplicationName(LocalizedText.english("TestApplication"))
.setEndpoint(endpoint)
.setRequestTimeout(uint(5000))
.build();
OpcUaClient client = new OpcUaClient(config);
// Synchronous connect
client.connect().get();
// Read struct
NodeId nodeId = new NodeId(4, "MAIN.stSimpleStruct");
VariableNode node = client.getAddressSpace().createVariableNode(nodeId);
DataValue value = node.readValue().get();
ExtensionObject extensionObject = (ExtensionObject) value.getValue().getValue();
스택 오버플로에 오신 것을 환영합니다! 디버깅 도움을 요청하는 질문 ("이 코드가 작동하지 않는 이유는 무엇입니까?")에는 원하는 동작, 특정 문제 또는 오류 및 질문 자체에서이를 재현하는 데 필요한 가장 짧은 코드가 포함되어야합니다. 분명한 문제 설명이없는 질문은 다른 독자에게 유용하지 않습니다. 참조 : [mcve]를 만드는 방법. '편집'링크를 사용하여 * 질문 *을 개선하십시오 - 의견을 통해 더 많은 정보를 추가하지 마십시오. 감사! – GhostCat
스크린 샷이있는 외부 사이트로 연결되는 링크 또는 연결되지 않는 링크. 질문에 형식이 지정된 텍스트뿐만 아니라 소스 코드도 포함하십시오! – GhostCat
또한 질문에 직접 스크린 샷을 추가 할 수 있습니다. –