[英]Unable to build an array from a class in Xamarin.Forms with C#
I'm working on a Xamarin.Forms project with C# to connect to an OPC server and read values.我正在使用 C# 处理 Xamarin.Forms 项目以连接到 OPC 服务器并读取值。 I'm able to read the values, but I'm having trouble collating them into a list or array.
我能够读取这些值,但无法将它们整理到列表或数组中。 After I do, I'd like to convert the values to ASCII.
完成后,我想将值转换为 ASCII。
Below is the code that is passed;下面是通过的代码;
var readRequest = new ReadRequest
{
// set the NodesToRead to an array of ReadValueIds.
NodesToRead = new[] {
// construct a ReadValueId from a NodeId and AttributeId.
new ReadValueId {
// you can parse the nodeId from a string.
// e.g. NodeId.Parse("ns=2;s=Demo.Static.Scalar.Double")
NodeId = NodeId.Parse("ns=2;s=Link_CatConHybrid.2D.InStr1"),
//NodeId.Parse(VariableIds.Server_ServerStatus),
// variable class nodes have a Value attribute.
AttributeId = AttributeIds.Value
},
new ReadValueId
{
NodeId = NodeId.Parse("ns=2;s=Link_CatConHybrid.2D.InStr2"),
AttributeId = AttributeIds.Value
}
}
};
// send the ReadRequest to the server.
var readResult = await channel.ReadAsync(readRequest);
// DataValue is a class containing value, timestamps and status code.
// the 'Results' array returns DataValues, one for every ReadValueId.
DataValue dvr = readResult.Results[0];
DataValue dvr2 = readResult.Results[1];
Console.WriteLine("The value of Instr1 is {0}, InStr2 is {1}", dvr.Variant.Value, dvr2.Variant.Value);
What am I doing wrong or overlooking?我做错了什么或忽略了什么?
Edit: How would I combine all of the readResults
into one ?编辑:我如何将所有的
readResults
合并为一个?
Just create a DataValue
list and store them.只需创建一个
DataValue
列表并存储它们。 Try like:尝试像:
List<DataValue> endResult = new List<DataValue>();
foreach (DataValue value in readResult.Results)
{
endResult.Add(value);
}
Since Results is alerady collection of DataValue, you can just say由于结果是数据值的alerady集合,你可以说
var dataValueCollection = readResult.Results; // if you want return collection you can just say return readResult.Results
If you are trying to write the values to console, then you can have loop directly on readResult.Results as below:如果您尝试将值写入控制台,则可以直接在 readResult.Results 上循环,如下所示:
foreach(var dv in readResult.Results)
{
Console.WriteLine("The Value of InStr = {0}", dv.Variant.Value);
Console.WriteLine("The Value of InStr = {0}", dv.Variant.ReadValueId); // This line shows how to access ReadValueId.
// You can access other properties same as above
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.