请教通过Simatic NET OPC连续读取多个PLC变量时的内存释放问题

已锁定

wuyang26

  • 帖子

    1
  • 精华

    0
  • 被关注

    0

论坛等级:新手

注册时间:2013-05-09

普通 普通 如何晋级?

请教通过Simatic NET OPC连续读取多个PLC变量时的内存释放问题

1567

1

2013-05-09 09:15:15

西门子Simatic NET 2010中提供的访问单个变量的例子如下:
IntPtr pItemValues = IntPtr.Zero;
IntPtr pErrors = IntPtr.Zero;
try
{
// Sync read from device
pIOPCSyncIO.Read(OPCDATASOURCE.OPC_DS_DEVICE, 1, nItemSvrID, out pItemValues, out pErrors);
// Unmarshal the returned memory to get the item state out fom the ppItemValues
// after checking errors
int[] errors = new int[1];
Marshal.Copy(pErrors, errors, 0, 1);
if (errors[0] == 0)
{
OPCITEMSTATE pItemState = (OPCITEMSTATE)Marshal.PtrToStructure(pItemValues, typeof(OPCITEMSTATE));
// update the UI
strReadVal.Text = String.Format("{0}", pItemState.vDataValue);
// quality
strSncQuality.Text = GetQuality(pItemState.wQuality);
// Time stamp - Date and time
DateTime dt = ToDateTime(pItemState.ftTimeStamp);
strSycTimeStamp.Text = dt.ToString();

// Free indirect variant element, other indirect elements are freed by Marshal.DestroyStructure(...)
DUMMY_VARIANT.VariantClear((IntPtr)((int)pItemValues + 0));
}
else
{
String pstrError;
pIOPCServer.GetErrorString(errors[0], LOCALE_ID, out pstrError);
MessageBox.Show(pstrError,
"Result - Read Items", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

// Free indirect structure elements
Marshal.DestroyStructure(pItemValues, typeof(OPCITEMSTATE));
}
catch(System.Exception error)
{
MessageBox.Show(error.Message,
"Result - Read Items", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// Free the unmanaged COM memory
if(pItemValues != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(pItemValues);
pItemValues = IntPtr.Zero;
}
if(pErrors != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(pErrors);
pErrors = IntPtr.Zero;
}
}

但找不到最新的连续访问多个变量的例子,以下是通过西门子下载中心搜到的老的simatic NET 访问多个例子的代码:
try
{
// Usually reading from CACHE is more efficient
// AND the underlying device has to do less communication work.
// BUT, you must have an active ITEM and GROUP, so that the opc server
// will update the data by itself; otherwise you'll get quality OUT_OF_SERVICE
m_OPCSyncIO.Read( OPCDataSrc, //OPCDATASOURCE.OPC_DS_DEVICE or OPCDATASOURCE.OPC_DS_CACHE,
NumItems,
itemServerHandles,
out pItemStates,
out pErrors);

if (pItemStates == IntPtr.Zero) throw new ArgumentNullException("EXT_OPC_RDSYNC_ITEMSTATES");
if (pErrors == IntPtr.Zero) throw new ArgumentNullException("EXT_OPC_RDSYNC_ERRORS");

//Evaluate return ErrorCodes to exclude possible Errors
errors = new int[NumItems];
Marshal.Copy(pErrors, errors, 0, NumItems);

IntPtr pos = pItemStates;

// Now get the read values and check errors
for(int dwCount = 0; dwCount < NumItems; dwCount++)
{
result[dwCount] = (OPCITEMSTATE) Marshal.PtrToStructure(pos, typeof(OPCITEMSTATE));

if (errors[dwCount] != 0)
{
m_OPCServer.GetErrorString(errors[dwCount], m_LocaleID, out ErrorStr);
System.Diagnostics.Debug.WriteLine(ErrorStr);
}

pos = (IntPtr)(pos.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE)));
}

if(ErrorStr != "")
{
throw new Exception(ErrorStr);
}

// Free allocated COM-ressouces
Marshal.FreeCoTaskMem(pItemStates);
Marshal.FreeCoTaskMem(pErrors);
}
catch (Exception)
{
// Free allocated COM-ressouces
Marshal.FreeCoTaskMem(pItemStates);
Marshal.FreeCoTaskMem(pErrors);

// just forward it
throw;
}

使用此代码测试,感觉内存并没有完全被释放,随着测试时间的增加,内存消耗会持续增加,不知目前有没有可靠的内存释放方式。
请教通过Simatic NET OPC连续读取多个PLC变量时的内存释放问题 已锁定
编辑推荐: 关闭

请填写推广理由:

本版热门话题

通信与网络组件

共有4537条技术帖

相关推荐

热门标签

相关帖子推荐

guzhang

恭喜,你发布的帖子

评为精华帖!

快扫描右侧二维码晒一晒吧!

再发帖或跟帖交流2条,就能晋升VIP啦!开启更多专属权限!

  • 分享

  • 只看
    楼主

top
X 图片
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。