Exemple #1
0
        internal void InternalAdd(int[] p_BiasTime, float[] p_PercentDeadband, int p_LocaleID)
        {
            Type   l_TypGrpMgt  = typeof(IOPCGroupStateMgt);
            Guid   l_GuidGrpTst = l_TypGrpMgt.GUID;
            object l_ObjTemp;

            if (m_State.Public)
            {
                IOPCServerPublicGroups l_IfPubGrps = null;
                l_IfPubGrps = (IOPCServerPublicGroups)m_IfServer;
                if (l_IfPubGrps == null)
                {
                    Marshal.ThrowExceptionForHR(HResults.E_NOINTERFACE);
                }

                l_IfPubGrps.GetPublicGroupByName(m_State.Name, ref l_GuidGrpTst, out l_ObjTemp);
                l_IfPubGrps = null;
            }
            else
            {
                m_IfServer.AddGroup(m_State.Name, m_State.Active, m_State.UpdateRate,
                                    m_State.HandleClient, p_BiasTime, p_PercentDeadband, m_State.LocaleID,
                                    out m_State.HandleServer, out m_State.UpdateRate, ref l_GuidGrpTst, out l_ObjTemp);
            }
            if (l_ObjTemp == null)
            {
                Marshal.ThrowExceptionForHR(HResults.E_NOINTERFACE);
            }
            m_IfMgt   = (IOPCGroupStateMgt)l_ObjTemp;
            l_ObjTemp = null;

            GetStates();
            GetInterfaces();
            AdviseIOPCDataCallback();
        }
Exemple #2
0
        public void Release()
        {
            try
            {
                if (dwCookie != 0)
                {
                    //pIConnectionPoint.Unadvise(dwCookie);
                    dwCookie = 0;
                }
                // Free the unmanaged COM memory
                //Marshal.ReleaseComObject(pIConnectionPoint);
                pIConnectionPoint = null;

                //Marshal.ReleaseComObject(pIConnectionPointContainer);
                pIConnectionPointContainer = null;

                if (pIOPCAsyncIO2 != null)
                {
                    //Marshal.ReleaseComObject(pIOPCAsyncIO2);
                    pIOPCAsyncIO2 = null;
                }
                if (pIOPCGroupStateMgt != null)
                {
                    //Marshal.ReleaseComObject(pIOPCGroupStateMgt);
                    pIOPCGroupStateMgt = null;
                }
                if (pobjGroup1 != null)
                {
                    //Marshal.ReleaseComObject(pobjGroup1);
                    pobjGroup1 = null;
                }
            }
            catch
            { }
        }
        /// <summary>
        /// 激活订阅回调事件
        /// </summary>
        private void ActiveDataChanged(IOPCGroupStateMgt IOPCGroupStateMgt)
        {
            IntPtr   pRequestedUpdateRate = IntPtr.Zero;
            IntPtr   hClientGroup         = IntPtr.Zero;
            IntPtr   pTimeBias            = IntPtr.Zero;
            IntPtr   pDeadband            = IntPtr.Zero;
            IntPtr   pLCID   = IntPtr.Zero;
            int      nActive = 0;
            GCHandle hActive = GCHandle.Alloc(nActive, GCHandleType.Pinned);

            try
            {
                hActive.Target = 1;
                int nRevUpdateRate = 0;
                IOPCGroupStateMgt.SetState(pRequestedUpdateRate, out nRevUpdateRate,
                                           hActive.AddrOfPinnedObject(), pTimeBias, pDeadband, pLCID, hClientGroup);
            }
            catch (COMException ex)
            {
                throw ex;
            }
            finally
            {
                hActive.Free();
            }
        }
Exemple #4
0
        public bool AddGroup(object Form_Main) //返回值false为失败,true为成功
        {
            Int32    dwRequestedUpdateRate = 2000;
            Int32    hClientGroup          = 1;
            Int32    pRevUpdaterate;
            float    deadband = 0;
            int      TimeBias = 0;
            GCHandle hTimeBias, hDeadband;

            hTimeBias = GCHandle.Alloc(TimeBias, GCHandleType.Pinned);
            hDeadband = GCHandle.Alloc(deadband, GCHandleType.Pinned);
            Guid iidRequiredInterface = typeof(IOPCItemMgt).GUID;

            if (!isConnected)
            {
                if (!Connect())
                {
                    return(false);            //如果还没有没有建立连接,先建立连接
                }
            }
            try
            {   //ServerObj.AddGroup的返回值类型为void
                ServerObj.AddGroup("OPCGroup", 0,
                                   dwRequestedUpdateRate, hClientGroup,
                                   hTimeBias.AddrOfPinnedObject(), hDeadband.AddrOfPinnedObject(),
                                   LOCALE_ID, out pSvrGroupHandle,
                                   out pRevUpdaterate, ref iidRequiredInterface, out GroupObj);
                IOPCAsyncIO2Obj = (IOPCAsyncIO2)GroupObj;//为组异步读写定义句柄
                //Query interface for Async calls on group object

                IOPCGroupStateMgtObj = (IOPCGroupStateMgt)GroupObj;

                pIConnectionPointContainer = (IConnectionPointContainer)GroupObj;
                //定义特定组的异步调用连接
                Guid iid = typeof(IOPCDataCallback).GUID;
                // Establish Callback for all async operations
                pIConnectionPointContainer.FindConnectionPoint(ref iid, out pIConnectionPoint);
                // Creates a connection between the OPC servers's connection point and this client's sink (the callback object)
                pIConnectionPoint.Advise(Form_Main, out dwCookie);
                isAddGroup = true;
            }
            catch (System.Exception error)
            {
                isAddGroup = false;
                MessageBox.Show(string.Format("创建组对象时出错:-{0}", error.Message), "建组出错",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (hDeadband.IsAllocated)
                {
                    hDeadband.Free();
                }
                if (hTimeBias.IsAllocated)
                {
                    hTimeBias.Free();
                }
            }
            return(isAddGroup);
        }
Exemple #5
0
        /// <summary> releases the IOPCGroupStateMgt-object </summary>
        private void freeGroup()
        {
            // *****************************************************
            // Release all interfaces
            // *****************************************************
            // MUST NOT be freed because we got it with
            //myOPCAsyncIO2 = (IOPCAsyncIO2) myOPCGroup;

            // MUST NOT be freed because we got it with
            //myOPCSyncIO = (IOPCSyncIO) myOPCGroup;

            // MUST NOT be freed because we got it with
            //myOPCItem = (IOPCItemMgt) myOPCGroup;

            // MUST NOT be freed because we got it with
            //myOPCConnectionPointContainer = (IConnectionPointContainer) myOPCGroup;

            // We must free it here, because we got it with
            //myOPCServer.AddGroup(	TextBox_AddGroup.Text,
            //						Convert.ToInt32(false),
            //						RequestedUpdateRate,
            //						1,
            //						pTimeBias,
            //						pDeadband,
            //						m_LocaleID,
            //						out m_Handle_myOPCGroup,
            //						out RevisedUpdateRate,
            //						ref  iid,
            //						out objGroup );
            //myOPCGroup = (IOPCGroupStateMgt) objGroup;
            // It doesn't matter you can eather free objGroup OR myOPCGroup
            // but, NEVER both.
            Marshal.ReleaseComObject(m_OPCGroupStateMgt);
            m_OPCGroupStateMgt = null;
        }
Exemple #6
0
        /// <summary>
        /// 断开PLC的连接
        /// </summary>
        /// <returns></returns>
        public string DisConnection()
        {
            string errText = string.Empty;

            try
            {
                if (IOPCSyncIO2Obj != null)
                {
                    Marshal.ReleaseComObject(IOPCSyncIO2Obj);
                    IOPCSyncIO2Obj = null;
                }

                ServerObj.RemoveGroup(pSvrGroupHandle, 0);
                if (IOPCGroupStateMgtObj != null)
                {
                    Marshal.ReleaseComObject(IOPCGroupStateMgtObj);
                    IOPCGroupStateMgtObj = null;
                }
                if (MyobjGroup1 != null)
                {
                    Marshal.ReleaseComObject(MyobjGroup1);
                    MyobjGroup1 = null;
                }
                if (ServerObj != null)
                {
                    Marshal.ReleaseComObject(ServerObj);
                    ServerObj = null;
                }
            }
            catch (System.Exception error)
            {
                errText = error.Message.ToString().Trim();
            }
            return(errText);
        }
Exemple #7
0
        public void internalAdd(int[] biasTime, float[] percentDeadband, int localeID)
        {
            Type   typGrpMgt  = typeof(IOPCGroupStateMgt);
            Guid   guidGrpTst = typGrpMgt.GUID;
            object objtemp;

            try
            {
                ifServer.AddGroup(state.Name, state.Active, state.UpdateRate, state.HandleClient, null, null, 0,
                                  out state.HandleServer, out state.UpdateRate, ref guidGrpTst, out objtemp);

                if (objtemp == null)
                {
                    Marshal.ThrowExceptionForHR(HRESULTS.E_NOINTERFACE);
                }
                ifMgt   = (IOPCGroupStateMgt)objtemp;
                ifItems = (IOPCItemMgt)ifMgt;
                ifSync  = (IOPCSyncIO)ifMgt;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Print(e.Message);
                throw e;
            }
        }
        internal void internalAdd(int[] biasTime, float[] percentDeadband, int localeID)
        {
            object obj2;
            Type   type = typeof(IOPCGroupStateMgt);
            Guid   gUID = type.GUID;

            if (this.state.Public)
            {
                IOPCServerPublicGroups ifServer = null;
                ifServer = (IOPCServerPublicGroups)this.ifServer;
                if (ifServer == null)
                {
                    Marshal.ThrowExceptionForHR(-2147467262);
                }
                ifServer.GetPublicGroupByName(this.state.Name, ref gUID, out obj2);
                ifServer = null;
            }
            else
            {
                this.ifServer.AddGroup(this.state.Name, this.state.Active, this.state.UpdateRate, this.state.HandleClient, biasTime, percentDeadband, this.state.LocaleID, out this.state.HandleServer, out this.state.UpdateRate, ref gUID, out obj2);
            }
            if (obj2 == null)
            {
                Marshal.ThrowExceptionForHR(-2147467262);
            }
            this.ifMgt = (IOPCGroupStateMgt)obj2;
            obj2       = null;
            this.GetStates();
            this.getinterfaces();
            this.AdviseIOPCDataCallback();
        }
Exemple #9
0
        public void Remove(bool p_Force)
        {
            if (m_CallbackPoint != null)
            {
                if (m_CallbackCookie != 0)
                {
                    m_CallbackPoint.Unadvise(m_CallbackCookie);
                    m_CallbackCookie = 0;
                }
                Marshal.ReleaseComObject(m_CallbackPoint);
                m_CallbackPoint = null;
            }

            m_PointContainer = null;
            m_IfItems        = null;
            m_IfSync         = null;
            m_IfAsync        = null;

            if (m_IfMgt != null)
            {
                Marshal.ReleaseComObject(m_IfMgt);
                m_IfMgt = null;
            }

            if (m_IfServer != null)
            {
                if (!m_State.Public)
                {
                    m_IfServer.RemoveGroup(m_State.HandleServer, p_Force);
                }
                m_IfServer = null;
            }

            m_State.HandleServer = 0;
        }
Exemple #10
0
        /*------------------------------------------------------
        *  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>
        /// 初始化IO接口
        /// </summary>
        /// <param name="opcGroup"></param>
        public void InitIoInterfaces(OpcDaCustomGroup opcGroup)
        {
            int cookie;

            //组状态管理对象,改变组的刷新率和激活状态
            IOPCGroupStateMgt         = (IOPCGroupStateMgt)opcGroup.Group;
            IConnectionPointContainer = (IConnectionPointContainer)opcGroup.Group;
            Guid iid = typeof(IOPCDataCallback).GUID;

            IConnectionPointContainer.FindConnectionPoint(ref iid, out IConnectionPoint);
            //创建客户端与服务端之间的连接
            IConnectionPoint.Advise(this, out
                                    cookie);
        }
        /// <summary>
        /// 增加一个group
        /// </summary>
        /// <param name="Form_Main"></param>
        /// <returns></returns>
        public string PLCGroupAdd(object Form_Main)
        {
            string   errText = string.Empty;
            Int32    dwRequestedUpdateRate = 250;
            Int32    pRevUpdateRate;
            float    deadband = 0;
            int      TimeBias = 0;
            GCHandle hTimeBias, hDeadband;

            hTimeBias = GCHandle.Alloc(TimeBias, GCHandleType.Pinned);
            hDeadband = GCHandle.Alloc(deadband, GCHandleType.Pinned);
            Guid iidRequiredInterface = typeof(IOPCItemMgt).GUID;

            try
            {
                ServerObj.AddGroup("MyOPCGroup1",//组对象
                                   0,
                                   dwRequestedUpdateRate,
                                   hClientGroup,
                                   hTimeBias.AddrOfPinnedObject(),
                                   hDeadband.AddrOfPinnedObject(),
                                   LOCALE_ID,
                                   out pSvrGroupHandle,
                                   out pRevUpdateRate,
                                   ref iidRequiredInterface,
                                   out MyobjGroup1);

                IOPCAsyncIO2Obj = (IOPCAsyncIO2)MyobjGroup1;
                //Query interface for Async calls on group object

                IOPCGroupStateMgtObj = (IOPCGroupStateMgt)MyobjGroup1;

                pIConnectionPointContainer = (IConnectionPointContainer)MyobjGroup1;
                //定义特定组的异步调用连接

                Guid iid = typeof(IOPCDataCallback).GUID;
                // Establish Callback for all async operations
                pIConnectionPointContainer.FindConnectionPoint(ref iid, out
                                                               pIConnectionPoint);

                // Creates a connection between the OPC servers's connection point and this client's sink (the callback object)
                pIConnectionPoint.Advise(Form_Main, out dwCookie);
            }
            catch (Exception ex)
            {
                errText = ex.Message.ToString();
            }
            return(errText);
        }
Exemple #13
0
        /// <summary>
        /// Modifies the group.
        /// </summary>
        /// <returns>The error. S_OK on success.</returns>
        public int ModifyGroup()
        {
            m_actualSamplingInterval = 0;

            int localeId = m_server.LocaleId;

            GCHandle hSamplingInterval = GCHandle.Alloc(m_samplingInterval, GCHandleType.Pinned);
            GCHandle hDeadband         = GCHandle.Alloc(m_deadband, GCHandleType.Pinned);

            string methodName = "IOPCGroupStateMgt.SetState";

            try
            {
                IOPCGroupStateMgt server = BeginComCall <IOPCGroupStateMgt>(methodName, true);

                server.SetState(
                    hSamplingInterval.AddrOfPinnedObject(),
                    out m_actualSamplingInterval,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    hDeadband.AddrOfPinnedObject(),
                    IntPtr.Zero,
                    IntPtr.Zero);

                /*
                 * Utils.Trace(
                 *  "Group {0} Modified({4}) {1}/{2}ms {3}%",
                 *  m_clientHandle,
                 *  m_samplingInterval,
                 *  m_actualSamplingInterval,
                 *  m_deadband,
                 *  m_items.Count);
                 */

                return(ResultIds.S_OK);
            }
            catch (Exception e)
            {
                ComUtils.TraceComError(e, methodName);
                return(Marshal.GetHRForException(e));
            }
            finally
            {
                EndComCall(methodName);
                hSamplingInterval.Free();
                hDeadband.Free();
            }
        }
        /// <summary>
        /// 断开PLC的连接
        /// </summary>
        /// <returns></returns>
        public string DisConnection()
        {
            string errText = string.Empty;

            try
            {
                if (dwCookie != 0)
                {
                    pIConnectionPoint.Unadvise(dwCookie);
                    dwCookie = 0;
                }
                // Free unmanaged code
                Marshal.ReleaseComObject(pIConnectionPoint);
                pIConnectionPoint = null;

                Marshal.ReleaseComObject(pIConnectionPointContainer);
                pIConnectionPointContainer = null;

                if (IOPCAsyncIO2Obj != null)
                {
                    Marshal.ReleaseComObject(IOPCAsyncIO2Obj);
                    IOPCAsyncIO2Obj = null;
                }

                ServerObj.RemoveGroup(pSvrGroupHandle, 0);
                if (IOPCGroupStateMgtObj != null)
                {
                    Marshal.ReleaseComObject(IOPCGroupStateMgtObj);
                    IOPCGroupStateMgtObj = null;
                }
                if (MyobjGroup1 != null)
                {
                    Marshal.ReleaseComObject(MyobjGroup1);
                    MyobjGroup1 = null;
                }
                if (ServerObj != null)
                {
                    Marshal.ReleaseComObject(ServerObj);
                    ServerObj = null;
                }
            }
            catch (System.Exception error)
            {
                errText = error.Message.ToString().Trim();
            }
            return(errText);
        }
Exemple #15
0
        public void MoveToPublic()
        {
            if (this.state.Public)
            {
                Marshal.ThrowExceptionForHR(-2147467259);
            }
            IOPCPublicGroupStateMgt ifMgt = null;

            ifMgt = (IOPCPublicGroupStateMgt)this.ifMgt;
            if (ifMgt == null)
            {
                Marshal.ThrowExceptionForHR(-2147467262);
            }
            ifMgt.MoveToPublic();
            ifMgt.GetState(out this.state.Public);
            ifMgt = null;
        }
Exemple #16
0
        public bool AddGroup(object Form_Main)
        {
            int      dwRequestedUpdateRate = 0x3e8;
            int      hClientGroup          = 1;
            float    num4    = 0f;
            int      num5    = 0;
            GCHandle handle  = GCHandle.Alloc(num5, GCHandleType.Pinned);
            GCHandle handle2 = GCHandle.Alloc(num4, GCHandleType.Pinned);
            Guid     gUID    = typeof(IOPCItemMgt).GUID;

            if (!this.isConnected && !this.Connect())
            {
                return(false);
            }
            try
            {
                int num3;
                this.ServerObj.AddGroup("OPCGroup", 0, dwRequestedUpdateRate, hClientGroup, handle.AddrOfPinnedObject(), handle2.AddrOfPinnedObject(), 0x804, out this.pSvrGroupHandle, out num3, ref gUID, out this.GroupObj);
                this.IOPCAsyncIO2Obj            = (IOPCAsyncIO2)this.GroupObj;
                this.IOPCGroupStateMgtObj       = (IOPCGroupStateMgt)this.GroupObj;
                this.pIConnectionPointContainer = (IConnectionPointContainer)this.GroupObj;
                Guid riid = typeof(IOPCDataCallback).GUID;
                this.pIConnectionPointContainer.FindConnectionPoint(ref riid, out this.pIConnectionPoint);
                this.pIConnectionPoint.Advise(Form_Main, out this.dwCookie);
                this.isAddGroup = true;
            }
            catch (Exception exception)
            {
                this.isAddGroup = false;
                MessageBox.Show($"创建组对象时出错:-{exception.Message}", "建组出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            finally
            {
                if (handle2.IsAllocated)
                {
                    handle2.Free();
                }
                if (handle.IsAllocated)
                {
                    handle.Free();
                }
            }
            return(this.isAddGroup);
        }
Exemple #17
0
 /// <summary>
 /// 释放对象
 /// </summary>
 public void Dispose()
 {
     try
     {
         if (dwCookie != 0)
         {
             ConnectionPoint.Unadvise(dwCookie);
             dwCookie = 0;
         }
     }
     catch (Exception)
     {
         dwCookie = 0;
     }
     Marshal.ReleaseComObject(ConnectionPoint);
     ConnectionPoint = null;
     Marshal.ReleaseComObject(connectionPointContainer);
     connectionPointContainer = null;
     if (oPCSyncIO != null)
     {
         Marshal.ReleaseComObject(oPCSyncIO);//减与指定的 COM 对象关联的运行时可调用包装器 (RCW) 的引用计数。
         oPCSyncIO = null;
     }
     if (oPCAsyncIO != null)
     {
         Marshal.ReleaseComObject(oPCAsyncIO);//减与指定的 COM 对象关联的运行时可调用包装器 (RCW) 的引用计数。
         oPCAsyncIO = null;
     }
     if (oPCGroupStateMgt != null)
     {
         Marshal.ReleaseComObject(oPCGroupStateMgt);
         oPCGroupStateMgt = null;
     }
     if (oPCItemMgt != null)
     {
         Marshal.ReleaseComObject(oPCItemMgt);
         oPCItemMgt = null;
     }
     if (ObjGroup != null)
     {
         Marshal.ReleaseComObject(ObjGroup);
         ObjGroup = null;
     }
 }
Exemple #18
0
        public string DisConnection()
        {
            string str = string.Empty;

            try
            {
                if (this.dwCookie != 0)
                {
                    this.pIConnectionPoint.Unadvise(this.dwCookie);
                    this.dwCookie = 0;
                }
                Marshal.ReleaseComObject(this.pIConnectionPoint);
                this.pIConnectionPoint = null;
                Marshal.ReleaseComObject(this.pIConnectionPointContainer);
                this.pIConnectionPointContainer = null;
                if (this.IOPCAsyncIO2Obj != null)
                {
                    Marshal.ReleaseComObject(this.IOPCAsyncIO2Obj);
                    this.IOPCAsyncIO2Obj = null;
                }
                this.ServerObj.RemoveGroup(this.pSvrGroupHandle, 0);
                if (this.IOPCGroupStateMgtObj != null)
                {
                    Marshal.ReleaseComObject(this.IOPCGroupStateMgtObj);
                    this.IOPCGroupStateMgtObj = null;
                }
                if (this.GroupObj != null)
                {
                    Marshal.ReleaseComObject(this.GroupObj);
                    this.GroupObj = null;
                }
                if (this.ServerObj != null)
                {
                    Marshal.ReleaseComObject(this.ServerObj);
                    this.ServerObj = null;
                }
            }
            catch (Exception exception)
            {
                str = exception.Message.ToString().Trim();
            }
            return(str);
        }
Exemple #19
0
        /// <summary>
        /// 增加group
        /// </summary>
        /// <param name="Form_Main"></param>
        /// <returns></returns>
        public string PLCGroupAdd()
        {
            string errText = string.Empty;
            Int32  dwRequestedUpdateRate = 1000;
            //Int32 hClientGroup;
            Int32 pRevUpdateRate;

            float deadband = 0;

            int      TimeBias = 0;
            GCHandle hTimeBias, hDeadband;

            hTimeBias = GCHandle.Alloc(TimeBias, GCHandleType.Pinned);
            hDeadband = GCHandle.Alloc(deadband, GCHandleType.Pinned);
            Guid iidRequiredInterface = typeof(IOPCItemMgt).GUID;

            try
            {
                ServerObj.AddGroup("ShearerInfoGroup",    //组对象
                                   1,
                                   dwRequestedUpdateRate,
                                   1,
                                   hTimeBias.AddrOfPinnedObject(),
                                   hDeadband.AddrOfPinnedObject(),
                                   0,
                                   out pSvrGroupHandle,
                                   out pRevUpdateRate,
                                   ref iidRequiredInterface,
                                   out MyobjGroup1);

                IOPCSyncIO2Obj = (IOPCSyncIO)MyobjGroup1;
                //Query interface for Async calls on group object

                IOPCGroupStateMgtObj = (IOPCGroupStateMgt)MyobjGroup1;
            }

            catch (Exception ex)
            {
                errText = ex.Message.ToString();
            }
            return(errText);
        }
Exemple #20
0
 public void Remove(bool bForce)
 {
     try
     {
         int num;
         if (this.callbackcpoint != null)
         {
             if (this.callbackcookie != 0)
             {
                 this.callbackcpoint.Unadvise(this.callbackcookie);
                 this.callbackcookie = 0;
             }
             num = Marshal.ReleaseComObject(this.callbackcpoint);
             this.callbackcpoint = null;
         }
         this.cpointcontainer = null;
         this.ifItems         = null;
         this.ifSync          = null;
         this.ifAsync         = null;
         if (this.ifMgt != null)
         {
             num        = Marshal.ReleaseComObject(this.ifMgt);
             this.ifMgt = null;
         }
         if (this.ifServer != null)
         {
             if (!this.state.Public)
             {
                 this.ifServer.RemoveGroup(this.state.HandleServer, bForce);
             }
             this.ifServer = null;
         }
         this.state.HandleServer = 0;
     }
     catch (Exception exception)
     {
         LogHelper.Error("OPCTrendLib.OPCData.OpcGroup", "Remove", exception);
         Console.WriteLine(exception.ToString());
     }
 }
Exemple #21
0
        public void Remove(bool bForce)
        {
            ifItems = null;
            ifSync  = null;

            if (!(ifMgt == null))
            {
                int rc = Marshal.ReleaseComObject(ifMgt);
                ifMgt = null;
            }

            if (!(ifServer == null))
            {
                if (!state.Public)
                {
                    ifServer.RemoveGroup(state.HandleServer, bForce);
                }
                ifServer = null;
            }

            state.HandleServer = 0;
        }
Exemple #22
0
        private void InitReqIOInterfaces()
        {
            try
            {
                //Query interface for async calls on group object
                pIOPCAsyncIO2      = (IOPCAsyncIO2)pobjGroup1;
                pIOPCSyncIO        = (IOPCSyncIO)pobjGroup1;
                pIOPCGroupStateMgt = (IOPCGroupStateMgt)pobjGroup1;

                // Query interface for callbacks on group object
                pIConnectionPointContainer = (IConnectionPointContainer)pobjGroup1;

                // Establish Callback for all async operations
                Guid iid = typeof(IOPCDataCallback).GUID;
                pIConnectionPointContainer.FindConnectionPoint(ref iid, out pIConnectionPoint);

                // Creates a connection between the OPC servers's connection point and
                // this client's sink (the callback object).
                pIConnectionPoint.Advise(this, out dwCookie);
            }
            catch (System.Exception error) // catch for group adding
            {
            }
        }
Exemple #23
0
        // 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);
            }
        }
Exemple #24
0
        //======================================================================
        // State Management

        /// <summary>
        /// Returns the current state of the subscription.
        /// </summary>
        /// <returns>The current state of the subscription.</returns>
        public TsCDaSubscriptionState GetState()
        {
            lock (this)
            {
                TsCDaSubscriptionState state = new TsCDaSubscriptionState();

                state.ClientHandle = _handle;

                string methodName = "IOPCGroupStateMgt.GetState";
                try
                {
                    string name         = null;
                    int    active       = 0;
                    int    updateRate   = 0;
                    float  deadband     = 0;
                    int    timebias     = 0;
                    int    localeID     = 0;
                    int    clientHandle = 0;
                    int    serverHandle = 0;

                    IOPCGroupStateMgt subscription = BeginComCall <IOPCGroupStateMgt>(methodName, true);
                    subscription.GetState(
                        out updateRate,
                        out active,
                        out name,
                        out timebias,
                        out deadband,
                        out localeID,
                        out clientHandle,
                        out serverHandle);

                    state.Name         = name;
                    state.ServerHandle = serverHandle;
                    if (active == 1)
                    {
                        state.Active = true;
                    }
                    else
                    {
                        state.Active = false;
                    }
                    state.UpdateRate = updateRate;
                    state.TimeBias   = timebias;
                    state.Deadband   = deadband;
                    state.Locale     = Utilities.Interop.GetLocale(localeID);

                    // cache the name separately.
                    name_ = state.Name;

                    state.KeepAlive = 0;
                }
                catch (Exception e)
                {
                    ComCallError(methodName, e);
                    throw Utilities.Interop.CreateException(methodName, e);
                }
                finally
                {
                    EndComCall(methodName);
                }

                return(state);
            }
        }
        internal void internalAdd( int[] biasTime, float[] percentDeadband, int localeID )
        {
            Type	typGrpMgt = typeof( IOPCGroupStateMgt );
            Guid	guidGrpTst = typGrpMgt.GUID;

            object	objtemp;
            if( state.Public )
            {
            IOPCServerPublicGroups	ifPubGrps = null;
            ifPubGrps = (IOPCServerPublicGroups)	ifServer;
            if( ifPubGrps == null )
                Marshal.ThrowExceptionForHR( HRESULTS.E_NOINTERFACE );

            ifPubGrps.GetPublicGroupByName( state.Name, ref guidGrpTst, out objtemp );
            ifPubGrps = null;
            }
            else
            {
            ifServer.AddGroup(	state.Name, state.Active, state.UpdateRate, state.HandleClient, biasTime, percentDeadband, state.LocaleID,
                                out state.HandleServer, out state.UpdateRate, ref guidGrpTst, out objtemp );
            }
            if( objtemp == null )
            Marshal.ThrowExceptionForHR( HRESULTS.E_NOINTERFACE );

            ifMgt = (IOPCGroupStateMgt) objtemp;
            objtemp = null;
            GetStates();

            getinterfaces();
            AdviseIOPCDataCallback();
        }
Exemple #26
0
        /// <summary>adds a IOPCGroupStateMgt-object to the OPC-server</summary>
        /// <param name="commonInterface">the common interface</param>
        /// <returns> the revisedUpdateRate for further use </returns>
        /// <exception cref="Exception">throws and forwards any exception (with short error description)</exception>
        public int addOPCGroup(IOPCCommon commonInterface)
        {
            // initialize arguments.
            Guid   iid       = Guid.Empty;
            object objGroup  = null;
            IntPtr pTimeBias = IntPtr.Zero;
            IntPtr pDeadband = IntPtr.Zero;

            if (m_disposed)
            {
                throw new NullReferenceException("This object has been disposed!");
            }

            m_revUpdateRate = 0;

            try
            {
                // get the default locale for the server.
                commonInterface.GetLocaleID(out m_LocaleID);
                iid = typeof(IOPCGroupStateMgt).GUID;
                if (iid == Guid.Empty)
                {
                    throw new Exception("Could not get the interface 'IOPCGroupStateMgt'!");
                }

                // Add a group object "m_OPCGroup" and query for interface IOPCItemMgt
                // Parameter as following:
                // [in] active, so do OnDataChange callback
                // [in] Request this Update Rate from Server
                // [in] Client Handle, not necessary in this sample
                // [in] No time interval to system UTC time
                // [in] No Deadband, so all data changes are reported
                // [in] Server uses english language to for text values
                // [out] Server handle to identify this group in later calls
                // [out] The answer from Server to the requested Update Rate
                // [in] requested interface type of the group object
                // [out] pointer to the requested interface
                m_OPCServer.AddGroup(m_grpName,
                                     Convert.ToInt32(m_isActive),
                                     m_reqUpdateRate,
                                     m_grpClntHndl,
                                     pTimeBias,
                                     pDeadband,
                                     m_LocaleID,
                                     out m_grpSrvHndl,
                                     out m_revUpdateRate,
                                     ref iid,
                                     out objGroup);

                if (objGroup == null)
                {
                    string strMsg = "Couldn't add the group '"
                                    + m_grpName + "'  to the server!";
                    throw new Exception(strMsg);
                }

                // Get our reference from the created group
                m_OPCGroupStateMgt = (IOPCGroupStateMgt)objGroup;
                if (m_OPCGroupStateMgt == null)
                {
                    throw new Exception("Could not get the interface 'IOPCGroupStateMgt'!");
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(m_revUpdateRate);
        }
        public void Remove( bool bForce )
        {
            if( ! (callbackcpoint == null) )
            {
            if( callbackcookie != 0 )
                {
                callbackcpoint.Unadvise( callbackcookie );
                callbackcookie = 0;
                }
            int	rc = Marshal.ReleaseComObject( callbackcpoint );
            callbackcpoint = null;
            }

            cpointcontainer	= null;
            ifItems		= null;
            ifSync		= null;
            ifAsync		= null;

            if( ! (ifMgt == null) )
            {
            int	rc = Marshal.ReleaseComObject( ifMgt );
            ifMgt = null;
            }

            if( ! (ifServer == null) )
            {
            if( ! state.Public )
                ifServer.RemoveGroup( state.HandleServer, bForce );
            ifServer	= null;
            }

            state.HandleServer = 0;
        }
Exemple #28
0
        public void Remove(bool bForce)
        {
            ifItems = null;
            ifSync = null;

            if (!(ifMgt == null))
            {
                int rc = Marshal.ReleaseComObject(ifMgt);
                ifMgt = null;
            }

            if (!(ifServer == null))
            {
                if (!state.Public)
                    ifServer.RemoveGroup(state.HandleServer, bForce);
                ifServer = null;
            }

            state.HandleServer = 0;
        }
Exemple #29
0
        public void internalAdd(int[] biasTime, float[] percentDeadband, int localeID)
        {
            Type typGrpMgt = typeof(IOPCGroupStateMgt);
            Guid guidGrpTst = typGrpMgt.GUID;
            object objtemp;
            try
            {
                ifServer.AddGroup(state.Name, state.Active, state.UpdateRate, state.HandleClient, null, null, 0,
                        out state.HandleServer, out state.UpdateRate, ref guidGrpTst, out objtemp);

                if (objtemp == null)
                    Marshal.ThrowExceptionForHR(HRESULTS.E_NOINTERFACE);
                ifMgt = (IOPCGroupStateMgt)objtemp;
                ifItems = (IOPCItemMgt)ifMgt;
                ifSync = (IOPCSyncIO)ifMgt;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Print(e.Message);
                throw e;

            }
        }
Exemple #30
0
        // 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);
            }
        }
Exemple #31
0
        /*------------------------------------------------------
        *  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);
            }
        }
Exemple #32
0
        /// <summary>
        /// Returns the current state of the subscription.
        /// </summary>
        /// <returns>The current state of the subscription.</returns>
        public override TsCDaSubscriptionState GetState()
        {
            if (subscription_ == null)
            {
                throw new NotConnectedException();
            }
            lock (lock_)
            {
                string methodName            = "IOPCGroupStateMgt.GetState";
                TsCDaSubscriptionState state = new TsCDaSubscriptionState {
                    ClientHandle = _handle
                };

                string name = null;

                try
                {
                    int   active       = 0;
                    int   updateRate   = 0;
                    float deadband     = 0;
                    int   timebias     = 0;
                    int   localeID     = 0;
                    int   clientHandle = 0;
                    int   serverHandle = 0;

                    IOPCGroupStateMgt subscription = BeginComCall <IOPCGroupStateMgt>(methodName, true);
                    subscription.GetState(
                        out updateRate,
                        out active,
                        out name,
                        out timebias,
                        out deadband,
                        out localeID,
                        out clientHandle,
                        out serverHandle);

                    if (DCOMCallWatchdog.IsCancelled)
                    {
                        throw new Exception($"{methodName} call was cancelled due to response timeout");
                    }

                    state.Name         = name;
                    state.ServerHandle = serverHandle;
                    state.Active       = active != 0;
                    state.UpdateRate   = updateRate;
                    state.TimeBias     = timebias;
                    state.Deadband     = deadband;
                    state.Locale       = Technosoftware.DaAeHdaClient.Com.Interop.GetLocale(localeID);

                    // cache the name separately.
                    name_ = state.Name;
                }
                catch (Exception e)
                {
                    ComCallError(methodName, e);
                    throw Technosoftware.DaAeHdaClient.Com.Interop.CreateException(methodName, e);
                }
                finally
                {
                    EndComCall(methodName);
                }

                state.KeepAlive = 0;

                return(state);
            }
        }