private bool InitServerConn()
        {
            bool retValue = false;

            Console.WriteLine("正在尝试连接到OPCServer");
            while (retrycount > 0)
            {
                try
                {
                    rsLinxOPCDA = new RSLinxOPCDA();
                    bool isConnected = rsLinxOPCDA.Connect(ConnnectionUrl);
                    if (isConnected)
                    {
                        Logger.Info(string.Format("连接到OPC服务器{0}成功", ConnnectionUrl));
                        rsLinxOPCDA.CreateGroup(ConnnectionGroups, nFrequency);
                    }
                    if (tagList != null && tagList.Count > 0)
                    {
                        foreach (var item in tagList)
                        {
                            rsLinxOPCDA.AddItem(item.id);
                        }
                    }
                    retValue = true;
                    return(retValue);
                }
                catch (Exception ex)
                {
                    if (retrycount == 5)
                    {
                        Logger.Fatal("执行InitServerConn方法出错!" + ex.ToString());
                    }
                }
                finally
                {
                    if (!retValue)
                    {
                        Console.WriteLine(string.Format("连接到OPC服务器失败,剩余尝试次数为{0}!", retrycount.ToString()));
                        System.Threading.Thread.Sleep(30000);
                    }
                }
                retrycount--;
            }
            return(retValue);
        }
Exemple #2
0
        protected void ReadItems(RSLinxOPCDA rsLinxOPCDA)
        {
            do
            {
                try
                {
                    Opc.Da.ItemValueResult[] itemValues;
                    itemValues = rsLinxOPCDA.m_server.Read(rsLinxOPCDA.m_items);

                    foreach (Opc.Da.ItemValueResult itemValue in itemValues)
                    {
                        Console.WriteLine("Time: " + itemValue.Timestamp.ToString() + " Item: " + itemValue.ItemName + ", value: " + itemValue.Value.ToString());
                    }

                    System.Threading.Thread.Sleep(rsLinxOPCDA.m_groupState.UpdateRate);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            } while (true);
        }