Eclipse Milo(バージョン0.1.5)をテストして、TwinCAT PLCのOPC UAサーバーとクライアントとして通信します。私のJavaプログラムは、ミドルウェアの一種として機能します。 TwinCAT OPC UAサーバから構造体を読み取り、その値をJSONに入れて他のサーバに送信します。 - bVar1(ブール値)
- fVar2(フロート)
Picture of Struct in TwinCATデコードバイナリExtensionObject
私がもし
stSimpleStruct(PLCにおける構造definiend)
:
は、テストのために私は2つの変数と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
また、直接あなたの質問にスクリーンショットを追加することができます –