Skip to content

Commit

Permalink
work on tia binary reading
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 committed Aug 25, 2024
1 parent e0aeb0f commit ff40227
Show file tree
Hide file tree
Showing 27 changed files with 264 additions and 215 deletions.
69 changes: 0 additions & 69 deletions DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,75 +118,6 @@ Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)

private object tiaExport;
private Type tiaExportType;


internal Dictionary<TiaObjectId, TiaFileObject> TiaObjects = new Dictionary<TiaObjectId, TiaFileObject>();

internal void BinaryParseTIAFile()
{
using (var sourceStream = new FileStream(DataFile, FileMode.Open, FileAccess.Read, System.IO.FileShare.ReadWrite))
{
var buffer = new byte[Marshal.SizeOf(typeof(TiaFileHeader))];
sourceStream.Read(buffer, 0, buffer.Length);

GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
TiaFileHeader header = (TiaFileHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(TiaFileHeader));
handle.Free();
TiaMarker? lastMarker = null;

while (sourceStream.Position < sourceStream.Length)
{
if (TiaHelper.IsMarker(sourceStream))
{
var buffer2 = new byte[Marshal.SizeOf(typeof(TiaMarker))];
sourceStream.Read(buffer2, 0, buffer2.Length);
GCHandle handle2 = GCHandle.Alloc(buffer2, GCHandleType.Pinned);
TiaMarker marker = (TiaMarker)Marshal.PtrToStructure(handle2.AddrOfPinnedObject(), typeof(TiaMarker));
handle2.Free();

lastMarker = marker;
}
else
{
var buffer3 = new byte[Marshal.SizeOf(typeof(TiaObjectHeader))];
sourceStream.Read(buffer3, 0, buffer3.Length);
GCHandle handle3 = GCHandle.Alloc(buffer3, GCHandleType.Pinned);
TiaObjectHeader hd = (TiaObjectHeader)Marshal.PtrToStructure(handle3.AddrOfPinnedObject(), typeof(TiaObjectHeader));
handle3.Free();

var bytes = new byte[hd.Size - buffer3.Length];
sourceStream.Read(bytes, 0, bytes.Length);
var id = hd.GetTiaObjectId();
if (!TiaObjects.ContainsKey(id))
{
TiaObjects.Add(id, new TiaFileObject(hd, bytes));

var size = Marshal.SizeOf(typeof (TiaObjectHeader))+4+BitConverter.ToInt32(bytes, 0)+1;
if (hd.Size != size || bytes[bytes.Length - 1] != 0xff)
{
//Fehler ???
}


//var strm = new MemoryStream(bytes);
//var dec = TiaCompression.DecompressStream(strm);
//var rd = new StreamReader(dec);
//var wr = rd.ReadToEnd();
}
else
{
//Todo: look why this happens, and how TIA Handles this!!
//Console.WriteLine("double Id:" + id.ToString());
}
}
}

var rootId = new TiaObjectId(TiaFixedRootObjectInstanceIds.RootObjectCollectionId);
var rootObjects = new TiaRootObjectList(TiaObjects[rootId]);
var projectid = rootObjects.TiaRootObjectEntrys.FirstOrDefault(x => x.ObjectId.TypeId == (int)TiaTypeIds.Siemens_Automation_DomainModel_ProjectData).ObjectId;
var projectobj = TiaObjects[projectid];
}
}

public override ProjectType ProjectType
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)

private object tiaExport;
private Type tiaExportType;


internal Dictionary<TiaObjectId, TiaFileObject> TiaObjects = new Dictionary<TiaObjectId, TiaFileObject>();

public override ProjectType ProjectType
{
Expand Down
3 changes: 0 additions & 3 deletions DotNetSiemensPLCToolBoxLibrary.TIAV15/Step7ProjectV15.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
private object tiaExport;
private Type tiaExportType;


internal Dictionary<TiaObjectId, TiaFileObject> TiaObjects = new Dictionary<TiaObjectId, TiaFileObject>();

public override ProjectType ProjectType
{
get { return ProjectType.Tia15; }
Expand Down
3 changes: 0 additions & 3 deletions DotNetSiemensPLCToolBoxLibrary.TIAV15_1/Step7ProjectV15_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
private object tiaExport;
private Type tiaExportType;


internal Dictionary<TiaObjectId, TiaFileObject> TiaObjects = new Dictionary<TiaObjectId, TiaFileObject>();

public override ProjectType ProjectType
{
get { return ProjectType.Tia15_1; }
Expand Down
3 changes: 0 additions & 3 deletions DotNetSiemensPLCToolBoxLibrary.TIAV16/Step7ProjectV16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
private object tiaExport;
private Type tiaExportType;


internal Dictionary<TiaObjectId, TiaFileObject> TiaObjects = new Dictionary<TiaObjectId, TiaFileObject>();

public override ProjectType ProjectType
{
get { return ProjectType.Tia16; }
Expand Down
3 changes: 0 additions & 3 deletions DotNetSiemensPLCToolBoxLibrary.TIAV17/Step7ProjectV17.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
private object tiaExport;
private Type tiaExportType;


internal Dictionary<TiaObjectId, TiaFileObject> TiaObjects = new Dictionary<TiaObjectId, TiaFileObject>();

public override ProjectType ProjectType
{
get { return ProjectType.Tia17; }
Expand Down
3 changes: 0 additions & 3 deletions DotNetSiemensPLCToolBoxLibrary.TIAV18/Step7ProjectV18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
private object tiaExport;
private Type tiaExportType;


internal Dictionary<TiaObjectId, TiaFileObject> TiaObjects = new Dictionary<TiaObjectId, TiaFileObject>();

public override ProjectType ProjectType
{
get { return ProjectType.Tia18; }
Expand Down
3 changes: 0 additions & 3 deletions DotNetSiemensPLCToolBoxLibrary.TIAV19/Step7ProjectV19.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
private object tiaExport;
private Type tiaExportType;


internal Dictionary<TiaObjectId, TiaFileObject> TiaObjects = new Dictionary<TiaObjectId, TiaFileObject>();

public override ProjectType ProjectType
{
get { return ProjectType.Tia19; }
Expand Down
14 changes: 13 additions & 1 deletion DotNetSiemensPLCToolBoxLibrary.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33110.190
Expand Down Expand Up @@ -92,6 +91,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetSiemensPLCToolBoxLibr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetSiemensPLCToolBoxLibrary.TIAV19", "DotNetSiemensPLCToolBoxLibrary.TIAV19\DotNetSiemensPLCToolBoxLibrary.TIAV19.csproj", "{ADC06ADE-EB03-4422-A7B4-4291AD994D91}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{66A4ADD6-8EE2-49BD-B939-C5E69CAA3A9E}"
ProjectSection(SolutionItems) = preProject
.github\FUNDING.yml = .github\FUNDING.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{5FC399A5-1C21-480A-987F-16388079F245}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build.yml = .github\workflows\build.yml
.github\workflows\build-pull.yml = .github\workflows\build-pull.yml
.github\workflows\release.yml = .github\workflows\release.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -777,6 +788,7 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5FC399A5-1C21-480A-987F-16388079F245} = {66A4ADD6-8EE2-49BD-B939-C5E69CAA3A9E}
{D5525AD8-E501-45A8-BEA5-F9BDB468B427} = {A4F11331-531B-4A7C-84B2-E319658AE2B8}
{1182E730-FA4F-4712-8B8B-6432848C3048} = {A4F11331-531B-4A7C-84B2-E319658AE2B8}
{9EB7BDB5-FDEE-466A-B2FF-63F087B160CE} = {A4F11331-531B-4A7C-84B2-E319658AE2B8}
Expand Down
18 changes: 18 additions & 0 deletions LibNoDaveConnectionLibrary/General/BinaryReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.IO;

namespace DotNetSiemensPLCToolBoxLibrary.General
{
public static class BinaryReaderExtensions
{
public static Guid ReadGuid(this BinaryReader binaryReader)
{
return new Guid(binaryReader.ReadBytes(16));
}

public static Version ReadVersion(this BinaryReader binaryReader)
{
return new Version(binaryReader.ReadInt32(), binaryReader.ReadInt32(), binaryReader.ReadInt32(), binaryReader.ReadInt32());
}
}
}
23 changes: 21 additions & 2 deletions LibNoDaveConnectionLibrary/General/ZipHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

#if SHARPZIPLIB
using ICSharpCode.SharpZipLib.Zip;
#endif
Expand All @@ -17,7 +18,6 @@ public class ZipHelper
public string GetFirstZipEntryWithEnding(string ending)
{
#if SHARPZIPLIB


string name = null;
foreach (ZipEntry zipEntry in this._zipFile)
Expand All @@ -34,6 +34,25 @@ public string GetFirstZipEntryWithEnding(string ending)
#endif
}

public string GetFirstZipEntryWithMatch(string pattern)
{
#if SHARPZIPLIB

string name = null;
foreach (ZipEntry zipEntry in this._zipFile)
{
if (Regex.IsMatch(zipEntry.Name.ToLower(), pattern))
{
name = zipEntry.Name;
break;
}
}
return name;
#else
return null;
#endif
}

public static ZipHelper GetZipHelper(string zipfile)
{
#if SHARPZIPLIB
Expand Down
1 change: 1 addition & 0 deletions LibNoDaveConnectionLibrary/Projectfiles/ProjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum ProjectType
{
Step5,
Step7,
TiaBinary,
Tia13,
Tia14SP1,
Tia15,
Expand Down
2 changes: 1 addition & 1 deletion LibNoDaveConnectionLibrary/Projectfiles/Step5Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected override void LoadProject()
Stream symTabStream = _ziphelper.GetReadStream(_projectfilename.ToLower().Replace("st.s5d", "z0.seq"));

SymbolTable symtab=new SymbolTable();
symtab.LoadSymboltable(symTabStream);
symtab.LoadSymboltable(symTabStream);
symTabStream.Close();
symtab.Parent = ProjectStructure;
symtab.Project = this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Xml;
using DotNetSiemensPLCToolBoxLibrary.DataTypes;
using DotNetSiemensPLCToolBoxLibrary.General;
using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA.Structs;

namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA
{
public class Step7ProjectTiaBinaryParsed : Project
{
private readonly Credentials _credentials;

private string DataFile = null;

private XmlDocument tiaProject;

internal ZipHelper _ziphelper = new ZipHelper(null);

public CultureInfo Culture { get; set; }

//will be removed later...
public List<TiaObjectHeader> TiaObjectsList;

public void BinaryParseTIAFile()
{
using (var sourceStream = _ziphelper.GetReadStream(DataFile))
{
var rd = new BinaryReader(sourceStream);
var header = TiaFileHeader.Deserialize(rd);

TiaObjectsList = new List<TiaObjectHeader>();
try
{
while (true)
{
var hd = TiaObjectHeader.Deserialize(rd);
TiaObjectsList.Add(hd);
}
}
catch (EndOfStreamException) // Zip File Stream has no length
{ }

/*var rootId = new TiaObjectId(TiaFixedRootObjectInstanceIds.RootObjectCollectionId);
var rootObjects = new TiaRootObjectList(TiaObjects[rootId]);
var projectid = rootObjects.TiaRootObjectEntrys.FirstOrDefault(x => x.ObjectId.TypeId == (int)TiaTypeIds.Siemens_Automation_DomainModel_ProjectData).ObjectId;
var projectobj = TiaObjects[projectid];*/
}
}

public Step7ProjectTiaBinaryParsed(string projectfile, CultureInfo culture = null) : this(projectfile, culture, null)
{
}

public Step7ProjectTiaBinaryParsed(string projectfile, CultureInfo culture = null, Credentials credentials = null)
{
_credentials = credentials;
if (culture == null)
Culture = CultureInfo.CurrentCulture;
else
Culture = culture;

ProjectFile = projectfile;

if (ProjectFile.ToLower().EndsWith("zip") || Path.GetExtension(ProjectFile.ToLower()).Contains("zap"))
{
this._ziphelper = new ZipHelper(projectfile);
if (string.IsNullOrEmpty(ProjectFile))
ProjectFile = _ziphelper.GetFirstZipEntryWithMatch("\\.ap.*");
if (string.IsNullOrEmpty(ProjectFile))
ProjectFile = _ziphelper.GetFirstZipEntryWithMatch("\\.al.*");
if (string.IsNullOrEmpty(projectfile))
throw new Exception("Zip-File contains no valid TIA Project !");
}

try
{
using (var stream = _ziphelper.GetReadStream(projectfile))
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(stream);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("x", "http://www.siemens.com/2007/07/Automation/CommonServices/DataInfoValueData");

var nd = xmlDoc.SelectSingleNode("x:Data", nsmgr);
this.ProjectName = nd.Attributes["Name"].Value;
}
}
catch (Exception)
{ }

if (_ziphelper.IsZipFile)
DataFile = "System\\PEData.plf";
else
DataFile = Path.GetDirectoryName(projectfile) + "\\System\\PEData.plf";
ProjectFolder = projectfile.Substring(0, projectfile.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar;

BinaryParseTIAFile();
}

internal XmlDocument xmlDoc;

private object tiaExport;
private Type tiaExportType;


public override ProjectType ProjectType
{
get { return ProjectType.TiaBinary; }
}

protected override void LoadProject()
{
_projectLoaded = true;
}
}
}
Loading

0 comments on commit ff40227

Please sign in to comment.