C# OPCUA 读写结构体
时间: 2023-12-06 20:38:15 浏览: 196
以下是C# OPCUA读写结构体的示例代码:
```csharp
// 定义结构体
[DataContract(Namespace = "http://your-namespace-here")]
public class YourStruct
{
[DataMember(Order = 1)]
public int IntValue { get; set; }
[DataMember(Order = 2)]
public string StringValue { get; set; }
}
// 读取结构体
var nodeToRead = new ReadValueId
{
NodeId = new NodeId("YourNodeIdHere", 1), // 结构体的NodeId
AttributeId = Attributes.Value
};
var readRequest = new ReadRequest
{
NodesToRead = new[] { nodeToRead }
};
var readResponse = await _session.ReadAsync(readRequest);
var extensionObject = (ExtensionObject)readResponse.Results[0].Value;
var yourStruct = extensionObject.Body as YourStruct;
// 写入结构体
var nodeToWrite = new WriteValue
{
NodeId = new NodeId("YourNodeIdHere", 1), // 结构体的NodeId
AttributeId = Attributes.Value,
Value = new DataValue(new ExtensionObject(new YourStruct
{
IntValue = 123,
StringValue = "Hello World"
}))
};
var writeRequest = new WriteRequest
{
NodesToWrite = new[] { nodeToWrite }
};
var writeResponse = await _session.WriteAsync(writeRequest);
```
阅读全文