[英]Decode binary ExtensionObject
我正在測試Eclipse Milo(0.1.5版)以作為客戶端與TwinCAT PLC的OPC UA服務器進行通信。 我的Java程序充當一種中間件。 它從TwinCAT OPC UA服務器讀取結構,將其值放入JSON並將其發送到其他服務器。
為了進行測試,我在PLC中創建了一個帶有兩個變量的示例結構:
stSimpleStruct(PLC中的結構定義)
-bVar1(布爾值)
-fVar2(浮動)
TwinCAT中的Struct圖片
如果我讀取該節點,則該值是一個擴展對象,編碼為ByteString。 應該讀取結構變量(bVar1和fVar2)的值,並將其放入JSON對象。 所以結果是這樣的:
{
“bVar1” : false,
“fVar2” : 0
}
上面的結構僅是示例。 僅在運行時知道要讀取的結構。 如何解碼二進制擴展對象以訪問結構變量的值?
這是我的代碼:
// 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();
Milo目前不支持自定義數據類型。
我有可以在服務器中讀取DataTypeDictionary節點並在運行時構建自定義編解碼器的有效代碼,這些自定義編解碼器已在DataTypeManager中注冊,但是仍未確定是否,何時以及如何發布此代碼。 無論如何,它都是針對Milo的0.2.0開發分支編寫的,該分支進行了更改以適應自定義DataType,因此至少直到那時它才可用。
您可以給我發送電子郵件,列表,也可以在gitter.im頻道中停下來,如果您想進一步討論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.