/// <summary> /// 初始化 /// </summary> public static void Initialization(PlcConfig plc, WorkFolwConfig flow) { PlcConfig = plc; FlowConfig = flow; var modBuses = new List <Tuple <string, ModBusBase, Dictionary <string, PlcAddress> > >(); if (flow?.WorkFlows != null && flow?.WorkFlows?.Length > 0 && plc?.PlcDevices != null && plc?.PlcDevices?.Count > 0) { //创建设备对象和地址 foreach (var device in plc.PlcDevices) { ModBusBase modBus; var modbusmodel = ModbusConf.CnfModel.FirstOrDefault(x => x.Name == device.Name); if (modbusmodel == null) { continue; } if (modbusmodel.ConnectMode == Enums.ConnectMode.MODBUSRTU) { modBus = new ModbusRtuClient(modbusmodel.PortName, modbusmodel.BaudRate, modbusmodel.Station, modbusmodel.IsAddressStartWithZero, modbusmodel.DataBits, modbusmodel.StopBits, modbusmodel.Parity); } else { modBus = new ModbusTcp(modbusmodel.IpAddress, modbusmodel.Port, modbusmodel.Station, modbusmodel.IsAddressStartWithZero); } modBus.Name = device.Name; //地址 var addressdict = new Dictionary <string, PlcAddress>(); if (device.Addresses != null) { foreach (var address in device.Addresses) { addressdict[address.Name] = address; } } else { addressdict = new Dictionary <string, PlcAddress>(); } modBuses.Add(new Tuple <string, ModBusBase, Dictionary <string, PlcAddress> >(modBus.Name, modBus, addressdict)); } //创建工作流 foreach (var work in flow.WorkFlows) { var modbus = modBuses.FirstOrDefault(x => x.Item1 == work.PlcDeviceName); if (modbus == null) { Logger.Device.Info($"WorkFlow:{work.Name} Not Found PlcDeviceName:{work.PlcDeviceName}"); continue; } var engine = new WorkFlowEngine(work, modbus.Item2, modbus.Item3); workFlowEngines.Add(engine); ServiceManager.Services.Add(engine); } } }
private void Example() { var plccfg = Path.Combine(SysConf.ConfigPath, "ExamplePLCConfig.xml"); var workflowcfg = Path.Combine(SysConf.ConfigPath, "ExampleWorkFlow.xml"); var plcdevice = new PlcDevice() { Name = "测试设备", Addresses = new List <PlcAddress> { new PlcAddress { Name = "监听位1", Address = "1", DataType = PlcDataType.BIT }, new PlcAddress { Name = "监听位2", Address = "2", DataType = PlcDataType.BIT }, new PlcAddress { Name = "锁定位1", Address = "3", DataType = PlcDataType.BIT }, new PlcAddress { Name = "锁定位2", Address = "4", DataType = PlcDataType.BIT }, new PlcAddress { Name = "数据位1", Address = "5", DataType = PlcDataType.WORD }, new PlcAddress { Name = "数据位2", Address = "6", DataType = PlcDataType.WORD }, new PlcAddress { Name = "监听位3", Address = "20", DataType = PlcDataType.BIT } } }; var flow = new WorkFlow { Name = plcdevice.Name + "Flow", Listens = new List <PlcListen> { new PlcListen { Name = "监听1_2", StartAddressName = "监听位1", Len = 2, Items = new List <Item> { new Item { Index = 0, Value = "1", OnBlockName = "读取模块" }, new Item { Index = 1, Value = "1", OnBlockName = "写入模块" }, } }, new PlcListen { Name = "监听3", StartAddressName = "监听位3", Value = "1", OnBlockName = "自定义模块" } }, Blocks = new List <FlowBlock> { new FlowBlock { Name = "读取模块", Steps = new List <Step> { new Step { PlcAddressName = "数据位1", Type = ActionTypes.Read }, } }, new FlowBlock { Name = "写入模块", Steps = new List <Step> { new Step { PlcAddressName = "锁定位2", Type = ActionTypes.Write, Value = "0" }, new Step { PlcAddressName = "数据位2", Type = ActionTypes.Write }, new Step { PlcAddressName = "锁定位2", Type = ActionTypes.Write, Value = "1" }, } }, new FlowBlock { Name = "自定义模块", Steps = new List <Step> { new Step { ActionName = "redis" } } } } }; var work = new WorkFolwConfig { WorkFlows = new WorkFlow[] { flow } }; var plc = new PlcConfig { PlcDevices = new List <PlcDevice> { plcdevice } }; work.SaveToXMLFile(workflowcfg); plc.SaveToXMLFile(plccfg); }