/// <summary> /// 同步写 /// </summary> /// <param name="dataPointID"></param> /// <param name="value"></param> /// <returns></returns> public bool Write(int dataPointID, object value) { IntPtr pErrors = IntPtr.Zero; try { object[] obj = new object[] { value }; OPCSyncIO.Write(1, new int[] { OpcItems[dataPointID].ServerItemHandle }, obj, out pErrors); int[] errors = new int[1]; Marshal.Copy(pErrors, errors, 0, 1); if (errors[0] != 0) { throw new ApplicationException($"数据点(ID:{dataPointID})同步写入失败。"); } return(true); } catch (Exception ex) { throw ex; } finally { if (pErrors != IntPtr.Zero) { Marshal.FreeCoTaskMem(pErrors); pErrors = IntPtr.Zero; } } }
/*------------------------------------------------------ * Execute SyncWrite * * (ret) True OK * False NG * ------------------------------------------------------*/ public bool SyncWrite(int[] ServerHd, object[] Value) { int iItemCount = ServerHd.Length; IOPCSyncIO OPCSyncIO; IOPCSyncIO2 OPCSyncIO2; IntPtr ppErrors; int[] errors = new int[iItemCount]; try { switch (m_OpcdaVer) { case DEF_OPCDA.VER_30: OPCSyncIO2 = (IOPCSyncIO2)m_OPCGroup2; OPCSyncIO2.Write(iItemCount, ServerHd, Value, out ppErrors); Marshal.Copy(ppErrors, errors, 0, iItemCount); Marshal.FreeCoTaskMem(ppErrors); break; case DEF_OPCDA.VER_10: case DEF_OPCDA.VER_20: default: OPCSyncIO = (IOPCSyncIO)m_OPCGroup; OPCSyncIO.Write(iItemCount, ServerHd, Value, out ppErrors); Marshal.Copy(ppErrors, errors, 0, iItemCount); Marshal.FreeCoTaskMem(ppErrors); break; } } catch (Exception exc) { MessageBox.Show(exc.ToString(), "SyncWrite"); return(false); } // erase errors System.GC.Collect(); //04/12/29 Revised Memory Leak return(true); }