public void Dispose() { int count = _items.Count; int[] arrSvr = new int[count]; for (int i = 0; i < count; i++) { arrSvr[i] = _items[i].Address.Start; } _items.Clear(); IntPtr ptrErr; try { int result = _itemMgt.RemoveItems(count, arrSvr, out ptrErr); Marshal.FreeCoTaskMem(ptrErr); if (_grpObj != null) { Marshal.ReleaseComObject(_grpObj); } } catch (Exception err) { } _grpObj = null; _itemMgt = null; _grpState = null; _async = null; _sync = null; }
/*------------------------------------------------------ * Disconnect OPC Server * * (ret) True OK * False NG * ------------------------------------------------------*/ public bool Disconnect() { if (m_OPCServer == null) { return(true); } int ret; try { Unadvise(); if (m_OPCGroup != null) { ret = Marshal.ReleaseComObject(m_OPCGroup); m_OPCGroup = null; } if (m_OPCGroup2 != null) { ret = Marshal.ReleaseComObject(m_OPCGroup2); m_OPCGroup2 = null; } if (m_OPCConnPoint != null) { ret = Marshal.ReleaseComObject(m_OPCConnPoint); m_OPCConnPoint = null; } // 2011/11/14 シャットダウンイベントを受けれるようにする ( if (m_iShutdownConnectionCookie != 0) { m_OpcShutdownConnectionPoint.Unadvise(m_iShutdownConnectionCookie); } m_iShutdownConnectionCookie = 0; if (m_OpcShutdownConnectionPoint != null) { Marshal.ReleaseComObject(m_OpcShutdownConnectionPoint); m_OpcShutdownConnectionPoint = null; } // 2011/11/14 シャットダウンイベントを受けれるようにする ) if (m_iServerGroup != 0) // 06/10/19 Fixed the timing of 'RemoveGroup'. { // m_OPCServer.RemoveGroup(m_iServerGroup, 0); // m_iServerGroup = 0; // } // ret = Marshal.ReleaseComObject(m_OPCServer); m_OPCServer = null; m_bConnect = false; return(true); } catch (Exception exc) { MessageBox.Show(exc.ToString(), "Disconnect"); m_bConnect = false; return(false); } }
/// <summary> /// Creates a new subscription. /// </summary> /// <param name="state">The initial state of the subscription.</param> /// <returns>The new subscription object.</returns> public ITsCDaSubscription CreateSubscription(TsCDaSubscriptionState state) { if (state == null) { throw new ArgumentNullException(nameof(state)); } lock (this) { if (m_server == null) { throw new NotConnectedException(); } string methodName = "IOPCServer.AddGroup"; // copy the subscription state. TsCDaSubscriptionState result = (TsCDaSubscriptionState)state.Clone(); // initialize arguments. Guid iid = typeof(IOPCItemMgt).GUID; object group = null; int serverHandle = 0; int revisedUpdateRate = 0; GCHandle hDeadband = GCHandle.Alloc(result.Deadband, GCHandleType.Pinned); // invoke COM method. try { IOPCServer server = BeginComCall <IOPCServer>(methodName, true); server.AddGroup( (result.Name != null) ? result.Name : "", (result.Active) ? 1 : 0, result.UpdateRate, 0, IntPtr.Zero, hDeadband.AddrOfPinnedObject(), Technosoftware.DaAeHdaClient.Com.Interop.GetLocale(result.Locale), out serverHandle, out revisedUpdateRate, ref iid, out group); } catch (Exception e) { ComCallError(methodName, e); throw Technosoftware.DaAeHdaClient.Utilities.Interop.CreateException(methodName, e); } finally { if (hDeadband.IsAllocated) { hDeadband.Free(); } EndComCall(methodName); } if (group == null) { throw new OpcResultException(OpcResult.E_FAIL, "The subscription was not created."); } methodName = "IOPCGroupStateMgt2.SetKeepAlive"; // set the keep alive rate if requested. try { int keepAlive = 0; IOPCGroupStateMgt2 comObject = BeginComCall <IOPCGroupStateMgt2>(group, methodName, true); comObject.SetKeepAlive(result.KeepAlive, out keepAlive); result.KeepAlive = keepAlive; } catch (Exception e1) { result.KeepAlive = 0; ComCallError(methodName, e1); } finally { EndComCall(methodName); } // save server handle. result.ServerHandle = serverHandle; // set the revised update rate. if (revisedUpdateRate > result.UpdateRate) { result.UpdateRate = revisedUpdateRate; } // create the subscription object. Technosoftware.DaAeHdaClient.Com.Da.Subscription subscription = CreateSubscription(group, result, filters_); // index by server handle. subscriptions_[serverHandle] = subscription; // return subscription. return(subscription); } }
/*------------------------------------------------------ * Add OPC Group * * (ret) True OK * False NG * ------------------------------------------------------*/ public bool AddGroup(string sGrpName, int iUpdateRate) { if (m_OPCServer == null) { return(false); } if (m_OPCGroup != null || m_iServerGroup != 0) { return(false); } object group = null; bool bActive = true; int iClientGroup = 0; //1234; IntPtr ptrTimeBias = IntPtr.Zero; IntPtr ptrDeadBand = IntPtr.Zero; int iLCID = 0; Guid guidGroupStateMgt; Guid guidDataCallback; int iRevisedUpdateRate; int iKeepAliveTime = 10000; try { switch (m_OpcdaVer) { case DEF_OPCDA.VER_30: guidGroupStateMgt = Marshal.GenerateGuidForType(typeof(IOPCGroupStateMgt2)); m_OPCServer.AddGroup(sGrpName, (bActive) ? 1 : 0, iUpdateRate, iClientGroup, ptrTimeBias, ptrDeadBand, iLCID, out m_iServerGroup, out iRevisedUpdateRate, ref guidGroupStateMgt, out group); m_OPCGroup2 = (IOPCGroupStateMgt2)group; m_OPCGroup2.SetKeepAlive(iKeepAliveTime, out iKeepAliveTime); m_OPCConnPointCntnr = (IConnectionPointContainer)m_OPCGroup2; guidDataCallback = Marshal.GenerateGuidForType(typeof(IOPCDataCallback)); m_OPCConnPointCntnr.FindConnectionPoint(ref guidDataCallback, out m_OPCConnPoint); break; case DEF_OPCDA.VER_10: case DEF_OPCDA.VER_20: default: guidGroupStateMgt = Marshal.GenerateGuidForType(typeof(IOPCGroupStateMgt)); m_OPCServer.AddGroup(sGrpName, (bActive) ? 1 : 0, iUpdateRate, iClientGroup, ptrTimeBias, ptrDeadBand, iLCID, out m_iServerGroup, out iRevisedUpdateRate, ref guidGroupStateMgt, out group); m_OPCGroup = (IOPCGroupStateMgt)group; m_OPCConnPointCntnr = (IConnectionPointContainer)m_OPCGroup; guidDataCallback = Marshal.GenerateGuidForType(typeof(IOPCDataCallback)); m_OPCConnPointCntnr.FindConnectionPoint(ref guidDataCallback, out m_OPCConnPoint); break; } return(true); } catch (Exception exc) { MessageBox.Show(exc.ToString(), "AddGroup"); return(false); } }
// 2011/11/14 シャットダウンイベントを受けれるようにする ) // 2012/01/12 グループのアクティブ/非アクティブを切り替え ( //===================================================== //Function : SetGroupActiveStatus(bool bSetActiveState, out string sErrMsg) //Distribution : グループのアクティブ状態を設定 //return : true 接続完了 // false 接続失敗 //===================================================== public bool SetGroupActiveStatus(bool bSetActiveState, out string sErrMsg) { sErrMsg = string.Empty; if (m_OPCServer == null) { sErrMsg = "No connecting"; return(false); } if (((m_OPCGroup == null) && (m_OPCGroup2 == null)) || m_iServerGroup == 0) { sErrMsg = "No connecting"; return(false); } // アクティブ状態指定用 IntPtr pActive = IntPtr.Zero; try { int nUpdateRate = 0; int nActive = 0; string sName = ""; int nTimeBias = 0; float fDeadband = 0; int nLCID = 0; int nClientGroup = 0; int nServerGroup = 0; int nRevisedUpdateRate = 0; int nActiveState = (bSetActiveState == true) ? 1 : 0; // ポインタ実体メモリエリアを確保し、nActiveStateをコピー(アンセーフコードで確保) pActive = Marshal.AllocHGlobal(sizeof(int)); Marshal.StructureToPtr(nActiveState, pActive, false); switch (m_OpcdaVer) { case DEF_OPCDA.VER_30: { IOPCGroupStateMgt2 OPCGroup = (IOPCGroupStateMgt2)m_OPCGroup2; OPCGroup.GetState(out nUpdateRate, out nActive, out sName, out nTimeBias, out fDeadband, out nLCID, out nClientGroup, out nServerGroup); OPCGroup.SetState(IntPtr.Zero, out nRevisedUpdateRate, pActive, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } break; case DEF_OPCDA.VER_10: case DEF_OPCDA.VER_20: default: { IOPCGroupStateMgt OPCGroup = (IOPCGroupStateMgt)m_OPCGroup; OPCGroup.GetState(out nUpdateRate, out nActive, out sName, out nTimeBias, out fDeadband, out nLCID, out nClientGroup, out nServerGroup); OPCGroup.SetState(IntPtr.Zero, out nRevisedUpdateRate, pActive, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } break; } return(true); } catch (Exception exc) { // 例外処理 sErrMsg = exc.ToString(); return(false); } finally { // 確保したメモリエリアを解放(アンセーフコードで確保したため) Marshal.FreeHGlobal(pActive); } }
// 2011/11/14 �V���b�g?�E���C�x���g�����悤�ɂ��� ) // 2012/01/12 �O��?�v�̃A�N�e�B�u/��A�N�e�B�u���ւ� ( //===================================================== //Function : SetGroupActiveStatus(bool bSetActiveState, out string sErrMsg) //Distribution : �O��?�v�̃A�N�e�B�u��Ԃ�ݒ� //return : true �ڑ����� // false �ڑ����s //===================================================== public bool SetGroupActiveStatus(bool bSetActiveState, out string sErrMsg) { sErrMsg = string.Empty; if (m_OPCServer == null) { sErrMsg = "No connecting"; return(false); } if (((m_OPCGroup == null) && (m_OPCGroup2 == null)) || m_iServerGroup == 0) { sErrMsg = "No connecting"; return(false); } // �A�N�e�B�u��Ԏw��p IntPtr pActive = IntPtr.Zero; try { int nUpdateRate = 0; int nActive = 0; string sName = ""; int nTimeBias = 0; float fDeadband = 0; int nLCID = 0; int nClientGroup = 0; int nServerGroup = 0; int nRevisedUpdateRate = 0; int nActiveState = (bSetActiveState == true) ? 1 : 0; // ?�C��?���̃������G���A���m�ۂ��AnActiveState���R�s?(�A���Z?�t�R?�h�Ŋm�ہj pActive = Marshal.AllocHGlobal(sizeof(int)); Marshal.StructureToPtr(nActiveState, pActive, false); switch (m_OpcdaVer) { case DEF_OPCDA.VER_30: { IOPCGroupStateMgt2 OPCGroup = (IOPCGroupStateMgt2)m_OPCGroup2; OPCGroup.GetState(out nUpdateRate, out nActive, out sName, out nTimeBias, out fDeadband, out nLCID, out nClientGroup, out nServerGroup); OPCGroup.SetState(IntPtr.Zero, out nRevisedUpdateRate, pActive, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } break; case DEF_OPCDA.VER_10: case DEF_OPCDA.VER_20: default: { IOPCGroupStateMgt OPCGroup = (IOPCGroupStateMgt)m_OPCGroup; OPCGroup.GetState(out nUpdateRate, out nActive, out sName, out nTimeBias, out fDeadband, out nLCID, out nClientGroup, out nServerGroup); OPCGroup.SetState(IntPtr.Zero, out nRevisedUpdateRate, pActive, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } break; } return(true); } catch (Exception exc) { // ��O���� sErrMsg = exc.ToString(); return(false); } finally { // �m�ۂ����������G���A�����(�A���Z?�t�R?�h�Ŋm�ۂ������߁j Marshal.FreeHGlobal(pActive); } }