/// <summary>
 /// OPC初始化
 /// </summary>
 private void InitOpcHelper()
 {
     this.OpcHelper = new OpcUtilHelper(this.Shiploader);
     new Thread(new ThreadStart(() =>
     {
         this.OpcHelper.Init();
         if (!string.IsNullOrWhiteSpace(this.OpcHelper.LastErrorMessage))
         {
             MessageBox.Show(this.OpcHelper.LastErrorMessage);
         }
     }))
     {
         IsBackground = true
     }.Start();
 }
Exemplo n.º 2
0
        /// <summary>
        /// OPC初始化
        /// </summary>
        private void OpcInit()
        {
            if (!Config.OpcEnabled)
            {
                return;
            }

            Const.WriteConsoleLog(string.Format("开始连接IP地址为{0}的OPC SERVER {1}...", Config.OpcServerIp, Config.OpcServerName));
            DataService_Opc dataService_Opc = new DataService_Opc();

            opcHelper = new OpcUtilHelper(1000, true);
            string[] servers = opcHelper.ServerEnum(Config.OpcServerIp, out _errorMessage);
            if (!string.IsNullOrWhiteSpace(_errorMessage))
            {
                Const.WriteConsoleLog(string.Format("枚举过程中出现问题:{0}", _errorMessage));
                goto END_OF_OPC;
            }
            if (servers == null || !servers.Contains(Config.OpcServerName))
            {
                Const.WriteConsoleLog(string.Format("无法找到指定OPC SERVER:{0}", Config.OpcServerName));
                goto END_OF_OPC;
            }
            DataTable table = dataService_Opc.GetOpcInfo();

            if (table == null || table.Rows.Count == 0)
            {
                Const.WriteConsoleLog(string.Format("在表中未找到任何OPC记录,将不进行读取或写入", Config.OpcServerName));
                goto END_OF_OPC;
            }
            List <OpcGroupInfo> groups   = new List <OpcGroupInfo>();
            List <DataRow>      dataRows = table.Rows.Cast <DataRow>().ToList();
            List <OpcItemInfo>  items    = null;
            int id = 0;

            foreach (var row in dataRows)
            {
                string itemId = row["item_id"].ConvertType <string>();
                if (string.IsNullOrWhiteSpace(itemId))
                {
                    continue;
                }
                int       groupId = row["group_id"].ConvertType <int>(), clientHandle = row["record_id"].ConvertType <int>();
                string    groupName = row["group_name"].ConvertType <string>(), fieldName = row["field_name"].ConvertType <string>();
                GroupType type = (GroupType)row["group_type"].ConvertType <int>();
                if (groupId != id)
                {
                    id = groupId;
                    groups.Add(new OpcGroupInfo(null, groupName /*, OpcDatasource*/)
                    {
                        GroupType = type, ListItemInfo = new List <OpcItemInfo>()
                    });
                    OpcGroupInfo groupInfo = groups.Last();
                    items = groupInfo.ListItemInfo;
                }
                items.Add(new OpcItemInfo(itemId, clientHandle, fieldName));
            }
            opcHelper.ListGroupInfo = groups;
            opcHelper.ConnectRemoteServer(Config.OpcServerIp, Config.OpcServerName, out _errorMessage);
            Const.WriteConsoleLog(string.Format("OPC连接状态:{0}", opcHelper.OpcConnected));
            if (!string.IsNullOrWhiteSpace(_errorMessage))
            {
                Const.WriteConsoleLog(string.Format("连接过程中出现问题:{0}", _errorMessage));
            }
            END_OF_OPC :;
        }