Example #1
0
        /// <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);
            }
        }
Example #2
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);
            }
        }