Skip to content

Commit 77a562b

Browse files
authored
Merge pull request #55 from siemens/16-can-i-use-the-comfort-functions-also-in-software-units
feat(SoftwareUnits): add examples for unit creation and configuration…
2 parents 133c0cb + 8efc043 commit 77a562b

File tree

7 files changed

+154
-75
lines changed

7 files changed

+154
-75
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ csharp_style_var_for_built_in_types = true:suggestion
232232
csharp_style_var_when_type_is_apparent = true:suggestion
233233
csharp_style_var_elsewhere = true:suggestion
234234

235+
csharp_style_expression_bodied_methods = false:none
236+
235237
# Space preferences
236238
csharp_space_after_cast = false
237239
csharp_space_after_colon_in_inheritance_clause = true
Binary file not shown.

MAC_use_cases/MAC_use_cases.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>MAC_use_cases</id>
5-
<version>1.0.9236-pre20250415085633</version>
5+
<version>1.0.9236-pre20250415123600</version>
66
<title>MAC_use_cases</title>
77
<authors>SIEMENS AG</authors>
88
<projectUrl>http://equipmentmodule.info/</projectUrl>

MAC_use_cases/Model/MAC_use_casesEM.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block;
88
using Siemens.Automation.ModularApplicationCreator.Tia.Modules;
99
using Siemens.Automation.ModularApplicationCreator.Tia.Openness;
10+
using Siemens.Automation.ModularApplicationCreator.Tia.Openness.SoftwareUnit;
1011
using Siemens.Automation.ModularApplicationCreator.Tia.TiaAttributeFuncs;
1112
using Siemens.Automation.ModularApplicationCreatorBasics.Logging;
1213

@@ -23,6 +24,8 @@ public class MAC_use_casesEM : BaseMAC_use_casesEM
2324
/// </summary>
2425
private PlcDevice m_plcDevice;
2526

27+
private ISoftwareUnit m_softwareUnit;
28+
2629
// <auto-generated>
2730
// This code was generated by a tool. Do not remove or modify the syntax of it.
2831
// This constructor is used at first creation of this object.
@@ -99,8 +102,8 @@ public override bool GenerateTiaPortal(TiaTemplateContext tiaTemplateContext, st
99102
m_plcDevice = GetPlcDevice(tiaTemplateContext);
100103
break;
101104

102-
// Add equipment module specific code for each generation phase here, which is
103-
// necessary before the ResourceManagement is called.
105+
// Add equipment module specific code for each generation phase here, which is
106+
// necessary before the ResourceManagement is called.
104107
}
105108

106109
ResourceManagement.Generate(tiaTemplateContext, this, generationPhaseName);
@@ -112,10 +115,7 @@ public override bool GenerateTiaPortal(TiaTemplateContext tiaTemplateContext, st
112115
switch (generationPhaseName)
113116
{
114117
case TiaGenerationPhases.Init:
115-
if (!IsGenerateHardwareChecked)
116-
{
117-
break;
118-
}
118+
if (!IsGenerateHardwareChecked) break;
119119
//Hardware config has to be in the Init phase.Otherwise, it can't be used in the MAC.
120120
var s120 = HardwareGeneration.GenerateS120(this, "S120MACTest", "S120DeviceTest",
121121
"this drive is generated with MAC");
@@ -134,6 +134,7 @@ public override bool GenerateTiaPortal(TiaTemplateContext tiaTemplateContext, st
134134

135135
var opennessTIAPortalProject = GeneralSupport.GetOpennessProject(tiaTemplateContext.TiaProject);
136136
var opennessCPU = GeneralSupport.GetOpennessDeviceItem(tiaTemplateContext.TiaDevice);
137+
m_softwareUnit = SoftwareUnits.GetOrCreateSoftwareUnit(m_plcDevice, "MyUnit", this);
137138

138139
myTO.ConfigureTO(myTO.TechnologicalObject, this);
139140

@@ -175,10 +176,15 @@ public override bool GenerateTiaPortal(TiaTemplateContext tiaTemplateContext, st
175176
"CreatedDbFromFailSafeFbMasterCopy",
176177
ProgrammingLanguage.F_LAD,
177178
m_plcDevice);
179+
GenericBlockCreation.CreateFunctionBlockInSoftwareUnit(m_softwareUnit, "MyFb_FBD",
180+
ProgrammingLanguage.FBD);
178181

179182
var myTagTable = CreateVariables.CreateTagTable(m_plcDevice, "myTagTable");
183+
var mySoftwareUnitTagTable = CreateVariables.CreateTagTable(m_softwareUnit, "mySoftwareUnitTagTable");
180184

181-
CreateVariables.CreateTagInTagTable(myTagTable, "%I", "187", "0", "myTag", "Bool", "myTagComment");
185+
CreateVariables.CreateTagInTagTable(myTagTable, "%I", "187", "0", "myTag_0", "Bool", "myTagComment");
186+
CreateVariables.CreateTagInTagTable(mySoftwareUnitTagTable, "%I", "188", "0", "myTag_1", "Bool",
187+
"myTagComment");
182188

183189
if (IsGenerateHardwareChecked)
184190
{
Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,77 @@
1-
using Siemens.Automation.ModularApplicationCreator.Tia.Openness;
1+
using Siemens.Automation.ModularApplicationCreator.Tia.Openness;
2+
using Siemens.Automation.ModularApplicationCreator.Tia.Openness.SoftwareUnit;
23

4+
namespace MAC_use_cases.Model.UseCases;
35

4-
namespace MAC_use_cases.Model.UseCases
6+
/// <summary>
7+
/// All the functions to configure and generate Tags and Tag Tables are defined here.
8+
/// </summary>
9+
public class CreateVariables
510
{
611
/// <summary>
7-
/// All the functions to configure and generate Tags and Tag Tables are defined here.
12+
/// Creates a new tag table or retrieves an existing one in the specified PLC device.
13+
/// \image html CreateTagTable.png
814
/// </summary>
9-
public class CreateVariables
15+
/// <param name="plcDevice">The PLC device where the tag table should be created or retrieved</param>
16+
/// <param name="tableName">The name for the tag table</param>
17+
/// <returns>A ControllerTags object representing the new or existing tag table</returns>
18+
/// <remarks>
19+
/// If a tag table with the specified name already exists, it will be returned instead of creating a new one.
20+
/// The returned tag table can be used to add, modify, or remove tags.
21+
/// </remarks>
22+
/// <example>
23+
/// <code>
24+
/// var tagTable = CreateTagTable(myPlcDevice, "ProcessTags");
25+
/// </code>
26+
/// </example>
27+
public static ControllerTags CreateTagTable(PlcDevice plcDevice, string tableName)
1028
{
11-
/// <summary>
12-
/// This Function creates a Tag Table
13-
/// \image html CreateTagTable.png
14-
/// </summary>
15-
/// <param name="plcDevice">The PLC on which the equipment module is implemented</param>
16-
/// <param name="tableName">The Name of the tag table</param>
17-
/// <returns>newTable -> the created tag table (empty)</returns>
18-
///
19-
20-
public static ControllerTags CreateTagTable(PlcDevice plcDevice, string tableName)
21-
{
22-
var newTable = plcDevice.Tags.GetOrCreateGlobalTagTable(tableName);
23-
24-
return newTable;
25-
}
26-
27-
28-
/// <summary>
29-
/// This Function creates a Tag in a Tag Table
30-
/// \image html CreateTag.png
31-
/// </summary>
32-
/// <param name="tagTable">An existing tag table in which you want to create a tag</param>
33-
/// <param name="addressType">Type of the address. (Input, Output)</param>
34-
/// <param name="addressByte">The Byte of the address</param>
35-
/// <param name="addressBit">The Bit of the address</param>
36-
/// <param name="tagName">Name of the tag</param>
37-
/// <param name="dataType">Date type of the tag</param>
38-
/// <param name="tagComment">Comment of the tag</param>
39-
public static void CreateTagInTagTable(ControllerTags tagTable, string addressType, string addressByte, string addressBit, string tagName, string dataType, string tagComment)
40-
{
41-
string tagAddress = addressType + addressByte.ToString() + "." + addressBit.ToString();
42-
43-
var tag = tagTable[tagName];
44-
45-
if (tag != null)
46-
{
47-
tag.Delete();
48-
}
49-
50-
tag = tagTable.AddTag(tagName, dataType, tagAddress);
51-
tag.SetComment("en-EN", tagComment);
52-
}
29+
return plcDevice.Tags.GetOrCreateGlobalTagTable(tableName);
30+
}
31+
32+
/// <summary>
33+
/// Creates a new tag table or retrieves an existing one in the specified software unit.
34+
/// </summary>
35+
/// <param name="softwareUnit">The software unit where the tag table should be created or retrieved</param>
36+
/// <param name="tableName">The name for the tag table</param>
37+
/// <returns>A ControllerTags object representing the new or existing tag table</returns>
38+
/// <remarks>
39+
/// This overload creates/retrieves a tag table within a specific software unit rather than at the PLC device level.
40+
/// If a tag table with the specified name already exists in the software unit, it will be returned instead of creating
41+
/// a new one.
42+
/// </remarks>
43+
/// <example>
44+
/// <code>
45+
/// var tagTable = CreateTagTable(mySoftwareUnit, "ModuleSpecificTags");
46+
/// </code>
47+
/// </example>
48+
public static ControllerTags CreateTagTable(ISoftwareUnitBase softwareUnit, string tableName)
49+
{
50+
return softwareUnit.Tags.GetOrCreateGlobalTagTable(tableName);
51+
}
52+
53+
54+
/// <summary>
55+
/// This Function creates a Tag in a Tag Table
56+
/// \image html CreateTag.png
57+
/// </summary>
58+
/// <param name="tagTable">An existing tag table in which you want to create a tag</param>
59+
/// <param name="addressType">Type of the address. (Input, Output)</param>
60+
/// <param name="addressByte">The Byte of the address</param>
61+
/// <param name="addressBit">The Bit of the address</param>
62+
/// <param name="tagName">Name of the tag</param>
63+
/// <param name="dataType">Date type of the tag</param>
64+
/// <param name="tagComment">Comment of the tag</param>
65+
public static void CreateTagInTagTable(ControllerTags tagTable, string addressType, string addressByte,
66+
string addressBit, string tagName, string dataType, string tagComment)
67+
{
68+
var tagAddress = addressType + addressByte + "." + addressBit;
69+
70+
var tag = tagTable[tagName];
71+
72+
tag?.Delete();
5373

74+
tag = tagTable.AddTag(tagName, dataType, tagAddress);
75+
tag.SetComment("en-EN", tagComment);
5476
}
5577
}

MAC_use_cases/Model/UseCases/GenericBlockCreation.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block;
77
using Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block.XmlBlocks.BlockFrames;
88
using Siemens.Automation.ModularApplicationCreator.Tia.Openness;
9+
using Siemens.Automation.ModularApplicationCreator.Tia.Openness.SoftwareUnit;
910
using Siemens.Automation.ModularApplicationCreator.Tia.TiaAttributeFuncs;
1011
using ProgrammingLanguage =
1112
Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block.ProgrammingLanguage;
@@ -163,6 +164,50 @@ public static void GenerateDB(string dbName, PlcDevice plcDevice, MAC_use_casesE
163164
axesDataDB.GenerateXmlBlock(plcDevice);
164165
}
165166

167+
/// <summary>
168+
/// Creates a new Function Block (FB) within the specified software unit with predefined interface parameters.
169+
/// The FB will contain two boolean inputs and one boolean output, all with remanence ignored.
170+
/// </summary>
171+
/// <param name="softwareUnitBase">The base software unit where the Function Block will be created</param>
172+
/// <param name="fbName">The name for the new Function Block</param>
173+
/// <param name="programmingLanguage">The programming language to be used for the Function Block</param>
174+
/// <remarks>
175+
/// The created Function Block will have the following interface:
176+
/// - Inputs:
177+
/// - Input_1 (Bool)
178+
/// - Input_2 (Bool)
179+
/// - Output:
180+
/// - Output (Bool)
181+
/// All parameters are configured with IgnoreRemanence setting.
182+
/// </remarks>
183+
/// <example>
184+
/// <code>
185+
/// CreateFunctionBlockInSoftwareUnit(mySoftwareUnit, "MyNewFB", ProgrammingLanguage.LAD);
186+
/// </code>
187+
/// </example>
188+
public static void CreateFunctionBlockInSoftwareUnit(ISoftwareUnitBase softwareUnitBase, string fbName,
189+
ProgrammingLanguage programmingLanguage)
190+
{
191+
var fb = new XmlFB(fbName);
192+
193+
fb.Interface[InterfaceSections.Input].Add(new InterfaceParameter("Input_1", "Bool")
194+
{
195+
Remanence = RemanenceSettings.IgnoreRemanence
196+
});
197+
fb.Interface[InterfaceSections.Input].Add(new InterfaceParameter("Input_2", "Bool")
198+
{
199+
Remanence = RemanenceSettings.IgnoreRemanence
200+
});
201+
fb.Interface[InterfaceSections.Output].Add(new InterfaceParameter("Output", "Bool")
202+
{
203+
Remanence = RemanenceSettings.IgnoreRemanence
204+
});
205+
206+
207+
//here an example how to create a multi instance call in the function block
208+
fb.GenerateXmlBlock(softwareUnitBase, programmingLanguage);
209+
}
210+
166211
/// <summary>
167212
/// This function creates a Block on the targetDevice in the targetContainer (folder under program blocks)
168213
/// </summary>
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
using Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block;
2-
using Siemens.Automation.ModularApplicationCreator.Tia.Helper.Create_XML_Block.XmlBlocks.BlockFrames;
31
using Siemens.Automation.ModularApplicationCreator.Tia.Openness;
2+
using Siemens.Automation.ModularApplicationCreator.Tia.Openness.SoftwareUnit;
43

54
namespace MAC_use_cases.Model.UseCases;
65

76
public class SoftwareUnits
87
{
98
/// <summary>
10-
/// Example how to create a failsafe function block within a safety software unit
11-
/// \image html CreateInstanceDB_via_XmlInstDB.png
9+
/// Retrieves an existing software unit or creates a new one if it doesn't exist in the specified PLC device.
1210
/// </summary>
13-
/// <param name="name">The name of the SoftwareUnit</param>
14-
/// <param name="plcDevice">The plc device</param>
15-
/// <param name="equipmentModule">The corresponding equipment module</param>
16-
public static void CreateFailSafeFunctionBlock(string softwareUnitName, PlcDevice plcDevice, MAC_use_casesEM module)
11+
/// <param name="plcDevice">The PLC device where the software unit should be located or created</param>
12+
/// <param name="myUnitName">The name of the software unit to get or create</param>
13+
/// <param name="macUseCasesEm">The MAC use case enumeration specifying the unit's configuration</param>
14+
/// <returns>An interface to the existing or newly created software unit</returns>
15+
/// <remarks>
16+
/// This method provides a convenient way to ensure a software unit exists, creating it if necessary.
17+
/// </remarks>
18+
public static ISoftwareUnit GetOrCreateSoftwareUnit(PlcDevice
19+
plcDevice, string myUnitName, MAC_use_casesEM macUseCasesEm)
1720
{
18-
var safetySoftwareUnit = plcDevice.SoftwareUnits.GetSafetySoftwareUnit();
19-
20-
var interfaceName = "myParameter"; //TODO name of the parameter in the function block
21-
var interfaceType = "\"ToEdit\""; //TODO type of the parameter in the function block
22-
23-
var failSafeFb = new XmlFailSafeFB("MyFailSafeFB");
24-
failSafeFb.Interface[InterfaceSections.Static].Add(new InterfaceParameter(interfaceName, interfaceType)
25-
{
26-
Remanence = RemanenceSettings.IgnoreRemanence
27-
});
28-
29-
//here an example how to create a multi instance call in the function block
21+
return plcDevice.SoftwareUnits.GetOrCreateSoftwareUnit(myUnitName, macUseCasesEm);
22+
}
3023

31-
failSafeFb.GenerateXmlBlock(safetySoftwareUnit);
24+
/// <summary>
25+
/// Retrieves the safety software unit from the specified PLC device.
26+
/// </summary>
27+
/// <param name="plcDevice">The PLC device containing the safety software unit</param>
28+
/// <returns>An interface to the safety software unit</returns>
29+
/// <remarks>
30+
/// This method only retrieves an existing safety software unit and does not create one if it doesn't exist.
31+
/// </remarks>
32+
public static ISafetySoftwareUnit GetSafetySoftwareUnit(PlcDevice
33+
plcDevice)
34+
{
35+
return plcDevice.SoftwareUnits.GetSafetySoftwareUnit();
3236
}
3337
}

0 commit comments

Comments
 (0)