/// <summary> /// Casts result of monitoring and reading values /// </summary> /// <param name="value">Value to convert</param> /// <param name="casted">The casted result</param> /// <typeparam name="T">Type of object to try to cast</typeparam> public void TryCastResult <T>(object value, out T casted) { try { casted = (T)ConvertToT.ConvertT <T>(value); } catch (InvalidCastException ex) { throw new InvalidCastException( string.Format( "Could not monitor tag. Cast failed for type \"{0}\" on the new value \"{1}\" with type \"{2}\". Make sure tag data type matches.", typeof(T), value, value.GetType())); } }
/// <summary> /// Read a tag /// </summary> /// <typeparam name="T">The type of tag to read</typeparam> /// <param name="tag">The fully-qualified identifier of the tag. You can specify a subfolder by using a comma delimited name. /// E.g: the tag `foo.bar` reads the tag `bar` on the folder `foo`</param> /// <returns>The value retrieved from the OPC</returns> public T Read <T>(string tag) { var nodesToRead = BuildReadValueIdCollection(new string[] { tag }, Attributes.Value); DataValueCollection results; DiagnosticInfoCollection diag; _session.Read( requestHeader: null, maxAge: 0, timestampsToReturn: TimestampsToReturn.Neither, nodesToRead: nodesToRead, results: out results, diagnosticInfos: out diag); var val = results[0]; CheckReturnValue(val.StatusCode); return((T)ConvertToT.ConvertT <T>(val.Value)); }