Exemplo n.º 1
0
        /// <summary>
        /// 检测plc数据改变时,触发事件
        /// </summary>
        /// <returns></returns>
        public string DataChange()
        {
            string errText = string.Empty;
            IntPtr pRequestedUpdateRate = IntPtr.Zero;
            int    nRevUpdateRate       = 0;
            IntPtr hClientGroup         = IntPtr.Zero;
            IntPtr pTimeBias            = IntPtr.Zero;
            IntPtr pDeadband            = IntPtr.Zero;
            IntPtr pLCID   = IntPtr.Zero;
            int    nActive = 0;

            // activates or deactivates group according to checkbox status
            GCHandle hActive = GCHandle.Alloc(nActive, GCHandleType.Pinned);

            hActive.Target = 1;

            try
            {
                IOPCGroupStateMgtObj.SetState(pRequestedUpdateRate, out nRevUpdateRate,
                                              hActive.AddrOfPinnedObject(), pTimeBias, pDeadband, pLCID,
                                              hClientGroup);
            }
            catch (System.Exception error)
            {
                errText = error.Message.ToString();
            }

            finally
            {
                hActive.Free();
            }
            return(errText);
        }
Exemplo n.º 2
0
        /// <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();
            }
        }
Exemplo n.º 3
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();
            }
        }
Exemplo n.º 4
0
        public bool DataChange()
        {
            IntPtr pRequestedUpdateRate = IntPtr.Zero;
            int    nRevUpdateRate       = 0;
            IntPtr hClientGroup         = IntPtr.Zero;
            IntPtr pTimeBias            = IntPtr.Zero;
            IntPtr pDeadband            = IntPtr.Zero;
            IntPtr pLCID   = IntPtr.Zero;
            int    nActive = 0;
            // activates or deactivates group according to checkbox status
            GCHandle hActive = GCHandle.Alloc(nActive, GCHandleType.Pinned);

            if (boolValueChang)
            {
                hActive.Target = 1;
            }
            else
            {
                hActive.Target = 0;
            }
            try
            {
                IOPCGroupStateMgtObj.SetState(pRequestedUpdateRate, out nRevUpdateRate,
                                              hActive.AddrOfPinnedObject(), pTimeBias, pDeadband, pLCID,
                                              hClientGroup);
                isDataChange = true;
            }
            catch (System.Exception ex)
            {
                isDataChange = false;
                MessageBox.Show(string.Format("添加plc数据改变触发事件时出错:-{0}", ex.Message), "添加plc触发事件出错",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                hActive.Free();
            }
            return(isDataChange);
        }
Exemplo n.º 5
0
        /// <summary>This function provides the switching of the active state of the given group.</summary>
        /// <param name="reqUpdateRate">requested update rate</param>
        /// <param name="activateGroup">de/activate the group</param>
        /// <param name="revUpdateRate">revised update rate</param>
        /// <exception cref="Exception">forwards any exception (with short error description)</exception>
        /// <remarks>In order to minimize the traffic on the net and the demands on the opc server it
        /// is recommended to switch off the active groups if they are not necessary.</remarks>
        public void changeGroupState(int reqUpdateRate, bool activateGroup, out int revUpdateRate)
        {
            if (m_disposed)
            {
                throw new NullReferenceException("This object has been disposed!");
            }

            int Active = 0;
            int Loc    = 0x409;

            revUpdateRate = 0;

            IntPtr pTimeBias = IntPtr.Zero;
            IntPtr pDeadband = IntPtr.Zero;

            // Access unmanaged memory
            GCHandle hRequestedUpdateRate = GCHandle.Alloc(reqUpdateRate, GCHandleType.Pinned);
            GCHandle hLoc         = GCHandle.Alloc(Loc, GCHandleType.Pinned);
            GCHandle hActive      = GCHandle.Alloc(Active, GCHandleType.Pinned);
            GCHandle hServerGroup = GCHandle.Alloc(m_grpSrvHndl, GCHandleType.Pinned);

            try
            {
                // Set state
                if (activateGroup)
                {
                    hActive.Target = 1;
                }
                else
                {
                    hActive.Target = 0;
                }

                // Because we have to allocate unmanaged memory, we have to pinn them. Otherwise .Net's
                // garbage collector might free the memory and the opc server will do something randomly.
                m_OPCGroupStateMgt.SetState(hRequestedUpdateRate.AddrOfPinnedObject(),
                                            out revUpdateRate,
                                            hActive.AddrOfPinnedObject(),
                                            // if the parameter is used, it must be pinned!!!
                                            pTimeBias,
                                            // if the parameter is used, it must be pinned!!!
                                            pDeadband,
                                            hLoc.AddrOfPinnedObject(),
                                            hServerGroup.AddrOfPinnedObject());
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                // now free the unmanaged memory
                if (hActive.IsAllocated)
                {
                    hActive.Free();
                }
                if (hLoc.IsAllocated)
                {
                    hLoc.Free();
                }
                if (hRequestedUpdateRate.IsAllocated)
                {
                    hRequestedUpdateRate.Free();
                }
                if (hServerGroup.IsAllocated)
                {
                    hServerGroup.Free();
                }
            }
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
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);
            }
        }