private bool find_plc(TreeNodeCollection nodes, ref PlcSoftware plc) { foreach (TreeNode n in nodes) { if (!find_plc(n.Nodes, ref plc)) { return(false); } if (!n.Checked) { continue; } if (!(n.Tag is DeviceItem)) { continue; } SoftwareContainer sw_cont = ((DeviceItem)n.Tag).GetService <SoftwareContainer>(); if (sw_cont != null) { if (sw_cont.Software is PlcSoftware controller) { if (plc != null) { return(false); } plc = controller; } } } return(true); }
private Software GetDevice(Project project, string MLFB, string name, string devname) { foreach (Device device in project.Devices) { DeviceItemComposition deviceItemAggregation = device.DeviceItems; foreach (DeviceItem deviceItem in deviceItemAggregation) { if (deviceItem.Name == devname || device.Name == devname) { SoftwareContainer softwareContainer = deviceItem.GetService <SoftwareContainer>(); if (softwareContainer != null) { if (softwareContainer.Software is PlcSoftware) { return(softwareContainer.Software as PlcSoftware); } if (softwareContainer.Software is HmiTarget) { return(softwareContainer.Software as HmiTarget); } } } } } return(null); }
private void find_hmis(TreeNodeCollection nodes, ref List <HmiTarget> hmis) { foreach (TreeNode n in nodes) { find_hmis(n.Nodes, ref hmis); if (!n.Checked) { continue; } if (!(n.Tag is DeviceItem)) { continue; } SoftwareContainer sw_cont = ((DeviceItem)n.Tag).GetService <SoftwareContainer>(); if (sw_cont != null) { HmiTarget hmi_target = sw_cont.Software as HmiTarget; if (hmi_target != null) { hmis.Add(hmi_target); } } } }
public static IList <Device> GetHmiDevicesInGroup(DeviceUserGroup aDeviceUserGroup) { List <Device> returnHmiDevices = new List <Device>(); IList <Device> addHmiDevices; if (aDeviceUserGroup != null) { foreach (Device device in aDeviceUserGroup.Devices) { foreach (DeviceItem deviceItem in device.DeviceItems) { DeviceItem deviceItemToGetService = deviceItem as DeviceItem; SoftwareContainer container = deviceItemToGetService.GetService <SoftwareContainer>(); if (container != null) { if (container.Software is HmiTarget hmi) { returnHmiDevices.Add(device); break; } } } } //get PLCs in sub folders - recursive foreach (DeviceUserGroup group in aDeviceUserGroup.Groups) { addHmiDevices = GetPlcDevicesInGroup(group); returnHmiDevices.AddRange(addHmiDevices); } } return(returnHmiDevices); }
private bool find_hmi(TreeNodeCollection nodes, ref HmiTarget hmi) { foreach (TreeNode n in nodes) { if (!find_hmi(n.Nodes, ref hmi)) { return(false); } if (!n.Checked) { continue; } if (!(n.Tag is DeviceItem)) { continue; } SoftwareContainer sw_cont = ((DeviceItem)n.Tag).GetService <SoftwareContainer>(); if (sw_cont != null) { HmiTarget hmi_target = sw_cont.Software as HmiTarget; if (hmi_target != null) { if (hmi != null) { return(false); } hmi = hmi_target; } } } return(true); }
private void IterateThroughDevices(Project project) { // no project is open if (project == null) { return; } // shop a form to indicate work frmReadStructure read = new frmReadStructure(); read.Show(); read.BringToFront(); //Console.WriteLine(String.Format("Iterate through {0} device(s)", project.Devices.Count)); listBox1.Items.Clear(); listBox2.Items.Clear(); Application.DoEvents(); // search through devices foreach (Device device in project.Devices) { if (device.TypeIdentifier != null) { // we search only for PLCs if (device.TypeIdentifier == "System:Device.S71500") { //Console.WriteLine(String.Format("Found {0}", device.Name)); listBox1.Items.Add(device.Name); // let's get the CPU foreach (DeviceItem item in device.DeviceItems) { if (item.Classification.ToString() == "CPU") { //Console.WriteLine(String.Format("Found {0}", item.Name)); listBox2.Items.Add(item.Name); // get the software container SoftwareContainer softwareContainer = ((IEngineeringServiceProvider)item).GetService <SoftwareContainer>(); if (softwareContainer != null) { software = softwareContainer.Software as PlcSoftware; groups.LoadTreeView(treeView1, software); } } } } } } // close form read.Close(); read.Dispose(); this.BringToFront(); Application.DoEvents(); }
public void FindPlc() { _controller = null; PlcSoftware firstPlc = null; var number = 0; var devices = MyProject.Devices; foreach (var device in devices) { Console.WriteLine(@"Device: " + device.Name); if (device.Name != "SINAMICS G120") { foreach (var item in device.DeviceItems) { var deviceItem = (DeviceItem)item; //var plcSoftware = deviceItem.GetService<ISoftwareContainer>(); TIA Portal V14 SoftwareContainer softwareContainer = deviceItem.GetService <SoftwareContainer>(); if (softwareContainer != null) { Software softwareBase = softwareContainer.Software; PlcSoftware plcSoftware = softwareBase as PlcSoftware; if (plcSoftware != null) { Console.WriteLine(@"Software gefunden in: " + deviceItem.Name); _controller = plcSoftware; break; } } } } if (_controller != null) { break; } } if (_controller == null) { if (firstPlc != null) { if (number > 1) { throw new SystemException( "To much ControllerTarget in project"); } _controller = firstPlc; } else { throw new SystemException("No ControllerTarget in project."); } } }
static bool ProjectLeaf(object obj) { if (!(obj is DeviceItem)) { return(false); } SoftwareContainer sw_cont = ((DeviceItem)obj).GetService <SoftwareContainer>(); return(sw_cont != null); }
/// <summary> /// Get software object from provided DeviceItem /// </summary> /// <param name="device"></param> /// <returns>PlcSoftware object</returns> public static PlcSoftware GetSoftwareFrom(DeviceItem device) { SoftwareContainer softwareContainer = ((IEngineeringServiceProvider)device).GetService <SoftwareContainer>(); if (softwareContainer != null) { return(softwareContainer.Software as PlcSoftware); } return(null); }
private void btn_AddHW_Click(object sender, EventArgs e) { btn_AddHW.Enabled = false; string MLFB = "OrderNumber:" + txt_OrderNo.Text + "/" + txt_Version.Text; string name = txt_AddDevice.Text; string devname = "station" + txt_AddDevice.Text; bool found = false; foreach (Device device in MyProject.Devices) { DeviceItemComposition deviceItemAggregation = device.DeviceItems; foreach (DeviceItem deviceItem in deviceItemAggregation) { if (deviceItem.Name == devname || device.Name == devname) { SoftwareContainer softwareContainer = deviceItem.GetService <SoftwareContainer>(); if (softwareContainer != null) { if (softwareContainer.Software is PlcSoftware) { PlcSoftware controllerTarget = softwareContainer.Software as PlcSoftware; if (controllerTarget != null) { found = true; } } if (softwareContainer.Software is HmiTarget) { HmiTarget hmitarget = softwareContainer.Software as HmiTarget; if (hmitarget != null) { found = true; } } } } } } if (found == true) { txt_Status.Text = "Device " + txt_Device.Text + " already exists"; } else { Device deviceName = MyProject.Devices.CreateWithItem(MLFB, name, devname); txt_Status.Text = "Add Device Name: " + name + " with Order Number: " + txt_OrderNo.Text + " and Firmware Version: " + txt_Version.Text; } btn_AddHW.Enabled = true; }
// Device items private static void handleDeviceItem(IList <HmiTarget> hmi, DeviceItem item) { SoftwareContainer sw_cont = item.GetService <SoftwareContainer>(); if (sw_cont != null) { if (sw_cont.Software is HmiTarget hmi_target) { hmi.Add(hmi_target); } } IterDeviceItem(hmi, item.DeviceItems); }
private void Compile(object sender, EventArgs e) { btn_CompileHW.Enabled = false; string devname = txt_Device.Text; bool found = false; foreach (Device device in MyProject.Devices) { DeviceItemComposition deviceItemAggregation = device.DeviceItems; foreach (DeviceItem deviceItem in deviceItemAggregation) { if (deviceItem.Name == devname || device.Name == devname) { SoftwareContainer softwareContainer = deviceItem.GetService <SoftwareContainer>(); if (softwareContainer != null) { if (softwareContainer.Software is PlcSoftware) { PlcSoftware controllerTarget = softwareContainer.Software as PlcSoftware; if (controllerTarget != null) { found = true; ICompilable compiler = controllerTarget.GetService <ICompilable>(); CompilerResult result = compiler.Compile(); txt_Status.Text = "Compiling of " + controllerTarget.Name + ": State: " + result.State + " / Warning Count: " + result.WarningCount + " / Error Count: " + result.ErrorCount; } } if (softwareContainer.Software is HmiTarget) { HmiTarget hmitarget = softwareContainer.Software as HmiTarget; if (hmitarget != null) { found = true; ICompilable compiler = hmitarget.GetService <ICompilable>(); CompilerResult result = compiler.Compile(); txt_Status.Text = "Compiling of " + hmitarget.Name + ": State: " + result.State + " / Warning Count: " + result.WarningCount + " / Error Count: " + result.ErrorCount; } } } } } } if (found == false) { txt_Status.Text = "Found no device with name " + txt_Device.Text; } btn_CompileHW.Enabled = true; }
public delegate bool Filter(Object item); // Predicate for item // Discards all device items that isn't a controller or a container public static bool ControllerOnly(Object obj) { if (!(obj is DeviceItem)) { return(true); } DeviceItem item = (DeviceItem)obj; SoftwareContainer cont = item.GetService <SoftwareContainer>(); if (cont == null) { return(true); } return(cont.Software is PlcSoftware); }
public static PlcSoftware GetPlcSoftware(Device device) { //PlcSoftware plcSoftware = null; foreach (DeviceItem currentDeviceItem in device.DeviceItems.Where(d => d.Classification.ToString() == "CPU")) { //hole Softwareblöcke, die PLC_1 untergeordnet sind SoftwareContainer softwareContainer = currentDeviceItem.GetService <SoftwareContainer>(); if (softwareContainer != null) { //Console.WriteLine("DeviceItem: " + currentDeviceItem.Name); return(softwareContainer.Software as PlcSoftware); } } return(null); }
public static PlcSoftware GetPlcSoftware(Device device) //gets Plc SW of the device { // var allDeviceItems = device.DeviceItems; //also can use DeviceItemComppsition = var foreach (DeviceItem deviceItem in device.DeviceItems) { SoftwareContainer softwareContainer = deviceItem.GetService <SoftwareContainer>(); if (softwareContainer != null) { Software softwareBase = softwareContainer.Software; PlcSoftware plcSoftware = softwareBase as PlcSoftware; return(plcSoftware); } } return(null); }
static private PlcSoftware GetPlcSoftware(Device device) { DeviceItemComposition deviceItemComposition = device.DeviceItems; foreach (DeviceItem deviceItem in deviceItemComposition) { SoftwareContainer softwareContainer = deviceItem.GetService <SoftwareContainer>(); if (softwareContainer != null) { Software softwareBase = softwareContainer.Software; PlcSoftware plcSoftware = softwareBase as PlcSoftware; return(plcSoftware); } } return(null); }
public static HmiTarget GetHmiTarget(Device device) //gets HMI target of device { var allDeviceItems = device.DeviceItems; foreach (DeviceItem deviceItem in allDeviceItems) { SoftwareContainer softwareContainer = deviceItem.GetService <SoftwareContainer>(); if (softwareContainer != null) { Software software = softwareContainer.Software; HmiTarget hmiTarget = software as HmiTarget; return(hmiTarget); } } return(null); }
private void FindDeviceItems(DeviceItemAssociation items) { foreach (DeviceItem item in items) { SoftwareContainer sw_cont = item.GetService <SoftwareContainer>(); if (sw_cont != null) { Software sw = sw_cont.Software; if (sw.Name != plc?.Name && sw.Name != hmi?.Name) { if (sw is PlcSoftware || sw is HmiTarget) { worker.ReportProgress(50, new NodeData(NodeDest.To, item.Name, sw)); } } } FindDeviceItems(item.Items); } }
private static void GetDeviceObjects(DeviceItem deviceItem) { // Get software SoftwareContainer softwareContainer = ((IEngineeringServiceProvider)deviceItem).GetService <SoftwareContainer>(); if (softwareContainer != null) { switch (softwareContainer.Software) { case HmiTarget hmiTarget: Console.WriteLine("Get HmiTarget..."); _hmiTarget = hmiTarget; break; case PlcSoftware plcSoftware: Console.WriteLine("Get PlcSoftware..."); _plcSoftware = plcSoftware; break; } } // Get network interface var networkInterface = deviceItem.GetService <NetworkInterface>(); if (networkInterface != null) { foreach (var node in networkInterface.Nodes) { Nodes.Add(node); } } foreach (var subDeviceItem in deviceItem.DeviceItems) { GetDeviceObjects(subDeviceItem); } }
private void find_plcs(TreeNodeCollection nodes, ref List <PlcSoftware> plcs) { foreach (TreeNode n in nodes) { find_plcs(n.Nodes, ref plcs); if (!n.Checked) { continue; } if (!(n.Tag is DeviceItem)) { continue; } SoftwareContainer sw_cont = ((DeviceItem)n.Tag).GetService <SoftwareContainer>(); if (sw_cont != null) { if (sw_cont.Software is PlcSoftware controller) { plcs.Add(controller); } } } }
static void Main(string[] args) { if (args == null || args.Length == 0 || args.Length > 1) { Console.WriteLine("No arguments provided"); Console.WriteLine(@"Usage TiaExportBlocks.exe C:\Test"); Console.ReadLine(); } else { exportLocation = args[0]; Console.WriteLine("Export location is " + exportLocation); CheckDirectory(exportLocation); CheckDirectory(exportLocation + @"\scl"); CheckDirectory(exportLocation + @"\db"); CheckDirectory(exportLocation + @"\udt"); CheckDirectory(exportLocation + @"\tag_tables\xml"); CheckDirectory(exportLocation + @"\hmi_tag_tables\xml"); CheckDirectory(exportLocation + @"\hmi_text_lists\xml"); var watch = new System.Diagnostics.Stopwatch(); watch.Start(); Console.WriteLine("Enumerating TIA processes.."); foreach (TiaPortalProcess tiaPortalProcess in TiaPortal.GetProcesses()) { Console.WriteLine("Process ID " + tiaPortalProcess.Id); Console.WriteLine("Project PATH " + tiaPortalProcess.ProjectPath); TiaPortal tiaPortal = tiaPortalProcess.Attach(); foreach (Project project in tiaPortal.Projects) { Console.WriteLine("Handling project " + project.Name); foreach (Siemens.Engineering.HW.Device device in project.Devices) { Console.WriteLine("Handling device " + device.Name + " of type [" + device.TypeIdentifier + "]"); if (device.TypeIdentifier == "System:Device.S71500") { foreach (Siemens.Engineering.HW.DeviceItem deviceItem in device.DeviceItems) { Console.WriteLine("Handling device item " + deviceItem.Name + " of type " + deviceItem.TypeIdentifier); if (deviceItem.Name.Contains("PLC")) { Console.WriteLine("Handling PLC device item"); Siemens.Engineering.HW.Features.SoftwareContainer softwareContainer = ((IEngineeringServiceProvider)deviceItem).GetService <SoftwareContainer>(); if (softwareContainer != null) { PlcSoftware software = softwareContainer.Software as PlcSoftware; ExportBlocks(software); ExportTypes(software); ExportAllTagTables(software); } } } } else { foreach (Siemens.Engineering.HW.DeviceItem deviceItem in device.DeviceItems) { Console.WriteLine("Handling device item [" + deviceItem.Name + "] of type [" + deviceItem.TypeIdentifier + "]"); if (device.Name.Contains("APS")) { Console.WriteLine("Handling HMI device item"); DeviceItem deviceItemToGetService = deviceItem as DeviceItem; SoftwareContainer softwareContainer = deviceItemToGetService.GetService <SoftwareContainer>(); if (softwareContainer != null) { HmiTarget software = softwareContainer.Software as HmiTarget; ExportAllTagTablesFromHMITarget(software); ExportTextLists(software); } else { Console.WriteLine("SW Container is null"); } } } } } } } watch.Stop(); Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms"); Console.WriteLine("Done"); //Console.ReadLine(); return; } }
// Device items private static void handleDeviceItem(NodeHandler handler, DeviceItem item) { NodeHandler child_handler = handler.Enter(item, item.Name); if (child_handler != null) { SoftwareContainer sw_cont = item.GetService <SoftwareContainer>(); if (sw_cont != null) { PlcSoftware controller = sw_cont.Software as PlcSoftware; if (controller != null) { NodeHandler block_handler = child_handler.Enter(controller.BlockGroup, "Blocks"); if (block_handler != null) { iterDataBlock(block_handler, controller.BlockGroup.Blocks); iterBlockFolder(block_handler, controller.BlockGroup.Groups); } child_handler.Exit(controller.BlockGroup); NodeHandler type_handler = child_handler.Enter(controller.TypeGroup, "Types"); if (type_handler != null) { iterType(type_handler, controller.TypeGroup.Types); iterTypeFolder(block_handler, controller.TypeGroup.Groups); } child_handler.Exit(controller.TypeGroup); } HmiTarget hmi_target = sw_cont.Software as HmiTarget; if (hmi_target != null) { //Console.WriteLine("HMI target"); NodeHandler screen_handler = child_handler.Enter(hmi_target.ScreenFolder, "Screens"); if (screen_handler != null) { //Console.WriteLine("Iterating screens"); iterScreen(screen_handler, hmi_target.ScreenFolder.Screens); iterScreenFolder(screen_handler, hmi_target.ScreenFolder.Folders); } child_handler.Exit(hmi_target.ScreenFolder); NodeHandler template_handler = child_handler.Enter(hmi_target.ScreenTemplateFolder, "Templates"); if (template_handler != null) { //Console.WriteLine("Iterating templates"); iterScreenTemplate(template_handler, hmi_target.ScreenTemplateFolder.ScreenTemplates); iterScreenTemplateFolder(template_handler, hmi_target.ScreenTemplateFolder.Folders); } child_handler.Exit(hmi_target.ScreenTemplateFolder); NodeHandler popup_handler = child_handler.Enter(hmi_target.ScreenPopupFolder, "Popups"); if (popup_handler != null) { //Console.WriteLine("Iterating popups"); iterScreenPopup(template_handler, hmi_target.ScreenPopupFolder.ScreenPopups); iterScreenPopupFolder(template_handler, hmi_target.ScreenPopupFolder.Folders); } child_handler.Exit(hmi_target.ScreenPopupFolder); NodeHandler connection_handler = child_handler.Enter(hmi_target.Connections, "Connections"); if (connection_handler != null) { foreach (Connection connection in hmi_target.Connections) { connection_handler.Enter(connection, connection.Name); connection_handler.Exit(connection); } } child_handler.Exit(hmi_target.Connections); } } NetworkInterface netif = item.GetService <NetworkInterface>(); if (netif != null) { NodeHandler netif_handler = child_handler.Enter(netif, "Nodes"); if (netif_handler != null) { IterNetNodes(netif_handler, netif.Nodes); } child_handler.Exit(netif); } } handler.Exit(item); }
public static Software GetSoftware(DeviceItem deviceItem) { SoftwareContainer softwareContainer = deviceItem.GetService <SoftwareContainer>(); return(softwareContainer?.Software); }
private void IterateThroughDevices(Project project) { // no project is open if (project == null) { return; } // shop a form to indicate work frmReadStructure read = new frmReadStructure(); read.Show(); Application.DoEvents(); //Console.WriteLine(String.Format("Iterate through {0} device(s)", project.Devices.Count)); listBox1.Items.Clear(); listBox2.Items.Clear(); treeView1.Nodes.Clear(); // search through devices foreach (Device device in project.Devices) { if (device.TypeIdentifier != null) { // we search only for PLCs if (device.TypeIdentifier == "System:Device.S71500") { //Console.WriteLine(String.Format("Found {0}", device.Name)); listBox1.Items.Add(device.Name); // let's get the CPU foreach (DeviceItem item in device.DeviceItems) { if (item.Classification.ToString() == "CPU") { //Console.WriteLine(String.Format("Found {0}", item.Name)); listBox2.Items.Add(item.Name); // get the software container SoftwareContainer softwareContainer = ((IEngineeringServiceProvider)item).GetService <SoftwareContainer>(); if (softwareContainer != null) { software = softwareContainer.Software as PlcSoftware; //Console.WriteLine("Found : " + software.Name); // start update treeview treeView1.BeginUpdate(); // add root node TreeNode root = new TreeNode(software.Name); root.Tag = software.BlockGroup; treeView1.Nodes.Add(root); cFunctionGroups func = new cFunctionGroups(); func.AddPlcBlocks(software.BlockGroup, root); //AddPlcBlocks(software.BlockGroup, root); // add data types TreeNode dataTypes = new TreeNode("Data types"); dataTypes.Tag = software.TypeGroup; treeView1.Nodes.Add(dataTypes); func.AddPlcTypes(software.TypeGroup, dataTypes); //AddPlcTypes(software.TypeGroup, dataTypes); dataTypes.Expand(); // end update treeView1.EndUpdate(); root.Expand(); } } } } } } // close form read.Close(); read.Dispose(); }