/// <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); } }
/// <summary> /// 新增新的序号与位置对应 /// </summary> public void Add(int index, int pos) { TagIndexAndPos tagIndexAndPos = new TagIndexAndPos(index, pos); this.TagValuePosList.Add(tagIndexAndPos); }