/// <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); }