/// <summary> /// Creates an array of item value result objects from the callback data. /// </summary> internal static DaValue[] GetItemValues( int dwCount, object[] pvValues, short[] pwQualities, System.Runtime.InteropServices.ComTypes.FILETIME[] pftTimeStamps, int[] pErrors) { // contruct the item value results. DaValue[] values = new DaValue[dwCount]; for (int ii = 0; ii < dwCount; ii++) { DaValue value = values[ii] = new DaValue(); value.Error = pErrors[ii]; if (pErrors[ii] >= 0) { value.Value = ComUtils.ProcessComValue(pvValues[ii]); value.Quality = pwQualities[ii]; value.Timestamp = ComUtils.GetDateTime(pftTimeStamps[ii]); } } // return results return(values); }
/// <summary> /// Unmarshals and deallocates a OPCITEMSTATE structures. /// </summary> internal static DaValue[] GetItemValues(ref IntPtr pInput, int count, bool deallocate) { DaValue[] output = null; if (pInput != IntPtr.Zero && count > 0) { output = new DaValue[count]; IntPtr pos = pInput; for (int ii = 0; ii < count; ii++) { OpcRcw.Da.OPCITEMSTATE result = (OpcRcw.Da.OPCITEMSTATE)Marshal.PtrToStructure(pos, typeof(OpcRcw.Da.OPCITEMSTATE)); DaValue value = new DaValue(); value.Value = ComUtils.ProcessComValue(result.vDataValue); value.Quality = result.wQuality; value.Timestamp = ComUtils.GetDateTime(result.ftTimeStamp); output[ii] = value; if (deallocate) { Marshal.DestroyStructure(pos, typeof(OpcRcw.Da.OPCITEMSTATE)); } pos = (IntPtr)(pos.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMSTATE))); } if (deallocate) { Marshal.FreeCoTaskMem(pInput); pInput = IntPtr.Zero; } } return(output); }
/// <summary> /// Reads the values of the properties from the server. /// </summary> /// <param name="itemId">The item id.</param> /// <param name="propertyIds">The list of property ids to read. All properties are read if null.</param> /// <returns>True if the element has properies.</returns> private DaValue[] ReadPropertyValues(string itemId, params int[] propertyIds) { string methodName = "IOPCItemProperties.GetItemProperties"; // check for trivial case. if (propertyIds == null) { return(null); } int count = propertyIds.Length; DaValue[] results = new DaValue[count]; // check for empty list. if (count == 0) { return(results); } // get the values from the server. IntPtr pValues = IntPtr.Zero; IntPtr pErrors = IntPtr.Zero; try { IOPCItemProperties server = BeginComCall <IOPCItemProperties>(methodName, true); server.GetItemProperties( itemId, count, propertyIds, out pValues, out pErrors); } catch (Exception e) { if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL, ResultIds.E_UNKNOWNITEMID, ResultIds.E_INVALIDITEMID)) { ComUtils.TraceComError(e, methodName); return(null); } for (int ii = 0; ii < count; ii++) { DaValue result = results[ii] = new DaValue(); result.Value = null; result.Quality = OpcRcw.Da.Qualities.OPC_QUALITY_GOOD; result.Timestamp = DateTime.UtcNow; result.Error = Marshal.GetHRForException(e); } return(results); } finally { EndComCall(methodName); } // unmarshal results. object[] values = ComUtils.GetVARIANTs(ref pValues, count, true); int[] errors = ComUtils.GetInt32s(ref pErrors, count, true); for (int ii = 0; ii < count; ii++) { DaValue result = results[ii] = new DaValue(); result.Value = ComUtils.ProcessComValue(values[ii]); result.Quality = OpcRcw.Da.Qualities.OPC_QUALITY_GOOD; result.Timestamp = DateTime.UtcNow; result.Error = errors[ii]; } return(results); }