// ----------------------------------------------------------------------------------- public OpcEnumItemAttributes CreateAttrEnumerator() { Type typEnuAtt = typeof(IEnumOPCItemAttributes); Guid guidEnuAtt = typEnuAtt.GUID; object objtemp; int hresult = ifItems.CreateEnumerator(ref guidEnuAtt, out objtemp); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } if ((hresult == HRESULTS.S_FALSE) || (objtemp == null)) { return(null); } IEnumOPCItemAttributes ifenu = (IEnumOPCItemAttributes)objtemp; objtemp = null; OpcEnumItemAttributes enu = new OpcEnumItemAttributes(ifenu); return(enu); }
public void QueryAvailableLocaleIDs(out int[] lcids) { int num; IntPtr ptr; lcids = null; int hresultcode = this.ifCommon.QueryAvailableLocaleIDs(out num, out ptr); if (HRESULTS.Failed(hresultcode)) { Marshal.ThrowExceptionForHR(hresultcode); } if (((int)ptr) != 0) { if (num < 1) { Marshal.FreeCoTaskMem(ptr); } else { lcids = new int[num]; Marshal.Copy(ptr, lcids, 0, num); Marshal.FreeCoTaskMem(ptr); } } }
public bool Write(int[] arrHSrv, object[] arrVal, int transactionID, out int cancelID, out int[] arrErr) { arrErr = null; cancelID = 0; int count = arrHSrv.Length; if (count != arrVal.Length) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } IntPtr ptrErr; int hresult = ifAsync.Write(count, arrHSrv, arrVal, transactionID, out cancelID, out ptrErr); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } arrErr = new int[count]; Marshal.Copy(ptrErr, arrErr, 0, count); Marshal.FreeCoTaskMem(ptrErr); return(hresult == HRESULTS.S_OK); }
public void QueryAvailableLocaleIDs(out int[] lcids) { if (ifCommon == null) { throw (new Exception("服务器没有实现IOPCCommon接口,无法调用QueryAvailableLocaleIDs")); } lcids = null; int count; IntPtr ptrIds; int hresult = ifCommon.QueryAvailableLocaleIDs(out count, out ptrIds); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } if (((int)ptrIds) == 0) { return; } if (count < 1) { Marshal.FreeCoTaskMem(ptrIds); return; } lcids = new int[count]; Marshal.Copy(ptrIds, lcids, 0, count); Marshal.FreeCoTaskMem(ptrIds); }
public bool SetDatatypes(int[] arrHSrv, VarEnum[] arrVT, out int[] arrErr) { IntPtr ptr2; arrErr = null; int length = arrHSrv.Length; if (length != arrVT.Length) { Marshal.ThrowExceptionForHR(-2147467260); } IntPtr pRequestedDatatypes = Marshal.AllocCoTaskMem(length * 2); int num2 = (int)pRequestedDatatypes; foreach (VarEnum enum2 in arrVT) { Marshal.WriteInt16((IntPtr)num2, (short)enum2); num2 += 2; } int hresultcode = this.ifItems.SetDatatypes(length, arrHSrv, pRequestedDatatypes, out ptr2); Marshal.FreeCoTaskMem(pRequestedDatatypes); if (HRESULTS.Failed(hresultcode)) { Marshal.ThrowExceptionForHR(hresultcode); } arrErr = new int[length]; Marshal.Copy(ptr2, arrErr, 0, length); Marshal.FreeCoTaskMem(ptr2); return(hresultcode == 0); }
public bool Request(string req, object val) { bool bOK = false; //ErrorReportEngine rptEngine = new ErrorReportEngine(); if (SioGrp == null) { return(false); } try { int rtc = SioGrp.Write(req, val); if (HRESULTS.Failed(rtc)) { bOK = false; // MethodDebug(req.ItemID); //Console.WriteLine("Write failed with error 0x" + rtc.ToString("X")); // rptEngine.TESTPURPOSEOPCLOG("Write failed with error 0x" + rtc.ToString("X")); } else { bOK = true; } } catch (Exception errmsg) { bOK = false; } return(bOK); }
/// <summary> /// Async write /// </summary> /// <param name="serverHandles">List of server items handles for the items to be written</param> /// <param name="values">List of values to be written. The value data types /// are not required to match the requested or canonical item datatype but /// must be ‘convertible’ to the canonical type.</param> /// <param name="transactionID">The Client generated transaction ID. /// This is included in the ‘completion’ information provided to the OnWriteComplete.</param> /// <param name="cancelID">Place to return a Server generated ID to be used in case /// the operation needs to be canceled.</param> /// <param name="errors">Array of errors for each item - returned by the server.</param> /// <returns>Bool</returns> public bool Write(int[] serverHandles, object[] values, int transactionID, out int cancelID, out int[] errors) { ThrowIfDisposed(); errors = null; cancelID = 0; if (serverHandles.Length != values.Length) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } int hResult = opcAsyncIO.Write(serverHandles.Length, serverHandles, values, transactionID, out cancelID, out IntPtr ptrErr); if (HRESULTS.Failed(hResult)) { Marshal.ThrowExceptionForHR(hResult); } if (ptrErr != null && ptrErr != IntPtr.Zero) { errors = new int[serverHandles.Length]; Marshal.Copy(ptrErr, errors, 0, errors.Length); Marshal.FreeCoTaskMem(ptrErr); } return(hResult == HRESULTS.S_OK); }
public string escrita(string item, string valorAescrever) { string aux = ""; srwGroup.Write(item, valorAescrever); OPCItemState resultadoLeitura; int auxRtc = srwGroup.Read(OPCDATASOURCE.OPC_DS_DEVICE, item, out resultadoLeitura); if (HRESULTS.Failed(auxRtc)) { aux = "Erro na Leitura: " + resultado(auxRtc); } else { aux = resultadoLeitura.DataValue.ToString(); } if (!valorAescrever.Equals(aux)) { aux = "Erro"; } return(aux); }
/// <summary> /// Query the LocaleIDs supported by the OPC Server. /// </summary> /// <returns>An array with LocaleIDs</returns> public int[] QueryAvailableLocaleIDs() { ThrowIfDisposed(); int[] lcids; IntPtr ptrIds = IntPtr.Zero; try { int hResult = opcCommon.QueryAvailableLocaleIDs(out int count, out ptrIds); if (HRESULTS.Failed(hResult)) { Marshal.ThrowExceptionForHR(hResult); } if (((int)ptrIds) == 0) { return(new int[0]); } if (count < 1) { return(new int[0]); } lcids = new int[count]; Marshal.Copy(ptrIds, lcids, 0, count); } finally { if (ptrIds != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptrIds); } } return(lcids); }
/// <summary> /// Get item property values /// </summary> /// <param name="itemID">Item ID</param> /// <param name="propertyIDs">Property IDs to get values for</param> /// <returns>Property data</returns> public OpcPropertyData[] GetItemProperties(string itemID, int[] propertyIDs) { ThrowIfDisposed(); int count = propertyIDs.Length; if (count < 1) { return(new OpcPropertyData[0]); } IntPtr ptrDat = IntPtr.Zero; IntPtr ptrErr = IntPtr.Zero; try { int hresult = opcItemProperties.GetItemProperties(itemID, count, propertyIDs, out ptrDat, out ptrErr); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } if ((ptrDat == IntPtr.Zero) || (ptrErr == IntPtr.Zero)) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } OpcPropertyData[] propertiesData = new OpcPropertyData[count]; for (int i = 0; i < count; i++) { propertiesData[i] = new OpcPropertyData { PropertyID = propertyIDs[i], Error = Extensions.ReadInt32(ptrErr, i) }; if (propertiesData[i].Error == HRESULTS.S_OK) { propertiesData[i].Data = Extensions.GetObjectForNativeVariant(ptrDat, i); Extensions.VariantClear(ptrDat, i); } else { propertiesData[i].Data = null; } } return(propertiesData); } finally { if (ptrDat != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptrDat); } if (ptrErr != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptrErr); } } }
// ----------------------------------------------------------------------------------- public bool SetDatatypes(int[] arrHSrv, VarEnum[] arrVT, out int[] arrErr) { arrErr = null; int count = arrHSrv.Length; if (count != arrVT.Length) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } IntPtr ptrVT = Marshal.AllocCoTaskMem(count * 2); int runVT = (int)ptrVT; foreach (VarEnum v in arrVT) { Marshal.WriteInt16((IntPtr)runVT, (short)v); runVT += 2; } IntPtr ptrErr; int hresult = ifItems.SetDatatypes(count, arrHSrv, ptrVT, out ptrErr); Marshal.FreeCoTaskMem(ptrVT); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } arrErr = new int[count]; Marshal.Copy(ptrErr, arrErr, 0, count); Marshal.FreeCoTaskMem(ptrErr); return(hresult == HRESULTS.S_OK); }
public bool LookupItemIDs(string itemID, int[] propertyIDs, out OPCPropertyItem[] propertyItems) { propertyItems = null; int count = propertyIDs.Length; if (count < 1) { return(false); } IntPtr ptrErr; IntPtr ptrIds; int hresult = ifItmProps.LookupItemIDs(itemID, count, propertyIDs, out ptrIds, out ptrErr); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } int runIds = (int)ptrIds; int runErr = (int)ptrErr; if ((runIds == 0) || (runErr == 0)) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } propertyItems = new OPCPropertyItem[count]; IntPtr ptrString; for (int i = 0; i < count; i++) { propertyItems[i] = new OPCPropertyItem(); propertyItems[i].PropertyID = propertyIDs[i]; propertyItems[i].Error = Marshal.ReadInt32((IntPtr)runErr); runErr += 4; if (propertyItems[i].Error == HRESULTS.S_OK) { ptrString = (IntPtr)Marshal.ReadInt32((IntPtr)runIds); propertyItems[i].newItemID = Marshal.PtrToStringUni(ptrString); Marshal.FreeCoTaskMem(ptrString); } else { propertyItems[i].newItemID = null; } runIds += 4; } Marshal.FreeCoTaskMem(ptrIds); Marshal.FreeCoTaskMem(ptrErr); return(hresult == HRESULTS.S_OK); }
// ------------------------ IOPCSyncIO --------------- public bool Read(OPCDATASOURCE src, int[] arrHSrv, out OPCItemState[] arrStat) { arrStat = null; int count = arrHSrv.Length; IntPtr ptrStat; IntPtr ptrErr; int hresult = ifSync.Read(src, count, arrHSrv, out ptrStat, out ptrErr); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } int runErr = (int)ptrErr; int runStat = (int)ptrStat; if ((runErr == 0) || (runStat == 0)) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } arrStat = new OPCItemState[count]; for (int i = 0; i < count; i++) { // WORKAROUND !!! arrStat[i] = new OPCItemState(); arrStat[i].Error = Marshal.ReadInt32((IntPtr)runErr); runErr += 4; arrStat[i].HandleClient = Marshal.ReadInt32((IntPtr)runStat); if (HRESULTS.Succeeded(arrStat[i].Error)) { short vt = Marshal.ReadInt16((IntPtr)(runStat + 16)); if (vt == (short)VarEnum.VT_ERROR) { arrStat[i].Error = Marshal.ReadInt32((IntPtr)(runStat + 24)); } arrStat[i].TimeStamp = Marshal.ReadInt64((IntPtr)(runStat + 4)); arrStat[i].Quality = Marshal.ReadInt16((IntPtr)(runStat + 12)); arrStat[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(runStat + 16)); DUMMY_VARIANT.VariantClear((IntPtr)(runStat + 16)); } else { arrStat[i].DataValue = null; } runStat += 32; } Marshal.FreeCoTaskMem(ptrStat); Marshal.FreeCoTaskMem(ptrErr); return(hresult == HRESULTS.S_OK); }
public bool GetItemProperties(string itemID, int[] propertyIDs, out OPCPropertyData[] propertiesData) { propertiesData = null; int count = propertyIDs.Length; if (count < 1) { return(false); } IntPtr ptrDat; IntPtr ptrErr; int hresult = ifItmProps.GetItemProperties(itemID, count, propertyIDs, out ptrDat, out ptrErr); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } int runDat = (int)ptrDat; int runErr = (int)ptrErr; if ((runDat == 0) || (runErr == 0)) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } propertiesData = new OPCPropertyData[count]; for (int i = 0; i < count; i++) { propertiesData[i] = new OPCPropertyData(); propertiesData[i].PropertyID = propertyIDs[i]; propertiesData[i].Error = Marshal.ReadInt32((IntPtr)runErr); runErr += 4; if (propertiesData[i].Error == HRESULTS.S_OK) { propertiesData[i].Data = Marshal.GetObjectForNativeVariant((IntPtr)runDat); DUMMY_VARIANT.VariantClear((IntPtr)runDat); } else { propertiesData[i].Data = null; } runDat += DUMMY_VARIANT.ConstSize; } Marshal.FreeCoTaskMem(ptrDat); Marshal.FreeCoTaskMem(ptrErr); return(hresult == HRESULTS.S_OK); }
public void TestFailed01() { //Test Procedure Call bool b = HRESULTS.Failed(HRESULTS.OPC_E_INVALIDITEMID); //Post Condition Check Assert.IsTrue(b); b = HRESULTS.Failed(HRESULTS.S_OK); //Post Condition Check Assert.IsFalse(b); }
/// <summary> /// Read values /// </summary> /// <param name="dataSource">Read data source</param> /// <param name="serverHandles">Server handles of OPC items to read</param> /// <param name="itemStates">OPC Item states array</param> /// <returns>Bool</returns> public bool Read(OPCDATASOURCE dataSource, int[] serverHandles, out OpcItemState[] itemStates) { ThrowIfDisposed(); itemStates = null; int hResult = opcSyncIO.Read(dataSource, serverHandles.Length, serverHandles, out IntPtr ptrStat, out IntPtr ptrErr); if (HRESULTS.Failed(hResult)) { Marshal.ThrowExceptionForHR(hResult); } if ((ptrErr == IntPtr.Zero) || (ptrStat == IntPtr.Zero)) { Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT); } try { itemStates = new OpcItemState[serverHandles.Length]; for (int i = 0; i < serverHandles.Length; i++) { itemStates[i] = new OpcItemState { Error = Extensions.ReadInt32(ptrErr, i), HandleClient = Marshal.ReadInt32(ptrStat, i * sizeOPCREADRESULT) }; if (HRESULTS.Succeeded(itemStates[i].Error)) { short vt = Marshal.ReadInt16(ptrStat, i * sizeOPCREADRESULT + 16); if (vt == (short)VarEnum.VT_ERROR) { itemStates[i].Error = Marshal.ReadInt32(ptrStat, i * sizeOPCREADRESULT + 24); } itemStates[i].TimeStamp = Marshal.ReadInt64(ptrStat, i * sizeOPCREADRESULT + 4); itemStates[i].Quality = Marshal.ReadInt16(ptrStat, i * sizeOPCREADRESULT + 12); itemStates[i].DataValue = Marshal.GetObjectForNativeVariant(IntPtr.Add(ptrStat, i * sizeOPCREADRESULT + 16)); Extensions.VariantClear(IntPtr.Add(ptrStat, i * sizeOPCREADRESULT + 16)); } else { itemStates[i].DataValue = null; } } } finally { Marshal.FreeCoTaskMem(ptrStat); Marshal.FreeCoTaskMem(ptrErr); } return(hResult == HRESULTS.S_OK); }
//----------------------------------------------------- // Thread that dequeues the requests and makes the OPC server access private void OpcAccessThread() { for (; ;) // Thread loop { if (Requests.Count() > 0) { OpcRequest req = Requests.Remove(); // An item value is read and displayed in the defined TextBox. if (req.Cmd == Command.Read) { OPCItemState val; int rtc = SioGrp.Read(OPCDATASOURCE.OPC_DS_CACHE, req.ItemID, out val); if (HRESULTS.Failed(rtc)) { Console.WriteLine("Read failed with error 0x" + rtc.ToString("X")); } else { if (HRESULTS.Failed(val.Error)) { Console.WriteLine("Read failed with error 0x" + val.Error.ToString("X")); } else { Console.WriteLine(val.DataValue.ToString(), req.TxtBox); } } } // A value is written to the OPC server item else if (req.Cmd == Command.Write) { int rtc = SioGrp.Write(req.ItemID, req.Val); if (HRESULTS.Failed(rtc)) { Console.WriteLine("Write failed with error 0x" + rtc.ToString("X")); } } } Thread.Sleep(100); // ms if (StopThread != null) // Thread kill request { StopThread.Set(); return; // terminate the thread } } }
public bool LookupItemIDs(string itemID, int[] propertyIDs, out OPCPropertyItem[] propertyItems) { IntPtr ptr; IntPtr ptr2; propertyItems = null; int length = propertyIDs.Length; if (length < 1) { return(false); } int hresultcode = this.ifItmProps.LookupItemIDs(itemID, length, propertyIDs, out ptr2, out ptr); if (HRESULTS.Failed(hresultcode)) { Marshal.ThrowExceptionForHR(hresultcode); } int num3 = (int)ptr2; int num4 = (int)ptr; if ((num3 == 0) || (num4 == 0)) { Marshal.ThrowExceptionForHR(-2147467260); } propertyItems = new OPCPropertyItem[length]; for (int i = 0; i < length; i++) { propertyItems[i] = new OPCPropertyItem(); propertyItems[i].PropertyID = propertyIDs[i]; propertyItems[i].Error = Marshal.ReadInt32((IntPtr)num4); num4 += 4; if (propertyItems[i].Error == 0) { IntPtr ptr3 = (IntPtr)Marshal.ReadInt32((IntPtr)num3); propertyItems[i].newItemID = Marshal.PtrToStringUni(ptr3); Marshal.FreeCoTaskMem(ptr3); } else { propertyItems[i].newItemID = null; } num3 += 4; } Marshal.FreeCoTaskMem(ptr2); Marshal.FreeCoTaskMem(ptr); return(hresultcode == 0); }
public bool Read(OPCDATASOURCE src, int[] arrHSrv, out OPCItemState[] arrStat) { IntPtr ptr; IntPtr ptr2; arrStat = null; int length = arrHSrv.Length; int hresultcode = this.ifSync.Read(src, length, arrHSrv, out ptr, out ptr2); if (HRESULTS.Failed(hresultcode)) { Marshal.ThrowExceptionForHR(hresultcode); } int num3 = (int)ptr2; int num4 = (int)ptr; if ((num3 == 0) || (num4 == 0)) { Marshal.ThrowExceptionForHR(-2147467260); } arrStat = new OPCItemState[length]; for (int i = 0; i < length; i++) { arrStat[i] = new OPCItemState(); arrStat[i].Error = Marshal.ReadInt32((IntPtr)num3); num3 += 4; arrStat[i].HandleClient = Marshal.ReadInt32((IntPtr)num4); if (HRESULTS.Succeeded(arrStat[i].Error)) { if (Marshal.ReadInt16((IntPtr)(num4 + 0x10)) == 10) { arrStat[i].Error = Marshal.ReadInt32((IntPtr)(num4 + 0x18)); } arrStat[i].TimeStamp = Marshal.ReadInt64((IntPtr)(num4 + 4)); arrStat[i].Quality = Marshal.ReadInt16((IntPtr)(num4 + 12)); arrStat[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(num4 + 0x10)); DUMMY_VARIANT.VariantClear((IntPtr)(num4 + 0x10)); } else { arrStat[i].DataValue = null; } num4 += 0x20; } Marshal.FreeCoTaskMem(ptr); Marshal.FreeCoTaskMem(ptr2); return(hresultcode == 0); }
public bool GetItemProperties(string itemID, int[] propertyIDs, out OPCPropertyData[] propertiesData) { IntPtr ptr; IntPtr ptr2; propertiesData = null; int length = propertyIDs.Length; if (length < 1) { return(false); } int hresultcode = this.ifItmProps.GetItemProperties(itemID, length, propertyIDs, out ptr, out ptr2); if (HRESULTS.Failed(hresultcode)) { Marshal.ThrowExceptionForHR(hresultcode); } int num3 = (int)ptr; int num4 = (int)ptr2; if ((num3 == 0) || (num4 == 0)) { Marshal.ThrowExceptionForHR(-2147467260); } propertiesData = new OPCPropertyData[length]; for (int i = 0; i < length; i++) { propertiesData[i] = new OPCPropertyData(); propertiesData[i].PropertyID = propertyIDs[i]; propertiesData[i].Error = Marshal.ReadInt32((IntPtr)num4); num4 += 4; if (propertiesData[i].Error == 0) { propertiesData[i].Data = Marshal.GetObjectForNativeVariant((IntPtr)num3); DUMMY_VARIANT.VariantClear((IntPtr)num3); } else { propertiesData[i].Data = null; } num3 += DUMMY_VARIANT.ConstSize; } Marshal.FreeCoTaskMem(ptr); Marshal.FreeCoTaskMem(ptr2); return(hresultcode == 0); }
/// <summary> /// Add given item to OPC items group /// </summary> /// <param name="itemName"></param> private void AddItemToGroup(string itemName) { try { int rtc; rtc = AsyncRefrGroup.Add(itemName); if (HRESULTS.Failed(rtc)) { DBLogging.InsertLogs("Exception: AddItemToGroup", false, "ReadWriteGroup.GetErrorString(rtc) " + itemName, _connStr); } } catch (Exception ex) { DBLogging.InsertLogs("Exception: AddItemToGroup", false, "ReadWriteGroup.GetErrorString(rtc) " + ex.Message, _connStr); } }
public string leitura(string s) { string aux; OPCItemState resultadoLeitura; int auxRtc = srwGroup.Read(OPCDATASOURCE.OPC_DS_DEVICE, s, out resultadoLeitura); if (HRESULTS.Failed(auxRtc)) { aux = "Erro na Leitura: " + resultado(auxRtc); } else { aux = resultadoLeitura.DataValue.ToString(); } return(aux); }
/// <summary> /// Set active state /// </summary> /// <param name="serverHandles">Server handles of OPC items to set active</param> /// <param name="activate">Activate</param> /// <param name="errors">Errors set active items</param> /// <returns>Bool</returns> public bool SetActiveState(int[] serverHandles, bool activate, out int[] errors) { ThrowIfDisposed(); errors = null; int hResult = opcItemMgt.SetActiveState(serverHandles.Length, serverHandles, activate, out IntPtr ptrErr); if (HRESULTS.Failed(hResult)) { Marshal.ThrowExceptionForHR(hResult); } errors = new int[serverHandles.Length]; Marshal.Copy(ptrErr, errors, 0, errors.Length); Marshal.FreeCoTaskMem(ptrErr); return(hResult == HRESULTS.S_OK); }
public bool SetActiveState(int[] arrHSrv, bool activate, out int[] arrErr) { IntPtr ptr; arrErr = null; int length = arrHSrv.Length; int hresultcode = this.ifItems.SetActiveState(length, arrHSrv, activate, out ptr); if (HRESULTS.Failed(hresultcode)) { Marshal.ThrowExceptionForHR(hresultcode); } arrErr = new int[length]; Marshal.Copy(ptr, arrErr, 0, length); Marshal.FreeCoTaskMem(ptr); return(hresultcode == 0); }
// ----------------------------------------------------------------------------------- public bool SetActiveState(int[] arrHSrv, bool activate, out int[] arrErr) { arrErr = null; int count = arrHSrv.Length; IntPtr ptrErr; int hresult = ifItems.SetActiveState(count, arrHSrv, activate, out ptrErr); if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } arrErr = new int[count]; Marshal.Copy(ptrErr, arrErr, 0, count); Marshal.FreeCoTaskMem(ptrErr); return(hresult == HRESULTS.S_OK); }
public bool Read(int[] arrHSrv, int transactionID, out int cancelID, out int[] arrErr) { IntPtr ptr; arrErr = null; cancelID = 0; int length = arrHSrv.Length; int hresultcode = this.ifAsync.Read(length, arrHSrv, transactionID, out cancelID, out ptr); if (HRESULTS.Failed(hresultcode)) { Marshal.ThrowExceptionForHR(hresultcode); } arrErr = new int[length]; Marshal.Copy(ptr, arrErr, 0, length); Marshal.FreeCoTaskMem(ptr); return(hresultcode == 0); }
/// <summary> /// Async read values /// </summary> /// <param name="serverHandles">Server handles of OPC items to read</param> /// <param name="transactionID">The Client generated transaction ID. /// This is included in the ‘completion’ information provided to the OnWriteComplete.</param> /// <param name="cancelID">Place to return a Server generated ID to be used in case /// the operation needs to be canceled.</param> /// <param name="errors">Errors</param> /// <returns>Bool</returns> public bool Read(int[] serverHandles, int transactionID, out int cancelID, out int[] errors) { ThrowIfDisposed(); errors = null; cancelID = 0; int hResult = opcAsyncIO.Read(serverHandles.Length, serverHandles, transactionID, out cancelID, out IntPtr ptrErr); if (HRESULTS.Failed(hResult)) { Marshal.ThrowExceptionForHR(hResult); } errors = new int[serverHandles.Length]; Marshal.Copy(ptrErr, errors, 0, errors.Length); Marshal.FreeCoTaskMem(ptrErr); return(hResult == HRESULTS.S_OK); }
/// <summary> /// Create OPC item attributes enumerator /// </summary> /// <returns>OPC item attributes enumerator</returns> public IEnumerator <OpcItemAttributes> GetEnumerator() { ThrowIfDisposed(); Guid guidEnuAtt = typeof(IEnumOPCItemAttributes).GUID; int hResult = opcItemMgt.CreateEnumerator(ref guidEnuAtt, out object objtemp); if (HRESULTS.Failed(hResult)) { Marshal.ThrowExceptionForHR(hResult); } if ((hResult == HRESULTS.S_FALSE) || (objtemp == null)) { return(null); } return(new OpcEnumItemAttributes(objtemp as IEnumOPCItemAttributes)); }
// event handler: called if asynch write finished protected void theGrp_WriteComplete(object sender, WriteCompleteEventArgs e) { foreach (OPCWriteResult w in e.res) { if (w.HandleClient != itmHandleClient) // only one client handle { continue; } if (HRESULTS.Failed(w.Error)) { txtItemWriteRes.Text = "ERROR 0x" + w.Error.ToString("X"); } else { txtItemWriteRes.Text = "ok"; } } }
// ------------------------ IOPCAsyncIO2 --------------- public bool Read(int[] arrHSrv, int transactionID, out int cancelID, out int[] arrErr) { arrErr = null; cancelID = 0; int count = arrHSrv.Length; IntPtr ptrErr; int hresult = ifAsync.Read(count, arrHSrv, transactionID, out cancelID, out ptrErr); // if (HRESULTS.Failed(hresult)) { Marshal.ThrowExceptionForHR(hresult); } arrErr = new int[count]; Marshal.Copy(ptrErr, arrErr, 0, count); Marshal.FreeCoTaskMem(ptrErr); return(hresult == HRESULTS.S_OK); }