using Cowain.Bake.BLL; using Cowain.Bake.Common; using Cowain.Bake.Common.Core; using Cowain.Bake.Common.Enums; using Cowain.Bake.Common.Models; using Cowain.Bake.Communication.Interface; using Cowain.Bake.Model; using Cowain.Bake.Model.Models; using Prism.Ioc; using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; using Unity; namespace Cowain.Bake.Communication.PLC { public class PLCManage { IUnityContainer _unityContainer; private List plcList; public PLCManage(IUnityContainer unityContainer) { _unityContainer = unityContainer; plcList = _unityContainer.Resolve().GetConfig(EDeviceType.PLC); foreach (var item in plcList) { Assembly asm = Assembly.GetExecutingAssembly(); //获得当前程序的绝对路径 Type type = asm.GetType(MyPath.PLC + item.DriverName); unityContainer.RegisterSingleton(typeof(IPLCDevice), type, item.Name); var plc = _unityContainer.Resolve(item.Name); //几个PLC,就实例化几个 plc.Id = item.Id; plc.Name = item.Name; plc.DeviceName = item.DriverName; plc.GetJsonParam(item.Json); //2023.3.1修改,不需要读取StorageList,驱动自己分批读取 var plcTagList = _unityContainer.Resolve().GetTagList(item.Id); //获取PLC的标签和节点 if (plcTagList != null && plcTagList.Count > 0) { int q = 0; plcTagList.ForEach((x) => { plc.TagList.Add(x); plc.VariableList.Add(new Variable() { Id = (int)x.Id, Quality = false, VarType = x.VarType, ArrayLength = x.ArrayLen, StationId = x.StationId, ParamName = x.ParamName, Number = x.Number, Index = q++, TrigEnable = x.TirgEnable, TrigJson = x.TrigJson, OperType = x.OperType.Value, Json = x.Json, VarDesc = x.VarDesc, VarName = x.VarName, Address = x.Address, TagType = x.TagType }); }); foreach (var v in plc.VariableList) { if (!plc.VariableDic.ContainsKey(v.Id)) { plc.VariableDic.Add(v.Id, v); } else { LogHelper.Instance.Error($"PLC有相同的节点,Node=[{v.VarName}]"); } } } if (item.Enable) { plc.AnalysisAddress(); //把地址拆开,并计算长度 plc.GetStorageArea(); //获取存储区域 //plc.Connect(); //plc.StartRead(); Task t = new Task(() => //var t = Task.Run(async delegate { plc.Connect(); }); plc.StartRead(); } } } } }