Пример #1
0
 /// <summary>
 /// 获取标签
 /// </summary>
 /// <param name="Name">标签名称</param>
 /// <returns></returns>
 public static bool GetTag(string TagName, out Tag <short> TagObject, string PlcName = "")
 {
     TagObject = null;
     if (string.IsNullOrEmpty(PlcName))
     {
         foreach (var item in PLCGroups)
         {
             TagGroup <short> tagGroup = item.PlcDevice.TagGroups.FirstOrDefault(x => x.DataType == TagDataType.Short) as TagGroup <short>;
             if (GetTag <short>(tagGroup, TagName, out TagObject))
             {
                 return(true);
             }
         }
     }
     else
     {
         var plc = PLCGroups.FirstOrDefault(x => x.PlcDevice.Name == PlcName);
         if (plc != null)
         {
             TagGroup <short> tagGroup = plc.PlcDevice.TagGroups.FirstOrDefault(x => x.DataType == TagDataType.Short) as TagGroup <short>;
             return(GetTag <short>(tagGroup, TagName, out TagObject));
         }
     }
     return(false);
 }
Пример #2
0
 /// <summary>
 /// 获取标签
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="tagGroup"></param>
 /// <param name="TagName"></param>
 /// <param name="TagObject"></param>
 /// <returns></returns>
 private static bool GetTag <T>(TagGroup <T> tagGroup, string TagName, out Tag <T> TagObject)
 {
     TagObject = null;
     if (tagGroup != null)
     {
         if (tagGroup.Tags.Any(x => x.TagName == TagName))
         {
             TagObject = tagGroup.Tags.FirstOrDefault(x => x.TagName == TagName);
             return(true);
         }
     }
     return(false);
 }
Пример #3
0
        /// <summary>
        /// 标签队列初始化
        /// </summary>
        public void TagListInitial()
        {
            foreach (ITagGroup tagGroup in PlcDevice.TagGroups)
            {
                switch (tagGroup.DataType)
                {
                case TagDataType.Bool:
                    TagGroup <bool> boolGroup = tagGroup as TagGroup <bool>;
                    foreach (var item in boolGroup.Tags)
                    {
                        item.WriteValueEvent += plcDriverHelper.WriteValue;
                    }
                    break;

                case TagDataType.Short:
                    TagGroup <short> shortGroup = tagGroup as TagGroup <short>;
                    foreach (var item in shortGroup.Tags)
                    {
                        item.WriteValueEvent += plcDriverHelper.WriteValue;
                    }
                    break;

                case TagDataType.Int:
                    TagGroup <int> intGroup = tagGroup as TagGroup <int>;
                    foreach (var item in intGroup.Tags)
                    {
                        item.WriteValueEvent += plcDriverHelper.WriteValue;
                    }
                    break;

                case TagDataType.Float:
                    TagGroup <float> floatGroup = tagGroup as TagGroup <float>;
                    foreach (var item in floatGroup.Tags)
                    {
                        item.WriteValueEvent += plcDriverHelper.WriteValue;
                    }
                    break;

                case TagDataType.String:    //暂时不支持
                    TagGroup <string> stringGroup = tagGroup as TagGroup <string>;
                    foreach (var item in stringGroup.Tags)
                    {
                        item.WriteValueEvent += plcDriverHelper.WriteValue;
                    }
                    break;
                }
            }
        }
Пример #4
0
        /// <summary>
        /// 周期任务
        /// </summary>
        private void CycReadTagTask()
        {
            int count = 0;

            Thread.Sleep(3000);             //延时等待其他任务加载,然后再启动刷新
            plcDriverHelper.ConnectToPlc(); //连接放在线程里面,开始连接

            while (true)
            {
                Console.WriteLine($"周期任务:{PlcDevice.Name}-{PlcDevice.Ip}");
                plcDriverHelper.CheckConnect();
                if (PlcDevice.IsConnected)
                {
                    foreach (ITagGroup tagGroup in PlcDevice.TagGroups)
                    {
                        switch (tagGroup.DataType)
                        {
                        case TagDataType.Bool:
                            TagGroup <bool> boolGroup = tagGroup as TagGroup <bool>;
                            foreach (BatchReadSection section in boolGroup.SectionList)
                            {
                                bool[] values;
                                if (plcDriverHelper.BatchReadValue(section.StartAddr, section.ReadLength, out values))
                                {
                                    for (int i = 0; i < section.TagValuePosList.Count; i++)
                                    {
                                        TagIndexAndPos indexPost = section.TagValuePosList[i];
                                        boolGroup.Tags[indexPost.Index].TagValue = values[indexPost.Pos];
                                    }
                                }
                            }
                            break;

                        case TagDataType.Short:
                            TagGroup <short> shortGroup = tagGroup as TagGroup <short>;
                            foreach (BatchReadSection section in shortGroup.SectionList)
                            {
                                short[] values;
                                if (plcDriverHelper.BatchReadValue(section.StartAddr, section.ReadLength, out values))
                                {
                                    for (int i = 0; i < section.TagValuePosList.Count; i++)
                                    {
                                        TagIndexAndPos indexPost = section.TagValuePosList[i];
                                        shortGroup.Tags[indexPost.Index].TagValue = values[indexPost.Pos];
                                    }
                                }
                            }
                            break;

                        case TagDataType.Int:
                            TagGroup <int> intGroup = tagGroup as TagGroup <int>;
                            foreach (BatchReadSection section in intGroup.SectionList)
                            {
                                int[] values;
                                if (plcDriverHelper.BatchReadValue(section.StartAddr, section.ReadLength, out values))
                                {
                                    for (int i = 0; i < section.TagValuePosList.Count; i++)
                                    {
                                        TagIndexAndPos indexPost = section.TagValuePosList[i];
                                        intGroup.Tags[indexPost.Index].TagValue = values[indexPost.Pos];
                                    }
                                }
                            }
                            break;

                        case TagDataType.Float:
                            TagGroup <float> floatGroup = tagGroup as TagGroup <float>;
                            foreach (BatchReadSection section in floatGroup.SectionList)
                            {
                                float[] values;
                                if (plcDriverHelper.BatchReadValue(section.StartAddr, section.ReadLength, out values))
                                {
                                    for (int i = 0; i < section.TagValuePosList.Count; i++)
                                    {
                                        TagIndexAndPos indexPost = section.TagValuePosList[i];
                                        floatGroup.Tags[indexPost.Index].TagValue = values[indexPost.Pos];
                                    }
                                }
                            }
                            break;

                        case TagDataType.String:
                            TagGroup <string> stringGroup = tagGroup as TagGroup <string>;
                            foreach (BatchReadSection section in stringGroup.SectionList)
                            {
                                string[] values;
                                if (plcDriverHelper.BatchReadValue(section.StartAddr, section.ReadLength, out values))
                                {
                                    for (int i = 0; i < section.TagValuePosList.Count; i++)
                                    {
                                        TagIndexAndPos indexPost = section.TagValuePosList[i];
                                        stringGroup.Tags[indexPost.Index].TagValue = values[indexPost.Pos];
                                    }
                                }
                            }
                            break;
                        }
                    }
                    if (!string.IsNullOrEmpty(PlcDevice.HeartBit))//写入心跳位
                    {
                        plcDriverHelper.WriteValue(PlcDevice.HeartBit, true);
                    }
                }
                else
                {
                    if (NetworkHelper.IsNetWorkConnect(PlcDevice.Ip))//plc可以ping通
                    {
                        Thread.Sleep(1000);
                        ++count;
                        if (count >= 10)
                        {
                            count = 0;
                            _log.WriteLog($"{PlcDevice.Name} {PlcDevice.Ip}重新连接并初始化!");
                            plcDriverHelper.ConnectToPlc();
                        }
                    }
                    else
                    {
                        _log.WriteLog($"{PlcDevice.Name} {PlcDevice.Ip}连接断开,等待再次连接!");
                        Thread.Sleep(5000);//延时等待PLC再次连接
                    }
                }
                Thread.Sleep(PlcDevice.CycleTime);
            }
        }