Reputation: 41
I'm trying to read the date and time (#DT) from Opc ua Server (Siemens) using opc ua DLLs of unifiedautomation. But i get wrong value:
siemens S7 1500 opc ua client DT#2008-10-25-08:12:34.567 --> 17.09.1142 05:08:27
I'm using the following code:
var td = ReadValue(NodeId).ToByteArray();
long temp = BitConverter.ToInt64(td, 0);
DateTime dateTimeVar = new DateTime(temp);
ReadValue function:
public Variant ReadValue(string VariableIdentifier)
{
Variant data = new Variant();
List<DataValue> results = read(VariableIdentifier);
if (results.Count > 0)
{
if (StatusCode.IsGood(results[0].StatusCode))
{
data = results[0].WrappedValue;
m_OpcError = "OK";
}
else
{
m_OpcError = results[0].StatusCode.Message;
}
}
else
{
m_OpcError = "ReadValue function: it Couldn't read data from OPC UA Server (empty data)";
}
return data;
}
Upvotes: 3
Views: 2140
Reputation: 71
Just came across the same problem. You need the split the result in a byte array beforehand.
var bytes = new byte[] { 32, 6, 37, 20, 25, 4, 135, 37 };//Test 25.06.2020 14:19:04
//Remove BCD
List<int> vals = new List<int>();
foreach (var bcd in bytes)
{
int high = bcd >> 4;
int low = bcd & 0xF;
int number = 10 * high + low;
vals.Add(number);
}
//Create Datetime
DateTime dt = new DateTime(vals[0] + 2000, vals[1], vals[2], vals[3], vals[4], vals[5]);
Upvotes: 2