Skip to content

Commit

Permalink
Merge pull request #6 from ix-ax/4-Discover_and_copy_Openness_dll
Browse files Browse the repository at this point in the history
HwId added
  • Loading branch information
TomKovac authored Dec 1, 2023
2 parents 2d51382 + 79db72a commit 77048dd
Show file tree
Hide file tree
Showing 59 changed files with 10,560 additions and 47,112 deletions.
201 changes: 197 additions & 4 deletions src/tia2ax/V18_0/ApiResolver/ApiWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Tia2Ax.DTOs;
using Tia2Ax.Utils;
using Tia2Ax.Setup;
using System.Net;
using System.Runtime.InteropServices;

namespace Tia2Ax.Interfaces
{
Expand Down Expand Up @@ -318,6 +320,8 @@ public void GetPlcList(ProjectItem projectItem)
{
PlcItem plc = new PlcItem() {Name = name, DeviceName = device.Name, Classification = "CPU", GroupPathInProject = "" , ConfiguredSubnets = GetAllNetworkInterfaces(device), LocalModules = GetAllLocalModules(device, name) };
projectItem.PlcItems.Add(plc);
IList<HwIdentifierItem> hwIdentifiers = GetAllHwIdentifiers(device, name);
AssignToPlc(hwIdentifiers, projectItem);
}
}

Expand All @@ -334,6 +338,8 @@ public void GetPlcList(ProjectItem projectItem)
{
IList<ModuleItem> localModules = GetAllLocalModules(device, name);
AssignToPlc(localModules, projectItem);
IList<HwIdentifierItem> hwIdentifiers = GetAllHwIdentifiers(device, name);
AssignToPlc(hwIdentifiers, projectItem);
}
}

Expand All @@ -344,6 +350,8 @@ public void GetPlcList(ProjectItem projectItem)
{
IList<ModuleItem> localModules = GetAllLocalModules(device , name);
AssignToPlc(localModules, projectItem);
IList<HwIdentifierItem> hwIdentifiers = GetAllHwIdentifiers(device, name);
AssignToPlc(hwIdentifiers, projectItem);
}
}

Expand All @@ -368,6 +376,8 @@ public void GetGroupedPlcList(DeviceUserGroup deviceGroup, string path, ProjectI
{
PlcItem plc = new PlcItem() { Name = name, DeviceName = device.Name, Classification = "CPU", GroupPathInProject = path, ConfiguredSubnets = GetAllNetworkInterfaces(device) , LocalModules = GetAllLocalModules(device, name), DistributedModules = new List<ModuleItem>() };
projectItem.PlcItems.Add(plc);
IList<HwIdentifierItem> hwIdentifiers = GetAllHwIdentifiers(device, name);
AssignToPlc(hwIdentifiers, projectItem);
}
}

Expand Down Expand Up @@ -429,6 +439,8 @@ public void GetGroupedHMs(DeviceUserGroup deviceGroup, string path, ProjectItem
{
IList<ModuleItem> localModules = GetAllLocalModules(device, name);
AssignToPlc(localModules, projectItem);
IList<HwIdentifierItem> hwIdentifiers = GetAllHwIdentifiers(device, name);
AssignToPlc(hwIdentifiers, projectItem);
}
}

Expand Down Expand Up @@ -476,6 +488,35 @@ private void AssignToPlc(IList<ModuleItem> localModules, ProjectItem projectItem
}
}

/// <summary>
/// Assigned HwIdentifier items to the master PLC
/// </summary>
private void AssignToPlc(IList<HwIdentifierItem> HwIdentifiers, ProjectItem projectItem)
{
foreach (HwIdentifierItem HwIdentifier in HwIdentifiers)
{
foreach (var plcItem in projectItem.PlcItems)
{

if (plcItem.Name.Equals(HwIdentifier.Controller))
{
int index = projectItem.PlcItems.IndexOf(plcItem);
if (projectItem.PlcItems[index].HwIdentifiers != null)
{
projectItem.PlcItems[index].HwIdentifiers.Add(HwIdentifier);
}
else
{
List<HwIdentifierItem> hwIdentifierItems = new List<HwIdentifierItem>
{
HwIdentifier
};
projectItem.PlcItems[index].HwIdentifiers = hwIdentifierItems;
}
}
}
}
}
/// <summary>
/// Get NetworkInterfaces
/// </summary>
Expand Down Expand Up @@ -504,7 +545,7 @@ public IList<string> GetAllNetworkInterfaces(Device device)
/// <summary>
/// Get all local modules
/// </summary>
public IList<ModuleItem> GetAllLocalModules(Device device ,string name)
public IList<ModuleItem> GetAllLocalModules(Device device, string name)
{
IList<ModuleItem> localModules = new List<ModuleItem>();

Expand Down Expand Up @@ -535,7 +576,7 @@ public IList<ModuleItem> GetAllLocalModules(Device device ,string name)

foreach (var deviceSubItem in deviceItem.DeviceItems)
{
ModuleItem localModule = new ModuleItem() { Name = deviceItem.Name, FullName = name + "_" + deviceItem.Name , OrderNumber = _orderNumber , FirmwareVersion = _firmware, PositionNumber = deviceItem.PositionNumber , AddressItems = new List<AddressItem>() };
ModuleItem localModule = new ModuleItem() { Name = deviceSubItem.Name, FullName = name + "_" + deviceSubItem.Name, OrderNumber = _orderNumber, FirmwareVersion = _firmware, PositionNumber = deviceSubItem.PositionNumber, AddressItems = new List<AddressItem>() };

foreach (Address address in deviceSubItem.Addresses)
{
Expand All @@ -550,7 +591,30 @@ public IList<ModuleItem> GetAllLocalModules(Device device ,string name)
}
}
}
if(localModule.AddressItems.Count > 0)

foreach (DeviceItem deviceSubSubItem in deviceSubItem.Items)
{
localModule = new ModuleItem() { Name = deviceSubSubItem.Name, FullName = name + "_" + deviceSubItem.Name + "_" + deviceSubSubItem.Name, OrderNumber = _orderNumber, FirmwareVersion = _firmware, PositionNumber = deviceSubItem.PositionNumber, AddressItems = new List<AddressItem>() };

foreach (Address address in deviceSubSubItem.Addresses)
{
if (address != null && (address.IoType.Equals(AddressIoType.Input) || address.IoType.Equals(AddressIoType.Output)))
{
if (address.AddressControllers != null && address.AddressControllers.FirstOrDefault() != null)
{
string _controller = address.AddressControllers.FirstOrDefault().GetAttribute("Name").ToString();

AddressItem addressItem = new AddressItem() { Controller = _controller, IoType = address.IoType, StartAddress = address.StartAddress, BitLength = address.Length, ByteLength = address.Length / 8 };
localModule.AddressItems.Add(addressItem);
}
}
}
if (localModule.AddressItems.Count > 0 )
{
localModules.Add(localModule);
}
}
if (localModule.AddressItems.Count > 0 && deviceSubItem.Items.Count == 0)
{
localModules.Add(localModule);
}
Expand All @@ -563,7 +627,78 @@ public IList<ModuleItem> GetAllLocalModules(Device device ,string name)
throw;
}
}
return localModules.OrderBy(p=>p.PositionNumber).ToList();
return localModules.OrderBy(p => p.PositionNumber).ToList();
}

/// <summary>
/// Get all local modules
/// </summary>
public IList<HwIdentifierItem> GetAllHwIdentifiers(Device device, string name)
{
IList<HwIdentifierItem> HwIdentifiers = new List<HwIdentifierItem>();

foreach (DeviceItem deviceItem in device.DeviceItems)
{
try
{
if (deviceItem != null)
{
string controller = "";
foreach (HwIdentifier HwIdentifier in deviceItem.HwIdentifiers)
{
controller = HwIdentifier.HwIdentifierControllers != null ? HwIdentifier.HwIdentifierControllers.FirstOrDefault() != null ? HwIdentifier.HwIdentifierControllers.FirstOrDefault().GetAttribute("Name") != null ? HwIdentifier.HwIdentifierControllers.FirstOrDefault().GetAttribute("Name").ToString() : "" : "" :"";
HwIdentifierItem hwIdentifierItem = new HwIdentifierItem(){Controller = controller, Name = name + "_" + deviceItem.Name , Identifier = HwIdentifier.Identifier};
if (IsNewHardwareId(hwIdentifierItem.Name, HwIdentifiers))
{
HwIdentifiers.Add(hwIdentifierItem);
}
}
foreach (var deviceSubItem in deviceItem.DeviceItems)
{
foreach (HwIdentifier HwIdentifier in deviceSubItem.HwIdentifiers)
{
controller = HwIdentifier.HwIdentifierControllers != null ? HwIdentifier.HwIdentifierControllers.FirstOrDefault() != null ? HwIdentifier.HwIdentifierControllers.FirstOrDefault().GetAttribute("Name") != null ? HwIdentifier.HwIdentifierControllers.FirstOrDefault().GetAttribute("Name").ToString() : "" : "" : "";
HwIdentifierItem hwIdentifierItem = new HwIdentifierItem() { Controller = controller, Name = name + "_" + deviceSubItem.Name, Identifier = HwIdentifier.Identifier };
if (IsNewHardwareId(hwIdentifierItem.Name, HwIdentifiers))
{
HwIdentifiers.Add(hwIdentifierItem);
}
}
foreach (DeviceItem deviceSubSubItem in deviceSubItem.Items)
{
foreach (HwIdentifier HwIdentifier in deviceSubSubItem.HwIdentifiers)
{
controller = HwIdentifier.HwIdentifierControllers != null ? HwIdentifier.HwIdentifierControllers.FirstOrDefault() != null ? HwIdentifier.HwIdentifierControllers.FirstOrDefault().GetAttribute("Name") != null ? HwIdentifier.HwIdentifierControllers.FirstOrDefault().GetAttribute("Name").ToString() : "" : "" : "";
HwIdentifierItem hwIdentifierItem = new HwIdentifierItem() { Controller = controller, Name = name + "_" + deviceSubSubItem.Name, Identifier = HwIdentifier.Identifier };
if (IsNewHardwareId(hwIdentifierItem.Name, HwIdentifiers))
{
HwIdentifiers.Add(hwIdentifierItem);
}
}
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
throw;
}
}
return HwIdentifiers;
}
public bool IsNewHardwareId(string name, IList<HwIdentifierItem> HwIdentifiers)
{
bool isNew = true;
foreach (HwIdentifierItem hwIdentifierItem in HwIdentifiers)
{
if (hwIdentifierItem.Name.Equals(name))
{
isNew = false;
break;
}
}
return isNew;
}

/// <summary>
Expand All @@ -583,6 +718,7 @@ public void ExportAllPLCs(string exportDirectory, ProjectItem projectItem)
ExportMappingItems(plcItem, exportDir, ExportItemType.HwOutput);
ExportMappingItems(plcItem, exportDir, ExportItemType.PlcInput);
ExportMappingItems(plcItem, exportDir, ExportItemType.PlcOutput);
ExportHwIdentifiers(plcItem, exportDir);
ExportCopyInputs(plcItem, exportDir);
ExportCopyOutputs(plcItem, exportDir);
ExportConfiguration(exportDir);
Expand Down Expand Up @@ -687,6 +823,62 @@ string GetDataType(AddressItem addressItem)
}
}

/// <summary>
/// Export mapping items
/// </summary>
private static void ExportHwIdentifiers(PlcItem plcItem, string exportDir)
{
string filename = Path.Combine(exportDir, Constants.HwIdentifiersStructName + ".st");

using (StreamWriter sw = new StreamWriter(filename))
{
sw.WriteLine("TYPE");
sw.WriteLine("\t" + Constants.HwIdentifiersStructName + " : ULINT");
sw.WriteLine("\t(");

if (plcItem.HwIdentifiers != null)
{
foreach (HwIdentifierItem hwIdItem in plcItem.HwIdentifiers)
{
sw.WriteLine(GetHwIdentItem(hwIdItem));
}
}
sw.WriteLine("\t\tNONE:= ULINT#0");
sw.WriteLine("\t);");
sw.WriteLine("END_TYPE");
}

string GetHwItem(ModuleItem module, AddressItem addressItem)
{
return "\t\t" + ValidatePlcItem.Name(module.FullName) + " AT %B" + addressItem.StartAddress + " : " + GetDataType(addressItem) + ";";
}

string GetHwIdentItem(HwIdentifierItem hwIdItem)
{
return "\t\t" + ValidatePlcItem.Name(hwIdItem.Name) + " :=\tULINT#" + hwIdItem.Identifier.ToString() + ",";
}

string GetPlcItem(ModuleItem module, AddressItem addressItem)
{
return "\t\t" + ValidatePlcItem.Name(module.FullName) + " : " + GetDataType(addressItem) + ";";
}

string GetDataType(AddressItem addressItem)
{
switch (addressItem.ByteLength)
{
case 1:
return "BYTE";
case 2:
return "WORD";
case 4:
return "DWORD";
default:
return "ARRAY[0.." + (addressItem.ByteLength - 1) + "] OF BYTE";
}
}
}

private static void ExportCopyInputs(PlcItem plcItem, string exportDir)
{
string filename = Path.Combine(exportDir, Constants.CopyInputsFunctionName + ".st");
Expand Down Expand Up @@ -852,6 +1044,7 @@ private static void ExportProgram(string exportDir)
sw.WriteLine("\t" + Constants.CopyOutputsFunctionName + "();");
}
}

enum ExportItemType
{
HwInput,
Expand Down
23 changes: 23 additions & 0 deletions src/tia2ax/V18_0/DTOs/HwIdentifierItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Siemens.Engineering.HW;

namespace Tia2Ax.DTOs
{
public class HwIdentifierItem
{
public string Controller
{
get;
set;
}
public string Name
{
get;
set;
}
public long Identifier
{
get;
set;
}
}
}
5 changes: 5 additions & 0 deletions src/tia2ax/V18_0/DTOs/PlcItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public IList<ModuleItem> DistributedModules
get;
set;
}
public IList<HwIdentifierItem> HwIdentifiers
{
get;
set;
}
#endregion // properties
}
}
3 changes: 2 additions & 1 deletion src/tia2ax/V18_0/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"profiles": {
"tia2ax": {
"commandName": "Project",
"commandLineArgs": "-x D:\\_mts\\2023\\TiaPortalProjects\\HW\\HW_1516\\HW_1516.ap18 -o D:\\_mts\\2023\\Export"
"commandLineArgs": "-x D:\\github\\ix-ax\\TiaProjects\\AX_Plc1518V3_CmmtAs\\AX_Plc1518V3_CmmtAs.ap18 -o D:\\github\\ix-ax\\TiaProjects\\Export"
//"commandLineArgs": "-x D:\\_mts\\2023\\TiaPortalProjects\\Complete\\2000482_V18\\2000482_V18.ap18 -o D:\\_mts\\2023\\Export"
}
}
}
1 change: 1 addition & 0 deletions src/tia2ax/V18_0/Setup/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public static class Constants
{
public const string HwInputsStructName = "HwInputs";
public const string HwOutputsStructName = "HwOutputs";
public const string HwIdentifiersStructName = "HwIdentifiers";
public const string PlcInputsStructName = "PlcInputs";
public const string PlcOutputsStructName = "PlcOutputs";
public const string CopyInputsFunctionName = "CopyInputs";
Expand Down
10 changes: 5 additions & 5 deletions src/tia2ax/V18_0/V18_0_tia2ax.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
</PropertyGroup>

<ItemGroup>
<Reference Include="Siemens.Engineering" Pack="true">
<HintPath>..\..\..\third\V18\Siemens.Engineering.dll</HintPath>
<Pack>true</Pack>
<Private>True</Private>
</Reference>
<PackageReference Include="CommandLineParser" Version="2.9.1">
<Pack>true</Pack>
<IncludeAssets>All</IncludeAssets>
Expand All @@ -24,4 +19,9 @@
<ItemGroup>
<Content Include="..\..\..\third\V18\Siemens.Engineering.dll" IncludeInPackage="true" Pack="true" PackagePath="lib\net48" />
</ItemGroup>
<ItemGroup>
<Reference Include="Siemens.Engineering">
<HintPath>..\..\..\third\V18\Siemens.Engineering.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/tia2axtool/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"tia2ax": {
"commandName": "Project",
"commandLineArgs": "-x D:\\_mts\\2023\\TiaPortalProjects\\HW\\HW_1516\\HW_1516.ap18 -o D:\\_mts\\2023\\Export"
"commandLineArgs": "-x D:\\github\\ix-ax\\TiaProjects\\AX_Plc1518V3_CmmtAs\\AX_Plc1518V3_CmmtAs.ap18 -o D:\\github\\ix-ax\\TiaProjects\\Export"
}
}
}
7 changes: 7 additions & 0 deletions tests/test_projects/_expected/PLC_1/CopyInputs.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FUNCTION CopyInputs
VAR_EXTERNAL
HwInputs : HwInputs;
PlcInputs : PlcInputs;
END_VAR
PlcInputs.IO_device_1_DI_8x24VDC_BA_1 := HwInputs.IO_device_1_DI_8x24VDC_BA_1;
END_FUNCTION
Loading

0 comments on commit 77048dd

Please sign in to comment.