示例#1
0
        /// <summary>
        /// 枚举OPC SERVER
        /// </summary>
        private void ServerEnum()
        {
            string ipAddress = textBox_OpcServerIp.Text;

            comboBox_OpcServerList.Items.Clear(); //清空已显示的OPC Server列表
            string[] array = _opcHelper.ServerEnum(ipAddress, out string message);
            if (!string.IsNullOrWhiteSpace(message))
            {
                MessageBox.Show(message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //假如Server列表为空,退出方法,否则为ListBoxControl添加Item
            if (array.Length == 0)
            {
                return;
            }
            comboBox_OpcServerList.Items.AddRange(array);
            comboBox_OpcServerList.SelectedIndex = 0;
        }
示例#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 :;
        }