/// <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); }
/// <summary> /// 同步读 /// </summary> /// <param name="dataPointID"></param> /// <returns></returns> public object Read(int dataPointID) { IntPtr itemValues = IntPtr.Zero; IntPtr pErrors = IntPtr.Zero; try { OPCSyncIO.Read(OPCDATASOURCE.OPC_DS_DEVICE, 1, new int[] { OpcItems[dataPointID].ServerItemHandle }, out itemValues, out pErrors); int[] error = new int[1]; Marshal.Copy(pErrors, error, 0, error.Length); if (error[0] == 0) { OPCITEMSTATE itemState = (OPCITEMSTATE)Marshal.PtrToStructure(itemValues, typeof(OPCITEMSTATE)); return(itemState.vDataValue); } else { throw new ApplicationException($"数据点(ID:{dataPointID})同步读取失败。"); } } catch (Exception ex) { throw ex; } finally { if (itemValues != IntPtr.Zero) { Marshal.FreeCoTaskMem(itemValues); itemValues = IntPtr.Zero; } if (pErrors != IntPtr.Zero) { Marshal.FreeCoTaskMem(pErrors); pErrors = IntPtr.Zero; } } }
/*------------------------------------------------------ * Execute SyncRead * * (ret) True OK * False NG * ------------------------------------------------------*/ public bool SyncRead(OpcRcw.Da.OPCDATASOURCE DataSource, int[] ServerHd, object[] Values, OpcRcw.Da.FILETIME[] TimeStamps, short[] Qualities) { int iItemCount = ServerHd.Length; IOPCSyncIO OPCSyncIO; IOPCSyncIO2 OPCSyncIO2; IntPtr ppItemVal; IntPtr ppErrors; IntPtr posItem; int[] Errors = new int[iItemCount]; OPCITEMSTATE ItemState; int i; try { switch (m_OpcdaVer) { case DEF_OPCDA.VER_30: OPCSyncIO2 = (IOPCSyncIO2)m_OPCGroup2; OPCSyncIO2.Read(DataSource, iItemCount, ServerHd, out ppItemVal, out ppErrors); Marshal.Copy(ppErrors, Errors, 0, iItemCount); posItem = ppItemVal; for (i = 0; i < iItemCount; i++) { ItemState = (OPCITEMSTATE)Marshal.PtrToStructure(posItem, typeof(OPCITEMSTATE)); if (Errors[i] == 0) { Values[i] = ItemState.vDataValue; TimeStamps[i] = ItemState.ftTimeStamp; Qualities[i] = ItemState.wQuality; } Marshal.DestroyStructure(posItem, typeof(OPCITEMSTATE)); // 05/02/08 Release memory //posItem = new IntPtr(posItem.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE))); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak posItem = (IntPtr)(posItem.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMSTATE))); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak } Marshal.FreeCoTaskMem(ppItemVal); Marshal.FreeCoTaskMem(ppErrors); break; case DEF_OPCDA.VER_10: case DEF_OPCDA.VER_20: default: OPCSyncIO = (IOPCSyncIO)m_OPCGroup; OPCSyncIO.Read(DataSource, iItemCount, ServerHd, out ppItemVal, out ppErrors); Marshal.Copy(ppErrors, Errors, 0, iItemCount); posItem = ppItemVal; for (i = 0; i < iItemCount; i++) { ItemState = (OPCITEMSTATE)Marshal.PtrToStructure(posItem, typeof(OPCITEMSTATE)); if (Errors[i] == 0) { Values[i] = ItemState.vDataValue; TimeStamps[i] = ItemState.ftTimeStamp; Qualities[i] = ItemState.wQuality; } Marshal.DestroyStructure(posItem, typeof(OPCITEMSTATE)); // 05/02/08 Release memory //posItem = new IntPtr(posItem.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE))); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak posItem = (IntPtr)(posItem.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMSTATE))); // 1.0.0.5 10/06/21 Kishimoto Fixed for Memory Leak } Marshal.FreeCoTaskMem(ppItemVal); Marshal.FreeCoTaskMem(ppErrors); break; } } catch (Exception exc) { MessageBox.Show(exc.ToString(), "SyncRead"); return(false); } // erase Errors System.GC.Collect(); //04/12/29 Revised Memory Leak return(true); }