예제 #1
0
 public void Connect(string serverID)
 {
     this.itemMgtInterface = typeof(IOPCItemMgt).GUID;
     Type typeFromProgID = Type.GetTypeFromProgID(serverID);
     this.pIOPCServer = (IOPCServer) Activator.CreateInstance(typeFromProgID);
     this.groups = new OPCGroupCollection();
 }
		internal ItemProperties(IOPCServer server)
		{
			try
			{
				itemProperties = (IOPCItemProperties) server;
			}
			catch (InvalidCastException)
			{
				throw new NotSupportedException();
			}
		}
예제 #3
0
 public void Connect(string ProgID,string ServerName)
 {
     this.itemMgtInterface = typeof(IOPCItemMgt).GUID;
     Type typeFromProgID;
     if (ProgID == null)
         typeFromProgID = Type.GetTypeFromProgID(ServerName);
     else
         typeFromProgID = Type.GetTypeFromProgID(ProgID, ServerName);
     this.pIOPCServer = (IOPCServer)Activator.CreateInstance(typeFromProgID);
     this.groups = new OPCGroupCollection();
 }
        internal ServerAddressSpaceBrowser(IOPCServer server, int blockSize)
        {
            this.blockSize = blockSize;
			try
			{
				browseServerSpace = (IOPCBrowseServerAddressSpace) server;
			}
			catch (InvalidCastException)
			{
				throw new NotSupportedException();
			}
		}
예제 #5
0
 public void Disconnect()
 {
     if (pIOPCServer != null)
     {
         Marshal.ReleaseComObject(pIOPCServer);
         pIOPCServer = null;
     }
     if (groupList != null && groupList.Count > 0)
     {
         foreach (var item in groupList)
         {
             item.Release();
         }
     }
 }
예제 #6
0
        /// <summary>
        /// plc的链接
        /// </summary>
        /// <param name="remoteServerName">链接的名字()</param>
        /// <param name="remoteServerIP">链接的ip地址</param>
        /// <returns></returns>
        public string ConnectRemoteServer()
        {
            try
            {
                ////建议一个连接
                Type svrComponenttyp = Type.GetTypeFromProgID(remoteServerName, remoteServerIP);

                ServerObj = (IOPCServer)Activator.CreateInstance(svrComponenttyp);
            }

            catch (Exception ex)
            {
                errText = "连接远程服务器出现错误:" + ex.Message.ToString();
            }
            return(errText);
        }
예제 #7
0
        public void Connect(string ProgID, string ServerName)
        {
            this.itemMgtInterface = typeof(IOPCItemMgt).GUID;
            Type typeFromProgID;

            if (ProgID == null)
            {
                typeFromProgID = Type.GetTypeFromProgID(ServerName);
            }
            else
            {
                typeFromProgID = Type.GetTypeFromProgID(ProgID, ServerName);
            }
            this.pIOPCServer = (IOPCServer)Activator.CreateInstance(typeFromProgID);
            this.groups      = new OPCGroupCollection();
        }
예제 #8
0
 public bool Connect()
 {
     try
     {
         //System.Type typeFromProgID = System.Type.GetTypeFromProgID("ICONICS.SimulatorOPCDA", "127.0.0.1");
         System.Type typeFromProgID = System.Type.GetTypeFromProgID("OPC.SimaticNET", "127.0.0.1");
         this.ServerObj   = (IOPCServer)Activator.CreateInstance(typeFromProgID);
         this.isConnected = true;
     }
     catch (Exception exception)
     {
         this.isConnected = false;
         MessageBox.Show($"对象{this.objName}建立OPCServer连接失败:-{exception.Message}", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Hand);
     }
     return(this.isConnected);
 }
예제 #9
0
 /// <summary>
 /// 建立和OPCServer的连接
 /// </summary>
 public bool Connect() //返回值true为成功,false为失败
 {
     try
     {
         Type svrComponenttyp = Type.GetTypeFromProgID("OPC.SimaticNet", "localhost");
         ServerObj   = (IOPCServer)Activator.CreateInstance(svrComponenttyp);
         isConnected = true;
     }
     catch (System.Exception error)
     {
         isConnected = false;
         MessageBox.Show(String.Format("对象{0}建立OPCServer连接失败:-{1}", objName, error.Message),
                         "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     return(isConnected);
 }
예제 #10
0
        /// <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);
        }
예제 #11
0
        private void button12_Click(object sender, EventArgs e)
        {
            DialogResult MsgBoxResult = MessageBox.Show("确定要退出程序?",                       //对话框的显示内容
                                                        "操作提示",                           //对话框的标题
                                                        MessageBoxButtons.YesNo,          //定义对话框的按钮,这里定义了YSE和NO两个按钮
                                                        MessageBoxIcon.Question,          //定义对话框内的图表式样,这里是一个黄色三角型内加一个感叹号
                                                        MessageBoxDefaultButton.Button2); //定义对话框的按钮式样


            if (MsgBoxResult == DialogResult.Yes)
            {
                if (pIOPCServer != null)
                {
                    Marshal.ReleaseComObject(pIOPCServer);
                    pIOPCServer = null;
                }
                if (OnlyTaskGorup != null)
                {
                    OnlyTaskGorup.Release();
                }
                if (FinishOnlyGoroup != null)
                {
                    FinishOnlyGoroup.Release();
                }
                if (SpyBiaozhiGroup != null)
                {
                    SpyBiaozhiGroup.Release();
                }
                if (SpecialSmokeGroup1 != null)
                {
                    SpecialSmokeGroup1.Release();
                }
                if (SpecialSmokeGroup2 != null)
                {
                    SpecialSmokeGroup2.Release();
                }
                list_data.Items.Clear();
                updateListBox("任务停止发送!");
                writeLog.Write("任务停止发送!");
                updateControlEnable(true, button10);
            }
            else
            {
                return;
            }
            //  updateControlEnable(true, button10);
        }
예제 #12
0
 internal OpcGroup(ref IOPCServer ifServerLink, bool isPublic, string groupName, bool setActive, int requestedUpdateRate)
 {
     this.ifServer              = ifServerLink;
     this.state.Name            = groupName;
     this.state.Public          = isPublic;
     this.state.UpdateRate      = requestedUpdateRate;
     this.state.Active          = setActive;
     this.state.TimeBias        = 0;
     this.state.PercentDeadband = 0f;
     this.state.LocaleID        = 0;
     this.state.HandleClient    = this.GetHashCode();
     this.state.HandleServer    = 0;
     this.typeOPCITEMDEF        = typeof(OPCITEMDEFintern);
     this.sizeOPCITEMDEF        = Marshal.SizeOf(this.typeOPCITEMDEF);
     this.typeOPCITEMRESULT     = typeof(OPCITEMRESULTintern);
     this.sizeOPCITEMRESULT     = Marshal.SizeOf(this.typeOPCITEMRESULT);
 }
예제 #13
0
        /// <summary>
        /// Creates a group.
        /// </summary>
        public object CreateGroup(
            int groupId,
            int samplingInterval,
            float deadband,
            out int groupHandle,
            out int revisedSamplingInterval)
        {
            groupHandle             = 0;
            revisedSamplingInterval = 0;
            GCHandle hDeadband = GCHandle.Alloc(deadband, GCHandleType.Pinned);

            string methodName = "IOPCServer.AddGroup";

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

                object group = null;
                Guid   iid   = typeof(OpcRcw.Da.IOPCItemMgt).GUID;

                server.AddGroup(
                    String.Empty,
                    1,
                    samplingInterval,
                    0,
                    IntPtr.Zero,
                    hDeadband.AddrOfPinnedObject(),
                    ComUtils.LOCALE_SYSTEM_DEFAULT,
                    out groupHandle,
                    out revisedSamplingInterval,
                    ref iid,
                    out group);

                return(group);
            }
            catch (Exception e)
            {
                ComUtils.TraceComError(e, methodName);
                return(null);
            }
            finally
            {
                EndComCall(methodName);
                hDeadband.Free();
            }
        }
        /// <summary>Establishes the connection to the S韒aticNET OPC Server.</summary>
        /// <param name="localeID"> The locale ID returned from IOPCCommon the OPC-Server </param>
        /// <exception cref="InvalidOperationException"> if the opcserver is already connected </exception>
        /// <exception cref="Exception">throws and forwards any exception (with short error description)</exception>
        public void connectOPCServer(string progID)
        {
            if (!m_connected)
            {
                Type typeofOPCserver = null;

                try
                {
                    //Get the Type from ProgID.Text
                    typeofOPCserver = Type.GetTypeFromProgID(progID);

                    string strDummy = "Couldn't find a server with the ProgID '" + progID + "'!";
                    if (typeofOPCserver == null)
                    {
                        throw new Exception(strDummy);
                    }

                    // Must be freed with Marshal.ReleaseComObject(myOPCServer)
                    m_OPCServer = (IOPCServer)Activator.CreateInstance(typeofOPCserver);
                    if (m_OPCServer == null)
                    {
                        strDummy = "CreateInstance failed - server not connected!";
                        throw new Exception(strDummy);
                    }

                    // Don't free it - we don't AddRef it!!!
                    m_OPCCommon = (IOPCCommon)m_OPCServer;
                    if (m_OPCCommon == null)
                    {
                        strDummy = "No 'IOPCCommon' Interface found!";
                        throw new Exception(strDummy);
                    }

                    m_OPCCommon.GetLocaleID(out m_LocaleID);
                    m_connected = true;
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                throw new InvalidOperationException("Already connected to OPC-Server!");
            }
        }
        /// <summary> Dispose(bool disposing) executes in two distinct scenarios.
        /// If disposing equals true, the method has been called directly
        /// or indirectly by a user's code.
        /// If disposing equals false, the method has been called by the
        /// runtime from inside the finalizer and you should not reference
        /// other objects. /// </summary>
        /// <param name="disposing"> determines whether it is called from the finalizer or
        /// from the user. </param>
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!m_disposed)
            {
                IntPtr pErrors = IntPtr.Zero;

                if (m_OPCServer != null)
                {
                    try
                    {
                        // Do NOT forget to call dispose for the management-objects!
                        // in all properbility you will get an exception otherwise!
                        foreach (OPCGroupManagement theManager in m_grpManagerList)
                        {
                            theManager.Dispose();
                        }
                        m_grpManagerList.Clear();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        // MUST NOT be freed because we got it with
                        //myOPCCommon = (IOPCCommon) myOPCServer;

                        // We must free it here, because we got it with
                        // myOPCServer = (IOPCServer) Activator.CreateInstance(typeofOPCserver);
                        if (m_OPCServer != null)
                        {
                            Marshal.ReleaseComObject(m_OPCServer);
                            m_OPCServer = null;
                        }
                    }
                }
                // Note that this is not thread safe.
                // Another thread could start disposing the object
                // after the managed resources are disposed,
                // but before the disposed flag is set to true.
                // If thread safety is necessary, it must be
                // implemented by the client.
            }
            m_disposed = true;
        }
예제 #16
0
 /// <summary>
 /// 组集合
 /// </summary>
 //private List<OpcGroup> _lstOpcGroup = new List<OpcGroup>();
 /// <summary>
 /// 连接OpcServer
 /// </summary>
 /// <param name="opcServerName"></param>
 /// <param name="serverIP"></param>
 public bool OpcConnect(string opcServerName, string serverIP)
 {
     if (!string.IsNullOrEmpty(serverIP) && !string.IsNullOrEmpty(opcServerName))
     {
         Type type = Type.GetTypeFromProgID(opcServerName, serverIP);
         if (type != null)
         {
             opcServer = (IOPCServer)Activator.CreateInstance(type);
             connectionPointContainer = (IConnectionPointContainer)opcServer;
             Guid iid = typeof(IOPCShutdown).GUID;                                       //为所有的异步调用创建回调
             connectionPointContainer.FindConnectionPoint(ref iid, out connectionPoint); //为 OPC Server 的 连 接 点 与 客 户 端 接 收 点 之 间 建 立 连 接
             connectionPoint.Advise(this, out dwCookie);
             return(true);
         }
     }
     return(false);
 }
예제 #17
0
 public static void Connect1(int type = 1, string groupConnectionGroup = "S7:[FJConnectionGroup1]")
 {
     if (pIOPCServer1 == null)
     {
         Type svrComponenttyp;
         Guid iidRequiredInterface = typeof(IOPCItemMgt).GUID;
         svrComponenttyp = Type.GetTypeFromProgID(SERVER_NAME);
         pIOPCServer1    = (IOPCServer)Activator.CreateInstance(svrComponenttyp);
         YFJGroup        = new Group(pIOPCServer1, 1, "group1", 1, LOCALE_ID);
         AddYFJGroup(groupConnectionGroup);
     }
     else
     {
         YFJGroup.RemovedItem();
         AddYFJGroup(groupConnectionGroup);
     }
 }
예제 #18
0
        /// <summary>
        /// 创建opc连接
        /// </summary>
        public string[] CreateOPCServer()
        {
            string[] strmessage = new string[2];
            try
            {
                svrComponenttyp = Type.GetTypeFromProgID(SERVER_NAME);
                pIOPCServer     = (IOPCServer)Activator.CreateInstance(svrComponenttyp);        //创建服务器连接对象
            }
            catch (ArgumentNullException)
            {
                strmessage[0] += "服务器对象创建失败,未能建立异型烟链板机plc连接,请检查plc连接与opc服务是否正常";
                strmessage[1]  = "0";
                return(strmessage);
            }
            catch (Exception ex)
            {
                strmessage[0] += ex;
                strmessage[1]  = "0";
                return(strmessage);
            }
            ShapeGroup1 = new Group(pIOPCServer, 1, "group1", 1, LOCALE_ID);         //创建组
            ShapeGroup1.addItem(ItemCollection.GetTaskStatusBySend_yxy());           //添加项到组  包装机异型烟链板机(合包)数据写入DB块
            ShapeGroup2 = new Group(pIOPCServer, 2, "group2", 1, LOCALE_ID);         //创建组
            ShapeGroup2.addItem(ItemCollection.GetTaskStatusByComplete_yxy());       //添加项到组  (合包)完成信号的DB块

            ShapeGroup3 = new Group(pIOPCServer, 3, "group3", 1, LOCALE_ID);         //创建组
            ShapeGroup3.addItem(ItemCollection.GetTaskStatusByComplete_cgy());       //添加项到组  翻板机完成信号的DB块
            ShapeGroup4 = new Group(pIOPCServer, 4, "group4", 1, LOCALE_ID);         //创建组
            ShapeGroup4.addItem(ItemCollection.GetTaskStatusBySend_cgy());           //添加项到组  包装机常规烟翻板数据写入DB块

            UnNormalGroup = new Group(pIOPCServer, 5, "group5", 1, LOCALE_ID);
            UnNormalGroup.addItem(ItemCollection.GetUnNormalWorkPlaceItem());

            SpyGroup6 = new Group(pIOPCServer, 6, "group6", 1, LOCALE_ID);  //创建监控标志位的
            SpyGroup6.addItem(ItemCollection.GetSpyStateItem());

            ShapeGroup7 = new Group(pIOPCServer, 7, "group7", 1, LOCALE_ID);  //翻板任务清空
            ShapeGroup7.addItem(ItemCollection.ClearAndStop_cgy());

            ShapeGroup8 = new Group(pIOPCServer, 8, "group8", 1, LOCALE_ID);  //倍速链任务清空
            ShapeGroup8.addItem(ItemCollection.ClearAndStop_yxy());

            strmessage[0] += "";//写入校验plc连接尝试结果
            strmessage[1]  = "1";
            return(strmessage);
        }
예제 #19
0
 public Group(IOPCServer server, int cliengGroup, string groupName, int isActive, int LOCALE_ID)
 {
     pIOPCServer = server;
     hTimeBias   = GCHandle.Alloc(TimeBias, GCHandleType.Pinned);
     hDeadband   = GCHandle.Alloc(deadband, GCHandleType.Pinned);
     pIOPCServer.AddGroup(groupName,
                          isActive,
                          dwRequestedUpdateRate,
                          cliengGroup,
                          hTimeBias.AddrOfPinnedObject(),
                          hDeadband.AddrOfPinnedObject(),
                          LOCALE_ID,
                          out pSvrGroupHandle,
                          out pRevUpdateRate,
                          ref iidRequiredInterface,
                          out pobjGroup1);
     InitReqIOInterfaces();
 }
예제 #20
0
        public FmSortMainbelt1(bool plcstatetag, IOPCServer pIOPCServer, int LOCALE_ID, string SERVER_NAME, string linenum)
        {
            this.plcstatetag = plcstatetag;
            this.linenum     = linenum;
            if (plcstatetag)
            {
                svrComponenttyp = Type.GetTypeFromProgID(SERVER_NAME);

                pIOPCServer = (IOPCServer)Activator.CreateInstance(svrComponenttyp);

                ShapeGroup = new Group(pIOPCServer, 1, "group1", 1, LOCALE_ID);
                ShapeGroup.addItem(ItemCollection_new.GetSortBeltByShapeItem(linenum));
            }
            InitializeComponent();

            timer1.Interval = 8000;
            timer1.Start();
        }
예제 #21
0
        /// <summary>
        /// Removes a group.
        /// </summary>
        public void RemoveGroup(int groupHandle)
        {
            string methodName = "IOPCServer.RemoveGroup";

            try
            {
                IOPCServer server = BeginComCall <IOPCServer>(methodName, true);
                server.RemoveGroup(groupHandle, 0);
            }
            catch (Exception e)
            {
                ComUtils.TraceComError(e, methodName);
                return;
            }
            finally
            {
                EndComCall(methodName);
            }
        }
예제 #22
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);
        }
예제 #23
0
        public void DeletePublic(bool bForce)
        {
            if (!this.state.Public)
            {
                Marshal.ThrowExceptionForHR(-2147467259);
            }
            IOPCServerPublicGroups ifServer = null;

            ifServer = (IOPCServerPublicGroups)this.ifServer;
            if (ifServer == null)
            {
                Marshal.ThrowExceptionForHR(-2147467262);
            }
            int handleServer = this.state.HandleServer;

            this.Remove(false);
            ifServer.RemovePublicGroup(handleServer, bForce);
            ifServer = null;
        }
예제 #24
0
        void IOPCServer.AddGroup([In, MarshalAs(UnmanagedType.LPWStr)] string szName, [In, MarshalAs(UnmanagedType.Bool)] bool bActive, [In] int dwRequestedUpdateRate, [In] int hClientGroup, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0, SizeConst = 1)] int[] pTimeBias, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0, SizeConst = 1)] float[] pPercentDeadband, [In] int dwLCID, out int phServerGroup, out int pRevisedUpdateRate, [In] ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppUnk)
        {
            string currentTestMethod = Dottest.Framework.Stubs.CurrentTestMethod.Name;

            if (currentTestMethod.Equals("TestErr_internalAdd02"))
            {
                ppUnk = null;
                pRevisedUpdateRate = 500;
                phServerGroup      = 1;
            }
            else
            {
                IOPCServer opcServerinter = (IOPCServer)this;
                //OPCTrendLib.OPCData.OpcGroup group = new OPCTrendLib.OPCData.OpcGroup(ref opcServerinter, false, szName, bActive, dwRequestedUpdateRate);
                OPCGroupStateMgtClass opcGrp = new OPCGroupStateMgtClass();
                ppUnk = opcGrp;
                pRevisedUpdateRate = 500;
                phServerGroup      = 1;
            }
        }
예제 #25
0
        public OpcGroup(ref IOPCServer ifServerLink, bool isPublic, string groupName, bool setActive, int requestedUpdateRate)
        {
            ifServer = ifServerLink;

            state.Name = groupName;
            state.Public = isPublic;
            state.UpdateRate = requestedUpdateRate;
            state.Active = setActive;
            state.TimeBias = 0;
            state.PercentDeadband = 0.0f;
            state.LocaleID = 0;
            state.HandleClient = this.GetHashCode();
            state.HandleServer = 0;

            // marshaling helpers:
            typeOPCITEMDEF = typeof(OPCITEMDEFintern);
            sizeOPCITEMDEF = Marshal.SizeOf(typeOPCITEMDEF);
            typeOPCITEMRESULT = typeof(OPCITEMRESULTintern);
            sizeOPCITEMRESULT = Marshal.SizeOf(typeOPCITEMRESULT);
        }
예제 #26
0
 public void Disconnect()
 {
     if (pIOPCServer != null)
     {
         Marshal.ReleaseComObject(pIOPCServer);
         pIOPCServer = null;
     }
     if (OnlyTaskGorup != null)
     {
         OnlyTaskGorup.Release();
     }
     if (FinishOnlyGoroup != null)
     {
         FinishOnlyGoroup.Release();
     }
     if (SpyBiaozhiGroup != null)
     {
         SpyBiaozhiGroup.Release();
     }
 }
예제 #27
0
        void Connction()
        {
            txtMainInfo.Text = "连接服务器中.....";

            Type svrComponenttyp;
            Guid iidRequiredInterface = typeof(IOPCItemMgt).GUID;

            svrComponenttyp = Type.GetTypeFromProgID(SERVER_NAME);
            try
            {
                pIOPCServer = (IOPCServer)Activator.CreateInstance(svrComponenttyp); //连接本地服务器
                AddUnionTaskGroup();                                                 //合流
                AddSortingTaskGroup();                                               //预分拣
                CheckConnection();
            }
            catch (Exception ex)
            {
                txtMainInfo.Text = "错误异常:请检查环境配置!,连接失败!!!";
                writeLog.Write("错误异常:" + ex.Message);
            }
        }
예제 #28
0
        internal OPCGroup(ref IOPCServer p_IfServerLink, bool p_IsPublic, string p_GroupName, bool p_SetActive,
                          int p_RequestedUpdateRate)
        {
            m_IfServer = p_IfServerLink;

            m_State.Name            = p_GroupName;
            m_State.Public          = p_IsPublic;
            m_State.UpdateRate      = p_RequestedUpdateRate;
            m_State.Active          = p_SetActive;
            m_State.TimeBias        = 0;
            m_State.PercentDeadband = 0.0f;
            m_State.LocaleID        = 0;
            m_State.HandleClient    = GetHashCode();
            m_State.HandleServer    = 0;

            // Marshaling helpers
            m_TypeOPCItemDef    = typeof(OPCItemDefIntern);
            m_SizeOPCItemDef    = Marshal.SizeOf(m_TypeOPCItemDef);
            m_TypeOPCItemResult = typeof(OPCItemResultIntern);
            m_SizeOPCItemResult = Marshal.SizeOf(m_TypeOPCItemResult);
        }
예제 #29
0
        public void Connect(string clsidOPCserver)
        {
            this.Disconnect();
            Type typeFromProgID = Type.GetTypeFromProgID(clsidOPCserver);

            if (typeFromProgID == null)
            {
                Marshal.ThrowExceptionForHR(-1073479663);
            }
            this.OPCserverObj = Activator.CreateInstance(typeFromProgID);
            this.ifServer     = (IOPCServer)this.OPCserverObj;
            if (this.ifServer == null)
            {
                Marshal.ThrowExceptionForHR(-2147220992);
            }
            this.ifCommon        = (IOPCCommon)this.OPCserverObj;
            this.ifBrowse        = (IOPCBrowseServerAddressSpace)this.ifServer;
            this.ifItmProps      = (IOPCItemProperties)this.ifServer;
            this.cpointcontainer = (IConnectionPointContainer)this.OPCserverObj;
            this.AdviseIOPCShutdown();
        }
예제 #30
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());
     }
 }
예제 #31
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;
        }
        /// <summary>
        /// Returns the current server status.
        /// </summary>
        /// <returns>The current server status.</returns>
        public OpcServerStatus GetServerStatus()
        {
            lock (this)
            {
                if (server_ == null)
                {
                    throw new NotConnectedException();
                }
                string methodName = "IOPCServer.GetStatus";

                // initialize arguments.
                IntPtr pStatus;

                // invoke COM method.
                try
                {
                    IOPCServer server = BeginComCall <IOPCServer>(methodName, true);
                    (server).GetStatus(out pStatus);

                    if (DCOMCallWatchdog.IsCancelled)
                    {
                        throw new Exception($"{methodName} call was cancelled due to response timeout");
                    }
                }
                catch (Exception e)
                {
                    ComCallError(methodName, e);
                    throw Technosoftware.DaAeHdaClient.Com.Interop.CreateException(methodName, e);
                }
                finally
                {
                    EndComCall(methodName);
                }

                // return status.
                return(Interop.GetServerStatus(ref pStatus, true));
            }
        }
        /// <summary>
        /// Returns the localized text for the specified result code.
        /// </summary>
        /// <param name="locale">The locale name in the format "[languagecode]-[country/regioncode]".</param>
        /// <param name="resultId">The result code identifier.</param>
        /// <returns>A message localized for the best match for the requested locale.</returns>
        public override string GetErrorText(string locale, OpcResult resultId)
        {
            lock (this)
            {
                if (server_ == null)
                {
                    throw new NotConnectedException();
                }
                string methodName = "IOPCServer.GetErrorString";

                // invoke COM method.
                try
                {
                    IOPCServer server = BeginComCall <IOPCServer>(methodName, true);

                    (server).GetErrorString(
                        resultId.Code,
                        Technosoftware.DaAeHdaClient.Com.Interop.GetLocale(locale),
                        out var errorText);

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

                    return(errorText);
                }
                catch (Exception e)
                {
                    ComCallError(methodName, e);
                    throw Technosoftware.DaAeHdaClient.Com.Interop.CreateException("IOPCServer.GetErrorString", e);
                }
                finally
                {
                    EndComCall(methodName);
                }
            }
        }
예제 #34
0
        /// <summary>
        /// plc的连接
        /// </summary>
        /// <param name="remoteServerName">连接的名字()</param>
        /// <param name="remoteServerIP">连接的ip地址</param>
        /// <returns></returns>
        public string ConnectRemoteServer()
        {
            string errText = string.Empty;

            try
            {
                //建议一个连接
                Type svrComponenttyp = Type.GetTypeFromCLSID(Guid.Parse("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"), "localhost");

                //            Type svrComponenttyp = Type.GetTypeFromCLSID(Guid.Parse("B6EACB30-42D5-11D0-9517-0020AFAA4B3C"), "localhost");
//                Type svrComponenttyp = Type.GetTypeFromProgID("OPC.SimaticNet", "localhost");
//                Type svrComponenttyp = Type.GetTypeFromProgID("KEPware.KEPServerEx.V6", "localhost");

                ServerObj = (IOPCServer)Activator.CreateInstance(svrComponenttyp);
            }

            catch (Exception ex)
            {
                errText = "连接远程服务器出现错误:" + ex.Message.ToString();
            }

            return(errText);
        }
예제 #35
0
 public void Disconnect()
 {
     if (pIOPCServer != null)
     {
         Marshal.ReleaseComObject(pIOPCServer);
         pIOPCServer = null;
     }
     if (taskgroup1 != null)
     {
         taskgroup1.Release();
     }
     if (taskGroup2 != null)
     {
         taskGroup2.Release();
     }
     if (statusGroup2 != null)
     {
         statusGroup2.Release();
     }
     if (statusGroup3 != null)
     {
         statusGroup3.Release();
     }
 }
예제 #36
0
        public void Connect( string	clsidOPCserver )
        {
            Disconnect();

            Type	typeofOPCserver = Type.GetTypeFromProgID( clsidOPCserver );
            if( typeofOPCserver == null )
            Marshal.ThrowExceptionForHR( HRESULTS.OPC_E_NOTFOUND );

            OPCserverObj = Activator.CreateInstance( typeofOPCserver );
            ifServer = (IOPCServer) OPCserverObj;
            if( ifServer == null )
            Marshal.ThrowExceptionForHR( HRESULTS.CONNECT_E_NOCONNECTION );

            // connect all interfaces
            ifCommon		= (IOPCCommon)						OPCserverObj;
            ifBrowse		= (IOPCBrowseServerAddressSpace)	ifServer;
            ifItmProps		= (IOPCItemProperties)				ifServer;
            cpointcontainer	= (UCOMIConnectionPointContainer)	OPCserverObj;
            AdviseIOPCShutdown();
        }
예제 #37
0
        public void Disconnect()
        {
            if( ! (shutdowncpoint == null) )
            {
            if( shutdowncookie != 0 )
                {
                shutdowncpoint.Unadvise( shutdowncookie );
                shutdowncookie = 0;
                }
            int	rc = Marshal.ReleaseComObject( shutdowncpoint );
            shutdowncpoint = null;
            }

            cpointcontainer = null;
            ifBrowse		= null;
            ifItmProps		= null;
            ifCommon		= null;
            ifServer		= null;
            if( ! (OPCserverObj == null) )
            {
            int rc = Marshal.ReleaseComObject( OPCserverObj );
            OPCserverObj = null;
            }
        }
예제 #38
0
        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;
        }
예제 #39
0
 public void Disconnect()
 {
     ifServer = null;
     if (!(OPCserverObj == null))
     {
         int rc = Marshal.ReleaseComObject(OPCserverObj);
         OPCserverObj = null;
     }
 }
예제 #40
0
        public void Connect(Guid clsidOPCserver, string szPCName)
        {
            try
            {
                Disconnect();
                Guid[] iids = new System.Guid[1] { new System.Guid("39c13a4d-011e-11d0-9675-0020afd8adb3") }; // IOPServer - maybe unknown better
                if (nSimpleOPCActivate == 1)
                {
                    Type typeofOPCserver;

                    if (szPCName.Length > 0)
                        typeofOPCserver = Type.GetTypeFromCLSID(clsidOPCserver, szPCName);
                    else
                        typeofOPCserver = Type.GetTypeFromCLSID(clsidOPCserver);

                    if (typeofOPCserver == null)
                        Marshal.ThrowExceptionForHR(HRESULTS.E_FAIL);

                    OPCserverObj = Activator.CreateInstance(typeofOPCserver);
                }
                else
                {
                    object[] ptrs = DCOM.CoCreateInstanceEx(clsidOPCserver,
                        ClsCtx.All, szPCName, iids,
                        eRpcAuthzSrv,
                        RpcAuthzSrv.None,
                        "",
                        eRpcAuthnLevel,
                        eRpcImpersLevel,
                        sDomain,
                        sUser,
                        sPassword);
                    OPCserverObj = ptrs[0];
                }
                ifServer = (IOPCServer)OPCserverObj;
                if (ifServer == null)
                    Marshal.ThrowExceptionForHR(HRESULTS.CONNECT_E_NOCONNECTION);
                Logger.LogMessage("OPC Server Connected\n", Logger.INFORMATION );
            }
            catch (Exception e)
            {
                ifServer = null;
                Logger.LogMessage("OPC Connect Exception: " + e.Message, Logger.INFORMATION);
                throw e;
            }
        }
예제 #41
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;
        }