diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite deleted file mode 100644 index e6b379bf..00000000 Binary files a/.vs/slnx.sqlite and /dev/null differ diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV13/DotNetSiemensPLCToolBoxLibrary.TIAV13.csproj b/DotNetSiemensPLCToolBoxLibrary.TIAV13/DotNetSiemensPLCToolBoxLibrary.TIAV13.csproj index 3d1620be..9eba259b 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV13/DotNetSiemensPLCToolBoxLibrary.TIAV13.csproj +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV13/DotNetSiemensPLCToolBoxLibrary.TIAV13.csproj @@ -1,60 +1,60 @@ - - - - - Debug - AnyCPU - {BB3726B7-E1F9-4D3B-AEE5-06186285388C} - Library - Properties - DotNetSiemensPLCToolBoxLibrary.TIAV13 - DotNetSiemensPLCToolBoxLibrary.TIAV13 - v4.0 - 512 - - - - true - full - false - ..\compiled\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - False - ..\externalDlls\siemens\V13\Siemens.Engineering.dll - False - - - - - - - - - - - - - - - - - - {e3ed87e8-b550-46ac-9196-9688d30efd29} - DotNetSiemensPLCToolBoxLibrary - - - + + + + + Debug + AnyCPU + {BB3726B7-E1F9-4D3B-AEE5-06186285388C} + Library + Properties + DotNetSiemensPLCToolBoxLibrary.TIAV13 + DotNetSiemensPLCToolBoxLibrary.TIAV13 + v4.0 + 512 + + + + true + full + false + ..\compiled\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + ..\externalDlls\siemens\V13\Siemens.Engineering.dll + False + + + + + + + + + + + + + + + + + + {e3ed87e8-b550-46ac-9196-9688d30efd29} + DotNetSiemensPLCToolBoxLibrary + + + \ No newline at end of file diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV13/Properties/AssemblyInfo.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV13/Properties/AssemblyInfo.cs index a8da2070..fa657c5f 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV13/Properties/AssemblyInfo.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV13/Properties/AssemblyInfo.cs @@ -1,36 +1,36 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// Allgemeine Informationen über eine Assembly werden über die folgenden -// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, -// die einer Assembly zugeordnet sind. -[assembly: AssemblyTitle("DotNetSiemensPLCToolBoxLibrary.TIAV13")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("DotNetSiemensPLCToolBoxLibrary.TIAV13")] -[assembly: AssemblyCopyright("Copyright © 2017")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly -// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von -// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. -[assembly: ComVisible(false)] - -// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird -[assembly: Guid("bb3726b7-e1f9-4d3b-aee5-06186285388c")] - -// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: -// -// Hauptversion -// Nebenversion -// Buildnummer -// Revision -// -// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, -// indem Sie "*" wie unten gezeigt eingeben: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("DotNetSiemensPLCToolBoxLibrary.TIAV13")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("DotNetSiemensPLCToolBoxLibrary.TIAV13")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("bb3726b7-e1f9-4d3b-aee5-06186285388c")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// indem Sie "*" wie unten gezeigt eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV11.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV11.cs index e278a4fe..ef36c7eb 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV11.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV11.cs @@ -1,13 +1,13 @@ -using System; -using System.Globalization; - -namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles -{ - [Obsolete()] - public class Step7ProjectV11 : Step7ProjectV13 - { - public Step7ProjectV11(string projectfile, CultureInfo culture = null) : base(projectfile, culture) - { - } - } -} +using System; +using System.Globalization; + +namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles +{ + [Obsolete()] + public class Step7ProjectV11 : Step7ProjectV13 + { + public Step7ProjectV11(string projectfile, CultureInfo culture = null) : base(projectfile, culture) + { + } + } +} diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV13.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV13.cs index 853ff9ca..4ce0d0fa 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV13.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV13.cs @@ -1,371 +1,371 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Runtime.InteropServices; -using System.Xml; -using DotNetSiemensPLCToolBoxLibrary.General; -using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA; -using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA.Enums; -using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA.Structs; -using Microsoft.Win32; - -namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles -{ - public partial class Step7ProjectV13 : Project, IDisposable - { - public enum TiaVersionTypes - { - V11 = 11, - V12 = 12, - V13 = 13, - V14 = 14, - } - - public static TiaVersionTypes TiaVersion { get; private set; } - - private string DataFile = null; - - private XmlDocument tiaProject; - - internal ZipHelper _ziphelper = new ZipHelper(null); - - public CultureInfo Culture { get; set; } - - public Step7ProjectV13(string projectfile, CultureInfo culture = null) - { - if (culture == null) - Culture = CultureInfo.CurrentCulture; - else - Culture = culture; - - AppDomain currentDomain = AppDomain.CurrentDomain; - currentDomain.AssemblyResolve += currentDomain_AssemblyResolve; - - ProjectFile = projectfile; - - if (ProjectFile.ToLower().EndsWith("zip") || ProjectFile.ToLower().EndsWith("zap13") || ProjectFile.ToLower().EndsWith("zap14")) - { - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap11"); - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap12"); - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap13"); - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap14"); - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al11"); - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al12"); - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al13"); - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al14"); - - if (string.IsNullOrEmpty(projectfile)) - throw new Exception("Zip-File contains no valid TIA Project !"); - this._ziphelper = new ZipHelper(projectfile); - } - - - try - { - var xmlDoc = new XmlDocument(); - xmlDoc.Load(_ziphelper.GetReadStream(projectfile)); - - 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) - { } - - DataFile = Path.GetDirectoryName(projectfile) + "\\System\\PEData.plf"; - ProjectFolder = projectfile.Substring(0, projectfile.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar; - - //BinaryParseTIAFile(); - //LoadProject(); - LoadViaOpennessDlls(); - - currentDomain.AssemblyResolve -= currentDomain_AssemblyResolve; - } - - internal XmlDocument xmlDoc; - - Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) - { - int index = args.Name.IndexOf(','); - if (index == -1) - { - return null; - } - var name = args.Name.Substring(0, index) + ".dll"; - - var filePathReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Siemens\\Automation\\_InstalledSW\\TIAP13\\TIA_Opns") ?? - Registry.LocalMachine.OpenSubKey("SOFTWARE\\Siemens\\Automation\\_InstalledSW\\TIAP13\\TIA_Opns"); - if (filePathReg != null) - { - string filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V13"); - if (Directory.Exists(filePath) == false) - filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V13 SP1"); - var path = Path.Combine(filePath, name); - var fullPath = Path.GetFullPath(path); - if (File.Exists(fullPath)) - { - return Assembly.LoadFrom(fullPath); - } - } - - - return null; - } - - private object tiaExport; - private Type tiaExportType; - - - internal Dictionary TiaObjects = new Dictionary(); - - 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]; - } - } - - protected override void LoadProject() - { - _projectLoaded = true; - return; - - ////Stream stream = new FileStream("c:\\tia.xml", FileMode.OpenOrCreate); // new ChunkedMemoryStream(); - ////StreamWriter streamWriter = new StreamWriter(stream); - - ////XmlWriter xmlWriter = XmlWriter.Create(streamWriter, new XmlWriterSettings { Indent = true, CheckCharacters = false }); - - //var tiaObjectStructure = new TiaObjectStructure(); - //var xmlWriter = new TiaXmlWriter(tiaObjectStructure); - - ////xmlWriter.WriteStartDocument(); - ////xmlWriter.WriteStartElement("root"); - - //if (tiaExport == null) - //{ - // tiaExportType = Type.GetType("Siemens.Automation.ObjectFrame.FileStorage.Conversion.Export, Siemens.Automation.ObjectFrame.FileStorage"); - // if (TiaVersion < TiaVersionTypes.V13) - // { - // tiaExport = tiaExportType.InvokeMember("CreateInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, new object[] { DataFile, true }); - // } - // else - // { - // //V13 - // var helper = Type.GetType("Siemens.Automation.ObjectFrame.FileStorage.Base.Client.DataStoreClientHelper, Siemens.Automation.ObjectFrame.FileStorage.Base"); - // var metaManagerMth = helper.GetMethods(BindingFlags.NonPublic | BindingFlags.Static).FirstOrDefault(x => x.Name == "GetMetaManager"); - // var metaManager = metaManagerMth.Invoke(null, null); - // var crInst = tiaExportType.GetMethods().FirstOrDefault(x => x.Name == "CreateInstance"); - // tiaExport = crInst.Invoke(null, new object[] { DataFile, null, true, metaManager }); - // } - - // var memMgrType = Type.GetType("Siemens.Automation.ObjectFrame.Kernel.MemoryManager, Siemens.Automation.ObjectFrame.Kernel"); - // try - // { - // memMgrType.InvokeMember("Initialize", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, new object[] { 104857600 }); - // } - // catch (Exception) - // { - - // } - //} - - ////tiaExportType.InvokeMember("WriteCultures", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, tiaExport, new object[] { xmlWriter }); - ////tiaExportType.InvokeMember("StartExport", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, tiaExport, new object[] { xmlWriter }); - ////tiaExportType.InvokeMember("WriteRootObjectList", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, tiaExport, new object[] { xmlWriter }); - - //if (TiaVersion >= TiaVersionTypes.V13) - //{ - // var bgWorker = tiaExportType.GetField("m_BgWorker", BindingFlags.Instance | BindingFlags.NonPublic); - // bgWorker.SetValue(tiaExport, new BackgroundWorker() {WorkerReportsProgress = true, WorkerSupportsCancellation = true}); - //} - - //var serializeObjects = tiaExportType.GetMethod("SerializeObjects", - // BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, - // new Type[] {typeof (XmlWriter)}, null); - - //serializeObjects.Invoke(tiaExport, new object[] {xmlWriter}); - - //xmlWriter.Flush(); - //xmlWriter.Close(); - - ////streamWriter.Close(); - ////stream.Close(); - - ////stream.Position = 0; - ////ParseProject(stream); - } - - //internal Dictionary importTypeInfos; - //internal Dictionary asId2Names; - //internal Dictionary relationId2Names; - //internal string CoreAttributesId; - - //private TIAProjectFolder getProjectFolder(XmlNode Node) - //{ - // TIAProjectFolder fld = null; - // string id = Node.Attributes["id"].Value; - // string instid = Node.Attributes["instId"].Value; - - // string tiaType = importTypeInfos[id]; - - - - // switch (tiaType) - // { - // case "Siemens.Automation.DomainModel.ProjectData": - // fld = new TIAProjectFolder(this, Node); - // break; - // case "Siemens.Automation.DomainModel.FolderData": - // { - // var subType = Node.SelectSingleNode("attribSet[@id='" + CoreAttributesId + "']/attrib[@name='Subtype']").InnerText; - // if (subType == "ProgramBlocksFolder" || subType == "ProgramBlocksFolder.Subfolder") - // { - // fld = new TIABlocksFolder(this, Node); - // } - // else - // { - // fld = new TIAProjectFolder(this, Node); - // } - // break; - // } - // case "Siemens.Simatic.HwConfiguration.Model.DeviceData": - // fld = new TIAProjectFolder(this, Node); - // break; - // case "Siemens.Simatic.HwConfiguration.Model.S7ControllerTargetData": - // fld = new TIACPUFolder(this, Node); - // break; - // case "Siemens.Automation.DomainModel.EAMTZTagTableData": - // fld = new TIASymTabFolder(this, Node); - // break; - // //case "Siemens.Simatic.PlcLanguages.Model.DataBlockData": - // // fld = new TIAProjectFolder(this, Node); - // // break; - // default: - // break; - // } - - // if (fld != null) - // { - // _allFolders.Add(fld); - - // var subFolderNodes = xmlDoc.SelectNodes("root/objects/StorageObject[parentlink[@link='" + id + "-" + instid + "']]"); - - // fld.SubNodes = subFolderNodes; - - // foreach (XmlNode subFolderNode in subFolderNodes) - // { - // var subFld = this.getProjectFolder(subFolderNode); - // if (subFld != null) - // fld.SubItems.Add(subFld); - // } - // } - - // return fld; - //} - - //private void ParseProject(Stream data) - //{ - // xmlDoc = new XmlDocument(); - - // xmlDoc.Load(data); - - // //xmlDoc.Save("C:\\Temp\\tia-export.xml"); - - // importTypeInfos = new Dictionary(); - // foreach (XmlNode typeInfo in xmlDoc.SelectNodes("root/importTypes/typeInfo")) - // { - // importTypeInfos.Add(typeInfo.Attributes["id"].Value, typeInfo.Attributes["name"].Value); - // } - - // asId2Names = new Dictionary(); - // foreach (XmlNode typeInfo in xmlDoc.SelectNodes("root/asId2Name/typeInfo")) - // { - // asId2Names.Add(typeInfo.Attributes["id"].Value, typeInfo.Attributes["name"].Value); - // } - - // relationId2Names = new Dictionary(); - // foreach (XmlNode typeInfo in xmlDoc.SelectNodes("root/relationId2Name/typeInfo")) - // { - // relationId2Names.Add(typeInfo.Attributes["id"].Value, typeInfo.Attributes["name"].Value); - // } - - // CoreAttributesId = asId2Names.FirstOrDefault(itm=>itm.Value=="Siemens.Automation.ObjectFrame.ICoreAttributes").Key; - - // var nd = xmlDoc.SelectSingleNode("root/rootObjects/entry[@name='Project']"); - // var prjObjId = nd.Attributes["objectId"].Value; - // var projectNode = xmlDoc.SelectSingleNode("root/objects/StorageObject[@instId='" + prjObjId.Split('-')[1] + "']"); - // ProjectStructure = this.getProjectFolder(projectNode); - //} - } -} +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Xml; +using DotNetSiemensPLCToolBoxLibrary.General; +using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA; +using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA.Enums; +using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA.Structs; +using Microsoft.Win32; + +namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles +{ + public partial class Step7ProjectV13 : Project, IDisposable + { + public enum TiaVersionTypes + { + V11 = 11, + V12 = 12, + V13 = 13, + V14 = 14, + } + + public static TiaVersionTypes TiaVersion { get; private set; } + + private string DataFile = null; + + private XmlDocument tiaProject; + + internal ZipHelper _ziphelper = new ZipHelper(null); + + public CultureInfo Culture { get; set; } + + public Step7ProjectV13(string projectfile, CultureInfo culture = null) + { + if (culture == null) + Culture = CultureInfo.CurrentCulture; + else + Culture = culture; + + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.AssemblyResolve += currentDomain_AssemblyResolve; + + ProjectFile = projectfile; + + if (ProjectFile.ToLower().EndsWith("zip") || ProjectFile.ToLower().EndsWith("zap13") || ProjectFile.ToLower().EndsWith("zap14")) + { + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap11"); + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap12"); + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap13"); + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap14"); + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al11"); + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al12"); + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al13"); + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al14"); + + if (string.IsNullOrEmpty(projectfile)) + throw new Exception("Zip-File contains no valid TIA Project !"); + this._ziphelper = new ZipHelper(projectfile); + } + + + try + { + var xmlDoc = new XmlDocument(); + xmlDoc.Load(_ziphelper.GetReadStream(projectfile)); + + 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) + { } + + DataFile = Path.GetDirectoryName(projectfile) + "\\System\\PEData.plf"; + ProjectFolder = projectfile.Substring(0, projectfile.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar; + + //BinaryParseTIAFile(); + //LoadProject(); + LoadViaOpennessDlls(); + + currentDomain.AssemblyResolve -= currentDomain_AssemblyResolve; + } + + internal XmlDocument xmlDoc; + + Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + int index = args.Name.IndexOf(','); + if (index == -1) + { + return null; + } + var name = args.Name.Substring(0, index) + ".dll"; + + var filePathReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Siemens\\Automation\\_InstalledSW\\TIAP13\\TIA_Opns") ?? + Registry.LocalMachine.OpenSubKey("SOFTWARE\\Siemens\\Automation\\_InstalledSW\\TIAP13\\TIA_Opns"); + if (filePathReg != null) + { + string filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V13"); + if (Directory.Exists(filePath) == false) + filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V13 SP1"); + var path = Path.Combine(filePath, name); + var fullPath = Path.GetFullPath(path); + if (File.Exists(fullPath)) + { + return Assembly.LoadFrom(fullPath); + } + } + + + return null; + } + + private object tiaExport; + private Type tiaExportType; + + + internal Dictionary TiaObjects = new Dictionary(); + + 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]; + } + } + + protected override void LoadProject() + { + _projectLoaded = true; + return; + + ////Stream stream = new FileStream("c:\\tia.xml", FileMode.OpenOrCreate); // new ChunkedMemoryStream(); + ////StreamWriter streamWriter = new StreamWriter(stream); + + ////XmlWriter xmlWriter = XmlWriter.Create(streamWriter, new XmlWriterSettings { Indent = true, CheckCharacters = false }); + + //var tiaObjectStructure = new TiaObjectStructure(); + //var xmlWriter = new TiaXmlWriter(tiaObjectStructure); + + ////xmlWriter.WriteStartDocument(); + ////xmlWriter.WriteStartElement("root"); + + //if (tiaExport == null) + //{ + // tiaExportType = Type.GetType("Siemens.Automation.ObjectFrame.FileStorage.Conversion.Export, Siemens.Automation.ObjectFrame.FileStorage"); + // if (TiaVersion < TiaVersionTypes.V13) + // { + // tiaExport = tiaExportType.InvokeMember("CreateInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, new object[] { DataFile, true }); + // } + // else + // { + // //V13 + // var helper = Type.GetType("Siemens.Automation.ObjectFrame.FileStorage.Base.Client.DataStoreClientHelper, Siemens.Automation.ObjectFrame.FileStorage.Base"); + // var metaManagerMth = helper.GetMethods(BindingFlags.NonPublic | BindingFlags.Static).FirstOrDefault(x => x.Name == "GetMetaManager"); + // var metaManager = metaManagerMth.Invoke(null, null); + // var crInst = tiaExportType.GetMethods().FirstOrDefault(x => x.Name == "CreateInstance"); + // tiaExport = crInst.Invoke(null, new object[] { DataFile, null, true, metaManager }); + // } + + // var memMgrType = Type.GetType("Siemens.Automation.ObjectFrame.Kernel.MemoryManager, Siemens.Automation.ObjectFrame.Kernel"); + // try + // { + // memMgrType.InvokeMember("Initialize", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, new object[] { 104857600 }); + // } + // catch (Exception) + // { + + // } + //} + + ////tiaExportType.InvokeMember("WriteCultures", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, tiaExport, new object[] { xmlWriter }); + ////tiaExportType.InvokeMember("StartExport", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, tiaExport, new object[] { xmlWriter }); + ////tiaExportType.InvokeMember("WriteRootObjectList", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, tiaExport, new object[] { xmlWriter }); + + //if (TiaVersion >= TiaVersionTypes.V13) + //{ + // var bgWorker = tiaExportType.GetField("m_BgWorker", BindingFlags.Instance | BindingFlags.NonPublic); + // bgWorker.SetValue(tiaExport, new BackgroundWorker() {WorkerReportsProgress = true, WorkerSupportsCancellation = true}); + //} + + //var serializeObjects = tiaExportType.GetMethod("SerializeObjects", + // BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, + // new Type[] {typeof (XmlWriter)}, null); + + //serializeObjects.Invoke(tiaExport, new object[] {xmlWriter}); + + //xmlWriter.Flush(); + //xmlWriter.Close(); + + ////streamWriter.Close(); + ////stream.Close(); + + ////stream.Position = 0; + ////ParseProject(stream); + } + + //internal Dictionary importTypeInfos; + //internal Dictionary asId2Names; + //internal Dictionary relationId2Names; + //internal string CoreAttributesId; + + //private TIAProjectFolder getProjectFolder(XmlNode Node) + //{ + // TIAProjectFolder fld = null; + // string id = Node.Attributes["id"].Value; + // string instid = Node.Attributes["instId"].Value; + + // string tiaType = importTypeInfos[id]; + + + + // switch (tiaType) + // { + // case "Siemens.Automation.DomainModel.ProjectData": + // fld = new TIAProjectFolder(this, Node); + // break; + // case "Siemens.Automation.DomainModel.FolderData": + // { + // var subType = Node.SelectSingleNode("attribSet[@id='" + CoreAttributesId + "']/attrib[@name='Subtype']").InnerText; + // if (subType == "ProgramBlocksFolder" || subType == "ProgramBlocksFolder.Subfolder") + // { + // fld = new TIABlocksFolder(this, Node); + // } + // else + // { + // fld = new TIAProjectFolder(this, Node); + // } + // break; + // } + // case "Siemens.Simatic.HwConfiguration.Model.DeviceData": + // fld = new TIAProjectFolder(this, Node); + // break; + // case "Siemens.Simatic.HwConfiguration.Model.S7ControllerTargetData": + // fld = new TIACPUFolder(this, Node); + // break; + // case "Siemens.Automation.DomainModel.EAMTZTagTableData": + // fld = new TIASymTabFolder(this, Node); + // break; + // //case "Siemens.Simatic.PlcLanguages.Model.DataBlockData": + // // fld = new TIAProjectFolder(this, Node); + // // break; + // default: + // break; + // } + + // if (fld != null) + // { + // _allFolders.Add(fld); + + // var subFolderNodes = xmlDoc.SelectNodes("root/objects/StorageObject[parentlink[@link='" + id + "-" + instid + "']]"); + + // fld.SubNodes = subFolderNodes; + + // foreach (XmlNode subFolderNode in subFolderNodes) + // { + // var subFld = this.getProjectFolder(subFolderNode); + // if (subFld != null) + // fld.SubItems.Add(subFld); + // } + // } + + // return fld; + //} + + //private void ParseProject(Stream data) + //{ + // xmlDoc = new XmlDocument(); + + // xmlDoc.Load(data); + + // //xmlDoc.Save("C:\\Temp\\tia-export.xml"); + + // importTypeInfos = new Dictionary(); + // foreach (XmlNode typeInfo in xmlDoc.SelectNodes("root/importTypes/typeInfo")) + // { + // importTypeInfos.Add(typeInfo.Attributes["id"].Value, typeInfo.Attributes["name"].Value); + // } + + // asId2Names = new Dictionary(); + // foreach (XmlNode typeInfo in xmlDoc.SelectNodes("root/asId2Name/typeInfo")) + // { + // asId2Names.Add(typeInfo.Attributes["id"].Value, typeInfo.Attributes["name"].Value); + // } + + // relationId2Names = new Dictionary(); + // foreach (XmlNode typeInfo in xmlDoc.SelectNodes("root/relationId2Name/typeInfo")) + // { + // relationId2Names.Add(typeInfo.Attributes["id"].Value, typeInfo.Attributes["name"].Value); + // } + + // CoreAttributesId = asId2Names.FirstOrDefault(itm=>itm.Value=="Siemens.Automation.ObjectFrame.ICoreAttributes").Key; + + // var nd = xmlDoc.SelectSingleNode("root/rootObjects/entry[@name='Project']"); + // var prjObjId = nd.Attributes["objectId"].Value; + // var projectNode = xmlDoc.SelectSingleNode("root/objects/StorageObject[@instId='" + prjObjId.Split('-')[1] + "']"); + // ProjectStructure = this.getProjectFolder(projectNode); + //} + } +} diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV13Tia.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV13Tia.cs index a397e70d..3fea8fbb 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV13Tia.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV13/Step7ProjectV13Tia.cs @@ -1,952 +1,952 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Xml.Linq; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V11; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V5; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders; -using Siemens.Engineering.SW; -using DotNetSiemensPLCToolBoxLibrary.General; -using System.Text.RegularExpressions; - -namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles -{ - public interface ITiaProjectBlockInfo : IProjectBlockInfo - { - string ExportToString(); - - string GenerateSource(); - } - - public partial class Step7ProjectV13 - { - private Siemens.Engineering.TiaPortal tiaPortal; - private Siemens.Engineering.Project tiapProject; - - public virtual void Dispose() - { - tiaPortal.Dispose(); - } - - - public class TIAOpennessProjectFolder : ProjectFolder - { - //internal string ID { get; private set; } - internal string InstID { get; private set; } - - protected Step7ProjectV13 TiaProject; - - public object TiaPortalItem { get; set; } - - public override string Name { get; set; } - - public TIAOpennessProjectFolder(Step7ProjectV13 Project) - { - this.Project = Project; - this.TiaProject = Project; - } - - } - - public class TIAOpennessProjectBlockInfo : ProjectBlockInfo, ITiaProjectBlockInfo - { - public Siemens.Engineering.SW.IBlock IBlock { get; set; } - public int BlockNumber { get; set; } - - public string BlockName - { - get - { - string retVal = BlockType.ToString().Replace("S5_", "") + BlockNumber.ToString(); - return retVal; - } - } - - public string ProgrammingLanguage - { - get { return IBlock.ProgrammingLanguage.ToString(); } - } - - public override string ToString() - { - string retVal = ""; - if (Deleted) - retVal += "$$_"; - if (Name != null) - retVal += BlockName + " (" + Name + ")"; - else - retVal += BlockName; - return retVal; - } - - public virtual string ExportToString() - { - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); - IBlock.Export(file, Siemens.Engineering.ExportOptions.None); - - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - - public override string Export(ExportFormat exportFormat) - { - return GenerateSource(); - } - - public virtual string GenerateSource() - { - //if (this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_DB || - // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_FBD || - // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_FBD_LIB || - // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_LAD || - // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_LAD_LIB || - // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_STL) - //{ - // return null; - //} - if (this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.SCL && - this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.STL) - { - return GenerateSourceCode(); - } - - return GenerateSourceXML(); - } - - public virtual string GenerateSourceXML() - { - var rootFolder = (TIAOpennessProjectFolder)ParentFolder; - while (!(rootFolder.TiaPortalItem is Siemens.Engineering.SW.ProgramblockSystemFolder)) - { - rootFolder = (TIAOpennessProjectFolder)rootFolder.Parent; - } - var ext = this.IBlock.ProgrammingLanguage.ToString().ToLower(); - if (ext == "stl") - { - ext = "awl"; - } - - dynamic tiaItem = ((TIAOpennessProgramFolder)rootFolder).TiaPortalItem; - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); - tiaItem.GenerateSourceFromBlocks(new[] { this.IBlock }, file); - - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - - public virtual string GenerateSourceCode() - { - return this.ExportToString(); - } - } - - public class TIAOpennessProjectDataTypeInfo : ProjectBlockInfo, ITiaProjectBlockInfo - { - public Siemens.Engineering.SW.ControllerDatatype IBlock { get; set; } - - public override string ToString() - { - return Name; - } - - public virtual string ExportToString() - { - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); - - IBlock.Export(file, Siemens.Engineering.ExportOptions.None); - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - - public override string Export(ExportFormat exportFormat) - { - return GenerateSource(); - } - - public string GenerateSource() - { - return ExportToString(); - } - } - - public class TIAOpennessTagTable - { - public string Name { get; set; } - - public List Constants { get; set; } - - } - - public class TIAOpennessConstant - { - private readonly ControllerConstant controllerConstant; - - internal TIAOpennessConstant(ControllerConstant controllerConstant) - { - this.controllerConstant = controllerConstant; - } - - public string Name { get; set; } - - private Regex _rgx = new Regex("(.*)", RegexOptions.Compiled); - - public object Value - { - get - { - var tp = this.controllerConstant.DataTypeName; - var strg = ExportToString(); - - var m = _rgx.Match(strg).Groups[1].Value; - if (tp == "Int") - return int.Parse(m); - return m; - } - } - - private string ExportToString() - { - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); - controllerConstant.Export(file, Siemens.Engineering.ExportOptions.None); - - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - } - - public class TIAOpennessControllerFolder : TIAOpennessProjectFolder, IRootProgrammFolder - { - public TIAOpennessControllerFolder(Step7ProjectV13 Project) - : base(Project) - { - this.Project = Project; - this.TiaProject = Project; - } - - public TIAOpennessProgramFolder ProgramFolder { get; set; } - public TIAOpennessPlcDatatypeFolder PlcDatatypeFolder { get; set; } - public TIAVarTabFolder VarTabFolder { get; set; } - - public Block GetBlockRecursive(string name) - { - var block = GetBlockRecursive(ProgramFolder, name); - if (block == null) - { - block = GetBlockRecursive(PlcDatatypeFolder, name); - } - - return block; - } - - private Block GetBlockRecursive(TIAOpennessProgramFolder folder, string name) - { - var block = folder.GetBlock(name); - if (block == null) - { - foreach (TIAOpennessProgramFolder projectFolder in folder.SubItems) - { - block = GetBlockRecursive(projectFolder, name); - if (block != null) - return block; - } - } - - return block; - } - - private Block GetBlockRecursive(TIAOpennessPlcDatatypeFolder folder, string name) - { - var block = folder.GetBlock(name); - if (block == null) - { - foreach (TIAOpennessPlcDatatypeFolder projectFolder in folder.SubItems) - { - block = GetBlockRecursive(projectFolder, name); - if (block != null) - return block; - } - } - - return block; - } - } - - public class TIAVarTabFolder : TIAOpennessProjectFolder - { - public TIAOpennessControllerFolder ControllerFolder { get; set; } - - public TIAVarTabFolder(Step7ProjectV13 Project, TIAOpennessControllerFolder ControllerFolder) : base(Project) - { - this.ControllerFolder = ControllerFolder; - this.Project = Project; - this.TiaProject = Project; - } - - public TIAOpennessConstant FindConstant(string name) - { - foreach (var t in TagTables) - { - var c = t.Constants.FirstOrDefault(x => x.Name == name); - if (c != null) - return c; - } - foreach (var f in SubItems.Flatten(x => x.SubItems)) - { - foreach (var t in TagTables) - { - var c = t.Constants.FirstOrDefault(x => x.Name == name); - if (c != null) - return c; - } - } - return null; - } - - public List TagTables - { - get - { - Siemens.Engineering.SW.ControllerTagTableAggregation tags = null; - var o = this.TiaPortalItem as Siemens.Engineering.SW.ControllerTagUserFolder; - if (o != null) - tags = o.TagTables; - var q = this.TiaPortalItem as Siemens.Engineering.SW.ControllerTagSystemFolder; - if (q != null) - tags = q.TagTables; - - var retVal = new List(); - - foreach (var tagList in tags) - { - var info = new TIAOpennessTagTable() { Name = tagList.Name }; - retVal.Add(info); - info.Constants = new List(); - foreach (var c in tagList.Constants) - { - info.Constants.Add(new TIAOpennessConstant(c) { Name = c.Name }); - } - } - return retVal; - } - } - } - - public class TIAOpennessPlcDatatypeFolder : TIAOpennessProjectFolder, IBlocksFolder - { - public TIAOpennessControllerFolder ControllerFolder { get; set; } - - public TIAOpennessPlcDatatypeFolder(Step7ProjectV13 Project, TIAOpennessControllerFolder ControllerFolder) - : base(Project) - { - this.ControllerFolder = ControllerFolder; - this.Project = Project; - this.TiaProject = Project; - } - - public List readPlcBlocksList() - { - if (_blockInfos != null) - return _blockInfos; - Siemens.Engineering.SW.ControllerDatatypeAggregation blocks = null; - var o = this.TiaPortalItem as Siemens.Engineering.SW.ControllerDatatypeUserFolder; - if (o != null) - blocks = o.Datatypes; - var q = this.TiaPortalItem as Siemens.Engineering.SW.ControllerDatatypeSystemFolder; - if (q != null) - blocks = q.Datatypes; - - _blockInfos = new List(); - - foreach (var block in blocks) - { - var info = new TIAOpennessProjectDataTypeInfo() { Name = block.Name, IBlock = block, ParentFolder = this}; - info.BlockType = DataTypes.PLCBlockType.UDT; - _blockInfos.Add(info); - } - - return BlockInfos; - } - - private List _blockInfos; - public List BlockInfos - { - get - { - if (_blockInfos == null) - readPlcBlocksList(); - return _blockInfos; - } - private set { _blockInfos = value; } - } - - public Block GetBlock(string BlockName) - { - if (BlockInfos == null) - readPlcBlocksList(); - - return GetBlock(BlockInfos.FirstOrDefault(x => x.Name == BlockName)); - } - - public Block GetBlock(ProjectBlockInfo blkInfo) - { - if (blkInfo == null) - return null; - - var iv = blkInfo as ITiaProjectBlockInfo; - var text = iv.ExportToString(); - - return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.DataType); - } - } - - public class TIAOpennessProgramFolder : TIAOpennessProjectFolder, IBlocksFolder - { - public TIAOpennessControllerFolder ControllerFolder { get; set; } - - public TIAOpennessProgramFolder(Step7ProjectV13 Project, TIAOpennessControllerFolder ControllerFolder) - : base(Project) - { - this.ControllerFolder = ControllerFolder; - this.Project = Project; - this.TiaProject = Project; - } - - public List readPlcBlocksList() - { - if (_blockInfos != null) - return _blockInfos; - - Siemens.Engineering.SW.IBlockAggregation blocks = null; - var o = this.TiaPortalItem as Siemens.Engineering.SW.ProgramblockUserFolder; - if (o != null) - blocks = o.Blocks; - var q = this.TiaPortalItem as Siemens.Engineering.SW.ProgramblockSystemFolder; - if (q != null) - blocks = q.Blocks; - - _blockInfos = new List(); - - foreach (var block in blocks) - { - var info = new TIAOpennessProjectBlockInfo() { Name = block.Name, IBlock = block, ParentFolder = this}; - if (block.Type == Siemens.Engineering.SW.BlockType.DB) - info.BlockType = DataTypes.PLCBlockType.DB; - else if (block.Type == Siemens.Engineering.SW.BlockType.FB) - info.BlockType = DataTypes.PLCBlockType.FB; - else if (block.Type == Siemens.Engineering.SW.BlockType.FC) - info.BlockType = DataTypes.PLCBlockType.FC; - else if (block.Type == Siemens.Engineering.SW.BlockType.OB) - info.BlockType = DataTypes.PLCBlockType.OB; - else if (block.Type == Siemens.Engineering.SW.BlockType.UDT) - info.BlockType = DataTypes.PLCBlockType.UDT; - info.BlockNumber = block.Number; - _blockInfos.Add(info); - } - return _blockInfos; - } - - private List _blockInfos; - public List BlockInfos - { - get - { - if (_blockInfos == null) - readPlcBlocksList(); - return _blockInfos; - } - private set { _blockInfos = value; } - } - - public Block GetBlock(string BlockName) - { - if (BlockInfos == null) - readPlcBlocksList(); - - return - GetBlock( - BlockInfos.Cast() - .FirstOrDefault(x => x.Name == BlockName || x.BlockName == BlockName)); - } - - public Block GetBlock(ProjectBlockInfo blkInfo) - { - if (blkInfo == null) - return null; - - var iv = blkInfo as ITiaProjectBlockInfo; - var text = iv.ExportToString(); - - return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.Programm); - } - } - internal void LoadViaOpennessDlls() - { - for (int i = 0; i < 10; i++) - { - try - { - if (tiaPortal != null) - { - tiaPortal.Dispose(); - tiaPortal = null; - } - tiaPortal = new Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithoutUserInterface); - tiapProject = tiaPortal.Projects.Open(ProjectFile); - } - catch (Siemens.Engineering.EngineeringSecurityException ex) - { - throw; - } - catch (Exception ex) - { - if (i == 9) - throw; - } - if (tiapProject != null) - break; - } - - - var main = new TIAOpennessProjectFolder(this) { Name = "Main" }; - ProjectStructure = main; - - //var frm = new sliver.Windows.Forms.StateBrowserForm(); - //frm.ObjectToBrowse = tiapProject; - //frm.Show(); - - foreach (var d in tiapProject.Devices) - { - if (d.Subtype.EndsWith(".Device") && !d.Subtype.StartsWith("GSD.") && !d.Subtype.StartsWith("ET200eco.")) //d.Subtype.StartsWith("S7300") || d.Subtype.StartsWith("S7400") || d.Subtype.StartsWith("S71200") || d.Subtype.StartsWith("S71500")) - { - - - var controller = d.DeviceItems.OfType().FirstOrDefault(); - if (controller == null) - { - var fld = new TIAOpennessProjectFolder(this) - { - Name = d.Name, - TiaPortalItem = d, - Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null - }; - main.SubItems.Add(fld); - - LoadSubDevicesViaOpennessDlls(fld, d); - } - else - { - var fld = new TIAOpennessControllerFolder(this) - { - Name = d.Name, - TiaPortalItem = d, - Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null - }; - main.SubItems.Add(fld); - - LoadControlerFolderViaOpennessDlls(fld, controller); - } - } - } - } - - internal void LoadSubDevicesViaOpennessDlls(TIAOpennessProjectFolder parent, Siemens.Engineering.HW.IHardwareObject device) - { - foreach (var e in device.DeviceItems) - { - var fld = new TIAOpennessProjectFolder(this) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - var d = e as Siemens.Engineering.HW.DeviceItem; - //d.Elements.ToList() - - parent.SubItems.Add(fld); - LoadSubDevicesViaOpennessDlls(fld, e); - } - } - - internal void LoadControlerFolderViaOpennessDlls(TIAOpennessControllerFolder parent, Siemens.Engineering.HW.ControllerTarget controller) - { - var fld = new TIAOpennessProgramFolder(this, parent) - { - TiaPortalItem = controller.ProgramblockFolder, - Name = controller.ProgramblockFolder.Name, - Parent = parent, - }; - parent.ProgramFolder = fld; - parent.SubItems.Add(fld); - LoadSubProgramBlocksFoldersViaOpennessDlls(fld, controller.ProgramblockFolder); - - - var fld2 = new TIAOpennessPlcDatatypeFolder(this, parent) - { - TiaPortalItem = controller.ControllerDatatypeFolder, - Name = "PLC data types", - Parent = parent, - }; - parent.PlcDatatypeFolder = fld2; - parent.SubItems.Add(fld2); - LoadSubPlcDatatypeFoldersViaOpennessDlls(fld2, controller.ControllerDatatypeFolder); - - var fld3 = new TIAVarTabFolder(this, parent) - { - TiaPortalItem = controller.ControllerTagFolder, - Name = "PLC data types", - Parent = parent, - }; - parent.VarTabFolder = fld3; - parent.SubItems.Add(fld3); - LoadSubVartabFoldersViaOpennessDlls(fld3, controller.ControllerDatatypeFolder); - } - - internal void LoadSubProgramBlocksFoldersViaOpennessDlls(TIAOpennessProgramFolder parent, Siemens.Engineering.SW.ProgramblockSystemFolder blockFolder) - { - foreach (var e in blockFolder.Folders) - { - var fld = new TIAOpennessProgramFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubProgramBlocksFoldersViaOpennessDlls(fld, e); - } - } - internal void LoadSubProgramBlocksFoldersViaOpennessDlls(TIAOpennessProgramFolder parent, Siemens.Engineering.SW.ProgramblockUserFolder blockFolder) - { - foreach (var e in blockFolder.Folders) - { - var fld = new TIAOpennessProgramFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubProgramBlocksFoldersViaOpennessDlls(fld, e); - } - } - internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, Siemens.Engineering.SW.ControllerDatatypeSystemFolder blockFolder) - { - foreach (var e in blockFolder.Folders) - { - var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); - } - } - internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, Siemens.Engineering.SW.ControllerDatatypeUserFolder blockFolder) - { - foreach (var e in blockFolder.Folders) - { - var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, Siemens.Engineering.SW.ControllerDatatypeSystemFolder blockFolder) - { - foreach (var e in blockFolder.Folders) - { - var fld = new TIAVarTabFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubVartabFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, Siemens.Engineering.SW.ControllerDatatypeUserFolder blockFolder) - { - foreach (var e in blockFolder.Folders) - { - var fld = new TIAVarTabFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubVartabFoldersViaOpennessDlls(fld, e); - } - } - - #region Parse DB UDT XML - - internal enum ParseType - { - Programm, - DataType - } - - internal static Block ParseTiaDbUdtXml(string xml, ProjectBlockInfo projectBlockInfo, TIAOpennessControllerFolder controllerFolder, ParseType parseType) - { - XElement xelement = XElement.Parse(xml); - var structure = xelement.Elements().FirstOrDefault(x => x.Name.LocalName.StartsWith("SW.")); - - var sections = structure.Element("AttributeList").Element("Interface").Elements().First(); - - var block = new TIADataBlock(); - block.Name = projectBlockInfo.Name; - - if (projectBlockInfo is TIAOpennessProjectBlockInfo) - block.BlockNumber = ((TIAOpennessProjectBlockInfo) projectBlockInfo).BlockNumber; - - if (parseType == ParseType.DataType) - block.BlockType = DataTypes.PLCBlockType.UDT; - else if (parseType == ParseType.Programm) - block.BlockType = DataTypes.PLCBlockType.DB; - - var parameterRoot = ParseTiaDbUdtSections(sections, block, controllerFolder); - - block.BlockType = DataTypes.PLCBlockType.DB; - block.Structure = parameterRoot; - - return block; - } - - internal static TIADataRow ParseTiaDbUdtSections(XElement sections, TIADataBlock block, TIAOpennessControllerFolder controllerFolder) - { - var parameterRoot = new TIADataRow("ROOTNODE", S7DataRowType.STRUCT, block); - var parameterIN = new TIADataRow("IN", S7DataRowType.STRUCT, block); - parameterIN.Parent = parameterRoot; - var parameterOUT = new TIADataRow("OUT", S7DataRowType.STRUCT, block); - parameterOUT.Parent = parameterRoot; - var parameterINOUT = new TIADataRow("IN_OUT", S7DataRowType.STRUCT, block); - parameterINOUT.Parent = parameterRoot; - var parameterSTAT = new TIADataRow("STATIC", S7DataRowType.STRUCT, block); - parameterSTAT.Parent = parameterRoot; - var parameterTEMP = new TIADataRow("TEMP", S7DataRowType.STRUCT, block); - parameterTEMP.Parent = parameterRoot; - - foreach (var xElement in sections.Elements()) - { - TIADataRow useRow = parameterRoot; - //var sectionName = xElement.Attribute("Name").Value; - //if (sectionName == "None" || sectionName == "Static") - //{ - // useRow = parameterSTAT; - // parameterRoot.Add(useRow); - //} - //else if (sectionName == "In") - //{ - // useRow = parameterIN; - // parameterRoot.Add(useRow); - //} - - parseChildren(useRow, xElement, controllerFolder); - } - - return parameterRoot; - } - - internal static void parseChildren(TIADataRow parentRow, XElement parentElement, TIAOpennessControllerFolder controllerFolder) - { - foreach (var xElement in parentElement.Elements()) - { - if (xElement.Name.LocalName == "Comment") - { - var text = xElement.Elements().FirstOrDefault(x => x.Attribute("Lang").Value == "de-DE"); - if (text == null) - text = xElement.Elements().FirstOrDefault(); - if (text != null) - parentRow.Comment = text.Value; - } - else if (xElement.Name.LocalName == "StartValue") - { - parentRow.StartValue = xElement.Value; - } - else if (xElement.Name.LocalName == "Sections") - { - var row = ParseTiaDbUdtSections(xElement, (TIADataBlock) parentRow.CurrentBlock, controllerFolder); - parentRow.AddRange(row.Children); - } - else if (xElement.Name.LocalName == "Member") - { - var name = xElement.Attribute("Name").Value; - var datatype = xElement.Attribute("Datatype").Value; - - var row = new TIADataRow(name, S7DataRowType.STRUCT, (TIABlock) parentRow.PlcBlock); - row.Parent = parentRow; - - if (datatype.Contains("Array[")) - { - List arrayStart = new List(); - List arrayStop = new List(); - - int pos1 = datatype.IndexOf("["); - int pos2 = datatype.IndexOf("]", pos1); - string[] arrays = datatype.Substring(pos1 + 1, pos2 - pos1 - 1).Split(','); - - foreach (string array in arrays) - { - string[] akar = array.Split(new string[] {".."}, StringSplitOptions.RemoveEmptyEntries); - int start = 0; - if (akar[0].StartsWith("\"")) - { - start = (int)controllerFolder.VarTabFolder.FindConstant(akar[0].Substring(1, akar[0].Length - 2)).Value; - } - else - { - start = Convert.ToInt32(akar[0].Trim()); - } - - int stop = 0; - if (akar[1].StartsWith("\"")) - { - stop = (int)controllerFolder.VarTabFolder.FindConstant(akar[1].Substring(1, akar[1].Length - 2)).Value; - } - else - { - stop = Convert.ToInt32(akar[1].Trim()); - } - - arrayStart.Add(start); - arrayStop.Add(stop); - } - - row.ArrayStart = arrayStart; - row.ArrayStop = arrayStop; - row.IsArray = true; - datatype = datatype.Substring(pos2 + 5); - } - - parentRow.Add(row); - - parseChildren(row, xElement, controllerFolder); - - if (datatype.StartsWith("\"")) - { - var udt = - controllerFolder.PlcDatatypeFolder.GetBlock(datatype.Substring(1, datatype.Length - 2)); - if (udt != null) - { - var tiaUdt = udt as TIADataBlock; - row.AddRange(((TIADataRow) tiaUdt.Structure).DeepCopy().Children); - - row.DataTypeBlock = udt; - } - row.DataType = S7DataRowType.UDT; - } - else if (datatype == "Struct") - { - - } - else if (datatype.StartsWith("String[")) - { - row.DataType = S7DataRowType.STRING; - row.StringSize = int.Parse(datatype.Substring(7, datatype.Length - 8)); - } - else - { - switch (datatype.ToLower()) - { - case "byte": - row.DataType = S7DataRowType.BYTE; - break; - case "bool": - row.DataType = S7DataRowType.BOOL; - break; - case "int": - row.DataType = S7DataRowType.INT; - break; - case "uint": - row.DataType = S7DataRowType.UINT; - break; - case "dint": - row.DataType = S7DataRowType.DINT; - break; - case "udint": - row.DataType = S7DataRowType.UDINT; - break; - case "word": - row.DataType = S7DataRowType.WORD; - break; - case "dword": - row.DataType = S7DataRowType.DWORD; - break; - case "char": - row.DataType = S7DataRowType.CHAR; - break; - case "any": - row.DataType = S7DataRowType.ANY; - break; - case "date": - row.DataType = S7DataRowType.DATE; - break; - case "date_and_time": - row.DataType = S7DataRowType.DATE_AND_TIME; - break; - case "real": - row.DataType = S7DataRowType.REAL; - break; - case "s5time": - row.DataType = S7DataRowType.S5TIME; - break; - case "time_of_day": - row.DataType = S7DataRowType.TIME_OF_DAY; - break; - case "time": - row.DataType = S7DataRowType.TIME; - break; - case "sint": - row.DataType = S7DataRowType.SINT; - break; - case "usint": - row.DataType = S7DataRowType.USINT; - break; - case "ulint": - row.DataType = S7DataRowType.ULINT; - break; - case "lint": - row.DataType = S7DataRowType.LINT; - break; - case "lreal": - row.DataType = S7DataRowType.LREAL; - break; - default: - row.DataType = S7DataRowType.UNKNOWN; - break; - } - } - } - else - { - } - } - } - #endregion - } -} +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V11; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V5; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders; +using Siemens.Engineering.SW; +using DotNetSiemensPLCToolBoxLibrary.General; +using System.Text.RegularExpressions; + +namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles +{ + public interface ITiaProjectBlockInfo : IProjectBlockInfo + { + string ExportToString(); + + string GenerateSource(); + } + + public partial class Step7ProjectV13 + { + private Siemens.Engineering.TiaPortal tiaPortal; + private Siemens.Engineering.Project tiapProject; + + public virtual void Dispose() + { + tiaPortal.Dispose(); + } + + + public class TIAOpennessProjectFolder : ProjectFolder + { + //internal string ID { get; private set; } + internal string InstID { get; private set; } + + protected Step7ProjectV13 TiaProject; + + public object TiaPortalItem { get; set; } + + public override string Name { get; set; } + + public TIAOpennessProjectFolder(Step7ProjectV13 Project) + { + this.Project = Project; + this.TiaProject = Project; + } + + } + + public class TIAOpennessProjectBlockInfo : ProjectBlockInfo, ITiaProjectBlockInfo + { + public Siemens.Engineering.SW.IBlock IBlock { get; set; } + public int BlockNumber { get; set; } + + public string BlockName + { + get + { + string retVal = BlockType.ToString().Replace("S5_", "") + BlockNumber.ToString(); + return retVal; + } + } + + public string ProgrammingLanguage + { + get { return IBlock.ProgrammingLanguage.ToString(); } + } + + public override string ToString() + { + string retVal = ""; + if (Deleted) + retVal += "$$_"; + if (Name != null) + retVal += BlockName + " (" + Name + ")"; + else + retVal += BlockName; + return retVal; + } + + public virtual string ExportToString() + { + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); + IBlock.Export(file, Siemens.Engineering.ExportOptions.None); + + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + + public override string Export(ExportFormat exportFormat) + { + return GenerateSource(); + } + + public virtual string GenerateSource() + { + //if (this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_DB || + // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_FBD || + // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_FBD_LIB || + // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_LAD || + // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_LAD_LIB || + // this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.F_STL) + //{ + // return null; + //} + if (this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.SCL && + this.IBlock.ProgrammingLanguage != Siemens.Engineering.SW.ProgrammingLanguage.STL) + { + return GenerateSourceCode(); + } + + return GenerateSourceXML(); + } + + public virtual string GenerateSourceXML() + { + var rootFolder = (TIAOpennessProjectFolder)ParentFolder; + while (!(rootFolder.TiaPortalItem is Siemens.Engineering.SW.ProgramblockSystemFolder)) + { + rootFolder = (TIAOpennessProjectFolder)rootFolder.Parent; + } + var ext = this.IBlock.ProgrammingLanguage.ToString().ToLower(); + if (ext == "stl") + { + ext = "awl"; + } + + dynamic tiaItem = ((TIAOpennessProgramFolder)rootFolder).TiaPortalItem; + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); + tiaItem.GenerateSourceFromBlocks(new[] { this.IBlock }, file); + + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + + public virtual string GenerateSourceCode() + { + return this.ExportToString(); + } + } + + public class TIAOpennessProjectDataTypeInfo : ProjectBlockInfo, ITiaProjectBlockInfo + { + public Siemens.Engineering.SW.ControllerDatatype IBlock { get; set; } + + public override string ToString() + { + return Name; + } + + public virtual string ExportToString() + { + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); + + IBlock.Export(file, Siemens.Engineering.ExportOptions.None); + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + + public override string Export(ExportFormat exportFormat) + { + return GenerateSource(); + } + + public string GenerateSource() + { + return ExportToString(); + } + } + + public class TIAOpennessTagTable + { + public string Name { get; set; } + + public List Constants { get; set; } + + } + + public class TIAOpennessConstant + { + private readonly ControllerConstant controllerConstant; + + internal TIAOpennessConstant(ControllerConstant controllerConstant) + { + this.controllerConstant = controllerConstant; + } + + public string Name { get; set; } + + private Regex _rgx = new Regex("(.*)", RegexOptions.Compiled); + + public object Value + { + get + { + var tp = this.controllerConstant.DataTypeName; + var strg = ExportToString(); + + var m = _rgx.Match(strg).Groups[1].Value; + if (tp == "Int") + return int.Parse(m); + return m; + } + } + + private string ExportToString() + { + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); + controllerConstant.Export(file, Siemens.Engineering.ExportOptions.None); + + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + } + + public class TIAOpennessControllerFolder : TIAOpennessProjectFolder, IRootProgrammFolder + { + public TIAOpennessControllerFolder(Step7ProjectV13 Project) + : base(Project) + { + this.Project = Project; + this.TiaProject = Project; + } + + public TIAOpennessProgramFolder ProgramFolder { get; set; } + public TIAOpennessPlcDatatypeFolder PlcDatatypeFolder { get; set; } + public TIAVarTabFolder VarTabFolder { get; set; } + + public Block GetBlockRecursive(string name) + { + var block = GetBlockRecursive(ProgramFolder, name); + if (block == null) + { + block = GetBlockRecursive(PlcDatatypeFolder, name); + } + + return block; + } + + private Block GetBlockRecursive(TIAOpennessProgramFolder folder, string name) + { + var block = folder.GetBlock(name); + if (block == null) + { + foreach (TIAOpennessProgramFolder projectFolder in folder.SubItems) + { + block = GetBlockRecursive(projectFolder, name); + if (block != null) + return block; + } + } + + return block; + } + + private Block GetBlockRecursive(TIAOpennessPlcDatatypeFolder folder, string name) + { + var block = folder.GetBlock(name); + if (block == null) + { + foreach (TIAOpennessPlcDatatypeFolder projectFolder in folder.SubItems) + { + block = GetBlockRecursive(projectFolder, name); + if (block != null) + return block; + } + } + + return block; + } + } + + public class TIAVarTabFolder : TIAOpennessProjectFolder + { + public TIAOpennessControllerFolder ControllerFolder { get; set; } + + public TIAVarTabFolder(Step7ProjectV13 Project, TIAOpennessControllerFolder ControllerFolder) : base(Project) + { + this.ControllerFolder = ControllerFolder; + this.Project = Project; + this.TiaProject = Project; + } + + public TIAOpennessConstant FindConstant(string name) + { + foreach (var t in TagTables) + { + var c = t.Constants.FirstOrDefault(x => x.Name == name); + if (c != null) + return c; + } + foreach (var f in SubItems.Flatten(x => x.SubItems)) + { + foreach (var t in TagTables) + { + var c = t.Constants.FirstOrDefault(x => x.Name == name); + if (c != null) + return c; + } + } + return null; + } + + public List TagTables + { + get + { + Siemens.Engineering.SW.ControllerTagTableAggregation tags = null; + var o = this.TiaPortalItem as Siemens.Engineering.SW.ControllerTagUserFolder; + if (o != null) + tags = o.TagTables; + var q = this.TiaPortalItem as Siemens.Engineering.SW.ControllerTagSystemFolder; + if (q != null) + tags = q.TagTables; + + var retVal = new List(); + + foreach (var tagList in tags) + { + var info = new TIAOpennessTagTable() { Name = tagList.Name }; + retVal.Add(info); + info.Constants = new List(); + foreach (var c in tagList.Constants) + { + info.Constants.Add(new TIAOpennessConstant(c) { Name = c.Name }); + } + } + return retVal; + } + } + } + + public class TIAOpennessPlcDatatypeFolder : TIAOpennessProjectFolder, IBlocksFolder + { + public TIAOpennessControllerFolder ControllerFolder { get; set; } + + public TIAOpennessPlcDatatypeFolder(Step7ProjectV13 Project, TIAOpennessControllerFolder ControllerFolder) + : base(Project) + { + this.ControllerFolder = ControllerFolder; + this.Project = Project; + this.TiaProject = Project; + } + + public List readPlcBlocksList() + { + if (_blockInfos != null) + return _blockInfos; + Siemens.Engineering.SW.ControllerDatatypeAggregation blocks = null; + var o = this.TiaPortalItem as Siemens.Engineering.SW.ControllerDatatypeUserFolder; + if (o != null) + blocks = o.Datatypes; + var q = this.TiaPortalItem as Siemens.Engineering.SW.ControllerDatatypeSystemFolder; + if (q != null) + blocks = q.Datatypes; + + _blockInfos = new List(); + + foreach (var block in blocks) + { + var info = new TIAOpennessProjectDataTypeInfo() { Name = block.Name, IBlock = block, ParentFolder = this}; + info.BlockType = DataTypes.PLCBlockType.UDT; + _blockInfos.Add(info); + } + + return BlockInfos; + } + + private List _blockInfos; + public List BlockInfos + { + get + { + if (_blockInfos == null) + readPlcBlocksList(); + return _blockInfos; + } + private set { _blockInfos = value; } + } + + public Block GetBlock(string BlockName) + { + if (BlockInfos == null) + readPlcBlocksList(); + + return GetBlock(BlockInfos.FirstOrDefault(x => x.Name == BlockName)); + } + + public Block GetBlock(ProjectBlockInfo blkInfo) + { + if (blkInfo == null) + return null; + + var iv = blkInfo as ITiaProjectBlockInfo; + var text = iv.ExportToString(); + + return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.DataType); + } + } + + public class TIAOpennessProgramFolder : TIAOpennessProjectFolder, IBlocksFolder + { + public TIAOpennessControllerFolder ControllerFolder { get; set; } + + public TIAOpennessProgramFolder(Step7ProjectV13 Project, TIAOpennessControllerFolder ControllerFolder) + : base(Project) + { + this.ControllerFolder = ControllerFolder; + this.Project = Project; + this.TiaProject = Project; + } + + public List readPlcBlocksList() + { + if (_blockInfos != null) + return _blockInfos; + + Siemens.Engineering.SW.IBlockAggregation blocks = null; + var o = this.TiaPortalItem as Siemens.Engineering.SW.ProgramblockUserFolder; + if (o != null) + blocks = o.Blocks; + var q = this.TiaPortalItem as Siemens.Engineering.SW.ProgramblockSystemFolder; + if (q != null) + blocks = q.Blocks; + + _blockInfos = new List(); + + foreach (var block in blocks) + { + var info = new TIAOpennessProjectBlockInfo() { Name = block.Name, IBlock = block, ParentFolder = this}; + if (block.Type == Siemens.Engineering.SW.BlockType.DB) + info.BlockType = DataTypes.PLCBlockType.DB; + else if (block.Type == Siemens.Engineering.SW.BlockType.FB) + info.BlockType = DataTypes.PLCBlockType.FB; + else if (block.Type == Siemens.Engineering.SW.BlockType.FC) + info.BlockType = DataTypes.PLCBlockType.FC; + else if (block.Type == Siemens.Engineering.SW.BlockType.OB) + info.BlockType = DataTypes.PLCBlockType.OB; + else if (block.Type == Siemens.Engineering.SW.BlockType.UDT) + info.BlockType = DataTypes.PLCBlockType.UDT; + info.BlockNumber = block.Number; + _blockInfos.Add(info); + } + return _blockInfos; + } + + private List _blockInfos; + public List BlockInfos + { + get + { + if (_blockInfos == null) + readPlcBlocksList(); + return _blockInfos; + } + private set { _blockInfos = value; } + } + + public Block GetBlock(string BlockName) + { + if (BlockInfos == null) + readPlcBlocksList(); + + return + GetBlock( + BlockInfos.Cast() + .FirstOrDefault(x => x.Name == BlockName || x.BlockName == BlockName)); + } + + public Block GetBlock(ProjectBlockInfo blkInfo) + { + if (blkInfo == null) + return null; + + var iv = blkInfo as ITiaProjectBlockInfo; + var text = iv.ExportToString(); + + return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.Programm); + } + } + internal void LoadViaOpennessDlls() + { + for (int i = 0; i < 10; i++) + { + try + { + if (tiaPortal != null) + { + tiaPortal.Dispose(); + tiaPortal = null; + } + tiaPortal = new Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithoutUserInterface); + tiapProject = tiaPortal.Projects.Open(ProjectFile); + } + catch (Siemens.Engineering.EngineeringSecurityException ex) + { + throw; + } + catch (Exception ex) + { + if (i == 9) + throw; + } + if (tiapProject != null) + break; + } + + + var main = new TIAOpennessProjectFolder(this) { Name = "Main" }; + ProjectStructure = main; + + //var frm = new sliver.Windows.Forms.StateBrowserForm(); + //frm.ObjectToBrowse = tiapProject; + //frm.Show(); + + foreach (var d in tiapProject.Devices) + { + if (d.Subtype.EndsWith(".Device") && !d.Subtype.StartsWith("GSD.") && !d.Subtype.StartsWith("ET200eco.")) //d.Subtype.StartsWith("S7300") || d.Subtype.StartsWith("S7400") || d.Subtype.StartsWith("S71200") || d.Subtype.StartsWith("S71500")) + { + + + var controller = d.DeviceItems.OfType().FirstOrDefault(); + if (controller == null) + { + var fld = new TIAOpennessProjectFolder(this) + { + Name = d.Name, + TiaPortalItem = d, + Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null + }; + main.SubItems.Add(fld); + + LoadSubDevicesViaOpennessDlls(fld, d); + } + else + { + var fld = new TIAOpennessControllerFolder(this) + { + Name = d.Name, + TiaPortalItem = d, + Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null + }; + main.SubItems.Add(fld); + + LoadControlerFolderViaOpennessDlls(fld, controller); + } + } + } + } + + internal void LoadSubDevicesViaOpennessDlls(TIAOpennessProjectFolder parent, Siemens.Engineering.HW.IHardwareObject device) + { + foreach (var e in device.DeviceItems) + { + var fld = new TIAOpennessProjectFolder(this) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + var d = e as Siemens.Engineering.HW.DeviceItem; + //d.Elements.ToList() + + parent.SubItems.Add(fld); + LoadSubDevicesViaOpennessDlls(fld, e); + } + } + + internal void LoadControlerFolderViaOpennessDlls(TIAOpennessControllerFolder parent, Siemens.Engineering.HW.ControllerTarget controller) + { + var fld = new TIAOpennessProgramFolder(this, parent) + { + TiaPortalItem = controller.ProgramblockFolder, + Name = controller.ProgramblockFolder.Name, + Parent = parent, + }; + parent.ProgramFolder = fld; + parent.SubItems.Add(fld); + LoadSubProgramBlocksFoldersViaOpennessDlls(fld, controller.ProgramblockFolder); + + + var fld2 = new TIAOpennessPlcDatatypeFolder(this, parent) + { + TiaPortalItem = controller.ControllerDatatypeFolder, + Name = "PLC data types", + Parent = parent, + }; + parent.PlcDatatypeFolder = fld2; + parent.SubItems.Add(fld2); + LoadSubPlcDatatypeFoldersViaOpennessDlls(fld2, controller.ControllerDatatypeFolder); + + var fld3 = new TIAVarTabFolder(this, parent) + { + TiaPortalItem = controller.ControllerTagFolder, + Name = "PLC data types", + Parent = parent, + }; + parent.VarTabFolder = fld3; + parent.SubItems.Add(fld3); + LoadSubVartabFoldersViaOpennessDlls(fld3, controller.ControllerDatatypeFolder); + } + + internal void LoadSubProgramBlocksFoldersViaOpennessDlls(TIAOpennessProgramFolder parent, Siemens.Engineering.SW.ProgramblockSystemFolder blockFolder) + { + foreach (var e in blockFolder.Folders) + { + var fld = new TIAOpennessProgramFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubProgramBlocksFoldersViaOpennessDlls(fld, e); + } + } + internal void LoadSubProgramBlocksFoldersViaOpennessDlls(TIAOpennessProgramFolder parent, Siemens.Engineering.SW.ProgramblockUserFolder blockFolder) + { + foreach (var e in blockFolder.Folders) + { + var fld = new TIAOpennessProgramFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubProgramBlocksFoldersViaOpennessDlls(fld, e); + } + } + internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, Siemens.Engineering.SW.ControllerDatatypeSystemFolder blockFolder) + { + foreach (var e in blockFolder.Folders) + { + var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); + } + } + internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, Siemens.Engineering.SW.ControllerDatatypeUserFolder blockFolder) + { + foreach (var e in blockFolder.Folders) + { + var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, Siemens.Engineering.SW.ControllerDatatypeSystemFolder blockFolder) + { + foreach (var e in blockFolder.Folders) + { + var fld = new TIAVarTabFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubVartabFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, Siemens.Engineering.SW.ControllerDatatypeUserFolder blockFolder) + { + foreach (var e in blockFolder.Folders) + { + var fld = new TIAVarTabFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubVartabFoldersViaOpennessDlls(fld, e); + } + } + + #region Parse DB UDT XML + + internal enum ParseType + { + Programm, + DataType + } + + internal static Block ParseTiaDbUdtXml(string xml, ProjectBlockInfo projectBlockInfo, TIAOpennessControllerFolder controllerFolder, ParseType parseType) + { + XElement xelement = XElement.Parse(xml); + var structure = xelement.Elements().FirstOrDefault(x => x.Name.LocalName.StartsWith("SW.")); + + var sections = structure.Element("AttributeList").Element("Interface").Elements().First(); + + var block = new TIADataBlock(); + block.Name = projectBlockInfo.Name; + + if (projectBlockInfo is TIAOpennessProjectBlockInfo) + block.BlockNumber = ((TIAOpennessProjectBlockInfo) projectBlockInfo).BlockNumber; + + if (parseType == ParseType.DataType) + block.BlockType = DataTypes.PLCBlockType.UDT; + else if (parseType == ParseType.Programm) + block.BlockType = DataTypes.PLCBlockType.DB; + + var parameterRoot = ParseTiaDbUdtSections(sections, block, controllerFolder); + + block.BlockType = DataTypes.PLCBlockType.DB; + block.Structure = parameterRoot; + + return block; + } + + internal static TIADataRow ParseTiaDbUdtSections(XElement sections, TIADataBlock block, TIAOpennessControllerFolder controllerFolder) + { + var parameterRoot = new TIADataRow("ROOTNODE", S7DataRowType.STRUCT, block); + var parameterIN = new TIADataRow("IN", S7DataRowType.STRUCT, block); + parameterIN.Parent = parameterRoot; + var parameterOUT = new TIADataRow("OUT", S7DataRowType.STRUCT, block); + parameterOUT.Parent = parameterRoot; + var parameterINOUT = new TIADataRow("IN_OUT", S7DataRowType.STRUCT, block); + parameterINOUT.Parent = parameterRoot; + var parameterSTAT = new TIADataRow("STATIC", S7DataRowType.STRUCT, block); + parameterSTAT.Parent = parameterRoot; + var parameterTEMP = new TIADataRow("TEMP", S7DataRowType.STRUCT, block); + parameterTEMP.Parent = parameterRoot; + + foreach (var xElement in sections.Elements()) + { + TIADataRow useRow = parameterRoot; + //var sectionName = xElement.Attribute("Name").Value; + //if (sectionName == "None" || sectionName == "Static") + //{ + // useRow = parameterSTAT; + // parameterRoot.Add(useRow); + //} + //else if (sectionName == "In") + //{ + // useRow = parameterIN; + // parameterRoot.Add(useRow); + //} + + parseChildren(useRow, xElement, controllerFolder); + } + + return parameterRoot; + } + + internal static void parseChildren(TIADataRow parentRow, XElement parentElement, TIAOpennessControllerFolder controllerFolder) + { + foreach (var xElement in parentElement.Elements()) + { + if (xElement.Name.LocalName == "Comment") + { + var text = xElement.Elements().FirstOrDefault(x => x.Attribute("Lang").Value == "de-DE"); + if (text == null) + text = xElement.Elements().FirstOrDefault(); + if (text != null) + parentRow.Comment = text.Value; + } + else if (xElement.Name.LocalName == "StartValue") + { + parentRow.StartValue = xElement.Value; + } + else if (xElement.Name.LocalName == "Sections") + { + var row = ParseTiaDbUdtSections(xElement, (TIADataBlock) parentRow.CurrentBlock, controllerFolder); + parentRow.AddRange(row.Children); + } + else if (xElement.Name.LocalName == "Member") + { + var name = xElement.Attribute("Name").Value; + var datatype = xElement.Attribute("Datatype").Value; + + var row = new TIADataRow(name, S7DataRowType.STRUCT, (TIABlock) parentRow.PlcBlock); + row.Parent = parentRow; + + if (datatype.Contains("Array[")) + { + List arrayStart = new List(); + List arrayStop = new List(); + + int pos1 = datatype.IndexOf("["); + int pos2 = datatype.IndexOf("]", pos1); + string[] arrays = datatype.Substring(pos1 + 1, pos2 - pos1 - 1).Split(','); + + foreach (string array in arrays) + { + string[] akar = array.Split(new string[] {".."}, StringSplitOptions.RemoveEmptyEntries); + int start = 0; + if (akar[0].StartsWith("\"")) + { + start = (int)controllerFolder.VarTabFolder.FindConstant(akar[0].Substring(1, akar[0].Length - 2)).Value; + } + else + { + start = Convert.ToInt32(akar[0].Trim()); + } + + int stop = 0; + if (akar[1].StartsWith("\"")) + { + stop = (int)controllerFolder.VarTabFolder.FindConstant(akar[1].Substring(1, akar[1].Length - 2)).Value; + } + else + { + stop = Convert.ToInt32(akar[1].Trim()); + } + + arrayStart.Add(start); + arrayStop.Add(stop); + } + + row.ArrayStart = arrayStart; + row.ArrayStop = arrayStop; + row.IsArray = true; + datatype = datatype.Substring(pos2 + 5); + } + + parentRow.Add(row); + + parseChildren(row, xElement, controllerFolder); + + if (datatype.StartsWith("\"")) + { + var udt = + controllerFolder.PlcDatatypeFolder.GetBlock(datatype.Substring(1, datatype.Length - 2)); + if (udt != null) + { + var tiaUdt = udt as TIADataBlock; + row.AddRange(((TIADataRow) tiaUdt.Structure).DeepCopy().Children); + + row.DataTypeBlock = udt; + } + row.DataType = S7DataRowType.UDT; + } + else if (datatype == "Struct") + { + + } + else if (datatype.StartsWith("String[")) + { + row.DataType = S7DataRowType.STRING; + row.StringSize = int.Parse(datatype.Substring(7, datatype.Length - 8)); + } + else + { + switch (datatype.ToLower()) + { + case "byte": + row.DataType = S7DataRowType.BYTE; + break; + case "bool": + row.DataType = S7DataRowType.BOOL; + break; + case "int": + row.DataType = S7DataRowType.INT; + break; + case "uint": + row.DataType = S7DataRowType.UINT; + break; + case "dint": + row.DataType = S7DataRowType.DINT; + break; + case "udint": + row.DataType = S7DataRowType.UDINT; + break; + case "word": + row.DataType = S7DataRowType.WORD; + break; + case "dword": + row.DataType = S7DataRowType.DWORD; + break; + case "char": + row.DataType = S7DataRowType.CHAR; + break; + case "any": + row.DataType = S7DataRowType.ANY; + break; + case "date": + row.DataType = S7DataRowType.DATE; + break; + case "date_and_time": + row.DataType = S7DataRowType.DATE_AND_TIME; + break; + case "real": + row.DataType = S7DataRowType.REAL; + break; + case "s5time": + row.DataType = S7DataRowType.S5TIME; + break; + case "time_of_day": + row.DataType = S7DataRowType.TIME_OF_DAY; + break; + case "time": + row.DataType = S7DataRowType.TIME; + break; + case "sint": + row.DataType = S7DataRowType.SINT; + break; + case "usint": + row.DataType = S7DataRowType.USINT; + break; + case "ulint": + row.DataType = S7DataRowType.ULINT; + break; + case "lint": + row.DataType = S7DataRowType.LINT; + break; + case "lreal": + row.DataType = S7DataRowType.LREAL; + break; + default: + row.DataType = S7DataRowType.UNKNOWN; + break; + } + } + } + else + { + } + } + } + #endregion + } +} diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1.csproj b/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1.csproj index e14f8089..2c547dfc 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1.csproj +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1.csproj @@ -1,68 +1,68 @@ - - - - - Debug - AnyCPU - {AB0895AE-446F-4CD8-96AB-C8007D618593} - Library - Properties - DotNetSiemensPLCToolBoxLibrary.TIAV14SP1 - DotNetSiemensPLCToolBoxLibrary.TIAV14SP1 - v4.6.1 - 512 - - - - true - full - false - ..\compiled\ - DEBUG;TRACE - prompt - 4 - false - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - false - - - - False - ..\externalDlls\siemens\V14SP1\Siemens.Engineering.dll - False - - - False - ..\externalDlls\siemens\V14SP1\Siemens.Engineering.Hmi.dll - False - - - - - - - - - - - - - - - - - - {e3ed87e8-b550-46ac-9196-9688d30efd29} - DotNetSiemensPLCToolBoxLibrary - - - - + + + + + Debug + AnyCPU + {AB0895AE-446F-4CD8-96AB-C8007D618593} + Library + Properties + DotNetSiemensPLCToolBoxLibrary.TIAV14SP1 + DotNetSiemensPLCToolBoxLibrary.TIAV14SP1 + v4.6.1 + 512 + + + + true + full + false + ..\compiled\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + false + + + + False + ..\externalDlls\siemens\V14SP1\Siemens.Engineering.dll + False + + + False + ..\externalDlls\siemens\V14SP1\Siemens.Engineering.Hmi.dll + False + + + + + + + + + + + + + + + + + + {e3ed87e8-b550-46ac-9196-9688d30efd29} + DotNetSiemensPLCToolBoxLibrary + + + + \ No newline at end of file diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Properties/AssemblyInfo.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Properties/AssemblyInfo.cs index 1e51f915..90a08479 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Properties/AssemblyInfo.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Properties/AssemblyInfo.cs @@ -1,36 +1,36 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// Allgemeine Informationen über eine Assembly werden über die folgenden -// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, -// die einer Assembly zugeordnet sind. -[assembly: AssemblyTitle("DotNetSiemensPLCToolBoxLibrary.TIAV14SP1")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("DotNetSiemensPLCToolBoxLibrary.TIAV14SP1")] -[assembly: AssemblyCopyright("Copyright © 2017")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly -// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von -// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. -[assembly: ComVisible(false)] - -// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird -[assembly: Guid("ab0895ae-446f-4cd8-96ab-c8007d618593")] - -// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: -// -// Hauptversion -// Nebenversion -// Buildnummer -// Revision -// -// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, -// indem Sie "*" wie unten gezeigt eingeben: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("DotNetSiemensPLCToolBoxLibrary.TIAV14SP1")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("DotNetSiemensPLCToolBoxLibrary.TIAV14SP1")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("ab0895ae-446f-4cd8-96ab-c8007d618593")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// indem Sie "*" wie unten gezeigt eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Step7ProjectV14SP1.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Step7ProjectV14SP1.cs index ce01436b..0f93a467 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Step7ProjectV14SP1.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Step7ProjectV14SP1.cs @@ -1,123 +1,123 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Reflection; -using System.Xml; -using DotNetSiemensPLCToolBoxLibrary.General; -using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA; -using Microsoft.Win32; - -namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V14SP1 -{ - public partial class Step7ProjectV14SP1 : Project, IDisposable - { - public enum TiaVersionTypes - { - V11 = 11, - V12 = 12, - V13 = 13, - V14 = 14, - } - - public static TiaVersionTypes TiaVersion { get; private set; } - - private string DataFile = null; - - private XmlDocument tiaProject; - - internal ZipHelper _ziphelper = new ZipHelper(null); - - public CultureInfo Culture { get; set; } - - public Step7ProjectV14SP1(string projectfile, CultureInfo culture = null) - { - if (culture == null) - Culture = CultureInfo.CurrentCulture; - else - Culture = culture; - - AppDomain currentDomain = AppDomain.CurrentDomain; - currentDomain.AssemblyResolve += currentDomain_AssemblyResolve; - - ProjectFile = projectfile; - - if (ProjectFile.ToLower().EndsWith("zip") || ProjectFile.ToLower().EndsWith("zap14")) - { - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap14"); - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al14"); - - if (string.IsNullOrEmpty(projectfile)) - throw new Exception("Zip-File contains no valid TIA Project !"); - this._ziphelper = new ZipHelper(projectfile); - } - - - try - { - var xmlDoc = new XmlDocument(); - xmlDoc.Load(_ziphelper.GetReadStream(projectfile)); - - 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) - { } - - DataFile = Path.GetDirectoryName(projectfile) + "\\System\\PEData.plf"; - ProjectFolder = projectfile.Substring(0, projectfile.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar; - - //BinaryParseTIAFile(); - //LoadProject(); - LoadViaOpennessDlls(); - - currentDomain.AssemblyResolve -= currentDomain_AssemblyResolve; - } - - internal XmlDocument xmlDoc; - - Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) - { - int index = args.Name.IndexOf(','); - if (index == -1) - { - return null; - } - var name = args.Name.Substring(0, index) + ".dll"; - - var filePathReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Siemens\\Automation\\_InstalledSW\\TIAP14\\Global") ?? - Registry.LocalMachine.OpenSubKey("SOFTWARE\\Siemens\\Automation\\_InstalledSW\\TIAP14\\Global"); - - if (filePathReg != null) - { - string filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V14"); - if (Directory.Exists(filePath) == false) - filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V14 SP1"); - var path = Path.Combine(filePath, name); - var fullPath = Path.GetFullPath(path); - if (File.Exists(fullPath)) - { - return Assembly.LoadFrom(fullPath); - } - } - - return null; - } - - private object tiaExport; - private Type tiaExportType; - - - internal Dictionary TiaObjects = new Dictionary(); - - protected override void LoadProject() - { - _projectLoaded = true; - } - } -} +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Reflection; +using System.Xml; +using DotNetSiemensPLCToolBoxLibrary.General; +using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA; +using Microsoft.Win32; + +namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V14SP1 +{ + public partial class Step7ProjectV14SP1 : Project, IDisposable + { + public enum TiaVersionTypes + { + V11 = 11, + V12 = 12, + V13 = 13, + V14 = 14, + } + + public static TiaVersionTypes TiaVersion { get; private set; } + + private string DataFile = null; + + private XmlDocument tiaProject; + + internal ZipHelper _ziphelper = new ZipHelper(null); + + public CultureInfo Culture { get; set; } + + public Step7ProjectV14SP1(string projectfile, CultureInfo culture = null) + { + if (culture == null) + Culture = CultureInfo.CurrentCulture; + else + Culture = culture; + + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.AssemblyResolve += currentDomain_AssemblyResolve; + + ProjectFile = projectfile; + + if (ProjectFile.ToLower().EndsWith("zip") || ProjectFile.ToLower().EndsWith("zap14")) + { + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap14"); + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al14"); + + if (string.IsNullOrEmpty(projectfile)) + throw new Exception("Zip-File contains no valid TIA Project !"); + this._ziphelper = new ZipHelper(projectfile); + } + + + try + { + var xmlDoc = new XmlDocument(); + xmlDoc.Load(_ziphelper.GetReadStream(projectfile)); + + 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) + { } + + DataFile = Path.GetDirectoryName(projectfile) + "\\System\\PEData.plf"; + ProjectFolder = projectfile.Substring(0, projectfile.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar; + + //BinaryParseTIAFile(); + //LoadProject(); + LoadViaOpennessDlls(); + + currentDomain.AssemblyResolve -= currentDomain_AssemblyResolve; + } + + internal XmlDocument xmlDoc; + + Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + int index = args.Name.IndexOf(','); + if (index == -1) + { + return null; + } + var name = args.Name.Substring(0, index) + ".dll"; + + var filePathReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Siemens\\Automation\\_InstalledSW\\TIAP14\\Global") ?? + Registry.LocalMachine.OpenSubKey("SOFTWARE\\Siemens\\Automation\\_InstalledSW\\TIAP14\\Global"); + + if (filePathReg != null) + { + string filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V14"); + if (Directory.Exists(filePath) == false) + filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V14 SP1"); + var path = Path.Combine(filePath, name); + var fullPath = Path.GetFullPath(path); + if (File.Exists(fullPath)) + { + return Assembly.LoadFrom(fullPath); + } + } + + return null; + } + + private object tiaExport; + private Type tiaExportType; + + + internal Dictionary TiaObjects = new Dictionary(); + + protected override void LoadProject() + { + _projectLoaded = true; + } + } +} diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Step7ProjectV14SP1Tia.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Step7ProjectV14SP1Tia.cs index 2eb11fa9..f1dcbc29 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Step7ProjectV14SP1Tia.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV14SP1/Step7ProjectV14SP1Tia.cs @@ -1,1020 +1,1020 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Xml.Linq; -using DotNetSiemensPLCToolBoxLibrary.DataTypes; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V11; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V5; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders; -using Siemens.Engineering; -using Siemens.Engineering.HW.Features; -using Siemens.Engineering.SW; -using Siemens.Engineering.HW; -using Siemens.Engineering.SW.Blocks; -using Siemens.Engineering.SW.Types; -using Siemens.Engineering.SW.Tags; -using DotNetSiemensPLCToolBoxLibrary.General; -using System.Text.RegularExpressions; - -namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V14SP1 -{ - public interface ITiaProjectBlockInfo : IProjectBlockInfo - { - } - - public partial class Step7ProjectV14SP1 - { - - private Siemens.Engineering.TiaPortal tiaPortal; - - private Siemens.Engineering.Project tiapProject; - - public virtual void Dispose() - { - tiaPortal.Dispose(); - } - - - public class TIAOpennessProjectFolder : ProjectFolder - { - internal string InstID { get; private set; } - - protected Step7ProjectV14SP1 TiaProject; - - public override string Name { get; set; } - - public TIAOpennessProjectFolder(Step7ProjectV14SP1 Project) - { - this.Project = Project; - this.TiaProject = Project; - } - } - - public class TIAOpennessProjectBlockInfo : ProjectBlockInfo, ITiaProjectBlockInfo - { - internal TIAOpennessProjectBlockInfo(PlcBlock plcBlock) - { - this.plcBlock = plcBlock; - } - PlcBlock plcBlock; - - internal PLCLanguage SetBlockLanguage; - public override PLCLanguage BlockLanguage { get { return SetBlockLanguage; } } - - public int BlockNumber { get; set; } - - public string BlockName - { - get - { - string retVal = BlockType.ToString().Replace("S5_", "") + BlockNumber.ToString(); - return retVal; - } - } - - public string ProgrammingLanguage - { - get { return plcBlock.ProgrammingLanguage.ToString(); } - } - - public override string ToString() - { - string retVal = ""; - if (Deleted) - retVal += "$$_"; - if (Name != null) - retVal += BlockName + " (" + Name + ")"; - else - retVal += BlockName; - return retVal; - } - - public override string Export(ExportFormat exportFormat) - { - var ext = "xml"; - if (exportFormat != ExportFormat.Xml) - { - if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.SCL) - { - ext = "scl"; - } - else if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.STL || this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.F_STL) - { - ext = "awl"; - } - else if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.DB || this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.F_DB) - { - ext = "db"; - } - } - - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); - if (ext == "xml") - { - plcBlock.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); - } - else - { - var fld = this.ParentFolder; - while (!(fld is TIAOpennessControllerFolder)) - { - fld = fld.Parent; - } - ((TIAOpennessControllerFolder)fld).plcSoftware.ExternalSourceGroup.GenerateSource(new[] { this.plcBlock }, new FileInfo(file), Siemens.Engineering.SW.ExternalSources.GenerateOptions.None); - } - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - private PLCBlockType? _plcBlockType; - - public override PLCBlockType BlockType - { - get - { - if (_plcBlockType == null) - { - _plcBlockType = PLCBlockType.FC; - if (plcBlock is FB) - { - _plcBlockType = PLCBlockType.FB; - } - else if (plcBlock is OB) - { - _plcBlockType = PLCBlockType.OB; - } - } - return _plcBlockType.Value; - } - set { _plcBlockType = value; } - } - - private string xml; - } - - public class TIAOpennessProjectDataTypeInfo : ProjectBlockInfo, ITiaProjectBlockInfo - { - internal TIAOpennessProjectDataTypeInfo(PlcType plcType) - { - this.plcType = plcType; - } - private PlcType plcType; - - public override string ToString() - { - return Name; - } - - public override PLCBlockType BlockType - { - get - { - return PLCBlockType.UDT; - } - set { } - } - - public override PLCLanguage BlockLanguage - { - get - { - return PLCLanguage.DB; - } - } - - public override string Export(ExportFormat exportFormat) - { - var ext = "udt"; - if (exportFormat == ExportFormat.Xml) - { - ext = "xml"; - } - - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); - if (ext == "xml") - { - plcType.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); - } - else - { - var fld = this.ParentFolder; - while (!(fld is TIAOpennessControllerFolder)) - { - fld = fld.Parent; - } - ((TIAOpennessControllerFolder)fld).plcSoftware.ExternalSourceGroup.GenerateSource(new[] { this.plcType }, new FileInfo(file), Siemens.Engineering.SW.ExternalSources.GenerateOptions.None); - } - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - } - - public class TIAOpennessTagTable - { - public string Name { get; set; } - - public List Constants { get; set; } - - } - - public class TIAOpennessConstant - { - private readonly PlcUserConstant controllerConstant; - - internal TIAOpennessConstant(PlcUserConstant controllerConstant) - { - this.controllerConstant = controllerConstant; - } - - public string Name { get; set; } - - private Regex _rgx = new Regex("(.*)", RegexOptions.Compiled); - - public object Value - { - get - { - var tp = this.controllerConstant.DataTypeName; - var strg = ExportToString(); - - var m = _rgx.Match(strg).Groups[1].Value; - if (tp == "Int") - return int.Parse(m); - return m; - } - } - - private string ExportToString() - { - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); - controllerConstant.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); - - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - } - - public class TIAOpennessControllerFolder : TIAOpennessProjectFolder, IRootProgrammFolder - { - internal TIAOpennessControllerFolder(Step7ProjectV14SP1 Project, PlcSoftware plcSoftware) - : base(Project) - { - this.Project = Project; - this.TiaProject = Project; - this.plcSoftware = plcSoftware; - } - - internal PlcSoftware plcSoftware; - - public TIAOpennessProgramFolder ProgramFolder { get; set; } - public TIAOpennessPlcDatatypeFolder PlcDatatypeFolder { get; set; } - public TIAVarTabFolder VarTabFolder { get; set; } - - public Block GetBlockRecursive(string name) - { - var block = GetBlockRecursive(ProgramFolder, name); - if (block == null) - { - block = GetBlockRecursive(PlcDatatypeFolder, name); - } - - return block; - } - - private Block GetBlockRecursive(TIAOpennessProgramFolder folder, string name) - { - var block = folder.GetBlock(name); - if (block == null) - { - foreach (TIAOpennessProgramFolder projectFolder in folder.SubItems) - { - block = GetBlockRecursive(projectFolder, name); - if (block != null) - return block; - } - } - - return block; - } - - private Block GetBlockRecursive(TIAOpennessPlcDatatypeFolder folder, string name) - { - var block = folder.GetBlock(name); - if (block == null) - { - foreach (TIAOpennessPlcDatatypeFolder projectFolder in folder.SubItems) - { - block = GetBlockRecursive(projectFolder, name); - if (block != null) - return block; - } - } - - return block; - } - } - - public class TIAVarTabFolder : TIAOpennessProjectFolder - { - public TIAOpennessControllerFolder ControllerFolder { get; set; } - - internal object TiaPortalItem { get; set; } - - public TIAVarTabFolder(Step7ProjectV14SP1 Project, TIAOpennessControllerFolder ControllerFolder) : base(Project) - { - this.ControllerFolder = ControllerFolder; - this.Project = Project; - this.TiaProject = Project; - } - - public TIAOpennessConstant FindConstant(string name) - { - foreach (var t in TagTables) - { - var c = t.Constants.FirstOrDefault(x => x.Name == name); - if (c != null) - return c; - } - foreach (var f in SubItems.Flatten(x => x.SubItems)) - { - foreach (var t in TagTables) - { - var c = t.Constants.FirstOrDefault(x => x.Name == name); - if (c != null) - return c; - } - } - return null; - } - - public List TagTables - { - get - { - PlcTagTableComposition tags = null; - var o = this.TiaPortalItem as PlcTagTableUserGroup; - if (o != null) - tags = o.TagTables; - var q = this.TiaPortalItem as PlcTagTableSystemGroup; - if (q != null) - tags = q.TagTables; - - var retVal = new List(); - - foreach (var tagList in tags) - { - var info = new TIAOpennessTagTable() { Name = tagList.Name }; - retVal.Add(info); - info.Constants = new List(); - foreach (var c in tagList.UserConstants) - { - info.Constants.Add(new TIAOpennessConstant(c) { Name = c.Name }); - } - } - return retVal; - } - } - } - - public class TIAOpennessPlcDatatypeFolder : TIAOpennessProjectFolder, IBlocksFolder - { - PlcTypeComposition composition; - - public TIAOpennessControllerFolder ControllerFolder { get; set; } - - public TIAOpennessPlcDatatypeFolder(Step7ProjectV14SP1 Project, TIAOpennessControllerFolder ControllerFolder, PlcTypeComposition composition) - : base(Project) - { - this.ControllerFolder = ControllerFolder; - this.Project = Project; - this.TiaProject = Project; - this.composition = composition; - } - - public List readPlcBlocksList() - { - if (_blockInfos != null) - return _blockInfos; - - _blockInfos = new List(); - - if (composition != null) - { - foreach (var block in composition) - { - var info = new TIAOpennessProjectDataTypeInfo(block) { Name = block.Name, ParentFolder = this }; - info.BlockType = DataTypes.PLCBlockType.UDT; - _blockInfos.Add(info); - } - } - - return BlockInfos; - } - - private List _blockInfos; - public List BlockInfos - { - get - { - if (_blockInfos == null) - readPlcBlocksList(); - return _blockInfos; - } - private set { _blockInfos = value; } - } - - public Block GetBlock(string BlockName) - { - if (BlockInfos == null) - readPlcBlocksList(); - - var block = GetBlock(BlockInfos.FirstOrDefault(x => x.Name == BlockName)); - - if (block != null) - return block; - - foreach (TIAOpennessPlcDatatypeFolder s in this.SubItems) - { - block = s.GetBlock(BlockName); - if (block != null) - return block; - } - - return null; - } - - public Block GetBlock(ProjectBlockInfo blkInfo) - { - if (blkInfo == null) - return null; - - var iv = blkInfo as TIAOpennessProjectDataTypeInfo; - var text = iv.Export(ExportFormat.Xml); - - return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.DataType); - } - } - - public class TIAOpennessProgramFolder : TIAOpennessProjectFolder, IBlocksFolder - { - public TIAOpennessControllerFolder ControllerFolder { get; set; } - - PlcBlockComposition blocks; - - public TIAOpennessProgramFolder(Step7ProjectV14SP1 Project, TIAOpennessControllerFolder ControllerFolder, PlcBlockComposition blocks) - : base(Project) - { - this.ControllerFolder = ControllerFolder; - this.Project = Project; - this.TiaProject = Project; - this.blocks = blocks; - } - - public List readPlcBlocksList() - { - if (_blockInfos != null) - return _blockInfos; - - _blockInfos = new List(); - - foreach (var block in blocks) - { - var info = new TIAOpennessProjectBlockInfo(block) { Name = block.Name, ParentFolder = this }; - info.BlockType = DataTypes.PLCBlockType.FB; - info.SetBlockLanguage = PLCLanguage.unkown; - if (block.ProgrammingLanguage == ProgrammingLanguage.DB || - block.ProgrammingLanguage == ProgrammingLanguage.CPU_DB || - block.ProgrammingLanguage == ProgrammingLanguage.F_DB || - block.ProgrammingLanguage == ProgrammingLanguage.Motion_DB) - { - info.BlockType = DataTypes.PLCBlockType.DB; - info.SetBlockLanguage = PLCLanguage.DB; - } - else if (block.ProgrammingLanguage == ProgrammingLanguage.LAD || - block.ProgrammingLanguage == ProgrammingLanguage.F_LAD || - block.ProgrammingLanguage == ProgrammingLanguage.F_LAD_LIB) - info.SetBlockLanguage = PLCLanguage.KOP; - else if (block.ProgrammingLanguage == ProgrammingLanguage.STL || - block.ProgrammingLanguage == ProgrammingLanguage.F_STL) - info.SetBlockLanguage = PLCLanguage.AWL; - else if (block.ProgrammingLanguage == ProgrammingLanguage.FBD || - block.ProgrammingLanguage == ProgrammingLanguage.F_FBD || - block.ProgrammingLanguage == ProgrammingLanguage.F_FBD_LIB) - info.SetBlockLanguage = PLCLanguage.FUP; - else if (block.ProgrammingLanguage == ProgrammingLanguage.CFC) - info.SetBlockLanguage = PLCLanguage.CFC; - else if (block.ProgrammingLanguage == ProgrammingLanguage.SCL) - info.SetBlockLanguage = PLCLanguage.SCL; - info.BlockNumber = block.Number; - _blockInfos.Add(info); - } - return _blockInfos; - } - - private List _blockInfos; - public List BlockInfos - { - get - { - if (_blockInfos == null) - readPlcBlocksList(); - return _blockInfos; - } - private set { _blockInfos = value; } - } - - public Block GetBlock(string BlockName) - { - if (BlockInfos == null) - readPlcBlocksList(); - - return - GetBlock( - BlockInfos.Cast() - .FirstOrDefault(x => x.Name == BlockName || x.BlockName == BlockName)); - } - - public Block GetBlock(ProjectBlockInfo blkInfo) - { - if (blkInfo == null) - return null; - - var iv = blkInfo as TIAOpennessProjectBlockInfo; - var text = iv.Export(ExportFormat.Xml); - - return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.Programm); - } - } - internal void LoadViaOpennessDlls() - { - for (int i = 0; i < 10; i++) - { - try - { - if (tiaPortal != null) - { - tiaPortal.Dispose(); - tiaPortal = null; - } - tiaPortal = new Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithoutUserInterface); - tiapProject = tiaPortal.Projects.Open(new FileInfo(ProjectFile)); - } - catch (Siemens.Engineering.EngineeringSecurityException ex) - { - throw; - } - catch (Exception ex) - { - if (i == 9) - throw; - } - if (tiapProject != null) - break; - } - - - var main = new TIAOpennessProjectFolder(this) { Name = "Main" }; - ProjectStructure = main; - - //var frm = new sliver.Windows.Forms.StateBrowserForm(); - //frm.ObjectToBrowse = tiapProject; - //frm.Show(); - - foreach (var d in tiapProject.Devices) - { - if (d.TypeIdentifier != null && d.TypeIdentifier.EndsWith(".S71500")) - { - foreach (DeviceItem deviceItem in d.DeviceItems) - { - var target = ((IEngineeringServiceProvider)deviceItem).GetService(); - if (target != null && target.Software is PlcSoftware) - { - var software =(PlcSoftware)target.Software; - var fld = new TIAOpennessControllerFolder(this, software) - { - Name = software.Name, - //TiaPortalItem = software, - //Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null - }; - main.SubItems.Add(fld); - - LoadControlerFolderViaOpennessDlls(fld, software); - - } - } - - //var controller = d.DeviceItems.OfType().FirstOrDefault(); - //if (controller == null) - //{ - // var fld = new TIAOpennessProjectFolder(this) - // { - // Name = d.Name, - // TiaPortalItem = d, - // Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null - // }; - // main.SubItems.Add(fld); - - // //LoadSubDevicesViaOpennessDlls(fld, d); - //} - //else - //{ - // var fld = new TIAOpennessControllerFolder(this) - // { - // Name = d.Name, - // TiaPortalItem = d, - // Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null - // }; - // main.SubItems.Add(fld); - - // //LoadControlerFolderViaOpennessDlls(fld, controller); - //} - } - } - } - - //internal void LoadSubDevicesViaOpennessDlls(TIAOpennessProjectFolder parent, Siemens.Engineering.HW.IHardwareObject device) - //{ - // foreach (var e in device.DeviceItems) - // { - // var fld = new TIAOpennessProjectFolder(this) - // { - // TiaPortalItem = e, - // Name = e.Name, - // Parent = parent, - // }; - // var d = e as Siemens.Engineering.HW.DeviceItem; - // //d.Elements.ToList() - - // parent.SubItems.Add(fld); - // LoadSubDevicesViaOpennessDlls(fld, e); - // } - //} - - internal void LoadControlerFolderViaOpennessDlls(TIAOpennessControllerFolder parent, PlcSoftware software) - { - var fld = new TIAOpennessProgramFolder(this, parent, software.BlockGroup.Blocks) - { - //TiaPortalItem = controller.ProgramblockFolder, - Name = "software", - Parent = parent, - }; - parent.ProgramFolder = fld; - parent.SubItems.Add(fld); - LoadSubProgramBlocksFoldersViaOpennessDlls(fld, software.BlockGroup); - - var t = (PlcTypeGroup)software.TypeGroup; - - var fld2 = new TIAOpennessPlcDatatypeFolder(this, parent, t.Types) - { - //TiaPortalItem = controller.ControllerDatatypeFolder, - Name = "data types", - Parent = parent, - }; - parent.PlcDatatypeFolder = fld2; - parent.SubItems.Add(fld2); - LoadSubPlcDatatypeFoldersViaOpennessDlls(fld2, software.TypeGroup); - - var fld3 = new TIAVarTabFolder(this, parent) - { - //TiaPortalItem = controller.ControllerTagFolder, - Name = "PLC data types", - Parent = parent, - }; - parent.VarTabFolder = fld3; - parent.SubItems.Add(fld3); - LoadSubVartabFoldersViaOpennessDlls(fld3, software.TagTableGroup); - } - - internal void LoadSubProgramBlocksFoldersViaOpennessDlls(TIAOpennessProgramFolder parent, PlcBlockGroup plcBlockGroup) - { - foreach (var e in plcBlockGroup.Groups) - { - var fld = new TIAOpennessProgramFolder(this, parent.ControllerFolder, e.Blocks) - { - //TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubProgramBlocksFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, PlcTypeSystemGroup p) - { - foreach (var e in p.Groups) - { - var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder, e.Types) - { - //TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, PlcTypeUserGroup p) - { - foreach (var e in p.Groups) - { - var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder, e.Types) - { - //TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, PlcTagTableSystemGroup blockFolder) - { - foreach (var e in blockFolder.Groups) - { - var fld = new TIAVarTabFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubVartabFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, PlcTagTableUserGroup blockFolder) - { - foreach (var e in blockFolder.Groups) - { - var fld = new TIAVarTabFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubVartabFoldersViaOpennessDlls(fld, e); - } - } - #region Parse DB UDT XML - - internal enum ParseType - { - Programm, - DataType - } - - internal static Block ParseTiaDbUdtXml(string xml, ProjectBlockInfo projectBlockInfo, TIAOpennessControllerFolder controllerFolder, ParseType parseType) - { - XElement xelement = XElement.Parse(xml); - var structure = xelement.Elements().FirstOrDefault(x => x.Name.LocalName.StartsWith("SW.")); - - var sections = structure.Element("AttributeList").Element("Interface").Elements().First(); - - var block = new TIADataBlock(); - block.Name = projectBlockInfo.Name; - - if (projectBlockInfo is TIAOpennessProjectBlockInfo) - block.BlockNumber = ((TIAOpennessProjectBlockInfo) projectBlockInfo).BlockNumber; - - if (parseType == ParseType.DataType) - block.BlockType = DataTypes.PLCBlockType.UDT; - else if (parseType == ParseType.Programm) - block.BlockType = DataTypes.PLCBlockType.DB; - - var parameterRoot = ParseTiaDbUdtSections(sections, block, controllerFolder); - - block.BlockType = DataTypes.PLCBlockType.DB; - block.Structure = parameterRoot; - - return block; - } - - internal static TIADataRow ParseTiaDbUdtSections(XElement sections, TIADataBlock block, TIAOpennessControllerFolder controllerFolder) - { - var parameterRoot = new TIADataRow("ROOTNODE", S7DataRowType.STRUCT, block); - var parameterIN = new TIADataRow("IN", S7DataRowType.STRUCT, block); - parameterIN.Parent = parameterRoot; - var parameterOUT = new TIADataRow("OUT", S7DataRowType.STRUCT, block); - parameterOUT.Parent = parameterRoot; - var parameterINOUT = new TIADataRow("IN_OUT", S7DataRowType.STRUCT, block); - parameterINOUT.Parent = parameterRoot; - var parameterSTAT = new TIADataRow("STATIC", S7DataRowType.STRUCT, block); - parameterSTAT.Parent = parameterRoot; - var parameterTEMP = new TIADataRow("TEMP", S7DataRowType.STRUCT, block); - parameterTEMP.Parent = parameterRoot; - - foreach (var xElement in sections.Elements()) - { - TIADataRow useRow = parameterRoot; - //var sectionName = xElement.Attribute("Name").Value; - //if (sectionName == "None" || sectionName == "Static") - //{ - // useRow = parameterSTAT; - // parameterRoot.Add(useRow); - //} - //else if (sectionName == "In") - //{ - // useRow = parameterIN; - // parameterRoot.Add(useRow); - //} - - parseChildren(useRow, xElement, controllerFolder); - } - - return parameterRoot; - } - - internal static void parseChildren(TIADataRow parentRow, XElement parentElement, TIAOpennessControllerFolder controllerFolder) - { - foreach (var xElement in parentElement.Elements()) - { - if (xElement.Name.LocalName == "Comment") - { - var text = xElement.Elements().FirstOrDefault(x => x.Attribute("Lang").Value == "de-DE"); - if (text == null) - text = xElement.Elements().FirstOrDefault(); - if (text != null) - parentRow.Comment = text.Value; - } - else if (xElement.Name.LocalName == "StartValue") - { - parentRow.StartValue = xElement.Value; - } - else if (xElement.Name.LocalName == "Sections") - { - var row = ParseTiaDbUdtSections(xElement, (TIADataBlock)parentRow.CurrentBlock, controllerFolder); - parentRow.AddRange(row.Children); - } - else if (xElement.Name.LocalName == "Member") - { - var name = xElement.Attribute("Name").Value; - var datatype = xElement.Attribute("Datatype").Value; - - var row = new TIADataRow(name, S7DataRowType.STRUCT, (TIABlock)parentRow.PlcBlock); - row.Parent = parentRow; - - if (datatype.Contains("Array[")) - { - List arrayStart = new List(); - List arrayStop = new List(); - - int pos1 = datatype.IndexOf("["); - int pos2 = datatype.IndexOf("]", pos1); - string[] arrays = datatype.Substring(pos1 + 1, pos2 - pos1 - 1).Split(','); - - foreach (string array in arrays) - { - string[] akar = array.Split(new string[] { ".." }, StringSplitOptions.RemoveEmptyEntries); - int start = 0; - if (akar[0].StartsWith("\"")) - { - start = (int)controllerFolder.VarTabFolder.FindConstant(akar[0].Substring(1, akar[0].Length - 2)).Value; - } - else - { - start = Convert.ToInt32(akar[0].Trim()); - } - - int stop = 0; - if (akar[1].StartsWith("\"")) - { - stop = (int)controllerFolder.VarTabFolder.FindConstant(akar[1].Substring(1, akar[1].Length - 2)).Value; - } - else - { - stop = Convert.ToInt32(akar[1].Trim()); - } - - arrayStart.Add(start); - arrayStop.Add(stop); - } - - row.ArrayStart = arrayStart; - row.ArrayStop = arrayStop; - row.IsArray = true; - datatype = datatype.Substring(pos2 + 5); - } - - parentRow.Add(row); - - parseChildren(row, xElement, controllerFolder); - - if (datatype.StartsWith("\"")) - { - var udt = - controllerFolder.PlcDatatypeFolder.GetBlock(datatype.Substring(1, datatype.Length - 2)); - if (udt != null) - { - var tiaUdt = udt as TIADataBlock; - row.AddRange(((TIADataRow)tiaUdt.Structure).DeepCopy().Children); - - row.DataTypeBlock = udt; - } - row.DataType = S7DataRowType.UDT; - } - else if (datatype == "Struct") - { - - } - else if (datatype.StartsWith("String[")) - { - row.DataType = S7DataRowType.STRING; - row.StringSize = int.Parse(datatype.Substring(7, datatype.Length - 8)); - } - else - { - switch (datatype.ToLower()) - { - case "byte": - row.DataType = S7DataRowType.BYTE; - break; - case "bool": - row.DataType = S7DataRowType.BOOL; - break; - case "int": - row.DataType = S7DataRowType.INT; - break; - case "uint": - row.DataType = S7DataRowType.WORD; - break; - case "dint": - row.DataType = S7DataRowType.DINT; - break; - case "udint": - row.DataType = S7DataRowType.DWORD; - break; - case "word": - row.DataType = S7DataRowType.WORD; - break; - case "dword": - row.DataType = S7DataRowType.DWORD; - break; - case "char": - row.DataType = S7DataRowType.CHAR; - break; - case "any": - row.DataType = S7DataRowType.ANY; - break; - case "date": - row.DataType = S7DataRowType.DATE; - break; - case "date_and_time": - row.DataType = S7DataRowType.DATE_AND_TIME; - break; - case "real": - row.DataType = S7DataRowType.REAL; - break; - case "s5time": - row.DataType = S7DataRowType.S5TIME; - break; - case "time_of_day": - row.DataType = S7DataRowType.TIME_OF_DAY; - break; - case "time": - row.DataType = S7DataRowType.TIME; - break; - default: - row.DataType = S7DataRowType.UNKNOWN; - - var udt = controllerFolder.PlcDatatypeFolder.GetBlock(datatype); - if (udt != null) - { - var tiaUdt = udt as TIADataBlock; - row.AddRange(((TIADataRow)tiaUdt.Structure).DeepCopy().Children); - - row.DataTypeBlock = udt; - row.DataType = S7DataRowType.UDT; - } - - //Console.WriteLine("unkown Datatype: " + datatype); - break; - } - } - } - else if (xElement.Name.LocalName == "AttributeList") - { } - else if (xElement.Name.LocalName == "Subelement") //todo -> startwerte von arrays von UDTs - { } - else - { - //Console.WriteLine("unkown XML Element"); - } - } - } - #endregion - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using DotNetSiemensPLCToolBoxLibrary.DataTypes; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V11; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V5; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders; +using Siemens.Engineering; +using Siemens.Engineering.HW.Features; +using Siemens.Engineering.SW; +using Siemens.Engineering.HW; +using Siemens.Engineering.SW.Blocks; +using Siemens.Engineering.SW.Types; +using Siemens.Engineering.SW.Tags; +using DotNetSiemensPLCToolBoxLibrary.General; +using System.Text.RegularExpressions; + +namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V14SP1 +{ + public interface ITiaProjectBlockInfo : IProjectBlockInfo + { + } + + public partial class Step7ProjectV14SP1 + { + + private Siemens.Engineering.TiaPortal tiaPortal; + + private Siemens.Engineering.Project tiapProject; + + public virtual void Dispose() + { + tiaPortal.Dispose(); + } + + + public class TIAOpennessProjectFolder : ProjectFolder + { + internal string InstID { get; private set; } + + protected Step7ProjectV14SP1 TiaProject; + + public override string Name { get; set; } + + public TIAOpennessProjectFolder(Step7ProjectV14SP1 Project) + { + this.Project = Project; + this.TiaProject = Project; + } + } + + public class TIAOpennessProjectBlockInfo : ProjectBlockInfo, ITiaProjectBlockInfo + { + internal TIAOpennessProjectBlockInfo(PlcBlock plcBlock) + { + this.plcBlock = plcBlock; + } + PlcBlock plcBlock; + + internal PLCLanguage SetBlockLanguage; + public override PLCLanguage BlockLanguage { get { return SetBlockLanguage; } } + + public int BlockNumber { get; set; } + + public string BlockName + { + get + { + string retVal = BlockType.ToString().Replace("S5_", "") + BlockNumber.ToString(); + return retVal; + } + } + + public string ProgrammingLanguage + { + get { return plcBlock.ProgrammingLanguage.ToString(); } + } + + public override string ToString() + { + string retVal = ""; + if (Deleted) + retVal += "$$_"; + if (Name != null) + retVal += BlockName + " (" + Name + ")"; + else + retVal += BlockName; + return retVal; + } + + public override string Export(ExportFormat exportFormat) + { + var ext = "xml"; + if (exportFormat != ExportFormat.Xml) + { + if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.SCL) + { + ext = "scl"; + } + else if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.STL || this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.F_STL) + { + ext = "awl"; + } + else if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.DB || this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.F_DB) + { + ext = "db"; + } + } + + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); + if (ext == "xml") + { + plcBlock.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); + } + else + { + var fld = this.ParentFolder; + while (!(fld is TIAOpennessControllerFolder)) + { + fld = fld.Parent; + } + ((TIAOpennessControllerFolder)fld).plcSoftware.ExternalSourceGroup.GenerateSource(new[] { this.plcBlock }, new FileInfo(file), Siemens.Engineering.SW.ExternalSources.GenerateOptions.None); + } + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + private PLCBlockType? _plcBlockType; + + public override PLCBlockType BlockType + { + get + { + if (_plcBlockType == null) + { + _plcBlockType = PLCBlockType.FC; + if (plcBlock is FB) + { + _plcBlockType = PLCBlockType.FB; + } + else if (plcBlock is OB) + { + _plcBlockType = PLCBlockType.OB; + } + } + return _plcBlockType.Value; + } + set { _plcBlockType = value; } + } + + private string xml; + } + + public class TIAOpennessProjectDataTypeInfo : ProjectBlockInfo, ITiaProjectBlockInfo + { + internal TIAOpennessProjectDataTypeInfo(PlcType plcType) + { + this.plcType = plcType; + } + private PlcType plcType; + + public override string ToString() + { + return Name; + } + + public override PLCBlockType BlockType + { + get + { + return PLCBlockType.UDT; + } + set { } + } + + public override PLCLanguage BlockLanguage + { + get + { + return PLCLanguage.DB; + } + } + + public override string Export(ExportFormat exportFormat) + { + var ext = "udt"; + if (exportFormat == ExportFormat.Xml) + { + ext = "xml"; + } + + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); + if (ext == "xml") + { + plcType.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); + } + else + { + var fld = this.ParentFolder; + while (!(fld is TIAOpennessControllerFolder)) + { + fld = fld.Parent; + } + ((TIAOpennessControllerFolder)fld).plcSoftware.ExternalSourceGroup.GenerateSource(new[] { this.plcType }, new FileInfo(file), Siemens.Engineering.SW.ExternalSources.GenerateOptions.None); + } + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + } + + public class TIAOpennessTagTable + { + public string Name { get; set; } + + public List Constants { get; set; } + + } + + public class TIAOpennessConstant + { + private readonly PlcUserConstant controllerConstant; + + internal TIAOpennessConstant(PlcUserConstant controllerConstant) + { + this.controllerConstant = controllerConstant; + } + + public string Name { get; set; } + + private Regex _rgx = new Regex("(.*)", RegexOptions.Compiled); + + public object Value + { + get + { + var tp = this.controllerConstant.DataTypeName; + var strg = ExportToString(); + + var m = _rgx.Match(strg).Groups[1].Value; + if (tp == "Int") + return int.Parse(m); + return m; + } + } + + private string ExportToString() + { + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); + controllerConstant.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); + + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + } + + public class TIAOpennessControllerFolder : TIAOpennessProjectFolder, IRootProgrammFolder + { + internal TIAOpennessControllerFolder(Step7ProjectV14SP1 Project, PlcSoftware plcSoftware) + : base(Project) + { + this.Project = Project; + this.TiaProject = Project; + this.plcSoftware = plcSoftware; + } + + internal PlcSoftware plcSoftware; + + public TIAOpennessProgramFolder ProgramFolder { get; set; } + public TIAOpennessPlcDatatypeFolder PlcDatatypeFolder { get; set; } + public TIAVarTabFolder VarTabFolder { get; set; } + + public Block GetBlockRecursive(string name) + { + var block = GetBlockRecursive(ProgramFolder, name); + if (block == null) + { + block = GetBlockRecursive(PlcDatatypeFolder, name); + } + + return block; + } + + private Block GetBlockRecursive(TIAOpennessProgramFolder folder, string name) + { + var block = folder.GetBlock(name); + if (block == null) + { + foreach (TIAOpennessProgramFolder projectFolder in folder.SubItems) + { + block = GetBlockRecursive(projectFolder, name); + if (block != null) + return block; + } + } + + return block; + } + + private Block GetBlockRecursive(TIAOpennessPlcDatatypeFolder folder, string name) + { + var block = folder.GetBlock(name); + if (block == null) + { + foreach (TIAOpennessPlcDatatypeFolder projectFolder in folder.SubItems) + { + block = GetBlockRecursive(projectFolder, name); + if (block != null) + return block; + } + } + + return block; + } + } + + public class TIAVarTabFolder : TIAOpennessProjectFolder + { + public TIAOpennessControllerFolder ControllerFolder { get; set; } + + internal object TiaPortalItem { get; set; } + + public TIAVarTabFolder(Step7ProjectV14SP1 Project, TIAOpennessControllerFolder ControllerFolder) : base(Project) + { + this.ControllerFolder = ControllerFolder; + this.Project = Project; + this.TiaProject = Project; + } + + public TIAOpennessConstant FindConstant(string name) + { + foreach (var t in TagTables) + { + var c = t.Constants.FirstOrDefault(x => x.Name == name); + if (c != null) + return c; + } + foreach (var f in SubItems.Flatten(x => x.SubItems)) + { + foreach (var t in TagTables) + { + var c = t.Constants.FirstOrDefault(x => x.Name == name); + if (c != null) + return c; + } + } + return null; + } + + public List TagTables + { + get + { + PlcTagTableComposition tags = null; + var o = this.TiaPortalItem as PlcTagTableUserGroup; + if (o != null) + tags = o.TagTables; + var q = this.TiaPortalItem as PlcTagTableSystemGroup; + if (q != null) + tags = q.TagTables; + + var retVal = new List(); + + foreach (var tagList in tags) + { + var info = new TIAOpennessTagTable() { Name = tagList.Name }; + retVal.Add(info); + info.Constants = new List(); + foreach (var c in tagList.UserConstants) + { + info.Constants.Add(new TIAOpennessConstant(c) { Name = c.Name }); + } + } + return retVal; + } + } + } + + public class TIAOpennessPlcDatatypeFolder : TIAOpennessProjectFolder, IBlocksFolder + { + PlcTypeComposition composition; + + public TIAOpennessControllerFolder ControllerFolder { get; set; } + + public TIAOpennessPlcDatatypeFolder(Step7ProjectV14SP1 Project, TIAOpennessControllerFolder ControllerFolder, PlcTypeComposition composition) + : base(Project) + { + this.ControllerFolder = ControllerFolder; + this.Project = Project; + this.TiaProject = Project; + this.composition = composition; + } + + public List readPlcBlocksList() + { + if (_blockInfos != null) + return _blockInfos; + + _blockInfos = new List(); + + if (composition != null) + { + foreach (var block in composition) + { + var info = new TIAOpennessProjectDataTypeInfo(block) { Name = block.Name, ParentFolder = this }; + info.BlockType = DataTypes.PLCBlockType.UDT; + _blockInfos.Add(info); + } + } + + return BlockInfos; + } + + private List _blockInfos; + public List BlockInfos + { + get + { + if (_blockInfos == null) + readPlcBlocksList(); + return _blockInfos; + } + private set { _blockInfos = value; } + } + + public Block GetBlock(string BlockName) + { + if (BlockInfos == null) + readPlcBlocksList(); + + var block = GetBlock(BlockInfos.FirstOrDefault(x => x.Name == BlockName)); + + if (block != null) + return block; + + foreach (TIAOpennessPlcDatatypeFolder s in this.SubItems) + { + block = s.GetBlock(BlockName); + if (block != null) + return block; + } + + return null; + } + + public Block GetBlock(ProjectBlockInfo blkInfo) + { + if (blkInfo == null) + return null; + + var iv = blkInfo as TIAOpennessProjectDataTypeInfo; + var text = iv.Export(ExportFormat.Xml); + + return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.DataType); + } + } + + public class TIAOpennessProgramFolder : TIAOpennessProjectFolder, IBlocksFolder + { + public TIAOpennessControllerFolder ControllerFolder { get; set; } + + PlcBlockComposition blocks; + + public TIAOpennessProgramFolder(Step7ProjectV14SP1 Project, TIAOpennessControllerFolder ControllerFolder, PlcBlockComposition blocks) + : base(Project) + { + this.ControllerFolder = ControllerFolder; + this.Project = Project; + this.TiaProject = Project; + this.blocks = blocks; + } + + public List readPlcBlocksList() + { + if (_blockInfos != null) + return _blockInfos; + + _blockInfos = new List(); + + foreach (var block in blocks) + { + var info = new TIAOpennessProjectBlockInfo(block) { Name = block.Name, ParentFolder = this }; + info.BlockType = DataTypes.PLCBlockType.FB; + info.SetBlockLanguage = PLCLanguage.unkown; + if (block.ProgrammingLanguage == ProgrammingLanguage.DB || + block.ProgrammingLanguage == ProgrammingLanguage.CPU_DB || + block.ProgrammingLanguage == ProgrammingLanguage.F_DB || + block.ProgrammingLanguage == ProgrammingLanguage.Motion_DB) + { + info.BlockType = DataTypes.PLCBlockType.DB; + info.SetBlockLanguage = PLCLanguage.DB; + } + else if (block.ProgrammingLanguage == ProgrammingLanguage.LAD || + block.ProgrammingLanguage == ProgrammingLanguage.F_LAD || + block.ProgrammingLanguage == ProgrammingLanguage.F_LAD_LIB) + info.SetBlockLanguage = PLCLanguage.KOP; + else if (block.ProgrammingLanguage == ProgrammingLanguage.STL || + block.ProgrammingLanguage == ProgrammingLanguage.F_STL) + info.SetBlockLanguage = PLCLanguage.AWL; + else if (block.ProgrammingLanguage == ProgrammingLanguage.FBD || + block.ProgrammingLanguage == ProgrammingLanguage.F_FBD || + block.ProgrammingLanguage == ProgrammingLanguage.F_FBD_LIB) + info.SetBlockLanguage = PLCLanguage.FUP; + else if (block.ProgrammingLanguage == ProgrammingLanguage.CFC) + info.SetBlockLanguage = PLCLanguage.CFC; + else if (block.ProgrammingLanguage == ProgrammingLanguage.SCL) + info.SetBlockLanguage = PLCLanguage.SCL; + info.BlockNumber = block.Number; + _blockInfos.Add(info); + } + return _blockInfos; + } + + private List _blockInfos; + public List BlockInfos + { + get + { + if (_blockInfos == null) + readPlcBlocksList(); + return _blockInfos; + } + private set { _blockInfos = value; } + } + + public Block GetBlock(string BlockName) + { + if (BlockInfos == null) + readPlcBlocksList(); + + return + GetBlock( + BlockInfos.Cast() + .FirstOrDefault(x => x.Name == BlockName || x.BlockName == BlockName)); + } + + public Block GetBlock(ProjectBlockInfo blkInfo) + { + if (blkInfo == null) + return null; + + var iv = blkInfo as TIAOpennessProjectBlockInfo; + var text = iv.Export(ExportFormat.Xml); + + return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.Programm); + } + } + internal void LoadViaOpennessDlls() + { + for (int i = 0; i < 10; i++) + { + try + { + if (tiaPortal != null) + { + tiaPortal.Dispose(); + tiaPortal = null; + } + tiaPortal = new Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithoutUserInterface); + tiapProject = tiaPortal.Projects.Open(new FileInfo(ProjectFile)); + } + catch (Siemens.Engineering.EngineeringSecurityException ex) + { + throw; + } + catch (Exception ex) + { + if (i == 9) + throw; + } + if (tiapProject != null) + break; + } + + + var main = new TIAOpennessProjectFolder(this) { Name = "Main" }; + ProjectStructure = main; + + //var frm = new sliver.Windows.Forms.StateBrowserForm(); + //frm.ObjectToBrowse = tiapProject; + //frm.Show(); + + foreach (var d in tiapProject.Devices) + { + if (d.TypeIdentifier != null && d.TypeIdentifier.EndsWith(".S71500")) + { + foreach (DeviceItem deviceItem in d.DeviceItems) + { + var target = ((IEngineeringServiceProvider)deviceItem).GetService(); + if (target != null && target.Software is PlcSoftware) + { + var software =(PlcSoftware)target.Software; + var fld = new TIAOpennessControllerFolder(this, software) + { + Name = software.Name, + //TiaPortalItem = software, + //Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null + }; + main.SubItems.Add(fld); + + LoadControlerFolderViaOpennessDlls(fld, software); + + } + } + + //var controller = d.DeviceItems.OfType().FirstOrDefault(); + //if (controller == null) + //{ + // var fld = new TIAOpennessProjectFolder(this) + // { + // Name = d.Name, + // TiaPortalItem = d, + // Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null + // }; + // main.SubItems.Add(fld); + + // //LoadSubDevicesViaOpennessDlls(fld, d); + //} + //else + //{ + // var fld = new TIAOpennessControllerFolder(this) + // { + // Name = d.Name, + // TiaPortalItem = d, + // Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null + // }; + // main.SubItems.Add(fld); + + // //LoadControlerFolderViaOpennessDlls(fld, controller); + //} + } + } + } + + //internal void LoadSubDevicesViaOpennessDlls(TIAOpennessProjectFolder parent, Siemens.Engineering.HW.IHardwareObject device) + //{ + // foreach (var e in device.DeviceItems) + // { + // var fld = new TIAOpennessProjectFolder(this) + // { + // TiaPortalItem = e, + // Name = e.Name, + // Parent = parent, + // }; + // var d = e as Siemens.Engineering.HW.DeviceItem; + // //d.Elements.ToList() + + // parent.SubItems.Add(fld); + // LoadSubDevicesViaOpennessDlls(fld, e); + // } + //} + + internal void LoadControlerFolderViaOpennessDlls(TIAOpennessControllerFolder parent, PlcSoftware software) + { + var fld = new TIAOpennessProgramFolder(this, parent, software.BlockGroup.Blocks) + { + //TiaPortalItem = controller.ProgramblockFolder, + Name = "software", + Parent = parent, + }; + parent.ProgramFolder = fld; + parent.SubItems.Add(fld); + LoadSubProgramBlocksFoldersViaOpennessDlls(fld, software.BlockGroup); + + var t = (PlcTypeGroup)software.TypeGroup; + + var fld2 = new TIAOpennessPlcDatatypeFolder(this, parent, t.Types) + { + //TiaPortalItem = controller.ControllerDatatypeFolder, + Name = "data types", + Parent = parent, + }; + parent.PlcDatatypeFolder = fld2; + parent.SubItems.Add(fld2); + LoadSubPlcDatatypeFoldersViaOpennessDlls(fld2, software.TypeGroup); + + var fld3 = new TIAVarTabFolder(this, parent) + { + //TiaPortalItem = controller.ControllerTagFolder, + Name = "PLC data types", + Parent = parent, + }; + parent.VarTabFolder = fld3; + parent.SubItems.Add(fld3); + LoadSubVartabFoldersViaOpennessDlls(fld3, software.TagTableGroup); + } + + internal void LoadSubProgramBlocksFoldersViaOpennessDlls(TIAOpennessProgramFolder parent, PlcBlockGroup plcBlockGroup) + { + foreach (var e in plcBlockGroup.Groups) + { + var fld = new TIAOpennessProgramFolder(this, parent.ControllerFolder, e.Blocks) + { + //TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubProgramBlocksFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, PlcTypeSystemGroup p) + { + foreach (var e in p.Groups) + { + var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder, e.Types) + { + //TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, PlcTypeUserGroup p) + { + foreach (var e in p.Groups) + { + var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder, e.Types) + { + //TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, PlcTagTableSystemGroup blockFolder) + { + foreach (var e in blockFolder.Groups) + { + var fld = new TIAVarTabFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubVartabFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, PlcTagTableUserGroup blockFolder) + { + foreach (var e in blockFolder.Groups) + { + var fld = new TIAVarTabFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubVartabFoldersViaOpennessDlls(fld, e); + } + } + #region Parse DB UDT XML + + internal enum ParseType + { + Programm, + DataType + } + + internal static Block ParseTiaDbUdtXml(string xml, ProjectBlockInfo projectBlockInfo, TIAOpennessControllerFolder controllerFolder, ParseType parseType) + { + XElement xelement = XElement.Parse(xml); + var structure = xelement.Elements().FirstOrDefault(x => x.Name.LocalName.StartsWith("SW.")); + + var sections = structure.Element("AttributeList").Element("Interface").Elements().First(); + + var block = new TIADataBlock(); + block.Name = projectBlockInfo.Name; + + if (projectBlockInfo is TIAOpennessProjectBlockInfo) + block.BlockNumber = ((TIAOpennessProjectBlockInfo) projectBlockInfo).BlockNumber; + + if (parseType == ParseType.DataType) + block.BlockType = DataTypes.PLCBlockType.UDT; + else if (parseType == ParseType.Programm) + block.BlockType = DataTypes.PLCBlockType.DB; + + var parameterRoot = ParseTiaDbUdtSections(sections, block, controllerFolder); + + block.BlockType = DataTypes.PLCBlockType.DB; + block.Structure = parameterRoot; + + return block; + } + + internal static TIADataRow ParseTiaDbUdtSections(XElement sections, TIADataBlock block, TIAOpennessControllerFolder controllerFolder) + { + var parameterRoot = new TIADataRow("ROOTNODE", S7DataRowType.STRUCT, block); + var parameterIN = new TIADataRow("IN", S7DataRowType.STRUCT, block); + parameterIN.Parent = parameterRoot; + var parameterOUT = new TIADataRow("OUT", S7DataRowType.STRUCT, block); + parameterOUT.Parent = parameterRoot; + var parameterINOUT = new TIADataRow("IN_OUT", S7DataRowType.STRUCT, block); + parameterINOUT.Parent = parameterRoot; + var parameterSTAT = new TIADataRow("STATIC", S7DataRowType.STRUCT, block); + parameterSTAT.Parent = parameterRoot; + var parameterTEMP = new TIADataRow("TEMP", S7DataRowType.STRUCT, block); + parameterTEMP.Parent = parameterRoot; + + foreach (var xElement in sections.Elements()) + { + TIADataRow useRow = parameterRoot; + //var sectionName = xElement.Attribute("Name").Value; + //if (sectionName == "None" || sectionName == "Static") + //{ + // useRow = parameterSTAT; + // parameterRoot.Add(useRow); + //} + //else if (sectionName == "In") + //{ + // useRow = parameterIN; + // parameterRoot.Add(useRow); + //} + + parseChildren(useRow, xElement, controllerFolder); + } + + return parameterRoot; + } + + internal static void parseChildren(TIADataRow parentRow, XElement parentElement, TIAOpennessControllerFolder controllerFolder) + { + foreach (var xElement in parentElement.Elements()) + { + if (xElement.Name.LocalName == "Comment") + { + var text = xElement.Elements().FirstOrDefault(x => x.Attribute("Lang").Value == "de-DE"); + if (text == null) + text = xElement.Elements().FirstOrDefault(); + if (text != null) + parentRow.Comment = text.Value; + } + else if (xElement.Name.LocalName == "StartValue") + { + parentRow.StartValue = xElement.Value; + } + else if (xElement.Name.LocalName == "Sections") + { + var row = ParseTiaDbUdtSections(xElement, (TIADataBlock)parentRow.CurrentBlock, controllerFolder); + parentRow.AddRange(row.Children); + } + else if (xElement.Name.LocalName == "Member") + { + var name = xElement.Attribute("Name").Value; + var datatype = xElement.Attribute("Datatype").Value; + + var row = new TIADataRow(name, S7DataRowType.STRUCT, (TIABlock)parentRow.PlcBlock); + row.Parent = parentRow; + + if (datatype.Contains("Array[")) + { + List arrayStart = new List(); + List arrayStop = new List(); + + int pos1 = datatype.IndexOf("["); + int pos2 = datatype.IndexOf("]", pos1); + string[] arrays = datatype.Substring(pos1 + 1, pos2 - pos1 - 1).Split(','); + + foreach (string array in arrays) + { + string[] akar = array.Split(new string[] { ".." }, StringSplitOptions.RemoveEmptyEntries); + int start = 0; + if (akar[0].StartsWith("\"")) + { + start = (int)controllerFolder.VarTabFolder.FindConstant(akar[0].Substring(1, akar[0].Length - 2)).Value; + } + else + { + start = Convert.ToInt32(akar[0].Trim()); + } + + int stop = 0; + if (akar[1].StartsWith("\"")) + { + stop = (int)controllerFolder.VarTabFolder.FindConstant(akar[1].Substring(1, akar[1].Length - 2)).Value; + } + else + { + stop = Convert.ToInt32(akar[1].Trim()); + } + + arrayStart.Add(start); + arrayStop.Add(stop); + } + + row.ArrayStart = arrayStart; + row.ArrayStop = arrayStop; + row.IsArray = true; + datatype = datatype.Substring(pos2 + 5); + } + + parentRow.Add(row); + + parseChildren(row, xElement, controllerFolder); + + if (datatype.StartsWith("\"")) + { + var udt = + controllerFolder.PlcDatatypeFolder.GetBlock(datatype.Substring(1, datatype.Length - 2)); + if (udt != null) + { + var tiaUdt = udt as TIADataBlock; + row.AddRange(((TIADataRow)tiaUdt.Structure).DeepCopy().Children); + + row.DataTypeBlock = udt; + } + row.DataType = S7DataRowType.UDT; + } + else if (datatype == "Struct") + { + + } + else if (datatype.StartsWith("String[")) + { + row.DataType = S7DataRowType.STRING; + row.StringSize = int.Parse(datatype.Substring(7, datatype.Length - 8)); + } + else + { + switch (datatype.ToLower()) + { + case "byte": + row.DataType = S7DataRowType.BYTE; + break; + case "bool": + row.DataType = S7DataRowType.BOOL; + break; + case "int": + row.DataType = S7DataRowType.INT; + break; + case "uint": + row.DataType = S7DataRowType.WORD; + break; + case "dint": + row.DataType = S7DataRowType.DINT; + break; + case "udint": + row.DataType = S7DataRowType.DWORD; + break; + case "word": + row.DataType = S7DataRowType.WORD; + break; + case "dword": + row.DataType = S7DataRowType.DWORD; + break; + case "char": + row.DataType = S7DataRowType.CHAR; + break; + case "any": + row.DataType = S7DataRowType.ANY; + break; + case "date": + row.DataType = S7DataRowType.DATE; + break; + case "date_and_time": + row.DataType = S7DataRowType.DATE_AND_TIME; + break; + case "real": + row.DataType = S7DataRowType.REAL; + break; + case "s5time": + row.DataType = S7DataRowType.S5TIME; + break; + case "time_of_day": + row.DataType = S7DataRowType.TIME_OF_DAY; + break; + case "time": + row.DataType = S7DataRowType.TIME; + break; + default: + row.DataType = S7DataRowType.UNKNOWN; + + var udt = controllerFolder.PlcDatatypeFolder.GetBlock(datatype); + if (udt != null) + { + var tiaUdt = udt as TIADataBlock; + row.AddRange(((TIADataRow)tiaUdt.Structure).DeepCopy().Children); + + row.DataTypeBlock = udt; + row.DataType = S7DataRowType.UDT; + } + + //Console.WriteLine("unkown Datatype: " + datatype); + break; + } + } + } + else if (xElement.Name.LocalName == "AttributeList") + { } + else if (xElement.Name.LocalName == "Subelement") //todo -> startwerte von arrays von UDTs + { } + else + { + //Console.WriteLine("unkown XML Element"); + } + } + } + #endregion + } +} diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV15/DotNetSiemensPLCToolBoxLibrary.TIAV15.csproj b/DotNetSiemensPLCToolBoxLibrary.TIAV15/DotNetSiemensPLCToolBoxLibrary.TIAV15.csproj index 984bcbef..55327d75 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV15/DotNetSiemensPLCToolBoxLibrary.TIAV15.csproj +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV15/DotNetSiemensPLCToolBoxLibrary.TIAV15.csproj @@ -1,64 +1,64 @@ - - - - - Debug - AnyCPU - {61E6DB85-4280-4963-95EF-254BA1A81C04} - Library - Properties - DotNetSiemensPLCToolBoxLibrary.TIAV15 - DotNetSiemensPLCToolBoxLibrary.TIAV15 - v4.6.1 - 512 - - - - true - full - false - ..\compiled\ - DEBUG;TRACE - prompt - 4 - false - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - false - - - - ..\externalDlls\siemens\V15\Siemens.Engineering.dll - - - ..\externalDlls\siemens\V15\Siemens.Engineering.Hmi.dll - - - - - - - - - - - - - - - - - - {e3ed87e8-b550-46ac-9196-9688d30efd29} - DotNetSiemensPLCToolBoxLibrary - - - - + + + + + Debug + AnyCPU + {61E6DB85-4280-4963-95EF-254BA1A81C04} + Library + Properties + DotNetSiemensPLCToolBoxLibrary.TIAV15 + DotNetSiemensPLCToolBoxLibrary.TIAV15 + v4.6.1 + 512 + + + + true + full + false + ..\compiled\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + false + + + + ..\externalDlls\siemens\V15\Siemens.Engineering.dll + + + ..\externalDlls\siemens\V15\Siemens.Engineering.Hmi.dll + + + + + + + + + + + + + + + + + + {e3ed87e8-b550-46ac-9196-9688d30efd29} + DotNetSiemensPLCToolBoxLibrary + + + + \ No newline at end of file diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV15/Properties/AssemblyInfo.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV15/Properties/AssemblyInfo.cs index 1e51f915..90a08479 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV15/Properties/AssemblyInfo.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV15/Properties/AssemblyInfo.cs @@ -1,36 +1,36 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// Allgemeine Informationen über eine Assembly werden über die folgenden -// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, -// die einer Assembly zugeordnet sind. -[assembly: AssemblyTitle("DotNetSiemensPLCToolBoxLibrary.TIAV14SP1")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("DotNetSiemensPLCToolBoxLibrary.TIAV14SP1")] -[assembly: AssemblyCopyright("Copyright © 2017")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly -// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von -// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. -[assembly: ComVisible(false)] - -// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird -[assembly: Guid("ab0895ae-446f-4cd8-96ab-c8007d618593")] - -// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: -// -// Hauptversion -// Nebenversion -// Buildnummer -// Revision -// -// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, -// indem Sie "*" wie unten gezeigt eingeben: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("DotNetSiemensPLCToolBoxLibrary.TIAV14SP1")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("DotNetSiemensPLCToolBoxLibrary.TIAV14SP1")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("ab0895ae-446f-4cd8-96ab-c8007d618593")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// indem Sie "*" wie unten gezeigt eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV15/Step7ProjectV15.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV15/Step7ProjectV15.cs index 8939f08f..be006ba6 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV15/Step7ProjectV15.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV15/Step7ProjectV15.cs @@ -1,123 +1,123 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Reflection; -using System.Xml; -using DotNetSiemensPLCToolBoxLibrary.General; -using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA; -using Microsoft.Win32; - -namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V15 -{ - public partial class Step7ProjectV15 : Project, IDisposable - { - public enum TiaVersionTypes - { - V11 = 11, - V12 = 12, - V13 = 13, - V14 = 14, - } - - public static TiaVersionTypes TiaVersion { get; private set; } - - private string DataFile = null; - - private XmlDocument tiaProject; - - internal ZipHelper _ziphelper = new ZipHelper(null); - - public CultureInfo Culture { get; set; } - - public Step7ProjectV15(string projectfile, CultureInfo culture = null) - { - if (culture == null) - Culture = CultureInfo.CurrentCulture; - else - Culture = culture; - - AppDomain currentDomain = AppDomain.CurrentDomain; - currentDomain.AssemblyResolve += currentDomain_AssemblyResolve; - - ProjectFile = projectfile; - - if (ProjectFile.ToLower().EndsWith("zip") || ProjectFile.ToLower().EndsWith("zap15")) - { - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap15"); - if (string.IsNullOrEmpty(ProjectFile)) - ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al15"); - - if (string.IsNullOrEmpty(projectfile)) - throw new Exception("Zip-File contains no valid TIA Project !"); - this._ziphelper = new ZipHelper(projectfile); - } - - - try - { - var xmlDoc = new XmlDocument(); - xmlDoc.Load(_ziphelper.GetReadStream(projectfile)); - - 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) - { } - - DataFile = Path.GetDirectoryName(projectfile) + "\\System\\PEData.plf"; - ProjectFolder = projectfile.Substring(0, projectfile.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar; - - //BinaryParseTIAFile(); - //LoadProject(); - LoadViaOpennessDlls(); - - currentDomain.AssemblyResolve -= currentDomain_AssemblyResolve; - } - - internal XmlDocument xmlDoc; - - Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) - { - int index = args.Name.IndexOf(','); - if (index == -1) - { - return null; - } - var name = args.Name.Substring(0, index) + ".dll"; - - var filePathReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Siemens\\Automation\\_InstalledSW\\TIAP15\\Global") ?? - Registry.LocalMachine.OpenSubKey("SOFTWARE\\Siemens\\Automation\\_InstalledSW\\TIAP15\\Global"); - - if (filePathReg != null) - { - string filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V15"); - if (Directory.Exists(filePath) == false) - filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V15 SP1"); - var path = Path.Combine(filePath, name); - var fullPath = Path.GetFullPath(path); - if (File.Exists(fullPath)) - { - return Assembly.LoadFrom(fullPath); - } - } - - return null; - } - - private object tiaExport; - private Type tiaExportType; - - - internal Dictionary TiaObjects = new Dictionary(); - - protected override void LoadProject() - { - _projectLoaded = true; - } - } -} +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Reflection; +using System.Xml; +using DotNetSiemensPLCToolBoxLibrary.General; +using DotNetSiemensPLCToolBoxLibrary.Projectfiles.TIA; +using Microsoft.Win32; + +namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V15 +{ + public partial class Step7ProjectV15 : Project, IDisposable + { + public enum TiaVersionTypes + { + V11 = 11, + V12 = 12, + V13 = 13, + V14 = 14, + } + + public static TiaVersionTypes TiaVersion { get; private set; } + + private string DataFile = null; + + private XmlDocument tiaProject; + + internal ZipHelper _ziphelper = new ZipHelper(null); + + public CultureInfo Culture { get; set; } + + public Step7ProjectV15(string projectfile, CultureInfo culture = null) + { + if (culture == null) + Culture = CultureInfo.CurrentCulture; + else + Culture = culture; + + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.AssemblyResolve += currentDomain_AssemblyResolve; + + ProjectFile = projectfile; + + if (ProjectFile.ToLower().EndsWith("zip") || ProjectFile.ToLower().EndsWith("zap15")) + { + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap15"); + if (string.IsNullOrEmpty(ProjectFile)) + ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".al15"); + + if (string.IsNullOrEmpty(projectfile)) + throw new Exception("Zip-File contains no valid TIA Project !"); + this._ziphelper = new ZipHelper(projectfile); + } + + + try + { + var xmlDoc = new XmlDocument(); + xmlDoc.Load(_ziphelper.GetReadStream(projectfile)); + + 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) + { } + + DataFile = Path.GetDirectoryName(projectfile) + "\\System\\PEData.plf"; + ProjectFolder = projectfile.Substring(0, projectfile.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar; + + //BinaryParseTIAFile(); + //LoadProject(); + LoadViaOpennessDlls(); + + currentDomain.AssemblyResolve -= currentDomain_AssemblyResolve; + } + + internal XmlDocument xmlDoc; + + Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + int index = args.Name.IndexOf(','); + if (index == -1) + { + return null; + } + var name = args.Name.Substring(0, index) + ".dll"; + + var filePathReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Siemens\\Automation\\_InstalledSW\\TIAP15\\Global") ?? + Registry.LocalMachine.OpenSubKey("SOFTWARE\\Siemens\\Automation\\_InstalledSW\\TIAP15\\Global"); + + if (filePathReg != null) + { + string filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V15"); + if (Directory.Exists(filePath) == false) + filePath = Path.Combine(filePathReg.GetValue("Path").ToString(), "PublicAPI\\V15 SP1"); + var path = Path.Combine(filePath, name); + var fullPath = Path.GetFullPath(path); + if (File.Exists(fullPath)) + { + return Assembly.LoadFrom(fullPath); + } + } + + return null; + } + + private object tiaExport; + private Type tiaExportType; + + + internal Dictionary TiaObjects = new Dictionary(); + + protected override void LoadProject() + { + _projectLoaded = true; + } + } +} diff --git a/DotNetSiemensPLCToolBoxLibrary.TIAV15/Step7ProjectV15Tia.cs b/DotNetSiemensPLCToolBoxLibrary.TIAV15/Step7ProjectV15Tia.cs index 4288624b..408b509f 100644 --- a/DotNetSiemensPLCToolBoxLibrary.TIAV15/Step7ProjectV15Tia.cs +++ b/DotNetSiemensPLCToolBoxLibrary.TIAV15/Step7ProjectV15Tia.cs @@ -1,1020 +1,1020 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Xml.Linq; -using DotNetSiemensPLCToolBoxLibrary.DataTypes; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V11; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V5; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders; -using Siemens.Engineering; -using Siemens.Engineering.HW.Features; -using Siemens.Engineering.SW; -using Siemens.Engineering.HW; -using Siemens.Engineering.SW.Blocks; -using Siemens.Engineering.SW.Types; -using Siemens.Engineering.SW.Tags; -using DotNetSiemensPLCToolBoxLibrary.General; -using System.Text.RegularExpressions; - -namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V15 -{ - public interface ITiaProjectBlockInfo : IProjectBlockInfo - { - } - - public partial class Step7ProjectV15 - { - - private Siemens.Engineering.TiaPortal tiaPortal; - - private Siemens.Engineering.Project tiapProject; - - public virtual void Dispose() - { - tiaPortal.Dispose(); - } - - - public class TIAOpennessProjectFolder : ProjectFolder - { - internal string InstID { get; private set; } - - protected Step7ProjectV15 TiaProject; - - public override string Name { get; set; } - - public TIAOpennessProjectFolder(Step7ProjectV15 Project) - { - this.Project = Project; - this.TiaProject = Project; - } - } - - public class TIAOpennessProjectBlockInfo : ProjectBlockInfo, ITiaProjectBlockInfo - { - internal TIAOpennessProjectBlockInfo(PlcBlock plcBlock) - { - this.plcBlock = plcBlock; - } - PlcBlock plcBlock; - - internal PLCLanguage SetBlockLanguage; - public override PLCLanguage BlockLanguage { get { return SetBlockLanguage; } } - - public int BlockNumber { get; set; } - - public string BlockName - { - get - { - string retVal = BlockType.ToString().Replace("S5_", "") + BlockNumber.ToString(); - return retVal; - } - } - - public string ProgrammingLanguage - { - get { return plcBlock.ProgrammingLanguage.ToString(); } - } - - public override string ToString() - { - string retVal = ""; - if (Deleted) - retVal += "$$_"; - if (Name != null) - retVal += BlockName + " (" + Name + ")"; - else - retVal += BlockName; - return retVal; - } - - public override string Export(ExportFormat exportFormat) - { - var ext = "xml"; - if (exportFormat != ExportFormat.Xml) - { - if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.SCL) - { - ext = "scl"; - } - else if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.STL || this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.F_STL) - { - ext = "awl"; - } - else if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.DB || this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.F_DB) - { - ext = "db"; - } - } - - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); - if (ext == "xml") - { - plcBlock.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); - } - else - { - var fld = this.ParentFolder; - while (!(fld is TIAOpennessControllerFolder)) - { - fld = fld.Parent; - } - ((TIAOpennessControllerFolder)fld).plcSoftware.ExternalSourceGroup.GenerateSource(new[] { this.plcBlock }, new FileInfo(file), Siemens.Engineering.SW.ExternalSources.GenerateOptions.None); - } - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - private PLCBlockType? _plcBlockType; - - public override PLCBlockType BlockType - { - get - { - if (_plcBlockType == null) - { - _plcBlockType = PLCBlockType.FC; - if (plcBlock is FB) - { - _plcBlockType = PLCBlockType.FB; - } - else if (plcBlock is OB) - { - _plcBlockType = PLCBlockType.OB; - } - } - return _plcBlockType.Value; - } - set { _plcBlockType = value; } - } - - private string xml; - } - - public class TIAOpennessProjectDataTypeInfo : ProjectBlockInfo, ITiaProjectBlockInfo - { - internal TIAOpennessProjectDataTypeInfo(PlcType plcType) - { - this.plcType = plcType; - } - private PlcType plcType; - - public override string ToString() - { - return Name; - } - - public override PLCBlockType BlockType - { - get - { - return PLCBlockType.UDT; - } - set { } - } - - public override PLCLanguage BlockLanguage - { - get - { - return PLCLanguage.DB; - } - } - - public override string Export(ExportFormat exportFormat) - { - var ext = "udt"; - if (exportFormat == ExportFormat.Xml) - { - ext = "xml"; - } - - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); - if (ext == "xml") - { - plcType.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); - } - else - { - var fld = this.ParentFolder; - while (!(fld is TIAOpennessControllerFolder)) - { - fld = fld.Parent; - } - ((TIAOpennessControllerFolder)fld).plcSoftware.ExternalSourceGroup.GenerateSource(new[] { this.plcType }, new FileInfo(file), Siemens.Engineering.SW.ExternalSources.GenerateOptions.None); - } - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - } - - public class TIAOpennessTagTable - { - public string Name { get; set; } - - public List Constants { get; set; } - - } - - public class TIAOpennessConstant - { - private readonly PlcUserConstant controllerConstant; - - internal TIAOpennessConstant(PlcUserConstant controllerConstant) - { - this.controllerConstant = controllerConstant; - } - - public string Name { get; set; } - - private Regex _rgx = new Regex("(.*)", RegexOptions.Compiled); - - public object Value - { - get - { - var tp = this.controllerConstant.DataTypeName; - var strg = ExportToString(); - - var m = _rgx.Match(strg).Groups[1].Value; - if (tp == "Int") - return int.Parse(m); - return m; - } - } - - private string ExportToString() - { - var tmp = Path.GetTempPath(); - var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); - controllerConstant.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); - - var text = File.ReadAllText(file); - File.Delete(file); - - return text; - } - } - - public class TIAOpennessControllerFolder : TIAOpennessProjectFolder, IRootProgrammFolder - { - internal TIAOpennessControllerFolder(Step7ProjectV15 Project, PlcSoftware plcSoftware) - : base(Project) - { - this.Project = Project; - this.TiaProject = Project; - this.plcSoftware = plcSoftware; - } - - internal PlcSoftware plcSoftware; - - public TIAOpennessProgramFolder ProgramFolder { get; set; } - public TIAOpennessPlcDatatypeFolder PlcDatatypeFolder { get; set; } - public TIAVarTabFolder VarTabFolder { get; set; } - - public Block GetBlockRecursive(string name) - { - var block = GetBlockRecursive(ProgramFolder, name); - if (block == null) - { - block = GetBlockRecursive(PlcDatatypeFolder, name); - } - - return block; - } - - private Block GetBlockRecursive(TIAOpennessProgramFolder folder, string name) - { - var block = folder.GetBlock(name); - if (block == null) - { - foreach (TIAOpennessProgramFolder projectFolder in folder.SubItems) - { - block = GetBlockRecursive(projectFolder, name); - if (block != null) - return block; - } - } - - return block; - } - - private Block GetBlockRecursive(TIAOpennessPlcDatatypeFolder folder, string name) - { - var block = folder.GetBlock(name); - if (block == null) - { - foreach (TIAOpennessPlcDatatypeFolder projectFolder in folder.SubItems) - { - block = GetBlockRecursive(projectFolder, name); - if (block != null) - return block; - } - } - - return block; - } - } - - public class TIAVarTabFolder : TIAOpennessProjectFolder - { - public TIAOpennessControllerFolder ControllerFolder { get; set; } - - internal object TiaPortalItem { get; set; } - - public TIAVarTabFolder(Step7ProjectV15 Project, TIAOpennessControllerFolder ControllerFolder) : base(Project) - { - this.ControllerFolder = ControllerFolder; - this.Project = Project; - this.TiaProject = Project; - } - - public TIAOpennessConstant FindConstant(string name) - { - foreach (var t in TagTables) - { - var c = t.Constants.FirstOrDefault(x => x.Name == name); - if (c != null) - return c; - } - foreach (var f in SubItems.Flatten(x => x.SubItems)) - { - foreach (var t in TagTables) - { - var c = t.Constants.FirstOrDefault(x => x.Name == name); - if (c != null) - return c; - } - } - return null; - } - - public List TagTables - { - get - { - PlcTagTableComposition tags = null; - var o = this.TiaPortalItem as PlcTagTableUserGroup; - if (o != null) - tags = o.TagTables; - var q = this.TiaPortalItem as PlcTagTableSystemGroup; - if (q != null) - tags = q.TagTables; - - var retVal = new List(); - - foreach (var tagList in tags) - { - var info = new TIAOpennessTagTable() { Name = tagList.Name }; - retVal.Add(info); - info.Constants = new List(); - foreach (var c in tagList.UserConstants) - { - info.Constants.Add(new TIAOpennessConstant(c) { Name = c.Name }); - } - } - return retVal; - } - } - } - - public class TIAOpennessPlcDatatypeFolder : TIAOpennessProjectFolder, IBlocksFolder - { - PlcTypeComposition composition; - - public TIAOpennessControllerFolder ControllerFolder { get; set; } - - public TIAOpennessPlcDatatypeFolder(Step7ProjectV15 Project, TIAOpennessControllerFolder ControllerFolder, PlcTypeComposition composition) - : base(Project) - { - this.ControllerFolder = ControllerFolder; - this.Project = Project; - this.TiaProject = Project; - this.composition = composition; - } - - public List readPlcBlocksList() - { - if (_blockInfos != null) - return _blockInfos; - - _blockInfos = new List(); - - if (composition != null) - { - foreach (var block in composition) - { - var info = new TIAOpennessProjectDataTypeInfo(block) { Name = block.Name, ParentFolder = this }; - info.BlockType = DataTypes.PLCBlockType.UDT; - _blockInfos.Add(info); - } - } - - return BlockInfos; - } - - private List _blockInfos; - public List BlockInfos - { - get - { - if (_blockInfos == null) - readPlcBlocksList(); - return _blockInfos; - } - private set { _blockInfos = value; } - } - - public Block GetBlock(string BlockName) - { - if (BlockInfos == null) - readPlcBlocksList(); - - var block = GetBlock(BlockInfos.FirstOrDefault(x => x.Name == BlockName)); - - if (block != null) - return block; - - foreach (TIAOpennessPlcDatatypeFolder s in this.SubItems) - { - block = s.GetBlock(BlockName); - if (block != null) - return block; - } - - return null; - } - - public Block GetBlock(ProjectBlockInfo blkInfo) - { - if (blkInfo == null) - return null; - - var iv = blkInfo as TIAOpennessProjectDataTypeInfo; - var text = iv.Export(ExportFormat.Xml); - - return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.DataType); - } - } - - public class TIAOpennessProgramFolder : TIAOpennessProjectFolder, IBlocksFolder - { - public TIAOpennessControllerFolder ControllerFolder { get; set; } - - PlcBlockComposition blocks; - - public TIAOpennessProgramFolder(Step7ProjectV15 Project, TIAOpennessControllerFolder ControllerFolder, PlcBlockComposition blocks) - : base(Project) - { - this.ControllerFolder = ControllerFolder; - this.Project = Project; - this.TiaProject = Project; - this.blocks = blocks; - } - - public List readPlcBlocksList() - { - if (_blockInfos != null) - return _blockInfos; - - _blockInfos = new List(); - - foreach (var block in blocks) - { - var info = new TIAOpennessProjectBlockInfo(block) { Name = block.Name, ParentFolder = this }; - info.BlockType = DataTypes.PLCBlockType.FB; - info.SetBlockLanguage = PLCLanguage.unkown; - if (block.ProgrammingLanguage == ProgrammingLanguage.DB || - block.ProgrammingLanguage == ProgrammingLanguage.CPU_DB || - block.ProgrammingLanguage == ProgrammingLanguage.F_DB || - block.ProgrammingLanguage == ProgrammingLanguage.Motion_DB) - { - info.BlockType = DataTypes.PLCBlockType.DB; - info.SetBlockLanguage = PLCLanguage.DB; - } - else if (block.ProgrammingLanguage == ProgrammingLanguage.LAD || - block.ProgrammingLanguage == ProgrammingLanguage.F_LAD || - block.ProgrammingLanguage == ProgrammingLanguage.F_LAD_LIB) - info.SetBlockLanguage = PLCLanguage.KOP; - else if (block.ProgrammingLanguage == ProgrammingLanguage.STL || - block.ProgrammingLanguage == ProgrammingLanguage.F_STL) - info.SetBlockLanguage = PLCLanguage.AWL; - else if (block.ProgrammingLanguage == ProgrammingLanguage.FBD || - block.ProgrammingLanguage == ProgrammingLanguage.F_FBD || - block.ProgrammingLanguage == ProgrammingLanguage.F_FBD_LIB) - info.SetBlockLanguage = PLCLanguage.FUP; - else if (block.ProgrammingLanguage == ProgrammingLanguage.CFC) - info.SetBlockLanguage = PLCLanguage.CFC; - else if (block.ProgrammingLanguage == ProgrammingLanguage.SCL) - info.SetBlockLanguage = PLCLanguage.SCL; - info.BlockNumber = block.Number; - _blockInfos.Add(info); - } - return _blockInfos; - } - - private List _blockInfos; - public List BlockInfos - { - get - { - if (_blockInfos == null) - readPlcBlocksList(); - return _blockInfos; - } - private set { _blockInfos = value; } - } - - public Block GetBlock(string BlockName) - { - if (BlockInfos == null) - readPlcBlocksList(); - - return - GetBlock( - BlockInfos.Cast() - .FirstOrDefault(x => x.Name == BlockName || x.BlockName == BlockName)); - } - - public Block GetBlock(ProjectBlockInfo blkInfo) - { - if (blkInfo == null) - return null; - - var iv = blkInfo as TIAOpennessProjectBlockInfo; - var text = iv.Export(ExportFormat.Xml); - - return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.Programm); - } - } - internal void LoadViaOpennessDlls() - { - for (int i = 0; i < 10; i++) - { - try - { - if (tiaPortal != null) - { - tiaPortal.Dispose(); - tiaPortal = null; - } - tiaPortal = new Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithoutUserInterface); - tiapProject = tiaPortal.Projects.Open(new FileInfo(ProjectFile)); - } - catch (Siemens.Engineering.EngineeringSecurityException ex) - { - throw; - } - catch (Exception ex) - { - if (i == 9) - throw; - } - if (tiapProject != null) - break; - } - - - var main = new TIAOpennessProjectFolder(this) { Name = "Main" }; - ProjectStructure = main; - - //var frm = new sliver.Windows.Forms.StateBrowserForm(); - //frm.ObjectToBrowse = tiapProject; - //frm.Show(); - - foreach (var d in tiapProject.Devices) - { - if (d.TypeIdentifier != null && d.TypeIdentifier.EndsWith(".S71500")) - { - foreach (DeviceItem deviceItem in d.DeviceItems) - { - var target = ((IEngineeringServiceProvider)deviceItem).GetService(); - if (target != null && target.Software is PlcSoftware) - { - var software =(PlcSoftware)target.Software; - var fld = new TIAOpennessControllerFolder(this, software) - { - Name = software.Name, - //TiaPortalItem = software, - //Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null - }; - main.SubItems.Add(fld); - - LoadControlerFolderViaOpennessDlls(fld, software); - - } - } - - //var controller = d.DeviceItems.OfType().FirstOrDefault(); - //if (controller == null) - //{ - // var fld = new TIAOpennessProjectFolder(this) - // { - // Name = d.Name, - // TiaPortalItem = d, - // Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null - // }; - // main.SubItems.Add(fld); - - // //LoadSubDevicesViaOpennessDlls(fld, d); - //} - //else - //{ - // var fld = new TIAOpennessControllerFolder(this) - // { - // Name = d.Name, - // TiaPortalItem = d, - // Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null - // }; - // main.SubItems.Add(fld); - - // //LoadControlerFolderViaOpennessDlls(fld, controller); - //} - } - } - } - - //internal void LoadSubDevicesViaOpennessDlls(TIAOpennessProjectFolder parent, Siemens.Engineering.HW.IHardwareObject device) - //{ - // foreach (var e in device.DeviceItems) - // { - // var fld = new TIAOpennessProjectFolder(this) - // { - // TiaPortalItem = e, - // Name = e.Name, - // Parent = parent, - // }; - // var d = e as Siemens.Engineering.HW.DeviceItem; - // //d.Elements.ToList() - - // parent.SubItems.Add(fld); - // LoadSubDevicesViaOpennessDlls(fld, e); - // } - //} - - internal void LoadControlerFolderViaOpennessDlls(TIAOpennessControllerFolder parent, PlcSoftware software) - { - var fld = new TIAOpennessProgramFolder(this, parent, software.BlockGroup.Blocks) - { - //TiaPortalItem = controller.ProgramblockFolder, - Name = "software", - Parent = parent, - }; - parent.ProgramFolder = fld; - parent.SubItems.Add(fld); - LoadSubProgramBlocksFoldersViaOpennessDlls(fld, software.BlockGroup); - - var t = (PlcTypeGroup)software.TypeGroup; - - var fld2 = new TIAOpennessPlcDatatypeFolder(this, parent, t.Types) - { - //TiaPortalItem = controller.ControllerDatatypeFolder, - Name = "data types", - Parent = parent, - }; - parent.PlcDatatypeFolder = fld2; - parent.SubItems.Add(fld2); - LoadSubPlcDatatypeFoldersViaOpennessDlls(fld2, software.TypeGroup); - - var fld3 = new TIAVarTabFolder(this, parent) - { - //TiaPortalItem = controller.ControllerTagFolder, - Name = "PLC data types", - Parent = parent, - }; - parent.VarTabFolder = fld3; - parent.SubItems.Add(fld3); - LoadSubVartabFoldersViaOpennessDlls(fld3, software.TagTableGroup); - } - - internal void LoadSubProgramBlocksFoldersViaOpennessDlls(TIAOpennessProgramFolder parent, PlcBlockGroup plcBlockGroup) - { - foreach (var e in plcBlockGroup.Groups) - { - var fld = new TIAOpennessProgramFolder(this, parent.ControllerFolder, e.Blocks) - { - //TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubProgramBlocksFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, PlcTypeSystemGroup p) - { - foreach (var e in p.Groups) - { - var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder, e.Types) - { - //TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, PlcTypeUserGroup p) - { - foreach (var e in p.Groups) - { - var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder, e.Types) - { - //TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, PlcTagTableSystemGroup blockFolder) - { - foreach (var e in blockFolder.Groups) - { - var fld = new TIAVarTabFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubVartabFoldersViaOpennessDlls(fld, e); - } - } - - internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, PlcTagTableUserGroup blockFolder) - { - foreach (var e in blockFolder.Groups) - { - var fld = new TIAVarTabFolder(this, parent.ControllerFolder) - { - TiaPortalItem = e, - Name = e.Name, - Parent = parent, - }; - parent.SubItems.Add(fld); - LoadSubVartabFoldersViaOpennessDlls(fld, e); - } - } - #region Parse DB UDT XML - - internal enum ParseType - { - Programm, - DataType - } - - internal static Block ParseTiaDbUdtXml(string xml, ProjectBlockInfo projectBlockInfo, TIAOpennessControllerFolder controllerFolder, ParseType parseType) - { - XElement xelement = XElement.Parse(xml); - var structure = xelement.Elements().FirstOrDefault(x => x.Name.LocalName.StartsWith("SW.")); - - var sections = structure.Element("AttributeList").Element("Interface").Elements().First(); - - var block = new TIADataBlock(); - block.Name = projectBlockInfo.Name; - - if (projectBlockInfo is TIAOpennessProjectBlockInfo) - block.BlockNumber = ((TIAOpennessProjectBlockInfo) projectBlockInfo).BlockNumber; - - if (parseType == ParseType.DataType) - block.BlockType = DataTypes.PLCBlockType.UDT; - else if (parseType == ParseType.Programm) - block.BlockType = DataTypes.PLCBlockType.DB; - - var parameterRoot = ParseTiaDbUdtSections(sections, block, controllerFolder); - - block.BlockType = DataTypes.PLCBlockType.DB; - block.Structure = parameterRoot; - - return block; - } - - internal static TIADataRow ParseTiaDbUdtSections(XElement sections, TIADataBlock block, TIAOpennessControllerFolder controllerFolder) - { - var parameterRoot = new TIADataRow("ROOTNODE", S7DataRowType.STRUCT, block); - var parameterIN = new TIADataRow("IN", S7DataRowType.STRUCT, block); - parameterIN.Parent = parameterRoot; - var parameterOUT = new TIADataRow("OUT", S7DataRowType.STRUCT, block); - parameterOUT.Parent = parameterRoot; - var parameterINOUT = new TIADataRow("IN_OUT", S7DataRowType.STRUCT, block); - parameterINOUT.Parent = parameterRoot; - var parameterSTAT = new TIADataRow("STATIC", S7DataRowType.STRUCT, block); - parameterSTAT.Parent = parameterRoot; - var parameterTEMP = new TIADataRow("TEMP", S7DataRowType.STRUCT, block); - parameterTEMP.Parent = parameterRoot; - - foreach (var xElement in sections.Elements()) - { - TIADataRow useRow = parameterRoot; - //var sectionName = xElement.Attribute("Name").Value; - //if (sectionName == "None" || sectionName == "Static") - //{ - // useRow = parameterSTAT; - // parameterRoot.Add(useRow); - //} - //else if (sectionName == "In") - //{ - // useRow = parameterIN; - // parameterRoot.Add(useRow); - //} - - parseChildren(useRow, xElement, controllerFolder); - } - - return parameterRoot; - } - - internal static void parseChildren(TIADataRow parentRow, XElement parentElement, TIAOpennessControllerFolder controllerFolder) - { - foreach (var xElement in parentElement.Elements()) - { - if (xElement.Name.LocalName == "Comment") - { - var text = xElement.Elements().FirstOrDefault(x => x.Attribute("Lang").Value == "de-DE"); - if (text == null) - text = xElement.Elements().FirstOrDefault(); - if (text != null) - parentRow.Comment = text.Value; - } - else if (xElement.Name.LocalName == "StartValue") - { - parentRow.StartValue = xElement.Value; - } - else if (xElement.Name.LocalName == "Sections") - { - var row = ParseTiaDbUdtSections(xElement, (TIADataBlock)parentRow.CurrentBlock, controllerFolder); - parentRow.AddRange(row.Children); - } - else if (xElement.Name.LocalName == "Member") - { - var name = xElement.Attribute("Name").Value; - var datatype = xElement.Attribute("Datatype").Value; - - var row = new TIADataRow(name, S7DataRowType.STRUCT, (TIABlock)parentRow.PlcBlock); - row.Parent = parentRow; - - if (datatype.Contains("Array[")) - { - List arrayStart = new List(); - List arrayStop = new List(); - - int pos1 = datatype.IndexOf("["); - int pos2 = datatype.IndexOf("]", pos1); - string[] arrays = datatype.Substring(pos1 + 1, pos2 - pos1 - 1).Split(','); - - foreach (string array in arrays) - { - string[] akar = array.Split(new string[] { ".." }, StringSplitOptions.RemoveEmptyEntries); - int start = 0; - if (akar[0].StartsWith("\"")) - { - start = (int)controllerFolder.VarTabFolder.FindConstant(akar[0].Substring(1, akar[0].Length - 2)).Value; - } - else - { - start = Convert.ToInt32(akar[0].Trim()); - } - - int stop = 0; - if (akar[1].StartsWith("\"")) - { - stop = (int)controllerFolder.VarTabFolder.FindConstant(akar[1].Substring(1, akar[1].Length - 2)).Value; - } - else - { - stop = Convert.ToInt32(akar[1].Trim()); - } - - arrayStart.Add(start); - arrayStop.Add(stop); - } - - row.ArrayStart = arrayStart; - row.ArrayStop = arrayStop; - row.IsArray = true; - datatype = datatype.Substring(pos2 + 5); - } - - parentRow.Add(row); - - parseChildren(row, xElement, controllerFolder); - - if (datatype.StartsWith("\"")) - { - var udt = - controllerFolder.PlcDatatypeFolder.GetBlock(datatype.Substring(1, datatype.Length - 2)); - if (udt != null) - { - var tiaUdt = udt as TIADataBlock; - row.AddRange(((TIADataRow)tiaUdt.Structure).DeepCopy().Children); - - row.DataTypeBlock = udt; - } - row.DataType = S7DataRowType.UDT; - } - else if (datatype == "Struct") - { - - } - else if (datatype.StartsWith("String[")) - { - row.DataType = S7DataRowType.STRING; - row.StringSize = int.Parse(datatype.Substring(7, datatype.Length - 8)); - } - else - { - switch (datatype.ToLower()) - { - case "byte": - row.DataType = S7DataRowType.BYTE; - break; - case "bool": - row.DataType = S7DataRowType.BOOL; - break; - case "int": - row.DataType = S7DataRowType.INT; - break; - case "uint": - row.DataType = S7DataRowType.WORD; - break; - case "dint": - row.DataType = S7DataRowType.DINT; - break; - case "udint": - row.DataType = S7DataRowType.DWORD; - break; - case "word": - row.DataType = S7DataRowType.WORD; - break; - case "dword": - row.DataType = S7DataRowType.DWORD; - break; - case "char": - row.DataType = S7DataRowType.CHAR; - break; - case "any": - row.DataType = S7DataRowType.ANY; - break; - case "date": - row.DataType = S7DataRowType.DATE; - break; - case "date_and_time": - row.DataType = S7DataRowType.DATE_AND_TIME; - break; - case "real": - row.DataType = S7DataRowType.REAL; - break; - case "s5time": - row.DataType = S7DataRowType.S5TIME; - break; - case "time_of_day": - row.DataType = S7DataRowType.TIME_OF_DAY; - break; - case "time": - row.DataType = S7DataRowType.TIME; - break; - default: - row.DataType = S7DataRowType.UNKNOWN; - - var udt = controllerFolder.PlcDatatypeFolder.GetBlock(datatype); - if (udt != null) - { - var tiaUdt = udt as TIADataBlock; - row.AddRange(((TIADataRow)tiaUdt.Structure).DeepCopy().Children); - - row.DataTypeBlock = udt; - row.DataType = S7DataRowType.UDT; - } - - //Console.WriteLine("unkown Datatype: " + datatype); - break; - } - } - } - else if (xElement.Name.LocalName == "AttributeList") - { } - else if (xElement.Name.LocalName == "Subelement") //todo -> startwerte von arrays von UDTs - { } - else - { - //Console.WriteLine("unkown XML Element"); - } - } - } - #endregion - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using DotNetSiemensPLCToolBoxLibrary.DataTypes; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V11; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Blocks.Step7V5; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders; +using Siemens.Engineering; +using Siemens.Engineering.HW.Features; +using Siemens.Engineering.SW; +using Siemens.Engineering.HW; +using Siemens.Engineering.SW.Blocks; +using Siemens.Engineering.SW.Types; +using Siemens.Engineering.SW.Tags; +using DotNetSiemensPLCToolBoxLibrary.General; +using System.Text.RegularExpressions; + +namespace DotNetSiemensPLCToolBoxLibrary.Projectfiles.V15 +{ + public interface ITiaProjectBlockInfo : IProjectBlockInfo + { + } + + public partial class Step7ProjectV15 + { + + private Siemens.Engineering.TiaPortal tiaPortal; + + private Siemens.Engineering.Project tiapProject; + + public virtual void Dispose() + { + tiaPortal.Dispose(); + } + + + public class TIAOpennessProjectFolder : ProjectFolder + { + internal string InstID { get; private set; } + + protected Step7ProjectV15 TiaProject; + + public override string Name { get; set; } + + public TIAOpennessProjectFolder(Step7ProjectV15 Project) + { + this.Project = Project; + this.TiaProject = Project; + } + } + + public class TIAOpennessProjectBlockInfo : ProjectBlockInfo, ITiaProjectBlockInfo + { + internal TIAOpennessProjectBlockInfo(PlcBlock plcBlock) + { + this.plcBlock = plcBlock; + } + PlcBlock plcBlock; + + internal PLCLanguage SetBlockLanguage; + public override PLCLanguage BlockLanguage { get { return SetBlockLanguage; } } + + public int BlockNumber { get; set; } + + public string BlockName + { + get + { + string retVal = BlockType.ToString().Replace("S5_", "") + BlockNumber.ToString(); + return retVal; + } + } + + public string ProgrammingLanguage + { + get { return plcBlock.ProgrammingLanguage.ToString(); } + } + + public override string ToString() + { + string retVal = ""; + if (Deleted) + retVal += "$$_"; + if (Name != null) + retVal += BlockName + " (" + Name + ")"; + else + retVal += BlockName; + return retVal; + } + + public override string Export(ExportFormat exportFormat) + { + var ext = "xml"; + if (exportFormat != ExportFormat.Xml) + { + if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.SCL) + { + ext = "scl"; + } + else if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.STL || this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.F_STL) + { + ext = "awl"; + } + else if (this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.DB || this.plcBlock.ProgrammingLanguage == Siemens.Engineering.SW.Blocks.ProgrammingLanguage.F_DB) + { + ext = "db"; + } + } + + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); + if (ext == "xml") + { + plcBlock.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); + } + else + { + var fld = this.ParentFolder; + while (!(fld is TIAOpennessControllerFolder)) + { + fld = fld.Parent; + } + ((TIAOpennessControllerFolder)fld).plcSoftware.ExternalSourceGroup.GenerateSource(new[] { this.plcBlock }, new FileInfo(file), Siemens.Engineering.SW.ExternalSources.GenerateOptions.None); + } + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + private PLCBlockType? _plcBlockType; + + public override PLCBlockType BlockType + { + get + { + if (_plcBlockType == null) + { + _plcBlockType = PLCBlockType.FC; + if (plcBlock is FB) + { + _plcBlockType = PLCBlockType.FB; + } + else if (plcBlock is OB) + { + _plcBlockType = PLCBlockType.OB; + } + } + return _plcBlockType.Value; + } + set { _plcBlockType = value; } + } + + private string xml; + } + + public class TIAOpennessProjectDataTypeInfo : ProjectBlockInfo, ITiaProjectBlockInfo + { + internal TIAOpennessProjectDataTypeInfo(PlcType plcType) + { + this.plcType = plcType; + } + private PlcType plcType; + + public override string ToString() + { + return Name; + } + + public override PLCBlockType BlockType + { + get + { + return PLCBlockType.UDT; + } + set { } + } + + public override PLCLanguage BlockLanguage + { + get + { + return PLCLanguage.DB; + } + } + + public override string Export(ExportFormat exportFormat) + { + var ext = "udt"; + if (exportFormat == ExportFormat.Xml) + { + ext = "xml"; + } + + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + "." + ext); + if (ext == "xml") + { + plcType.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); + } + else + { + var fld = this.ParentFolder; + while (!(fld is TIAOpennessControllerFolder)) + { + fld = fld.Parent; + } + ((TIAOpennessControllerFolder)fld).plcSoftware.ExternalSourceGroup.GenerateSource(new[] { this.plcType }, new FileInfo(file), Siemens.Engineering.SW.ExternalSources.GenerateOptions.None); + } + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + } + + public class TIAOpennessTagTable + { + public string Name { get; set; } + + public List Constants { get; set; } + + } + + public class TIAOpennessConstant + { + private readonly PlcUserConstant controllerConstant; + + internal TIAOpennessConstant(PlcUserConstant controllerConstant) + { + this.controllerConstant = controllerConstant; + } + + public string Name { get; set; } + + private Regex _rgx = new Regex("(.*)", RegexOptions.Compiled); + + public object Value + { + get + { + var tp = this.controllerConstant.DataTypeName; + var strg = ExportToString(); + + var m = _rgx.Match(strg).Groups[1].Value; + if (tp == "Int") + return int.Parse(m); + return m; + } + } + + private string ExportToString() + { + var tmp = Path.GetTempPath(); + var file = Path.Combine(tmp, "tmp_dnspt_" + Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "").Replace(" ", "") + ".tmp"); + controllerConstant.Export(new FileInfo(file), Siemens.Engineering.ExportOptions.None); + + var text = File.ReadAllText(file); + File.Delete(file); + + return text; + } + } + + public class TIAOpennessControllerFolder : TIAOpennessProjectFolder, IRootProgrammFolder + { + internal TIAOpennessControllerFolder(Step7ProjectV15 Project, PlcSoftware plcSoftware) + : base(Project) + { + this.Project = Project; + this.TiaProject = Project; + this.plcSoftware = plcSoftware; + } + + internal PlcSoftware plcSoftware; + + public TIAOpennessProgramFolder ProgramFolder { get; set; } + public TIAOpennessPlcDatatypeFolder PlcDatatypeFolder { get; set; } + public TIAVarTabFolder VarTabFolder { get; set; } + + public Block GetBlockRecursive(string name) + { + var block = GetBlockRecursive(ProgramFolder, name); + if (block == null) + { + block = GetBlockRecursive(PlcDatatypeFolder, name); + } + + return block; + } + + private Block GetBlockRecursive(TIAOpennessProgramFolder folder, string name) + { + var block = folder.GetBlock(name); + if (block == null) + { + foreach (TIAOpennessProgramFolder projectFolder in folder.SubItems) + { + block = GetBlockRecursive(projectFolder, name); + if (block != null) + return block; + } + } + + return block; + } + + private Block GetBlockRecursive(TIAOpennessPlcDatatypeFolder folder, string name) + { + var block = folder.GetBlock(name); + if (block == null) + { + foreach (TIAOpennessPlcDatatypeFolder projectFolder in folder.SubItems) + { + block = GetBlockRecursive(projectFolder, name); + if (block != null) + return block; + } + } + + return block; + } + } + + public class TIAVarTabFolder : TIAOpennessProjectFolder + { + public TIAOpennessControllerFolder ControllerFolder { get; set; } + + internal object TiaPortalItem { get; set; } + + public TIAVarTabFolder(Step7ProjectV15 Project, TIAOpennessControllerFolder ControllerFolder) : base(Project) + { + this.ControllerFolder = ControllerFolder; + this.Project = Project; + this.TiaProject = Project; + } + + public TIAOpennessConstant FindConstant(string name) + { + foreach (var t in TagTables) + { + var c = t.Constants.FirstOrDefault(x => x.Name == name); + if (c != null) + return c; + } + foreach (var f in SubItems.Flatten(x => x.SubItems)) + { + foreach (var t in TagTables) + { + var c = t.Constants.FirstOrDefault(x => x.Name == name); + if (c != null) + return c; + } + } + return null; + } + + public List TagTables + { + get + { + PlcTagTableComposition tags = null; + var o = this.TiaPortalItem as PlcTagTableUserGroup; + if (o != null) + tags = o.TagTables; + var q = this.TiaPortalItem as PlcTagTableSystemGroup; + if (q != null) + tags = q.TagTables; + + var retVal = new List(); + + foreach (var tagList in tags) + { + var info = new TIAOpennessTagTable() { Name = tagList.Name }; + retVal.Add(info); + info.Constants = new List(); + foreach (var c in tagList.UserConstants) + { + info.Constants.Add(new TIAOpennessConstant(c) { Name = c.Name }); + } + } + return retVal; + } + } + } + + public class TIAOpennessPlcDatatypeFolder : TIAOpennessProjectFolder, IBlocksFolder + { + PlcTypeComposition composition; + + public TIAOpennessControllerFolder ControllerFolder { get; set; } + + public TIAOpennessPlcDatatypeFolder(Step7ProjectV15 Project, TIAOpennessControllerFolder ControllerFolder, PlcTypeComposition composition) + : base(Project) + { + this.ControllerFolder = ControllerFolder; + this.Project = Project; + this.TiaProject = Project; + this.composition = composition; + } + + public List readPlcBlocksList() + { + if (_blockInfos != null) + return _blockInfos; + + _blockInfos = new List(); + + if (composition != null) + { + foreach (var block in composition) + { + var info = new TIAOpennessProjectDataTypeInfo(block) { Name = block.Name, ParentFolder = this }; + info.BlockType = DataTypes.PLCBlockType.UDT; + _blockInfos.Add(info); + } + } + + return BlockInfos; + } + + private List _blockInfos; + public List BlockInfos + { + get + { + if (_blockInfos == null) + readPlcBlocksList(); + return _blockInfos; + } + private set { _blockInfos = value; } + } + + public Block GetBlock(string BlockName) + { + if (BlockInfos == null) + readPlcBlocksList(); + + var block = GetBlock(BlockInfos.FirstOrDefault(x => x.Name == BlockName)); + + if (block != null) + return block; + + foreach (TIAOpennessPlcDatatypeFolder s in this.SubItems) + { + block = s.GetBlock(BlockName); + if (block != null) + return block; + } + + return null; + } + + public Block GetBlock(ProjectBlockInfo blkInfo) + { + if (blkInfo == null) + return null; + + var iv = blkInfo as TIAOpennessProjectDataTypeInfo; + var text = iv.Export(ExportFormat.Xml); + + return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.DataType); + } + } + + public class TIAOpennessProgramFolder : TIAOpennessProjectFolder, IBlocksFolder + { + public TIAOpennessControllerFolder ControllerFolder { get; set; } + + PlcBlockComposition blocks; + + public TIAOpennessProgramFolder(Step7ProjectV15 Project, TIAOpennessControllerFolder ControllerFolder, PlcBlockComposition blocks) + : base(Project) + { + this.ControllerFolder = ControllerFolder; + this.Project = Project; + this.TiaProject = Project; + this.blocks = blocks; + } + + public List readPlcBlocksList() + { + if (_blockInfos != null) + return _blockInfos; + + _blockInfos = new List(); + + foreach (var block in blocks) + { + var info = new TIAOpennessProjectBlockInfo(block) { Name = block.Name, ParentFolder = this }; + info.BlockType = DataTypes.PLCBlockType.FB; + info.SetBlockLanguage = PLCLanguage.unkown; + if (block.ProgrammingLanguage == ProgrammingLanguage.DB || + block.ProgrammingLanguage == ProgrammingLanguage.CPU_DB || + block.ProgrammingLanguage == ProgrammingLanguage.F_DB || + block.ProgrammingLanguage == ProgrammingLanguage.Motion_DB) + { + info.BlockType = DataTypes.PLCBlockType.DB; + info.SetBlockLanguage = PLCLanguage.DB; + } + else if (block.ProgrammingLanguage == ProgrammingLanguage.LAD || + block.ProgrammingLanguage == ProgrammingLanguage.F_LAD || + block.ProgrammingLanguage == ProgrammingLanguage.F_LAD_LIB) + info.SetBlockLanguage = PLCLanguage.KOP; + else if (block.ProgrammingLanguage == ProgrammingLanguage.STL || + block.ProgrammingLanguage == ProgrammingLanguage.F_STL) + info.SetBlockLanguage = PLCLanguage.AWL; + else if (block.ProgrammingLanguage == ProgrammingLanguage.FBD || + block.ProgrammingLanguage == ProgrammingLanguage.F_FBD || + block.ProgrammingLanguage == ProgrammingLanguage.F_FBD_LIB) + info.SetBlockLanguage = PLCLanguage.FUP; + else if (block.ProgrammingLanguage == ProgrammingLanguage.CFC) + info.SetBlockLanguage = PLCLanguage.CFC; + else if (block.ProgrammingLanguage == ProgrammingLanguage.SCL) + info.SetBlockLanguage = PLCLanguage.SCL; + info.BlockNumber = block.Number; + _blockInfos.Add(info); + } + return _blockInfos; + } + + private List _blockInfos; + public List BlockInfos + { + get + { + if (_blockInfos == null) + readPlcBlocksList(); + return _blockInfos; + } + private set { _blockInfos = value; } + } + + public Block GetBlock(string BlockName) + { + if (BlockInfos == null) + readPlcBlocksList(); + + return + GetBlock( + BlockInfos.Cast() + .FirstOrDefault(x => x.Name == BlockName || x.BlockName == BlockName)); + } + + public Block GetBlock(ProjectBlockInfo blkInfo) + { + if (blkInfo == null) + return null; + + var iv = blkInfo as TIAOpennessProjectBlockInfo; + var text = iv.Export(ExportFormat.Xml); + + return ParseTiaDbUdtXml(text, blkInfo, ControllerFolder, ParseType.Programm); + } + } + internal void LoadViaOpennessDlls() + { + for (int i = 0; i < 10; i++) + { + try + { + if (tiaPortal != null) + { + tiaPortal.Dispose(); + tiaPortal = null; + } + tiaPortal = new Siemens.Engineering.TiaPortal(Siemens.Engineering.TiaPortalMode.WithoutUserInterface); + tiapProject = tiaPortal.Projects.Open(new FileInfo(ProjectFile)); + } + catch (Siemens.Engineering.EngineeringSecurityException ex) + { + throw; + } + catch (Exception ex) + { + if (i == 9) + throw; + } + if (tiapProject != null) + break; + } + + + var main = new TIAOpennessProjectFolder(this) { Name = "Main" }; + ProjectStructure = main; + + //var frm = new sliver.Windows.Forms.StateBrowserForm(); + //frm.ObjectToBrowse = tiapProject; + //frm.Show(); + + foreach (var d in tiapProject.Devices) + { + if (d.TypeIdentifier != null && d.TypeIdentifier.EndsWith(".S71500")) + { + foreach (DeviceItem deviceItem in d.DeviceItems) + { + var target = ((IEngineeringServiceProvider)deviceItem).GetService(); + if (target != null && target.Software is PlcSoftware) + { + var software =(PlcSoftware)target.Software; + var fld = new TIAOpennessControllerFolder(this, software) + { + Name = software.Name, + //TiaPortalItem = software, + //Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null + }; + main.SubItems.Add(fld); + + LoadControlerFolderViaOpennessDlls(fld, software); + + } + } + + //var controller = d.DeviceItems.OfType().FirstOrDefault(); + //if (controller == null) + //{ + // var fld = new TIAOpennessProjectFolder(this) + // { + // Name = d.Name, + // TiaPortalItem = d, + // Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null + // }; + // main.SubItems.Add(fld); + + // //LoadSubDevicesViaOpennessDlls(fld, d); + //} + //else + //{ + // var fld = new TIAOpennessControllerFolder(this) + // { + // Name = d.Name, + // TiaPortalItem = d, + // Comment = d.Comment != null ? d.Comment.GetText(CultureInfo.CurrentCulture) : null + // }; + // main.SubItems.Add(fld); + + // //LoadControlerFolderViaOpennessDlls(fld, controller); + //} + } + } + } + + //internal void LoadSubDevicesViaOpennessDlls(TIAOpennessProjectFolder parent, Siemens.Engineering.HW.IHardwareObject device) + //{ + // foreach (var e in device.DeviceItems) + // { + // var fld = new TIAOpennessProjectFolder(this) + // { + // TiaPortalItem = e, + // Name = e.Name, + // Parent = parent, + // }; + // var d = e as Siemens.Engineering.HW.DeviceItem; + // //d.Elements.ToList() + + // parent.SubItems.Add(fld); + // LoadSubDevicesViaOpennessDlls(fld, e); + // } + //} + + internal void LoadControlerFolderViaOpennessDlls(TIAOpennessControllerFolder parent, PlcSoftware software) + { + var fld = new TIAOpennessProgramFolder(this, parent, software.BlockGroup.Blocks) + { + //TiaPortalItem = controller.ProgramblockFolder, + Name = "software", + Parent = parent, + }; + parent.ProgramFolder = fld; + parent.SubItems.Add(fld); + LoadSubProgramBlocksFoldersViaOpennessDlls(fld, software.BlockGroup); + + var t = (PlcTypeGroup)software.TypeGroup; + + var fld2 = new TIAOpennessPlcDatatypeFolder(this, parent, t.Types) + { + //TiaPortalItem = controller.ControllerDatatypeFolder, + Name = "data types", + Parent = parent, + }; + parent.PlcDatatypeFolder = fld2; + parent.SubItems.Add(fld2); + LoadSubPlcDatatypeFoldersViaOpennessDlls(fld2, software.TypeGroup); + + var fld3 = new TIAVarTabFolder(this, parent) + { + //TiaPortalItem = controller.ControllerTagFolder, + Name = "PLC data types", + Parent = parent, + }; + parent.VarTabFolder = fld3; + parent.SubItems.Add(fld3); + LoadSubVartabFoldersViaOpennessDlls(fld3, software.TagTableGroup); + } + + internal void LoadSubProgramBlocksFoldersViaOpennessDlls(TIAOpennessProgramFolder parent, PlcBlockGroup plcBlockGroup) + { + foreach (var e in plcBlockGroup.Groups) + { + var fld = new TIAOpennessProgramFolder(this, parent.ControllerFolder, e.Blocks) + { + //TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubProgramBlocksFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, PlcTypeSystemGroup p) + { + foreach (var e in p.Groups) + { + var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder, e.Types) + { + //TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubPlcDatatypeFoldersViaOpennessDlls(TIAOpennessPlcDatatypeFolder parent, PlcTypeUserGroup p) + { + foreach (var e in p.Groups) + { + var fld = new TIAOpennessPlcDatatypeFolder(this, parent.ControllerFolder, e.Types) + { + //TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubPlcDatatypeFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, PlcTagTableSystemGroup blockFolder) + { + foreach (var e in blockFolder.Groups) + { + var fld = new TIAVarTabFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubVartabFoldersViaOpennessDlls(fld, e); + } + } + + internal void LoadSubVartabFoldersViaOpennessDlls(TIAVarTabFolder parent, PlcTagTableUserGroup blockFolder) + { + foreach (var e in blockFolder.Groups) + { + var fld = new TIAVarTabFolder(this, parent.ControllerFolder) + { + TiaPortalItem = e, + Name = e.Name, + Parent = parent, + }; + parent.SubItems.Add(fld); + LoadSubVartabFoldersViaOpennessDlls(fld, e); + } + } + #region Parse DB UDT XML + + internal enum ParseType + { + Programm, + DataType + } + + internal static Block ParseTiaDbUdtXml(string xml, ProjectBlockInfo projectBlockInfo, TIAOpennessControllerFolder controllerFolder, ParseType parseType) + { + XElement xelement = XElement.Parse(xml); + var structure = xelement.Elements().FirstOrDefault(x => x.Name.LocalName.StartsWith("SW.")); + + var sections = structure.Element("AttributeList").Element("Interface").Elements().First(); + + var block = new TIADataBlock(); + block.Name = projectBlockInfo.Name; + + if (projectBlockInfo is TIAOpennessProjectBlockInfo) + block.BlockNumber = ((TIAOpennessProjectBlockInfo) projectBlockInfo).BlockNumber; + + if (parseType == ParseType.DataType) + block.BlockType = DataTypes.PLCBlockType.UDT; + else if (parseType == ParseType.Programm) + block.BlockType = DataTypes.PLCBlockType.DB; + + var parameterRoot = ParseTiaDbUdtSections(sections, block, controllerFolder); + + block.BlockType = DataTypes.PLCBlockType.DB; + block.Structure = parameterRoot; + + return block; + } + + internal static TIADataRow ParseTiaDbUdtSections(XElement sections, TIADataBlock block, TIAOpennessControllerFolder controllerFolder) + { + var parameterRoot = new TIADataRow("ROOTNODE", S7DataRowType.STRUCT, block); + var parameterIN = new TIADataRow("IN", S7DataRowType.STRUCT, block); + parameterIN.Parent = parameterRoot; + var parameterOUT = new TIADataRow("OUT", S7DataRowType.STRUCT, block); + parameterOUT.Parent = parameterRoot; + var parameterINOUT = new TIADataRow("IN_OUT", S7DataRowType.STRUCT, block); + parameterINOUT.Parent = parameterRoot; + var parameterSTAT = new TIADataRow("STATIC", S7DataRowType.STRUCT, block); + parameterSTAT.Parent = parameterRoot; + var parameterTEMP = new TIADataRow("TEMP", S7DataRowType.STRUCT, block); + parameterTEMP.Parent = parameterRoot; + + foreach (var xElement in sections.Elements()) + { + TIADataRow useRow = parameterRoot; + //var sectionName = xElement.Attribute("Name").Value; + //if (sectionName == "None" || sectionName == "Static") + //{ + // useRow = parameterSTAT; + // parameterRoot.Add(useRow); + //} + //else if (sectionName == "In") + //{ + // useRow = parameterIN; + // parameterRoot.Add(useRow); + //} + + parseChildren(useRow, xElement, controllerFolder); + } + + return parameterRoot; + } + + internal static void parseChildren(TIADataRow parentRow, XElement parentElement, TIAOpennessControllerFolder controllerFolder) + { + foreach (var xElement in parentElement.Elements()) + { + if (xElement.Name.LocalName == "Comment") + { + var text = xElement.Elements().FirstOrDefault(x => x.Attribute("Lang").Value == "de-DE"); + if (text == null) + text = xElement.Elements().FirstOrDefault(); + if (text != null) + parentRow.Comment = text.Value; + } + else if (xElement.Name.LocalName == "StartValue") + { + parentRow.StartValue = xElement.Value; + } + else if (xElement.Name.LocalName == "Sections") + { + var row = ParseTiaDbUdtSections(xElement, (TIADataBlock)parentRow.CurrentBlock, controllerFolder); + parentRow.AddRange(row.Children); + } + else if (xElement.Name.LocalName == "Member") + { + var name = xElement.Attribute("Name").Value; + var datatype = xElement.Attribute("Datatype").Value; + + var row = new TIADataRow(name, S7DataRowType.STRUCT, (TIABlock)parentRow.PlcBlock); + row.Parent = parentRow; + + if (datatype.Contains("Array[")) + { + List arrayStart = new List(); + List arrayStop = new List(); + + int pos1 = datatype.IndexOf("["); + int pos2 = datatype.IndexOf("]", pos1); + string[] arrays = datatype.Substring(pos1 + 1, pos2 - pos1 - 1).Split(','); + + foreach (string array in arrays) + { + string[] akar = array.Split(new string[] { ".." }, StringSplitOptions.RemoveEmptyEntries); + int start = 0; + if (akar[0].StartsWith("\"")) + { + start = (int)controllerFolder.VarTabFolder.FindConstant(akar[0].Substring(1, akar[0].Length - 2)).Value; + } + else + { + start = Convert.ToInt32(akar[0].Trim()); + } + + int stop = 0; + if (akar[1].StartsWith("\"")) + { + stop = (int)controllerFolder.VarTabFolder.FindConstant(akar[1].Substring(1, akar[1].Length - 2)).Value; + } + else + { + stop = Convert.ToInt32(akar[1].Trim()); + } + + arrayStart.Add(start); + arrayStop.Add(stop); + } + + row.ArrayStart = arrayStart; + row.ArrayStop = arrayStop; + row.IsArray = true; + datatype = datatype.Substring(pos2 + 5); + } + + parentRow.Add(row); + + parseChildren(row, xElement, controllerFolder); + + if (datatype.StartsWith("\"")) + { + var udt = + controllerFolder.PlcDatatypeFolder.GetBlock(datatype.Substring(1, datatype.Length - 2)); + if (udt != null) + { + var tiaUdt = udt as TIADataBlock; + row.AddRange(((TIADataRow)tiaUdt.Structure).DeepCopy().Children); + + row.DataTypeBlock = udt; + } + row.DataType = S7DataRowType.UDT; + } + else if (datatype == "Struct") + { + + } + else if (datatype.StartsWith("String[")) + { + row.DataType = S7DataRowType.STRING; + row.StringSize = int.Parse(datatype.Substring(7, datatype.Length - 8)); + } + else + { + switch (datatype.ToLower()) + { + case "byte": + row.DataType = S7DataRowType.BYTE; + break; + case "bool": + row.DataType = S7DataRowType.BOOL; + break; + case "int": + row.DataType = S7DataRowType.INT; + break; + case "uint": + row.DataType = S7DataRowType.WORD; + break; + case "dint": + row.DataType = S7DataRowType.DINT; + break; + case "udint": + row.DataType = S7DataRowType.DWORD; + break; + case "word": + row.DataType = S7DataRowType.WORD; + break; + case "dword": + row.DataType = S7DataRowType.DWORD; + break; + case "char": + row.DataType = S7DataRowType.CHAR; + break; + case "any": + row.DataType = S7DataRowType.ANY; + break; + case "date": + row.DataType = S7DataRowType.DATE; + break; + case "date_and_time": + row.DataType = S7DataRowType.DATE_AND_TIME; + break; + case "real": + row.DataType = S7DataRowType.REAL; + break; + case "s5time": + row.DataType = S7DataRowType.S5TIME; + break; + case "time_of_day": + row.DataType = S7DataRowType.TIME_OF_DAY; + break; + case "time": + row.DataType = S7DataRowType.TIME; + break; + default: + row.DataType = S7DataRowType.UNKNOWN; + + var udt = controllerFolder.PlcDatatypeFolder.GetBlock(datatype); + if (udt != null) + { + var tiaUdt = udt as TIADataBlock; + row.AddRange(((TIADataRow)tiaUdt.Structure).DeepCopy().Children); + + row.DataTypeBlock = udt; + row.DataType = S7DataRowType.UDT; + } + + //Console.WriteLine("unkown Datatype: " + datatype); + break; + } + } + } + else if (xElement.Name.LocalName == "AttributeList") + { } + else if (xElement.Name.LocalName == "Subelement") //todo -> startwerte von arrays von UDTs + { } + else + { + //Console.WriteLine("unkown XML Element"); + } + } + } + #endregion + } +} diff --git a/DotNetSiemensPLCToolBoxLibrary.sln b/DotNetSiemensPLCToolBoxLibrary.sln index c3831e1c..aa783ae2 100644 --- a/DotNetSiemensPLCToolBoxLibrary.sln +++ b/DotNetSiemensPLCToolBoxLibrary.sln @@ -78,6 +78,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TiaExImPorter", "TiaExImPor {AB0895AE-446F-4CD8-96AB-C8007D618593} = {AB0895AE-446F-4CD8-96AB-C8007D618593} EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToolReader", "ReadTools\ToolReader.csproj", "{C0C1142F-592C-4F26-9655-A737C74DAF7E}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TiaGitHandler", "TiaGitHandler\TiaGitHandler.csproj", "{147CD31F-F449-4568-AF62-56B987851D26}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetSiemensPLCToolBoxLibrary.TIAV14SP1", "DotNetSiemensPLCToolBoxLibrary.TIAV14SP1\DotNetSiemensPLCToolBoxLibrary.TIAV14SP1.csproj", "{AB0895AE-446F-4CD8-96AB-C8007D618593}" @@ -478,6 +480,20 @@ Global {E2169C6E-6606-41FF-8ACB-485151574872}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {E2169C6E-6606-41FF-8ACB-485151574872}.Release|Mixed Platforms.Build.0 = Release|Any CPU {E2169C6E-6606-41FF-8ACB-485151574872}.Release|x86.ActiveCfg = Release|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Debug|iPhone.ActiveCfg = Debug|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Debug|x86.ActiveCfg = Debug|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Release|Any CPU.Build.0 = Release|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Release|iPhone.ActiveCfg = Release|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E}.Release|x86.ActiveCfg = Release|Any CPU {147CD31F-F449-4568-AF62-56B987851D26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {147CD31F-F449-4568-AF62-56B987851D26}.Debug|Any CPU.Build.0 = Debug|Any CPU {147CD31F-F449-4568-AF62-56B987851D26}.Debug|iPhone.ActiveCfg = Debug|Any CPU @@ -590,6 +606,7 @@ Global {E08F71C6-E98F-41BB-BA6B-056FD08D0EB4} = {A4F11331-531B-4A7C-84B2-E319658AE2B8} {A7D1B64C-7045-4B32-9AD7-7649E40512C7} = {A4F11331-531B-4A7C-84B2-E319658AE2B8} {E2169C6E-6606-41FF-8ACB-485151574872} = {A4F11331-531B-4A7C-84B2-E319658AE2B8} + {C0C1142F-592C-4F26-9655-A737C74DAF7E} = {A4F11331-531B-4A7C-84B2-E319658AE2B8} {147CD31F-F449-4568-AF62-56B987851D26} = {A4F11331-531B-4A7C-84B2-E319658AE2B8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution diff --git a/LibNoDaveConnectionLibrary/Communication/Editors/C_PropertyNck.cs b/LibNoDaveConnectionLibrary/Communication/Editors/C_PropertyNck.cs deleted file mode 100644 index 31721edd..00000000 --- a/LibNoDaveConnectionLibrary/Communication/Editors/C_PropertyNck.cs +++ /dev/null @@ -1,168 +0,0 @@ -using System; -using System.ComponentModel; -using System.Globalization; - -namespace DotNetSiemensPLCToolBoxLibrary.Communication -{ - class C_PropertyNck - { - private byte _SYNTAX_ID; - private byte _bereich_u_einheit; - private uint _spalte; - private uint _zeile; - private byte _bausteintyp; - private byte _ZEILENANZAHL; - private byte _typ; - private byte _laenge; - - #region NCK - [CategoryAttribute("PlcNckTag")] - [TypeConverter(typeof(ByteHexTypeConverter))] - public byte SYNTAX_ID - { - get { return _SYNTAX_ID; } - set { _SYNTAX_ID = value; } - } - - [CategoryAttribute("PlcNckTag")] - [TypeConverter(typeof(ByteHexTypeConverter))] - public byte bereich_u_einheit - { - get { return _bereich_u_einheit; } - set { _bereich_u_einheit = value; } - } - - [CategoryAttribute("PlcNckTag")] - [TypeConverter(typeof(uintHexTypeConverter))] - public uint spalte - { - get { return _spalte; } - set { _spalte = value; } - } - - [CategoryAttribute("PlcNckTag")] - [TypeConverter(typeof(uintHexTypeConverter))] - public uint zeile - { - get { return _zeile; } - set { _zeile = value; } - } - - [CategoryAttribute("PlcNckTag")] - [TypeConverter(typeof(ByteHexTypeConverter))] - public byte bausteintyp - { - get { return _bausteintyp; } - set { _bausteintyp = value; } - } - - [CategoryAttribute("PlcNckTag")] - [TypeConverter(typeof(ByteHexTypeConverter))] - public byte ZEILENANZAHL - { - get { return _ZEILENANZAHL; } - set { _ZEILENANZAHL = value; } - } - - [CategoryAttribute("PlcNckTag")] - [TypeConverter(typeof(ByteHexTypeConverter))] - public byte typ - { - get { return _typ; } - set { _typ = value; } - } - - [CategoryAttribute("PlcNckTag")] - [TypeConverter(typeof(ByteHexTypeConverter))] - public byte laenge - { - get { return _laenge; } - set { _laenge = value; } - } - #endregion - } - - //public class CustomFormatter : IFormatProvider, ICustomFormatter - //{ - // // implementing the GetFormat method of the IFormatProvider interface - // public object GetFormat(System.Type type) - // { - // return this; - // } - // // implementing the Format method of the ICustomFormatter interface - // public string Format(string format, object arg, IFormatProvider formatProvider) - // { - // return string.Format("{0:X}", Convert.ToInt32(arg)); - // } - //} - - public class ByteHexTypeConverter : TypeConverter - { - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - return sourceType == typeof(string); - } - - public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) - { - if (value is string) - { - string s = (string)value; - if (s.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) - { - s = s.Substring(2); - - return Byte.Parse(s, NumberStyles.HexNumber, culture); - } - return Byte.Parse(s, NumberStyles.AllowThousands, culture); - //byte s = (byte)value; - //return value.ToString("X"); - } - - return base.ConvertFrom(context, culture, value); - } - - public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) - { - if (destinationType == typeof(string) && value.GetType() == typeof(byte)) - return "0x" + ((byte)value).ToString("X2", culture); - - return base.ConvertTo(context, culture, value, destinationType); - } - } - - public class uintHexTypeConverter : TypeConverter - { - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - return sourceType == typeof(string); - } - - public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) - { - if (value is string) - { - string s = (string)value; - if (s.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) - { - s = s.Substring(2); - - return uint.Parse(s, NumberStyles.HexNumber, culture); - } - return uint.Parse(s, NumberStyles.AllowThousands, culture); - //byte s = (byte)value; - //return value.ToString("X"); - } - - return base.ConvertFrom(context, culture, value); - } - - public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) - { - if (destinationType == typeof(string) && value.GetType() == typeof(uint)) - return "0x" + ((uint)value).ToString("X4", culture); - - return base.ConvertTo(context, culture, value, destinationType); - } - } -} diff --git a/LibNoDaveConnectionLibrary/Communication/Editors/NckTagEditor.cs b/LibNoDaveConnectionLibrary/Communication/Editors/NckTagEditor.cs index 8f02766d..95744f32 100644 --- a/LibNoDaveConnectionLibrary/Communication/Editors/NckTagEditor.cs +++ b/LibNoDaveConnectionLibrary/Communication/Editors/NckTagEditor.cs @@ -21,7 +21,7 @@ internal NckTagEditor() InitializeComponent(); } - private C_PropertyNck pNCK = new C_PropertyNck(); + private NC_Var ncVar = new NC_Var(); private PLCNckTag _libnodavevalue; private bool startWasNull = false; @@ -29,14 +29,7 @@ private void LibNoDaveValueEditor_Load(object sender, EventArgs e) { if (_libnodavevalue != null) { - pNCK.SYNTAX_ID = 0x82; - pNCK.bereich_u_einheit = (byte)(_libnodavevalue.NckArea << 5 | _libnodavevalue.NckUnit); - pNCK.spalte = (uint)_libnodavevalue.NckColumn; - pNCK.zeile = (uint)_libnodavevalue.NckLine; - pNCK.bausteintyp = (byte)_libnodavevalue.NckModule; - pNCK.ZEILENANZAHL = (byte)_libnodavevalue.NckLinecount; - pNCK.typ = getType(_libnodavevalue.TagDataType); - pNCK.laenge = (byte)_libnodavevalue._internalGetSize(); + ncVar = new NC_Var(_libnodavevalue); } else { @@ -44,89 +37,12 @@ private void LibNoDaveValueEditor_Load(object sender, EventArgs e) _libnodavevalue = new PLCNckTag(); } - pGridNCK.SelectedObject = pNCK; + pGridNCK.SelectedObject = ncVar; } - - private byte getType(TagDataType type) - { - switch (type) - { - case TagDataType.Bool: - return 1; - case TagDataType.Byte: - return 3; - case TagDataType.Word: - return 4; - case TagDataType.Int: - return 5; - case TagDataType.Dword: - return 6; - case TagDataType.Dint: - return 7; - case TagDataType.Float: - return 8; - case TagDataType.LReal: - return 15; - case TagDataType.LInt: - return 18; - case TagDataType.String: - case TagDataType.CharArray: - return 19; - default: - return 0; - } - } - + private void OK_Click(object sender, EventArgs e) { - byte _bereich = (byte)((pNCK.bereich_u_einheit & 0xE0) >> 5); // (bereich_u_einheit & 2#11100000) schiebe rechts um 5 Bit - byte _einheit = (byte)(pNCK.bereich_u_einheit & 0x1F); // & 2#00011111 - - #region TYP - TagDataType dataType = new TagDataType(); - int _ArraySize = 0; - switch (pNCK.typ) - { - case 1: - dataType = TagDataType.Bool; - break; - case 3: - dataType = TagDataType.Byte; //eNCK_LE_Int8; - break; - case 4: - dataType = TagDataType.Word; - break; - case 5: - dataType = TagDataType.Int; //eNCK_LE_Int16; - break; - case 6: - dataType = TagDataType.Dword; //eNCK_LE_Uint32; - break; - case 7: - dataType = TagDataType.Dint; //eNCK_LE_Int32; - break; - case 8: - dataType = TagDataType.Float; //eNCK_LE_Float32; - break; - case 15: - dataType = TagDataType.LReal; //eNCK_LE_Float64; - break; - case 18: - dataType = TagDataType.LInt; //eNCK_LE_Int64; - break; - case 19: - if (_bereich == 2) - dataType = TagDataType.String; //eNCK_LE_String; - else - dataType = TagDataType.CharArray; //eNCK_LE_String; - _ArraySize = pNCK.laenge; - break; - default: - throw new Exception("Unknown Type"); - } - #endregion - - _libnodavevalue = new PLCNckTag() { TagDataType = dataType, NckArea = _bereich, NckUnit = _einheit, NckColumn = (int)pNCK.spalte, NckLine = (int)pNCK.zeile, NckModule = pNCK.bausteintyp, NckLinecount = pNCK.ZEILENANZAHL, ArraySize = _ArraySize }; + _libnodavevalue = ncVar.GetNckTag(); this.Close(); } diff --git a/LibNoDaveConnectionLibrary/Communication/LibNoDave/IDaveConnection.cs b/LibNoDaveConnectionLibrary/Communication/LibNoDave/IDaveConnection.cs index 70dfd766..8cc49660 100644 --- a/LibNoDaveConnectionLibrary/Communication/LibNoDave/IDaveConnection.cs +++ b/LibNoDaveConnectionLibrary/Communication/LibNoDave/IDaveConnection.cs @@ -45,6 +45,9 @@ public interface IDaveConnection int doUploadNC(out int more, byte[] buffer, out int len, byte[] uploadID); int endUploadNC(byte[] uploadID); int daveGetNCProgram(string filename, byte[] buffer, ref int length); + int daveGetNcFile(string filename, byte[] buffer, ref int length); + int daveGetNcFileSize(string filename, ref int length); int davePutNCProgram(string filename, string path, string ts, byte[] buffer, int length); + int alarmQueryAlarm_S(byte[] buffer, int length, ref int alarmCount); } } \ No newline at end of file diff --git a/LibNoDaveConnectionLibrary/Communication/LibNoDave/libnodave.net.cs b/LibNoDaveConnectionLibrary/Communication/LibNoDave/libnodave.net.cs index 00d093c4..fb795acd 100644 --- a/LibNoDaveConnectionLibrary/Communication/LibNoDave/libnodave.net.cs +++ b/LibNoDaveConnectionLibrary/Communication/LibNoDave/libnodave.net.cs @@ -172,8 +172,8 @@ public static extern string private static extern IntPtr _daveStrerror64(int res); -#if !IPHONE - [DllImport("libnodave_jfkmod.dll", EntryPoint="daveStrerror" )] +#if !IPHONE + [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveStrerror")] #else [DllImport("__Internal", EntryPoint = "daveStrerror")] #endif @@ -283,14 +283,14 @@ Max number of bytes in a single message. set and read debug level: */ -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod64.dll", EntryPoint = "daveSetDebug")] #else [DllImport("__Internal", EntryPoint = "daveSetDebug")] #endif public static extern void daveSetDebug64(int newDebugLevel); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveSetDebug")] #else [DllImport("__Internal", EntryPoint = "daveSetDebug")] @@ -313,7 +313,7 @@ public static void daveSetDebug(int newDebugLevel) #endif public static extern int daveGetDebug64(); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveGetDebug")] #else [DllImport("__Internal", EntryPoint = "daveGetDebug")] @@ -351,10 +351,10 @@ public class pseudoPointer #else [DllImport("__Internal", EntryPoint = "daveFree")] #endif - protected static extern int daveFree64(IntPtr p); + protected static extern int daveFree64(IntPtr p); -#if !IPHONE - [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveFree")] +#if !IPHONE + [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveFree")] #else [DllImport("__Internal", EntryPoint = "daveFree")] #endif @@ -633,7 +633,7 @@ public daveConnection(daveInterface di, int MPI, string IP, bool DestinationIsIP #endif protected static extern int daveConnectPLC64(IntPtr dc); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveConnectPLC")] #else [DllImport("__Internal", EntryPoint = "daveConnectPLC")] @@ -654,7 +654,7 @@ public int connectPLC() #endif protected static extern int daveDisconnectPLC64(IntPtr dc); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveDisconnectPLC")] #else [DllImport("__Internal", EntryPoint = "daveDisconnectPLC")] @@ -674,7 +674,7 @@ public int disconnectPLC() #endif protected static extern int daveReadBytes64(IntPtr dc, int area, int DBnumber, int start, int len, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveReadBytes")] #else [DllImport("__Internal", EntryPoint = "daveReadBytes")] @@ -727,7 +727,7 @@ public int readManyBytes(int area, int DBnumber, int start, int len, ref byte[] #endif protected static extern int daveReadBits64(IntPtr dc, int area, int DBnumber, int start, int len, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveReadBits")] #else [DllImport("__Internal", EntryPoint = "daveReadBits")] @@ -747,7 +747,7 @@ public int readBits(int area, int DBnumber, int start, int len, byte[] buffer) #endif protected static extern int daveWriteBytes64(IntPtr dc, int area, int DBnumber, int start, int len, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveWriteBytes")] #else [DllImport("__Internal", EntryPoint = "daveWriteBytes")] @@ -767,7 +767,7 @@ public int writeBytes(int area, int DBnumber, int start, int len, byte[] buffer) #endif protected static extern int daveWriteManyBytes64(IntPtr dc, int area, int DBnumber, int start, int len, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveWriteManyBytes")] #else [DllImport("__Internal", EntryPoint = "daveWriteManyBytes")] @@ -787,7 +787,7 @@ public int writeManyBytes(int area, int DBnumber, int start, int len, byte[] buf #endif protected static extern int daveWriteBits64(IntPtr dc, int area, int DBnumber, int start, int len, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveWriteBits")] #else [DllImport("__Internal", EntryPoint = "daveWriteBits")] @@ -807,7 +807,7 @@ public int writeBits(int area, int DBnumber, int start, int len, byte[] buffer) #endif protected static extern int daveBuildAndSendPDU64(IntPtr dc, IntPtr p, byte[] b1, int l1, byte[] b2, int l2); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveBuildAndSendPDU")] #else [DllImport("__Internal", EntryPoint = "daveBuildAndSendPDU")] @@ -818,7 +818,7 @@ public int daveBuildAndSendPDU(IPDU myPDU, byte[] Parameter, byte[] Data) int res = 0; if (IntPtr.Size == 8) if (Data == null) - res = daveBuildAndSendPDU64(pointer, myPDU.pointer, Parameter, Parameter.Length, null,0); + res = daveBuildAndSendPDU64(pointer, myPDU.pointer, Parameter, Parameter.Length, null, 0); else res = daveBuildAndSendPDU64(pointer, myPDU.pointer, Parameter, Parameter.Length, Data, Data.Length); @@ -857,7 +857,7 @@ public int getU8() #endif protected static extern int daveGetAnswLen64(IntPtr dc); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveGetAnswLen")] #else [DllImport("__Internal", EntryPoint = "daveGetAnswLen")] @@ -877,7 +877,7 @@ public int getAnswLen() #endif protected static extern int daveGetMaxPDULen64(IntPtr dc); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveGetMaxPDULen")] #else [DllImport("__Internal", EntryPoint = "daveGetMaxPDULen")] @@ -897,7 +897,7 @@ public int getMaxPDULen() #endif protected static extern int davePrepareReadRequest64(IntPtr dc, IntPtr p); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "davePrepareReadRequest")] #else [DllImport("__Internal", EntryPoint = "davePrepareReadRequest")] @@ -920,7 +920,7 @@ public IPDU prepareReadRequest() #endif protected static extern int davePrepareWriteRequest64(IntPtr dc, IntPtr p); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "davePrepareWriteRequest")] #else [DllImport("__Internal", EntryPoint = "davePrepareWriteRequest")] @@ -948,7 +948,7 @@ public IPDU createPDU() #endif protected static extern int daveExecReadRequest64(IntPtr dc, IntPtr p, IntPtr rl); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveExecReadRequest")] #else [DllImport("__Internal", EntryPoint = "daveExecReadRequest")] @@ -969,7 +969,7 @@ public int execReadRequest(IPDU p, IresultSet rl) #endif protected static extern int daveExecWriteRequest64(IntPtr dc, IntPtr p, IntPtr rl); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveExecWriteRequest")] #else [DllImport("__Internal", EntryPoint = "daveExecWriteRequest")] @@ -989,7 +989,7 @@ public int execWriteRequest(IPDU p, IresultSet rl) #endif protected static extern int daveUseResult64(IntPtr dc, IntPtr rs, int number, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveUseResult")] #else [DllImport("__Internal", EntryPoint = "daveUseResult")] @@ -1028,7 +1028,7 @@ public int useResultBuffer(IresultSet rs, int number, byte[] buffer) [DllImport("__Internal", EntryPoint = "daveReadSZL")] #endif protected static extern int daveReadSZL64(IntPtr dc, int id, int index, byte[] buffer, int len); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveReadSZL")] #else [DllImport("__Internal", EntryPoint = "daveReadSZL")] @@ -1048,7 +1048,7 @@ public int readSZL(int id, int index, byte[] buffer) [DllImport("__Internal", EntryPoint = "daveStart")] #endif protected static extern int daveStart64(IntPtr dc); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveStart")] #else [DllImport("__Internal", EntryPoint = "daveStart")] @@ -1069,7 +1069,7 @@ public int start() [DllImport("__Internal", EntryPoint = "daveStop")] #endif protected static extern int daveStop64(IntPtr dc); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveStop")] #else [DllImport("__Internal", EntryPoint = "daveStop")] @@ -1090,7 +1090,7 @@ public int stop() [DllImport("__Internal", EntryPoint = "daveForce200")] #endif protected static extern int daveForce200_64(IntPtr dc, int area, int start, int val); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveForce200")] #else [DllImport("__Internal", EntryPoint = "daveForce200")] @@ -1113,7 +1113,7 @@ public int force200(int area, int start, int val) #endif protected static extern int daveForceDisconnectIBH64(IntPtr dc, int src, int dest, int MPI); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveForceDisconnectIBH")] #else [DllImport("__Internal", EntryPoint = "daveForceDisconnectIBH")] @@ -1134,7 +1134,7 @@ public int forceDisconnectIBH(int src, int dest, int MPI) #endif protected static extern int daveResetIBH64(IntPtr dc); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveResetIBH")] #else [DllImport("__Internal", EntryPoint = "daveResetIBH")] @@ -1156,7 +1156,7 @@ public int resetIBH() #endif protected static extern int daveGetResponse64(IntPtr dc); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveGetResponse")] #else [DllImport("__Internal", EntryPoint = "daveGetResponse")] @@ -1177,7 +1177,7 @@ public int getGetResponse() #endif protected static extern int daveSendMessage64(IntPtr dc, IntPtr p); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveSendMessage")] #else [DllImport("__Internal", EntryPoint = "daveSendMessage")] @@ -1198,7 +1198,7 @@ public int getMessage(IPDU p) #endif protected static extern int daveGetProgramBlock64(IntPtr dc, int blockType, int number, byte[] buffer, ref int length); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveGetProgramBlock")] #else [DllImport("__Internal", EntryPoint = "daveGetProgramBlock")] @@ -1219,7 +1219,7 @@ public int getProgramBlock(int blockType, int number, byte[] buffer, ref int len #endif protected static extern int davePutProgramBlock64(IntPtr dc, int blockType, int number, byte[] buffer, int length); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "davePutProgramBlock")] #else [DllImport("__Internal", EntryPoint = "davePutProgramBlock")] @@ -1240,7 +1240,7 @@ public int putProgramBlock(int blockType, int number, byte[] buffer, int length) #endif protected static extern int daveDeleteProgramBlock64(IntPtr dc, int blockType, int number); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveDeleteProgramBlock")] #else [DllImport("__Internal", EntryPoint = "daveDeleteProgramBlock")] @@ -1261,7 +1261,7 @@ public int deleteProgramBlock(int blockType, int number) #endif protected static extern int daveListBlocksOfType64(IntPtr dc, int blockType, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveListBlocksOfType")] #else [DllImport("__Internal", EntryPoint = "daveListBlocksOfType")] @@ -1282,7 +1282,7 @@ public int ListBlocksOfType(int blockType, byte[] buffer) #endif protected static extern int daveSetPLCTime64(IntPtr dc, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveSetPLCTime")] #else [DllImport("__Internal", EntryPoint = "daveSetPLCTime")] @@ -1372,7 +1372,7 @@ public int daveReadPLCTime(out DateTime dateTime) #endif protected static extern int daveGetPDUData64(IntPtr dc, IntPtr p, byte[] data, ref int ldata, byte[] param, ref int lparam); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveGetPDUData")] #else [DllImport("__Internal", EntryPoint = "daveGetPDUData")] @@ -1404,7 +1404,7 @@ public int daveGetPDUData(IPDU myPDU, out byte[] data, out byte[] param) #endif private static extern int _daveSetupReceivedPDU64(IntPtr dc, IntPtr p); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "_daveSetupReceivedPDU")] #else [DllImport("__Internal", EntryPoint = "_daveSetupReceivedPDU")] @@ -1503,6 +1503,54 @@ public int daveGetNCProgram(string filename, byte[] buffer, ref int length) } #endregion + #region daveGetNCFile +#if !IPHONE + [DllImport("libnodave_jfkmod64.dll", EntryPoint = "daveGetNcFile")] +#else + [DllImport("__Internal", EntryPoint = "daveGetNcFile")] +#endif + protected static extern int daveGetNcFile64(IntPtr dc, string filename, byte[] buffer, ref int length); + +#if !IPHONE + [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveGetNcFile")] +#else + [DllImport("__Internal", EntryPoint = "daveGetNcFile")] +#endif + protected static extern int daveGetNcFile32(IntPtr dc, string filename, byte[] buffer, ref int length); + + public int daveGetNcFile(string filename, byte[] buffer, ref int length) + { + if (IntPtr.Size == 8) + return daveGetNcFile64(pointer, filename, buffer, ref length); + else + return daveGetNcFile32(pointer, filename, buffer, ref length); + } + #endregion + + #region daveGetNCFileSize +#if !IPHONE + [DllImport("libnodave_jfkmod64.dll", EntryPoint = "daveGetNcFileSize")] +#else + [DllImport("__Internal", EntryPoint = "daveGetNcFileSize")] +#endif + protected static extern int daveGetNcFileSize64(IntPtr dc, string filename, ref int length); + +#if !IPHONE + [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveGetNcFileSize")] +#else + [DllImport("__Internal", EntryPoint = "daveGetNcFileSize")] +#endif + protected static extern int daveGetNcFileSize32(IntPtr dc, string filename, ref int length); + + public int daveGetNcFileSize(string filename, ref int length) + { + if (IntPtr.Size == 8) + return daveGetNcFileSize64(pointer, filename, ref length); + else + return daveGetNcFileSize32(pointer, filename, ref length); + } + #endregion + #region davePutNCProgram #if !IPHONE [DllImport("libnodave_jfkmod64.dll", EntryPoint = "davePutNCProgram")] @@ -1604,6 +1652,32 @@ public int endUploadNC(byte[] uploadID) } #endregion #endregion + + #region Read SPS Alarm query + #region alarmQueryAlarm_S +#if !IPHONE + [DllImport("libnodave_jfkmod64.dll", EntryPoint = "alarmQueryAlarm_S")] +#else + [DllImport("__Internal", EntryPoint = "alarmQueryAlarm_S")] +#endif + protected static extern int alarmQueryAlarm_S_64(IntPtr dc, byte[] buffer, int length, ref int alarmCount); + +#if !IPHONE + [DllImport("libnodave_jfkmod.dll", EntryPoint = "alarmQueryAlarm_S")] +#else + [DllImport("__Internal", EntryPoint = "alarmQueryAlarm_S")] +#endif + protected static extern int alarmQueryAlarm_S_32(IntPtr dc, byte[] buffer, int length, ref int alarmCount); + + public int alarmQueryAlarm_S(byte[] buffer, int length, ref int alarmCount) + { + if (IntPtr.Size == 8) + return alarmQueryAlarm_S_64(pointer, buffer, length, ref alarmCount); + else + return alarmQueryAlarm_S_32(pointer, buffer, length, ref alarmCount); + } + #endregion + #endregion } public class PDU : pseudoPointer, IPDU @@ -1615,7 +1689,7 @@ public class PDU : pseudoPointer, IPDU #endif protected static extern IntPtr daveNewPDU64(); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveNewPDU")] #else [DllImport("__Internal", EntryPoint = "daveNewPDU")] @@ -1637,7 +1711,7 @@ public PDU() #endif protected static extern void daveAddVarToReadRequest64(IntPtr p, int area, int DBnum, int start, int bytes); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveAddVarToReadRequest")] #else [DllImport("__Internal", EntryPoint = "daveAddVarToReadRequest")] @@ -1713,7 +1787,7 @@ public void addSymbolVarToReadRequest(string completeSymbol) else daveAddSymbolVarToReadRequest32(pointer, bytes, bytes.Length); } - + #if !IPHONE [DllImport("libnodave_jfkmod64.dll", EntryPoint = "daveAddFillByteToReadRequest")] #else @@ -1734,7 +1808,7 @@ public void daveAddFillByteToReadRequest() else daveAddFillByteToReadRequest32(pointer); } - + #if !IPHONE [DllImport("libnodave_jfkmod64.dll", EntryPoint = "daveAddDbRead400ToReadRequest")] #else @@ -1763,7 +1837,7 @@ public void addDbRead400ToReadRequest(int DBnum, int offset, int byteCount) #endif protected static extern void daveAddBitVarToReadRequest64(IntPtr p, int area, int DBnum, int start, int bytes); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveAddBitVarToReadRequest")] #else [DllImport("__Internal", EntryPoint = "daveAddBitVarToReadRequest")] @@ -1784,7 +1858,7 @@ public void addBitVarToReadRequest(int area, int DBnum, int start, int bytes) #endif protected static extern void daveAddVarToWriteRequest64(IntPtr p, int area, int DBnum, int start, int bytes, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveAddVarToWriteRequest")] #else [DllImport("__Internal", EntryPoint = "daveAddVarToWriteRequest")] @@ -1805,7 +1879,7 @@ public void addVarToWriteRequest(int area, int DBnum, int start, int bytes, byte #endif protected static extern void daveAddBitVarToWriteRequest64(IntPtr p, int area, int DBnum, int start, int bytes, byte[] buffer); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "daveAddBitVarToWriteRequest")] #else [DllImport("__Internal", EntryPoint = "daveAddBitVarToWriteRequest")] @@ -1918,7 +1992,7 @@ protected static extern IntPtr openSocket64( [MarshalAs(UnmanagedType.LPStr)] string portName ); -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll", EntryPoint = "openSocket")] #else [DllImport("__Internal", EntryPoint = "openSocket")] @@ -1935,7 +2009,7 @@ public static IntPtr openSocket(int port, string portName) return openSocket32(port, portName); } -#if !IPHONE +#if !IPHONE [DllImport("libnodave_jfkmod.dll"/*, PreserveSig=false */ )] public static extern IntPtr openS7online( [MarshalAs(UnmanagedType.LPStr)] string portName, @@ -2331,14 +2405,14 @@ public static float getS5Floatfrom(byte[] b, int pos) var exp = (b[pos + 0]);// & 0x7f); - if (sign>0) + if (sign > 0) { mantissa = mantissa ^ 0xffffffff; mantissa = mantissa + 0x00800000; wrt = mantissa; wrt = -wrt; } - + /*exp = (byte)(exp >> 1); @@ -2347,7 +2421,7 @@ public static float getS5Floatfrom(byte[] b, int pos) wrt = wrt * 2.0f; exp--; }*/ - + while (exp > 23) { wrt = wrt * 2.0f; @@ -2415,7 +2489,7 @@ public static void putS7Stringat(byte[] b, int pos, string value, int length) b[pos] = (byte)length; b[pos + 1] = length > value.Length ? (byte)value.Length : (byte)length; //Array.Copy(Encoding.ASCII.GetBytes(value), 0, b, pos + 2, value.Length > length ? length : value.Length); - Array.Copy(Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage).GetBytes(value), 0, b, pos + 2, value.Length > length ? length : value.Length); + Array.Copy(Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage).GetBytes(value), 0, b, pos + 2, value.Length > length ? length : value.Length); } /// @@ -2428,7 +2502,7 @@ public static void putS7Stringat(byte[] b, int pos, string value, int length) public static void putStringat(byte[] b, int pos, string value, int length) { //Array.Copy(Encoding.ASCII.GetBytes(value), 0, b, pos, value.Length > length ? length : value.Length); - Array.Copy(Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage).GetBytes(value), 0, b, pos, value.Length > length ? length : value.Length); + Array.Copy(Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage).GetBytes(value), 0, b, pos, value.Length > length ? length : value.Length); } public static void putFloatat(byte[] b, int pos, Single value) @@ -2470,7 +2544,7 @@ public static int getBCD8from(byte[] b, int pos) bool neg = (bt1 & 0xf0) == 0xf0 ? true : false; if (neg) { - bt1 = -1*(bt1 & 0x0f); + bt1 = -1 * (bt1 & 0x0f); //bt1 = 0; } else diff --git a/LibNoDaveConnectionLibrary/Communication/Library/TcpNETdave.cs b/LibNoDaveConnectionLibrary/Communication/Library/TcpNETdave.cs index 9545c8b8..c30f1de4 100644 --- a/LibNoDaveConnectionLibrary/Communication/Library/TcpNETdave.cs +++ b/LibNoDaveConnectionLibrary/Communication/Library/TcpNETdave.cs @@ -23,7 +23,7 @@ public class resultN public int error; public byte[] bytes; } - public class daveResultN :IresultSet + public class daveResultN : IresultSet { public List allResults; public daveResultN() @@ -94,64 +94,64 @@ public int connectPLC() 9 // requested TPDU-Size 8=256 Bytes, 9=512 Bytes , a=1024 Bytes }; - /* byte[] b4R ={ // for routing - 6 + 30 + 30 + 3, // Length over all without this byte (fixed - // Data 6 Bytes + size of Parameters (3 for C0h,30 for C1h+C2h) - - 0xE0, // TDPU Type CR = Connection Request (see RFC1006/ISO8073) - 0x00,0x00, // TPDU Destination Reference (unknown) - 0x00,0x01, // TPDU Source-Reference (my own reference, should not be zero) - 0x00, // TPDU Class 0 and no Option - - 0xC1, // Parameter Source-TSAP - 28, // Length of this parameter - 1, // one block of data (???) - 0, // Length for S7-Subnet-ID - 0, // Length of PLC-Number - 2, // Length of Function/Rack/Slot - 0,0,0,0,0,0,0,0, // empty Data - 0,0,0,0,0,0,0,0, - 0,0,0,0,0,0, - (byte)config.ConnectionType, // Function (1=PG,2=OP,3=Step7Basic) - (byte)(slot + rack * 32), // Rack (Bit 7-5) and Slot (Bit 4-0) - - 0xC2, // Parameter Destination-TSAP - 28, // Length of this parameter - 1, // one block of data (???) - 6, // Length for S7-Subnet-ID - dc->_routingDestinationSize, // Length of PLC-Number - 04 if you use a IP as Destination! - 2, // Length of Function/Rack/Slot - - 0,//(unsigned char) (dc->routingSubnetFirst >> 8), (unsigned char) dc->routingSubnetFirst, // first part of S7-Subnet-ID - // (look into the S7Project/Network configuration) - 0x00,0x00, // fix always 0000 (reserved for later use ?) - 0,//(unsigned char) (dc->routingSubnetSecond >> 8), (unsigned char) dc->routingSubnetSecond, // second part of S7-Subnet-ID - // (see S7Project/Network configuration) - - 0,//dc->_routingDestination1, // PLC-Number (0-126) or IP Adress (then 4 Bytes are used) - 0,//dc->_routingDestination2, - 0,//dc->_routingDestination3, - 0,//dc->_routingDestination4, - - 0,0,0,0,0, // empty - 0,0,0,0,0,0,0, - - 1,//dc->routingConnectionType, // Function (1=PG,2=OP,3=Step7Basic) - 0,//(dc->routingSlot + dc->routingRack*32), // Rack (Bit 7-5) and Slot (Bit 4-0) - // 0 for slot = let select the plc itself the correct slotnumber - - 0xC0, // Parameter requested TPDU-Size - 1, // Length of this parameter - 9 // requested TPDU-Size 8=256 Bytes, 9=512 Bytes , a=1024 Bytes - }; - - /* byte[] b243 ={ - 0x11,0xE0,0x00, - 0x00,0x00,0x01,0x00, - 0xC1,2,(byte)'M',(byte)'W', - 0xC2,2,(byte)'M',(byte)'W', - 0xC0,1,9 - };*/ + /* byte[] b4R ={ // for routing + 6 + 30 + 30 + 3, // Length over all without this byte (fixed + // Data 6 Bytes + size of Parameters (3 for C0h,30 for C1h+C2h) + + 0xE0, // TDPU Type CR = Connection Request (see RFC1006/ISO8073) + 0x00,0x00, // TPDU Destination Reference (unknown) + 0x00,0x01, // TPDU Source-Reference (my own reference, should not be zero) + 0x00, // TPDU Class 0 and no Option + + 0xC1, // Parameter Source-TSAP + 28, // Length of this parameter + 1, // one block of data (???) + 0, // Length for S7-Subnet-ID + 0, // Length of PLC-Number + 2, // Length of Function/Rack/Slot + 0,0,0,0,0,0,0,0, // empty Data + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0, + (byte)config.ConnectionType, // Function (1=PG,2=OP,3=Step7Basic) + (byte)(slot + rack * 32), // Rack (Bit 7-5) and Slot (Bit 4-0) + + 0xC2, // Parameter Destination-TSAP + 28, // Length of this parameter + 1, // one block of data (???) + 6, // Length for S7-Subnet-ID + dc->_routingDestinationSize, // Length of PLC-Number - 04 if you use a IP as Destination! + 2, // Length of Function/Rack/Slot + + 0,//(unsigned char) (dc->routingSubnetFirst >> 8), (unsigned char) dc->routingSubnetFirst, // first part of S7-Subnet-ID + // (look into the S7Project/Network configuration) + 0x00,0x00, // fix always 0000 (reserved for later use ?) + 0,//(unsigned char) (dc->routingSubnetSecond >> 8), (unsigned char) dc->routingSubnetSecond, // second part of S7-Subnet-ID + // (see S7Project/Network configuration) + + 0,//dc->_routingDestination1, // PLC-Number (0-126) or IP Adress (then 4 Bytes are used) + 0,//dc->_routingDestination2, + 0,//dc->_routingDestination3, + 0,//dc->_routingDestination4, + + 0,0,0,0,0, // empty + 0,0,0,0,0,0,0, + + 1,//dc->routingConnectionType, // Function (1=PG,2=OP,3=Step7Basic) + 0,//(dc->routingSlot + dc->routingRack*32), // Rack (Bit 7-5) and Slot (Bit 4-0) + // 0 for slot = let select the plc itself the correct slotnumber + + 0xC0, // Parameter requested TPDU-Size + 1, // Length of this parameter + 9 // requested TPDU-Size 8=256 Bytes, 9=512 Bytes , a=1024 Bytes + }; + + /* byte[] b243 ={ + 0x11,0xE0,0x00, + 0x00,0x00,0x01,0x00, + 0xC1,2,(byte)'M',(byte)'W', + 0xC2,2,(byte)'M',(byte)'W', + 0xC0,1,9 + };*/ //Pdu p1 = new Pdu(1); success = 0; @@ -201,7 +201,7 @@ public int disconnectPLC() Dispose(); return 0; } - + public bool Connected() { return tcpClient != null ? tcpClient.Connected : false; @@ -353,7 +353,7 @@ public int stop() return 0; } - + private int NegPDUlengthRequest() { byte[] pa = { 0xF0, 0, 0, 1, 0, 1, 3, 0xC0 }; @@ -417,7 +417,7 @@ public int daveReadPLCTime(out DateTime dateTime) //tmp[0] = Convert.ToByte(tmp[0] >> 4); //millisecond += getBCD8from(tmp, 0); millisecond += ByteFunctions.getBCD8from(res, 9) >> 4; - dateTime= new DateTime(year, month, day, hour, minute, second, millisecond); + dateTime = new DateTime(year, month, day, hour, minute, second, millisecond); return ret; } @@ -742,7 +742,7 @@ private int endUpload(int uploadID) int res; //p1.header=dc->msgOut+dc->PDUstartO; - _daveConstructEndUpload(p1,uploadID); + _daveConstructEndUpload(p1, uploadID); Pdu ret = ExchangePdu(p1); //res=_daveExchange(dc, &p1); //if(res!=daveResOK) return res; @@ -1197,7 +1197,7 @@ public int readManyBytes(int area, int DBnumber, int start, int len, ref byte[] public int writeBits(int area, int DB, int start, int len, byte[] buffer) { var p1 = new Pdu_WriteRequest(); - Pdu p2; + Pdu p2; //p1.header = dc->msgOut + dc->PDUstartO; //davePrepareWriteRequest(ref p1); p1.addBitVarToWriteRequest(area, DB, start, len, buffer); @@ -1251,7 +1251,7 @@ public IPDU prepareWriteRequest() { return new Pdu_WriteRequest(); } - + public int execWriteRequest(IPDU p, IresultSet rl)//not checked { Pdu p2; @@ -1573,9 +1573,24 @@ public int daveGetNCProgram(string filename, byte[] buffer, ref int length) throw new NotImplementedException(); } + public int daveGetNcFile(string filename, byte[] buffer, ref int length) + { + throw new NotImplementedException(); + } + + public int daveGetNcFileSize(string filename, ref int length) + { + throw new NotImplementedException(); + } + public int davePutNCProgram(string filename, string path, string ts, byte[] buffer, int length) { throw new NotImplementedException(); } + + public int alarmQueryAlarm_S(byte[] buffer, int length, ref int alarmCount) + { + throw new NotImplementedException(); + } } } diff --git a/LibNoDaveConnectionLibrary/Communication/PLCConnection.cs b/LibNoDaveConnectionLibrary/Communication/PLCConnection.cs index 2a8fbb9b..94cdfc2b 100644 --- a/LibNoDaveConnectionLibrary/Communication/PLCConnection.cs +++ b/LibNoDaveConnectionLibrary/Communication/PLCConnection.cs @@ -9,6 +9,12 @@ This ConnectionLibrary was written by Jochen Kuehner * Steffen Krayer -> For his work on MC7 decoding and the Source for his Decoder * Zottel -> For LibNoDave + The NCK part was written by J.Eger + * + * Thanks go to: + * Jochen Kuehner -> For his nice ConnectionLibrary + * Thomas_v2.1 -> For the support of the telegram analyze + WPFToolboxForSiemensPLCs is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2, or (at your option) @@ -23,6 +29,8 @@ You should have received a copy of the GNU Library General Public License along with Libnodave; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ +//#define daveDebug + using System; using System.Collections.Generic; using System.ComponentModel; @@ -227,7 +235,7 @@ public void Connect() _fds.rfd = libnodave.setPort(_configuration.ComPort, _configuration.ComPortSpeed, (int)_configuration.ComPortParity); break; -#if !IPHONE +#if !IPHONE case LibNodaveConnectionTypes.Use_Step7_DLL: _errorCodeConverter = libnodave.daveStrerror; _fds.rfd = libnodave.openS7online(_configuration.EntryPoint, 0); @@ -1391,16 +1399,16 @@ public void PLCPutBlockFromMC7toPLC(string BlockName, byte[] buffer) case "SDB": blk = DataTypes.PLCBlockType.SDB; break; - } - - - //Transfer crc: - //Benötigt für Safety übertragung! - //Es ist eine CRC16 Prüfsumme mit dem Generator Polynom 0x9003 , Init = 0x0000 , RefIn = False, RefOut = False, XorOut = 0x0000. - //Die Prüfsumme wird aus folgenden Bytes gebildet: - //Byte 5, Bausteinkennung(0x0A for DB, 0x0C für FC, .... ) - // Byte 34, 35 Länge des Arbeitsspeicher in Bytes(ohne die 36 Bytes Header Länge) Länge MC7 Code - // Byte 36 bis Byte (36 + Länge des Arbeitsspeicher - 1 ) + } + + + //Transfer crc: + //Benötigt für Safety übertragung! + //Es ist eine CRC16 Prüfsumme mit dem Generator Polynom 0x9003 , Init = 0x0000 , RefIn = False, RefOut = False, XorOut = 0x0000. + //Die Prüfsumme wird aus folgenden Bytes gebildet: + //Byte 5, Bausteinkennung(0x0A for DB, 0x0C für FC, .... ) + // Byte 34, 35 Länge des Arbeitsspeicher in Bytes(ohne die 36 Bytes Header Länge) Länge MC7 Code + // Byte 36 bis Byte (36 + Länge des Arbeitsspeicher - 1 ) //var crcbyte = new[] {0x9003, (short) blk,}; var sizeHighByte = (buffer.Length - 36) / 256; var sizeLowByte = ((buffer.Length - 36) - 256 * sizeHighByte); @@ -1600,6 +1608,9 @@ public SZLData PLCGetSZL(Int16 SZLNummer, Int16 Index) case 1: datsets.Add(EndianessMarshaler.BytesToStruct(objBuffer)); break; + case 4: + datsets.Add(EndianessMarshaler.BytesToStruct(objBuffer)); + break; case 8: datsets.Add(EndianessMarshaler.BytesToStruct(objBuffer)); break; @@ -2818,6 +2829,7 @@ public void ReadValues(IEnumerable valueList, bool useReadOptimization) //If there is space for a tag left.... Then look how much Bytes we can put into this PDU if (nckT == null && !symbolicTag && gesAskSize + currentAskSize <= maxReadSize && (!libNoDaveValue.DontSplitValue || readSize > maxReadSize)) { + #region Without NCK int restBytes = maxReadSize - gesReadSize - HeaderTagSize; //Howmany Bytes can be added to this call if (restBytes > 0) @@ -2860,6 +2872,7 @@ public void ReadValues(IEnumerable valueList, bool useReadOptimization) //useresult muss noch programmiert werden. } + #endregion } var rs = _dc.getResultSet(); int res; @@ -2961,7 +2974,7 @@ public void ReadValues(IEnumerable valueList, bool useReadOptimization) { usedShortRequest.Add(false); tagWasSplitted.Add(false); - myPDU.addNCKToReadRequest(nckT.NckArea, nckT.NckUnit, nckT.NckColumn, nckT.NckLine, nckT.NckModule, nckT.NckLinecount); + myPDU.addNCKToReadRequest((int)nckT.NckArea, nckT.NckUnit, nckT.NckColumn, nckT.NckLine, nckT.NckModule, nckT.NckLinecount); } else if (symbolicTag) { @@ -3043,9 +3056,6 @@ public void ReadValues(IEnumerable valueList, bool useReadOptimization) else { NotExistedValue.Add(false); - var nckT = readTagList.ToList()[akVar] as PLCNckTag; - if (nckT != null && nckT.TagDataType != TagDataType.String && nckT.TagDataType != TagDataType.CharArray && nckT.NckArea != 5 && nckT.NckArea != 6) - System.Array.Reverse(myBuff, 0, myBuff.Length - 1); Array.Copy(myBuff, myBuffStart, completeData, positionInCompleteData, readenSizes[akVar]); positionInCompleteData += readenSizes[akVar]; } @@ -3249,6 +3259,29 @@ public T ReadValue(string address) return (T)wrt; } + /// + /// Read one single value from the NCK + /// + /// An Sinumerik Address Identifier. see for syntax + /// + public object ReadValue(NC_Var address) + { + var tag = address.GetNckTag(0, 0); + this.ReadValue(tag); + return tag.Value; + } + + /// + /// Read one single value from the NCK + /// + /// An Sinumerik Address Identifier. see for syntax + /// + public T ReadValue(NC_Var address) + { + var wrt = ReadValue(address); + return (T)wrt; + } + /// /// This Function Reads One LibNoDave Value from the PLC /// @@ -3595,7 +3628,7 @@ public void WriteValues(IEnumerable valueList, bool useWriteOptimation) var currValSize = currVal._internalGetSize(); if (!(currVal is PLCNckTag) && gesWriteSize < maxWriteSize && //Maximale Byte Anzahl noch nicht erreicht - /*anzWriteVar < maxWriteVar &&*/ + /*anzWriteVar < maxWriteVar &&*/ ( //maximale Variablenanzahl noch nicht erreicht splitPos != 0 || //Value ist schon gesplitted !currVal.DontSplitValue || //Value Kann gesplitted Werden @@ -3682,7 +3715,7 @@ public void WriteValues(IEnumerable valueList, bool useWriteOptimation) currVal._putControlValueIntoBuffer(wrt, 0); var nckT = currVal as PLCNckTag; #region Reverse - if (nckT != null && nckT.TagDataType != TagDataType.String && nckT.TagDataType != TagDataType.CharArray && nckT.NckArea != 5 && nckT.NckArea != 6) + if (nckT != null && nckT.TagDataType != TagDataType.String && nckT.TagDataType != TagDataType.CharArray && nckT.NckArea != NCK_Area.AreaFeedDrive && nckT.NckArea != NCK_Area.AreaMainDrive) System.Array.Reverse(wrt, 0, wrt.Length); #endregion @@ -3704,7 +3737,7 @@ public void WriteValues(IEnumerable valueList, bool useWriteOptimation) // transsize = 9; #endregion - myPDU.addNCKToWriteRequest(nckT.NckArea, nckT.NckUnit, nckT.NckColumn, nckT.NckLine, nckT.NckModule, nckT.NckLinecount, wrt.Length, wrt); + myPDU.addNCKToWriteRequest((int)nckT.NckArea, nckT.NckUnit, nckT.NckColumn, nckT.NckLine, nckT.NckModule, nckT.NckLinecount, wrt.Length, wrt); valueListT.Remove(currVal); //Wert erledigt... löschen.... } @@ -3749,10 +3782,10 @@ public void WriteValues(IEnumerable valueList, bool useWriteOptimation) #region NC PI-Service - public void PI_Service(string piservice, string[] param, int paramCount) + public void PI_Service(string piservice, string[] param) { libnodave.resultSet rs = new libnodave.resultSet(); - int res = _dc.PI_StartNC(piservice, param, paramCount); + int res = _dc.PI_StartNC(piservice, param, param.Length); if (res == -1025) { @@ -3769,15 +3802,60 @@ public void PI_Service(string piservice, string[] param, int paramCount) /// Load complete file from NC /// /// full filename inc. path + /// Start PI-Service F_XFER before upload /// - public string UploadFromNC(string fullFileName) + public byte[] BinaryUploadFromNC(string fullFileName, bool F_XFER = false) + { + libnodave.resultSet rs = new libnodave.resultSet(); + byte[] id = new byte[4]; + List lRet = new List(); + string file = fullFileName.Remove(0, fullFileName.LastIndexOf('/') > 0 ? fullFileName.LastIndexOf('/') + 1 : 0); + + if (F_XFER) + PI_Service("_N_F_XFER", new string[] { "P01", fullFileName }); + + int res = _dc.initUploadNC(file, ref id); + if (res != 0) + throw new Exception("UploadFromNC: " + res); + + int more = 0; + int len = 0; + byte[] buffer = new byte[1024]; + + do + { + res = _dc.doUploadNC(out more, buffer, out len, id); + if (res != 0) + break; + + for (int i = 0; i < len; i++) + { + lRet.Add(buffer[i]); + } + } while (more != 0); + + res = _dc.endUploadNC(id); + if (res != 0) + throw new Exception("BinaryUploadFromNC: " + res); + + return lRet.ToArray(); + } + + /// + /// Load complete file from NC + /// + /// full filename inc. path + /// Start PI-Service F_XFER before upload + /// + public string UploadFromNC(string fullFileName, bool F_XFER = true) { libnodave.resultSet rs = new libnodave.resultSet(); byte[] id = new byte[4]; string ret = string.Empty; string file = fullFileName.Remove(0, fullFileName.LastIndexOf('/') > 0 ? fullFileName.LastIndexOf('/') + 1 : 0); - PI_Service("_N_F_XFER", new string[] { "P01", fullFileName }, 2); + if (F_XFER) + PI_Service("_N_F_XFER", new string[] { "P01", fullFileName }); int res = _dc.initUploadNC(file, ref id); if (res != 0) @@ -3807,14 +3885,16 @@ public string UploadFromNC(string fullFileName) /// /// full filename inc. path /// size of the file (buffer) + /// Start PI-Service F_XFER before upload /// - public string UploadFromNC(string fullFileName, int size) + public string UploadFromNC(string fullFileName, int size, bool F_XFER = true) { libnodave.resultSet rs = new libnodave.resultSet(); string ret = string.Empty; string filename = fullFileName.Remove(0, fullFileName.LastIndexOf('/') > 0 ? fullFileName.LastIndexOf('/') + 1 : 0); - PI_Service("_N_F_XFER", new string[] { "P01", fullFileName }, 2); + if (F_XFER) + PI_Service("_N_F_XFER", new string[] { "P01", fullFileName }); int length = 0; byte[] buffer = new byte[size]; @@ -3828,6 +3908,121 @@ public string UploadFromNC(string fullFileName, int size) return ret; } + /// + /// Load complete file from NC + /// + /// full filename inc. path + /// size of the file (buffer) + /// Start PI-Service F_XFER before upload + /// + public byte[] BinaryUploadNcFile(string fullFileName, int size = 0, bool F_XFER = false) + { + libnodave.resultSet rs = new libnodave.resultSet(); + string filename = fullFileName.Remove(0, fullFileName.LastIndexOf('/') > 0 ? fullFileName.LastIndexOf('/') + 1 : 0); + + if (size == 0) + size = UploadNcFileSize(fullFileName, F_XFER); + if (F_XFER) + PI_Service("_N_F_XFER", new string[] { "P01", fullFileName }); + + int length = 0; + byte[] buffer = new byte[size]; + int res = _dc.daveGetNcFile(filename, buffer, ref length); + + if (res != 0) + throw new Exception("BinaryUploadNcFile: " + res); + + byte[] ret = new byte[length]; + Array.Copy(buffer, ret, length); + + return ret; + } + + /// + /// Load complete file from NC + /// + /// full filename inc. path + /// size of the file (buffer) + /// Start PI-Service F_XFER before upload + /// + public string UploadNcFile(string fullFileName, int size = 0, bool F_XFER = true) + { + libnodave.resultSet rs = new libnodave.resultSet(); + string ret = string.Empty; + string filename = fullFileName.Remove(0, fullFileName.LastIndexOf('/') > 0 ? fullFileName.LastIndexOf('/') + 1 : 0); + + if (size == 0) + size = UploadNcFileSize(fullFileName, F_XFER); + if (F_XFER) + PI_Service("_N_F_XFER", new string[] { "P01", fullFileName }); + +#if daveDebug + libnodave.daveSetDebug(0x1ffff); +#endif + + int length = 0; + byte[] buffer = new byte[size]; + int res = _dc.daveGetNcFile(filename, buffer, ref length); + +#if daveDebug + var a = libnodave.daveGetDebug(); + + if (res != 0) + for (int i = 0; i < buffer.Length; i++) + { + if (buffer[i] == 0) + { + length = i; + break; + } + } + + libnodave.daveSetDebug(0); +#endif + + if (res != 0) + throw new Exception("UploadNcFile: " + res); + else if (length > buffer.Length) + throw new ArgumentOutOfRangeException("size", size, "File size: " + length); + else + ret = System.Text.Encoding.Default.GetString(buffer, 0, length); + + return ret; + } + + /// + /// Load file size from NC + /// + /// full filename inc. path + /// Start PI-Service F_XFER before upload + /// + public int UploadNcFileSize(string fullFileName, bool F_XFER = true) + { + libnodave.resultSet rs = new libnodave.resultSet(); + string filename = fullFileName.Remove(0, fullFileName.LastIndexOf('/') > 0 ? fullFileName.LastIndexOf('/') + 1 : 0); + + if (filename.ToUpper().EndsWith("WPD") || filename.ToUpper().EndsWith("DIR")) + return Int16.MaxValue; + + if (F_XFER) + PI_Service("_N_F_XFER", new string[] { "P01", fullFileName }); + +#if daveDebug + libnodave.daveSetDebug(0x1ffff); +#endif + + int length = 0; + int res = _dc.daveGetNcFileSize(filename, ref length); + +#if daveDebug + libnodave.daveSetDebug(0); +#endif + + if (res != 0) + throw new Exception("UploadNcFileSize: " + res); + return length; + } + /// /// Transfer file to NC /// @@ -3848,6 +4043,200 @@ public void DownloadToNC(string fullFileName, string ts, string data) } #endregion + #region SPS Alarm Query + public int[] GetAlarmS_IDs() + { + int size = 32767; + + libnodave.resultSet rs = new libnodave.resultSet(); + List lRet = new List(); + + try + { + int alarmCount = 0; + byte[] buffer = new byte[size]; + int res = _dc.alarmQueryAlarm_S(buffer, size, ref alarmCount); + + if (res != 0) + throw new Exception("GetSPS_AlarmQuery: " + res); + else + { + byte[] dummy = new byte[System.Runtime.InteropServices.Marshal.SizeOf(typeof(alarmMessageHeader))]; + int index = 0; + for (int i = 0; i < alarmCount; i++) + { + Array.Copy(buffer, index, dummy, 0, dummy.Length); + var alarmHeader = EndianessMarshaler.BytesToStruct(dummy); + lRet.Add(alarmHeader.EventID); + index += alarmHeader.Lenght + 2; + } + } + //ret = System.Text.Encoding.Default.GetString(buffer, 0, alarmCount); + } + catch (Exception) + { + } + + return lRet.ToArray(); + } + + [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi, Pack = 1)] + public class alarmMessageHeader + { + //[Endian(Endianness.BigEndian)] + private byte _Lenght; + public Byte Lenght + { + get { return _Lenght; } + set { _Lenght = value; } + } + + [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 2)] + private byte[] _x; + public Byte[] _ + { + get { return _x; } + set { _x = value; } + } + + private byte _Alarmtype; + public Byte Alarmtype + { + get { return _Alarmtype; } + set { _Alarmtype = value; } + } + + [Endian(Endianness.BigEndian)] + private int _EventID; + public Int32 EventID + { + get { return _EventID; } + set { _EventID = value; } + } + +#if TimestampMessageComing + private byte __x; + public Byte __ + { + get { return __x; } + set { __x = value; } + } + + private byte _EventState; + public Byte EventState + { + get { return _EventState; } + set { _EventState = value; } + } + + private byte _AckState_going; + public Byte AckState_going + { + get { return _AckState_going; } + set { _AckState_going = value; } + } + + private byte _AckState_coming; + public Byte AckState_coming + { + get { return _AckState_coming; } + set { _AckState_coming = value; } + } + + [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 8)] + private byte[] _TimestampMessageComing; + public DateTime TimestampMessageComing + { + get { return GetDateTimeFromByteArray(_TimestampMessageComing); } + set { _TimestampMessageComing = GetByteArrayFromBuffer(value); } + } +#endif + } + + private static DateTime GetDateTimeFromByteArray(byte[] buffer) + { + try + { + string[] sAr = BitConverter.ToString(buffer).Trim().Split('-'); + return new DateTime(int.Parse(sAr[0]) >= 90 ? 1900 + int.Parse(sAr[0]) : 2000 + int.Parse(sAr[0]), int.Parse(sAr[1]), int.Parse(sAr[2]), int.Parse(sAr[3]), int.Parse(sAr[4]), int.Parse(sAr[5]), int.Parse(sAr[6] + sAr[7].Substring(0, 1))); + } + catch (Exception) + { + return new DateTime(); // DateTime(1990, 1, 1); + } + } + + private static byte[] GetByteArrayFromBuffer(DateTime dt) + { + byte[] ret = new byte[8]; + try + { + if (Equals(dt, new DateTime())) + return new byte[8]; + + ret[0] = Convert.ToByte((dt.Year > 2000 ? dt.Year - 2000 : dt.Year - 1900).ToString(), 16); + ret[1] = Convert.ToByte(dt.Month.ToString(), 16);// (byte)dt.Month; + ret[2] = Convert.ToByte(dt.Day.ToString(), 16); + ret[3] = Convert.ToByte(dt.Hour.ToString(), 16); + ret[4] = Convert.ToByte(dt.Minute.ToString(), 16); + ret[5] = Convert.ToByte(dt.Second.ToString(), 16); + ret[6] = Convert.ToByte(dt.Millisecond.ToString("000").Substring(0, 2), 16); + ret[7] = Convert.ToByte(dt.Millisecond.ToString("000").Substring(2) + ((byte)dt.DayOfWeek + 1), 16); + } + catch (Exception) + { } + return ret; + } + +#if aaa + public class objMSG + { + objMSG() { } + + objMSG(byte[] Buffer) + { + this.lenght = Buffer[0]; + byte Alarmtype = Buffer[3]; + + dummy = new byte[4]; + Array.Copy(Buffer, index + 4, dummy, 0, dummy.Length); + Array.Reverse(dummy); + var EventID = BitConverter.ToUInt32(dummy, 0); + + byte EventState = Buffer[index + 9]; + byte ActStateGoing = Buffer[index + 10]; + byte ActStateComing = Buffer[index + 11]; + byte[] _TimeStampMessageComming = new byte[8]; + Array.Copy(Buffer, index + 12, _TimeStampMessageComming, 0, _TimeStampMessageComming.Length); + DateTime TimeStampMessageComming = getDateTimeFromDateAndTimeString(BitConverter.ToString(_TimeStampMessageComming)); + } + + [Endian(Endianness.BigEndian)] + private byte lenght; + public Byte Lenght + { + get { return lenght; } + set { lenght = value; } + } + + private byte alarmtype; + public Byte Alarmtype + { + get { return alarmtype; } + set { alarmtype = value; } + } + + } +#endif + #endregion + + #region Debug + public void SetDaveDebug(int newDebugLevel = 0) + { + libnodave.daveSetDebug(newDebugLevel); + } + #endregion + public void Dispose() { Connected = false; diff --git a/LibNoDaveConnectionLibrary/Communication/PLCException.cs b/LibNoDaveConnectionLibrary/Communication/PLCException.cs index aabf0fdd..67e2b8dc 100644 --- a/LibNoDaveConnectionLibrary/Communication/PLCException.cs +++ b/LibNoDaveConnectionLibrary/Communication/PLCException.cs @@ -1,29 +1,29 @@ -using DotNetSiemensPLCToolBoxLibrary.Communication.LibNoDave; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace DotNetSiemensPLCToolBoxLibrary.Communication -{ - public class PLCException:Exception - { - int _ErrorCode; - public int ErrorCode - { - get { return _ErrorCode; } - } - - public PLCException (int errorCode): - base(String.Format("Operation failed due to error from PLC {0}: {1}",errorCode, libnodave.daveStrerror(errorCode))) - { - _ErrorCode = errorCode; - } - - public PLCException(string msg, int errorCode) : - base(msg) - { - _ErrorCode = errorCode; - } - } -} +using DotNetSiemensPLCToolBoxLibrary.Communication.LibNoDave; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace DotNetSiemensPLCToolBoxLibrary.Communication +{ + public class PLCException:Exception + { + int _ErrorCode; + public int ErrorCode + { + get { return _ErrorCode; } + } + + public PLCException (int errorCode): + base(String.Format("Operation failed due to error from PLC {0}: {1}",errorCode, libnodave.daveStrerror(errorCode))) + { + _ErrorCode = errorCode; + } + + public PLCException(string msg, int errorCode) : + base(msg) + { + _ErrorCode = errorCode; + } + } +} diff --git a/LibNoDaveConnectionLibrary/Communication/PLCNckTag.cs b/LibNoDaveConnectionLibrary/Communication/PLCNckTag.cs index 71a34d05..439a99df 100644 --- a/LibNoDaveConnectionLibrary/Communication/PLCNckTag.cs +++ b/LibNoDaveConnectionLibrary/Communication/PLCNckTag.cs @@ -1,4 +1,5 @@ -/* +using DotNetSiemensPLCToolBoxLibrary.DataTypes; +/* This implements a high level Wrapper between libnodave.dll and applications written in MS .Net languages. @@ -9,6 +10,12 @@ This ConnectionLibrary was written by Jochen Kuehner * Steffen Krayer -> For his work on MC7 decoding and the Source for his Decoder * Zottel -> For LibNoDave + The NCK part was written by J.Eger + * + * Thanks go to: + * Jochen Kuehner -> For his nice ConnectionLibrary + * Thomas_v2.1 -> For the support of the telegram analyze + WPFToolboxForSiemensPLCs is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2, or (at your option) @@ -25,32 +32,98 @@ You should have received a copy of the GNU Library General Public License */ using System; - - namespace DotNetSiemensPLCToolBoxLibrary.Communication { #if !IPHONE - [System.ComponentModel.Editor(typeof(NckTagUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))] + using Scm = System.ComponentModel; + using SG = System.Globalization; + [Scm.TypeConverter(typeof(PLCNckTagTypeConverter))] + [Scm.Editor(typeof(NckTagUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))] +#endif + [Serializable] + public class PLCNckTag : PLCTag + { + public PLCNckTag() + { + } + + /// + /// Create an new Nck tag from an existing one by copying its information + /// + /// + /// + public PLCNckTag(PLCNckTag oldTag, object tag = null) + { + CopyTag(oldTag, tag); + } + + /// + /// Create an new Nck tag from an existing one by copying its information + /// + /// + /// + public PLCNckTag(NC_Var ncVar, object tag = null) + { + CopyTag(ncVar != null ? ncVar.GetNckTag() : null, tag); + } + + private void CopyTag(PLCNckTag oldTag, object tag = null) + { + if (oldTag != null) + { + this.TagDataSource = oldTag.TagDataSource; + this.TagDataType = oldTag.TagDataType; + this.ByteAddress = oldTag.ByteAddress; + this.BitAddress = oldTag.BitAddress; + this.ArraySize = oldTag.ArraySize; + this.DataTypeStringFormat = oldTag.DataTypeStringFormat; + this.DataBlockNumber = oldTag.DataBlockNumber; + this.Controlvalue = oldTag.Controlvalue; + this.DontSplitValue = oldTag.DontSplitValue; + + this.Tag = oldTag.Tag; + + this.NckArea = oldTag.NckArea; + this.NckUnit = oldTag.NckUnit; + this.NckColumn = oldTag.NckColumn; + this.NckLine = oldTag.NckLine; + this.NckModule = oldTag.NckModule; + this.NckLinecount = oldTag.NckLinecount; + } + if (tag != null) + this.Tag = tag; + } + +#if !IPHONE + [System.ComponentModel.Browsable(false), System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] + [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] #endif - [Serializable] - public class PLCNckTag : PLCTag - { - public PLCNckTag() - { - } - - public int NckArea { get; set; } - public int NckUnit { get; set; } - public int NckColumn { get; set; } - public int NckLine { get; set; } - public int NckModule { get; set; } - public int NckLinecount { get; set; } - - public override bool DontSplitValue - { - get { return true; } - set { } - } + [System.Xml.Serialization.XmlElement("NckArea")] + public int _NckArea + { + get + { + return (int)NckArea; + } + set + { + NckArea = (NCK_Area)value; + } + } + + [System.Xml.Serialization.XmlIgnore] + public /*int*/ NCK_Area NckArea { get; set; } + public int NckUnit { get; set; } + public int NckColumn { get; set; } + public int NckLine { get; set; } + public int NckModule { get; set; } + public int NckLinecount { get; set; } + + public override bool DontSplitValue + { + get { return true; } + set { } + } public override string ToString() { @@ -65,7 +138,9 @@ public override string ToString() old += ""; } - string s = string.Format("0x{0},0x{1},0x{2},0x{3},0x{4},0x{5},{6},0x{7}", NckArea.ToString("X"), NckUnit.ToString("X"), NckColumn.ToString("X"), NckLine.ToString("X"), NckModule.ToString("X"), NckLinecount.ToString("X"), TagDataType, _internalGetSize().ToString("X")); + //string s = string.Format("0x{0},0x{1},0x{2},0x{3},0x{4},0x{5},{6},0x{7}", NckArea.ToString("X"), NckUnit.ToString("X"), NckColumn.ToString("X"), NckLine.ToString("X"), NckModule.ToString("X"), NckLinecount.ToString("X"), TagDataType, _internalGetSize().ToString("X")); + NC_Var ncVar = new NC_Var(this); + string s = string.Format("0x{0},0x{1},0x{2},0x{3},0x{4},0x{5},0x{6},0x{7}", ncVar.SYNTAX_ID.ToString("X"), ncVar.Bereich_u_einheit.ToString("X"), ncVar.Spalte.ToString("X"), ncVar.Zeile.ToString("X"), ncVar.Bausteintyp.ToString("X"), ncVar.ZEILENANZAHL.ToString("X"), ncVar.Typ.ToString("X"), ncVar.Laenge.ToString("X")); if (Value != null) { @@ -74,10 +149,382 @@ public override string ToString() return s; } +#if !IPHONE + public class PLCNckTagTypeConverter : Scm.TypeConverter + { + public override bool CanConvertFrom(Scm.ITypeDescriptorContext context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object ConvertFrom(Scm.ITypeDescriptorContext context, SG.CultureInfo culture, object value) + { + if (value is string) + { + if (string.IsNullOrWhiteSpace((string)value)) + return null; + + string[] sAr = ((string)value).Split(','); + if (sAr.Length == 8) + { + return new NC_Var(HexParse(sAr[0], culture), HexParse(sAr[1], culture), HexParse(sAr[2], culture), HexParse(sAr[3], culture), HexParse(sAr[4], culture), HexParse(sAr[5], culture), HexParse(sAr[6], culture), HexParse(sAr[7], culture)).GetNckTag(); + } + } + + return base.ConvertFrom(context, culture, value); + } + + private static int HexParse(string s, SG.CultureInfo culture) + { + if (s.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) + { + s = s.Substring(2); + return int.Parse(s, SG.NumberStyles.HexNumber, culture); + } + return int.Parse(s, SG.NumberStyles.AllowThousands, culture); + } + } +#endif + //Todo: look how long a NCK Request is??? //internal override int _internalGetSize() //{ // return 1; //} - } + } + + public class NC_Var + { + public NC_Var() + { + } + + public NC_Var(NC_Var var, int unitOffset = 0, int rowOffset = 0, int columnOffset = 0) + { + this.SYNTAX_ID = var.SYNTAX_ID; + this.Bereich_u_einheit = (byte)(var.Bereich_u_einheit + unitOffset); + this.Spalte = (ushort)(var.Spalte + columnOffset); + this.Zeile = (ushort)(var.Zeile + rowOffset); + this.Bausteintyp = var.Bausteintyp; + this.ZEILENANZAHL = var.ZEILENANZAHL; + this.Typ = var.Typ; + this.Laenge = var.Laenge; + } + + public NC_Var(PLCNckTag nckTag) + { + if (nckTag != null) + { + this.SYNTAX_ID = 0x82; + this.Bereich_u_einheit = (byte)((byte)nckTag.NckArea << 5 | nckTag.NckUnit); + this.Spalte = (UInt16)nckTag.NckColumn; + this.Zeile = (UInt16)nckTag.NckLine; + this.Bausteintyp = (byte)nckTag.NckModule; + this.ZEILENANZAHL = (byte)nckTag.NckLinecount; + this.Typ = GetNckType(nckTag.TagDataType); + this.Laenge = (byte)nckTag._internalGetSize(); + } + } + + public NC_Var(string ncVarSelector) + { + throw new NotImplementedException(); + //bereich_u_einheit = 0x40; + //spalte = 0x78; + //zeile = 0x1; + //bausteintyp = 0x7F; + //ZEILENANZAHL = 0x1; + //typ = 0xF; + //laenge = 0x8; + } + + public NC_Var(int syntaxId, int bereich_u_einheit, int spalte, int zeile, int bausteinTyp, int zeilenAnzahl, int typ, int laenge) + { + this.SYNTAX_ID = (byte)syntaxId; + this.Bereich_u_einheit = (byte)bereich_u_einheit; + this.Spalte = (UInt16)spalte; + this.Zeile = (UInt16)zeile; + this.Bausteintyp = (byte)bausteinTyp; + this.ZEILENANZAHL = (byte)zeilenAnzahl; + this.Typ = (byte)typ; + this.Laenge = (byte)laenge; + } + + #region Fields + private byte syntaxId; + private byte bereich_u_einheit; + private UInt16 spalte; + private UInt16 zeile; + private byte bausteintyp; + private byte zeilenanzahl; + private byte typ; + private byte laenge; + #endregion + + #region Properties +#if !IPHONE + [Scm.CategoryAttribute("PlcNckTag")] + [Scm.TypeConverter(typeof(ByteHexTypeConverter))] +#endif + public byte SYNTAX_ID + { + get { return syntaxId; } + set { syntaxId = value; } + } + +#if !IPHONE + [Scm.CategoryAttribute("PlcNckTag")] + [Scm.TypeConverter(typeof(ByteHexTypeConverter))] +#endif + public byte Bereich_u_einheit + { + get { return bereich_u_einheit; } + set { bereich_u_einheit = value; } + } + +#if !IPHONE + [Scm.CategoryAttribute("PlcNckTag")] + [Scm.TypeConverter(typeof(UInt16HexTypeConverter))] +#endif + public UInt16 Spalte + { + get { return spalte; } + set { spalte = value; } + } + +#if !IPHONE + [Scm.CategoryAttribute("PlcNckTag")] + [Scm.TypeConverter(typeof(UInt16HexTypeConverter))] +#endif + public UInt16 Zeile + { + get { return zeile; } + set { zeile = value; } + } + +#if !IPHONE + [Scm.CategoryAttribute("PlcNckTag")] + [Scm.TypeConverter(typeof(ByteHexTypeConverter))] +#endif + public byte Bausteintyp + { + get { return bausteintyp; } + set { bausteintyp = value; } + } + +#if !IPHONE + [Scm.CategoryAttribute("PlcNckTag")] + [Scm.TypeConverter(typeof(ByteHexTypeConverter))] +#endif + public byte ZEILENANZAHL + { + get { return zeilenanzahl; } + set { zeilenanzahl = value; } + } + +#if !IPHONE + [Scm.CategoryAttribute("PlcNckTag")] + [Scm.TypeConverter(typeof(ByteHexTypeConverter))] +#endif + public byte Typ + { + get { return typ; } + set { typ = value; } + } + +#if !IPHONE + [Scm.CategoryAttribute("PlcNckTag")] + [Scm.TypeConverter(typeof(ByteHexTypeConverter))] +#endif + public byte Laenge + { + get { return laenge; } + set { laenge = value; } + } + #endregion + + public PLCNckTag GetNckTag(int unitOffset = 0, int rowOffset = 0, int columnOffset = 0) + { + if (this.syntaxId == 0 && this.bereich_u_einheit == 0 && this.spalte == 0 && this.zeile == 0 && this.bausteintyp == 0 && this.zeilenanzahl == 0 && this.typ == 0 && this.laenge == 0) + return null; + + //byte SYNTAX_ID = 0x82; + byte bereich_u_einheit = (byte)(this.Bereich_u_einheit + unitOffset); + byte _bereich = (byte)((bereich_u_einheit & 0xE0) >> 5); // (bereich_u_einheit & 2#11100000) schiebe rechts um 5 Bit + byte _einheit = (byte)(bereich_u_einheit & 0x1F); // & 2#00011111 + + #region TYP + TagDataType dataType = new TagDataType(); + int _ArraySize = 0; + switch (this.Typ) + { + case 1: + dataType = TagDataType.Bool; + break; + case 3: + dataType = TagDataType.Byte; //eNCK_LE_Int8; + break; + case 4: + dataType = TagDataType.Word; + break; + case 5: + dataType = TagDataType.Int; //eNCK_LE_Int16; + break; + case 6: + dataType = TagDataType.Dword; //eNCK_LE_Uint32; + break; + case 7: + dataType = TagDataType.Dint; //eNCK_LE_Int32; + break; + case 8: + dataType = TagDataType.Float; //eNCK_LE_Float32; + break; + case 14: + dataType = TagDataType.DateTime; + break; + case 15: + dataType = TagDataType.LReal; //eNCK_LE_Float64; + break; + case 18: + dataType = TagDataType.LInt; //eNCK_LE_Int64; + break; + case 19: + //if (_bereich == 2)// && NC_Var.bausteintyp == 0x7f) + // dataType = TagDataType.String; //eNCK_LE_String; + //else + dataType = TagDataType.CharArray; //eNCK_LE_String; + + _ArraySize = this.Laenge; + break; + default: + throw new Exception(string.Format("Unknown Type: {0}", this.Typ)); + } + #endregion + + return new PLCNckTag() { TagDataType = dataType, NckArea = (NCK_Area)_bereich, NckUnit = _einheit, NckColumn = this.Spalte + columnOffset, NckLine = this.Zeile + rowOffset, NckModule = this.Bausteintyp, NckLinecount = this.ZEILENANZAHL, ArraySize = _ArraySize }; + } + + public static NC_Var GetNC_Var(PLCNckTag nckTag) + { + var ret = new NC_Var(); + + if (nckTag != null) + { + ret.SYNTAX_ID = 0x82; + ret.Bereich_u_einheit = (byte)((byte)nckTag.NckArea << 5 | nckTag.NckUnit); + ret.Spalte = (UInt16)nckTag.NckColumn; + ret.Zeile = (UInt16)nckTag.NckLine; + ret.Bausteintyp = (byte)nckTag.NckModule; + ret.ZEILENANZAHL = (byte)nckTag.NckLinecount; + ret.Typ = GetNckType(nckTag.TagDataType); + ret.Laenge = (byte)nckTag._internalGetSize(); + } + return ret; + } + + public static byte GetNckType(TagDataType type) + { + switch (type) + { + case TagDataType.Bool: + return 1; + case TagDataType.Byte: + return 3; + case TagDataType.Word: + return 4; + case TagDataType.Int: + return 5; + case TagDataType.Dword: + return 6; + case TagDataType.Dint: + return 7; + case TagDataType.Float: + return 8; + case TagDataType.DateTime: + return 14; + case TagDataType.LReal: + return 15; + case TagDataType.LInt: + return 18; + case TagDataType.String: + case TagDataType.CharArray: + return 19; + default: + return 0; + } + } + + +#if !IPHONE + public class ByteHexTypeConverter : Scm.TypeConverter + { + public override bool CanConvertFrom(Scm.ITypeDescriptorContext context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object ConvertFrom(Scm.ITypeDescriptorContext context, SG.CultureInfo culture, object value) + { + if (value is string) + { + string s = (string)value; + if (s.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) + { + s = s.Substring(2); + + return Byte.Parse(s, SG.NumberStyles.HexNumber, culture); + } + return Byte.Parse(s, SG.NumberStyles.AllowThousands, culture); + //byte s = (byte)value; + //return value.ToString("X"); + } + + return base.ConvertFrom(context, culture, value); + } + + public override object ConvertTo(Scm.ITypeDescriptorContext context, SG.CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(string) && value.GetType() == typeof(byte)) + return "0x" + ((byte)value).ToString("X2", culture); + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + public class UInt16HexTypeConverter : Scm.TypeConverter + { + public override bool CanConvertFrom(Scm.ITypeDescriptorContext context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object ConvertFrom(Scm.ITypeDescriptorContext context, SG.CultureInfo culture, object value) + { + if (value is string) + { + string s = (string)value; + if (s.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) + { + s = s.Substring(2); + + return UInt16.Parse(s, SG.NumberStyles.HexNumber, culture); + } + return UInt16.Parse(s, SG.NumberStyles.AllowThousands, culture); + //byte s = (byte)value; + //return value.ToString("X"); + } + + return base.ConvertFrom(context, culture, value); + } + + public override object ConvertTo(Scm.ITypeDescriptorContext context, SG.CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(string) && value.GetType() == typeof(UInt16)) + return "0x" + ((UInt16)value).ToString("X4", culture); + + return base.ConvertTo(context, culture, value, destinationType); + } + } +#endif + } } diff --git a/LibNoDaveConnectionLibrary/Communication/PLCTag.cs b/LibNoDaveConnectionLibrary/Communication/PLCTag.cs index 52238676..f4504aa8 100644 --- a/LibNoDaveConnectionLibrary/Communication/PLCTag.cs +++ b/LibNoDaveConnectionLibrary/Communication/PLCTag.cs @@ -133,6 +133,9 @@ public string SymbolicAccessKey } } + + public object Tag { get; set; } + //For Tags used with Full Symbolic in TIA Portal private bool _itemDoesNotExist; @@ -1257,7 +1260,7 @@ public void ChangeAddressFromString(String newPlcAddress) { if (plcAddress.Substring(2, 3).Contains(" ")) plcAddress = plcAddress.Remove(plcAddress.IndexOf(" "), 1); - string[] myPlcAddress = plcAddress.ToLower().Replace("byte", " byte ").Replace(" ", " ").Replace("p#", "").Split(' '); + string[] myPlcAddress = plcAddress.ToLower().Replace("p#", "").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); BitAddress = 0; if (!myPlcAddress[0].Contains("db")) { @@ -1288,7 +1291,15 @@ public void ChangeAddressFromString(String newPlcAddress) this.DataBlockNumber = Convert.ToInt32(myPlcAddress[0].Split('.')[0].Replace("db", "")); ByteAddress = Convert.ToInt32(myPlcAddress[0].Split('.')[1].Replace("dbx", "")); } - double _ArraySize = Convert.ToInt32(myPlcAddress[2]); + + double _ArraySize; + if (myPlcAddress.Length >= 3) + _ArraySize = Convert.ToInt32(myPlcAddress[2]); + else + { + _ArraySize = Convert.ToInt32(myPlcAddress[1].Replace("dword", "").Replace("word", "").Replace("byte", "").Replace("bool", "").Trim()); + myPlcAddress[1] = myPlcAddress[1].Replace(_ArraySize.ToString(), ""); + } var tsize = 1; switch (myPlcAddress[1]) @@ -1371,6 +1382,11 @@ public void ChangeAddressFromString(String newPlcAddress) if (this._internalGetSize() != 4) this.TagDataType = TagDataType.Dword; } + else if (myPlcAddress[1].Contains("DBR")) + { + ArraySize = 1; + this.TagDataType = TagDataType.Float; + } else if (myPlcAddress[1].Contains("DBX")) { ArraySize = 1; @@ -1380,7 +1396,7 @@ public void ChangeAddressFromString(String newPlcAddress) else this.BitAddress = 0; } - this.ByteAddress = Convert.ToInt32(myPlcAddress[1].Replace("DBW", "").Replace("DBD", "").Replace("DBX", "").Replace("DBB", "").Replace("DBL", "").Trim()); + this.ByteAddress = Convert.ToInt32(myPlcAddress[1].Replace("DBW", "").Replace("DBD", "").Replace("DBR", "").Replace("DBX", "").Replace("DBB", "").Replace("DBL", "").Trim()); } else { @@ -1809,11 +1825,22 @@ public void ParseValueFromByteArray(byte[] buff, int startpos) internal virtual void _readValueFromBuffer(byte[] buff, int startpos) { - try + if (this is PLCNckTag && this.TagDataType != DataTypes.TagDataType.String && this.TagDataType != DataTypes.TagDataType.CharArray && this.TagDataType != DataTypes.TagDataType.DateTime) { - switch (this.TagDataType) + if ((this as PLCNckTag).NckArea != NCK_Area.AreaFeedDrive && (this as PLCNckTag).NckArea != NCK_Area.AreaMainDrive) { - case TagDataType.String: + int length = Math.Min(buff.Length - startpos, _internalGetSize()); + System.Array.Reverse(buff, startpos, length); + } + } + + try + { + switch (this.TagDataType) + { + case TagDataType.String: + { + if (!(this is PLCNckTag)) { int maxsize = (int)buff[startpos]; int size = (int)buff[startpos + 1]; @@ -1821,188 +1848,192 @@ internal virtual void _readValueFromBuffer(byte[] buff, int startpos) if (ArraySize == 1 && ArraySize != maxsize) ArraySize = Math.Max(ArraySize, maxsize); else - { _setValueProp = Encoding.Default.GetString(buff, startpos + 2, size); - } - } - break; - case TagDataType.CharArray: - { - //var sb = new StringBuilder(); - //for (var n = 0; n < ((buff.Length - startpos) < ArraySize ? buff.Length - startpos : ArraySize); n++) - // sb.Append((char)buff[n + startpos]); - //_setValueProp = sb.ToString(); - _setValueProp = Encoding.Default.GetString(buff, startpos, - Math.Min(buff.Length - startpos, ArraySize)); } - break; - case TagDataType.ByteArray: - { - var val = new Byte[ArraySize]; - Array.Copy(buff, startpos, val, 0, ArraySize); - - /* - for (var n = 0; n < ArraySize; n++) - val[n] = buff[n + startpos]; - */ - _setValueProp = val; - } - break; - case TagDataType.BCDArray: - { - ulong wrt = 0; + else + _setValueProp = Encoding.Default.GetString(buff, startpos, Math.Min(buff.Length - startpos, ArraySize)).Split('\0')[0]; + } + break; + case TagDataType.CharArray: + { + //var sb = new StringBuilder(); + //for (var n = 0; n < ((buff.Length - startpos) < ArraySize ? buff.Length - startpos : ArraySize); n++) + // sb.Append((char)buff[n + startpos]); + //_setValueProp = sb.ToString(); - for (int i = 0; i < ArraySize; i++) - { - wrt *= 10; - wrt += (ulong)libnodave.getBCD8from(buff, startpos + i); - } + if (this is PLCNckTag) //BugFix für NCK v2.6 + _setValueProp = Encoding.Default.GetString(buff, startpos, Math.Min(buff.Length - startpos, ArraySize)).Split('\0')[0]; + else + _setValueProp = Encoding.Default.GetString(buff, startpos, Math.Min(buff.Length - startpos, ArraySize)).Trim('\0'); + } + break; + case TagDataType.ByteArray: + { + var val = new Byte[ArraySize]; + Array.Copy(buff, startpos, val, 0, ArraySize); - _setValueProp = wrt; + /* + for (var n = 0; n < ArraySize; n++) + val[n] = buff[n + startpos]; + */ + _setValueProp = val; + } + break; + case TagDataType.BCDArray: + { + ulong wrt = 0; + + for (int i = 0; i < ArraySize; i++) + { + wrt *= 10; + wrt += (ulong)libnodave.getBCD8from(buff, startpos + i); } - break; - case TagDataType.Bool: - case TagDataType.Byte: - case TagDataType.SByte: - case TagDataType.Time: + _setValueProp = wrt; + } + break; + + case TagDataType.Bool: + case TagDataType.Byte: + case TagDataType.SByte: + case TagDataType.Time: case TagDataType.LTime: - case TagDataType.Date: - case TagDataType.TimeOfDay: + case TagDataType.Date: + case TagDataType.TimeOfDay: case TagDataType.LTimeOfDay: - case TagDataType.Word: - case TagDataType.BCDByte: - case TagDataType.Int: - case TagDataType.S5Time: - case TagDataType.BCDWord: - case TagDataType.BCDDWord: - case TagDataType.Dint: - case TagDataType.Dword: - case TagDataType.Float: - case TagDataType.LInt: - case TagDataType.LWord: - case TagDataType.LReal: - case TagDataType.DateTime: + case TagDataType.Word: + case TagDataType.BCDByte: + case TagDataType.Int: + case TagDataType.S5Time: + case TagDataType.BCDWord: + case TagDataType.BCDDWord: + case TagDataType.Dint: + case TagDataType.Dword: + case TagDataType.Float: + case TagDataType.LInt: + case TagDataType.LWord: + case TagDataType.LReal: + case TagDataType.DateTime: + { + if (ArraySize < 2) { - if (ArraySize < 2) + switch (this.TagDataType) { - switch (this.TagDataType) - { - case TagDataType.Bool: - _setValueProp = libnodave.getBit(buff[startpos], BitAddress); - break; - case TagDataType.Byte: - _setValueProp = buff[startpos]; - break; - case TagDataType.SByte: - _setValueProp = libnodave.getS8from(buff, startpos); - break; - case TagDataType.Time: - _setValueProp = libnodave.getTimefrom(buff, startpos); - break; + case TagDataType.Bool: + _setValueProp = libnodave.getBit(buff[startpos], BitAddress); + break; + case TagDataType.Byte: + _setValueProp = buff[startpos]; + break; + case TagDataType.SByte: + _setValueProp = libnodave.getS8from(buff, startpos); + break; + case TagDataType.Time: + _setValueProp = libnodave.getTimefrom(buff, startpos); + break; case TagDataType.LTime: _setValueProp = libnodave.getLTimefrom(buff, startpos); break; - case TagDataType.Date: - _setValueProp = libnodave.getDatefrom(buff, startpos); - break; - case TagDataType.TimeOfDay: - _setValueProp = libnodave.getTimeOfDayfrom(buff, startpos); + case TagDataType.Date: + _setValueProp = libnodave.getDatefrom(buff, startpos); + break; + case TagDataType.TimeOfDay: + _setValueProp = libnodave.getTimeOfDayfrom(buff, startpos); break; case TagDataType.LTimeOfDay: _setValueProp = libnodave.getLTimeOfDayfrom(buff, startpos); - break; - case TagDataType.Word: - _setValueProp = libnodave.getU16from(buff, startpos); - break; - case TagDataType.BCDByte: - _setValueProp = libnodave.getBCD8from(buff, startpos); - break; - case TagDataType.Int: - _setValueProp = libnodave.getS16from(buff, startpos); - break; - case TagDataType.S5Time: - _setValueProp = libnodave.getS5Timefrom(buff, startpos); - break; - case TagDataType.BCDWord: - _setValueProp = libnodave.getBCD16from(buff, startpos); - break; - case TagDataType.BCDDWord: - _setValueProp = libnodave.getBCD32from(buff, startpos); - break; - case TagDataType.Dint: - _setValueProp = libnodave.getS32from(buff, startpos); - break; - case TagDataType.Dword: - _setValueProp = libnodave.getU32from(buff, startpos); - break; - case TagDataType.LWord: - _setValueProp = libnodave.getU64from(buff, startpos); - break; - case TagDataType.LInt: - _setValueProp = libnodave.getS64from(buff, startpos); - break; - case TagDataType.Float: - _setValueProp = libnodave.getFloatfrom(buff, startpos); - break; - case TagDataType.LReal: - _setValueProp = libnodave.getDoublefrom(buff, startpos); - break; - case TagDataType.DateTime: - _setValueProp = libnodave.getDateTimefrom(buff, startpos); - break; - } + break; + case TagDataType.Word: + _setValueProp = libnodave.getU16from(buff, startpos); + break; + case TagDataType.BCDByte: + _setValueProp = libnodave.getBCD8from(buff, startpos); + break; + case TagDataType.Int: + _setValueProp = libnodave.getS16from(buff, startpos); + break; + case TagDataType.S5Time: + _setValueProp = libnodave.getS5Timefrom(buff, startpos); + break; + case TagDataType.BCDWord: + _setValueProp = libnodave.getBCD16from(buff, startpos); + break; + case TagDataType.BCDDWord: + _setValueProp = libnodave.getBCD32from(buff, startpos); + break; + case TagDataType.Dint: + _setValueProp = libnodave.getS32from(buff, startpos); + break; + case TagDataType.Dword: + _setValueProp = libnodave.getU32from(buff, startpos); + break; + case TagDataType.LWord: + _setValueProp = libnodave.getU64from(buff, startpos); + break; + case TagDataType.LInt: + _setValueProp = libnodave.getS64from(buff, startpos); + break; + case TagDataType.Float: + _setValueProp = libnodave.getFloatfrom(buff, startpos); + break; + case TagDataType.LReal: + _setValueProp = libnodave.getDoublefrom(buff, startpos); + break; + case TagDataType.DateTime: + _setValueProp = libnodave.getDateTimefrom(buff, startpos); + break; } - else + } + else + { + switch (this.TagDataType) { - switch (this.TagDataType) - { - case TagDataType.Bool: + case TagDataType.Bool: + { + var values = new List(); + var akBit = BitAddress; + var akbyte = startpos; + for (int n = 0; n < ArraySize; n++) { - var values = new List(); - var akBit = BitAddress; - var akbyte = startpos; - for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getBit(buff[akbyte], akBit)); + akBit++; + if (akBit > 7) { - values.Add(libnodave.getBit(buff[akbyte], akBit)); - akBit++; - if (akBit > 7) - { - akBit = 0; - akbyte++; - } + akBit = 0; + akbyte++; } - - _setValueProp = values.ToArray(); - break; - } - case TagDataType.Byte: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(buff[startpos + n * mSize]); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.SByte: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getS8from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.Time: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getTimefrom(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; } + + _setValueProp = values.ToArray(); + break; + } + case TagDataType.Byte: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(buff[startpos + n * mSize]); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.SByte: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getS8from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.Time: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getTimefrom(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } case TagDataType.LTime: { var values = new List(); @@ -2012,21 +2043,21 @@ internal virtual void _readValueFromBuffer(byte[] buff, int startpos) _setValueProp = values.ToArray(); break; } - case TagDataType.Date: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getDatefrom(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.TimeOfDay: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getTimeOfDayfrom(buff, startpos + n * mSize)); + case TagDataType.Date: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getDatefrom(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.TimeOfDay: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getTimeOfDayfrom(buff, startpos + n * mSize)); _setValueProp = values.ToArray(); break; } @@ -2036,131 +2067,131 @@ internal virtual void _readValueFromBuffer(byte[] buff, int startpos) var mSize = _internalGetBaseTypeSize(); for (int n = 0; n < ArraySize; n++) values.Add(libnodave.getLTimeOfDayfrom(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.Word: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getU16from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.BCDByte: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getBCD8from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.Int: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getS16from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.S5Time: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getS5Timefrom(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.BCDWord: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getBCD16from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.BCDDWord: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getBCD32from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.Dint: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getS32from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.Dword: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getU32from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.Float: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getFloatfrom(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.LInt: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getS64from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.LWord: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getU64from(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.LReal: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getDoublefrom(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - case TagDataType.DateTime: - { - var values = new List(); - var mSize = _internalGetBaseTypeSize(); - for (int n = 0; n < ArraySize; n++) - values.Add(libnodave.getDateTimefrom(buff, startpos + n * mSize)); - _setValueProp = values.ToArray(); - break; - } - } + _setValueProp = values.ToArray(); + break; + } + case TagDataType.Word: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getU16from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.BCDByte: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getBCD8from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.Int: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getS16from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.S5Time: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getS5Timefrom(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.BCDWord: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getBCD16from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.BCDDWord: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getBCD32from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.Dint: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getS32from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.Dword: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getU32from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.Float: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getFloatfrom(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.LInt: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getS64from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.LWord: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getU64from(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.LReal: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getDoublefrom(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } + case TagDataType.DateTime: + { + var values = new List(); + var mSize = _internalGetBaseTypeSize(); + for (int n = 0; n < ArraySize; n++) + values.Add(libnodave.getDateTimefrom(buff, startpos + n * mSize)); + _setValueProp = values.ToArray(); + break; + } } } - break; - } + } + break; + } } catch (Exception ex) { @@ -2259,7 +2290,7 @@ internal virtual int _internalGetBaseTypeSize() return 0; } - #region Events for Tag Checking + #region Events for Tag Checking private bool _raiseValueChangedOnFirstRead = true; public bool RaiseValueChangedOnFirstRead diff --git a/LibNoDaveConnectionLibrary/Communication/S7_xxx/SZLDatasets.cs b/LibNoDaveConnectionLibrary/Communication/S7_xxx/SZLDatasets.cs index ac0f80d6..ce4abf15 100644 --- a/LibNoDaveConnectionLibrary/Communication/S7_xxx/SZLDatasets.cs +++ b/LibNoDaveConnectionLibrary/Communication/S7_xxx/SZLDatasets.cs @@ -6,7 +6,7 @@ namespace DotNetSiemensPLCToolBoxLibrary.Communication.S7_xxx { - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public abstract class SZLDataset { } @@ -19,7 +19,7 @@ public class DefaultSZLDataset : SZLDataset [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public class xy00Dataset : SZLDataset { - [Endian(Endianness.BigEndian)] + [Endian(Endianness.BigEndian)] private ushort _szlId; public UInt16 SZL_id { @@ -31,7 +31,7 @@ public UInt16 SZL_id [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public class xy11Dataset : SZLDataset { - [Endian(Endianness.BigEndian)] + [Endian(Endianness.BigEndian)] private short _index; public short Index { @@ -39,15 +39,15 @@ public short Index set { _index = value; } } - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] - private string _mlfB; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] + private string _mlfB; public string MlfB { get { return _mlfB; } set { _mlfB = value; } } - [Endian(Endianness.BigEndian)] + [Endian(Endianness.BigEndian)] private ushort _bgTyp; public UInt16 BGTyp { @@ -213,7 +213,7 @@ public UInt16 Reman set { _reman = value; } } } - + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public class xy15Dataset : SZLDataset { @@ -718,7 +718,7 @@ public byte[] aseg set { _aseg = value; } } - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] private byte[] _eseg; public byte[] eseg { @@ -1005,7 +1005,7 @@ public byte ver set { _ver = value; } } - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)] + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)] private byte[] _res; public byte[] res { @@ -1403,6 +1403,187 @@ public byte[] res } } + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + public class xy32_4Dataset : SZLDataset + { + //[Endian(Endianness.BigEndian)] + private ushort _index; + /// + /// · Byte 1: B#16#04: CPU-Schutzstufe, Bedienschalterstellungen und Prüfsummen + /// · Byte 0:Standard-CPU: B#16#00H-CPU: Bits 0 bis 2: Baugruppenträger-Nr.Bit 3: 0 = Reserve-CPU, 1 = Master-CPUBits 4 bis 7: 1111 + /// + public UInt16 Index + { + get { return _index; } + set { _index = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _sch_schal; + /// + /// Durch Betriebsartenschalter eingestellte Schutzstufe (1, 2, 3) + /// + public UInt16 sch_schal + { + get { return _sch_schal; } + set { _sch_schal = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _sch_par; + /// + /// Parametrierte Schutzstufe (0, 1, 2, 3; 0: kein Paßword vergeben, parametrierte Schutzstufe ungültig). + /// + public UInt16 sch_par + { + get { return _sch_par; } + set { _sch_par = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _sch_rel; + /// + /// Gültige Schutzstufe der CPU + /// + public UInt16 sch_rel + { + get { return _sch_rel; } + set { _sch_rel = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _bart_sch; + /// + /// Stellung des Betriebsartenschalters (1:RUN, 2:RUN-P, 3:STOP, 4:MRES, 0:undefiniert bzw. nicht ermittelbar) + /// + public UInt16 bart_sch + { + get { return _bart_sch; } + set { _bart_sch = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _anl_sch; + /// + /// Stellung des Anlaufartenschalters (1:CRST, 2:WRST, 0:undefiniert, nicht vorhanden oder nicht ermittelbar) + /// + public UInt16 anl_sch + { + get { return _anl_sch; } + set { _anl_sch = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _ken_f; + /// + /// Reserviert + /// + public UInt16 ken_f + { + get { return _ken_f; } + set { _ken_f = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _ken_rel; + /// + /// Kennung für die Gültigkeit der vier folgenden Prüfsummen (0: ungültig) + /// + public UInt16 ken_rel + { + get { return _ken_rel; } + set { _ken_rel = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _ken_ver1_hw; + /// + /// Prüfsumme 1 der Hardwarekonfiguration (Intel-Format):Exklusiv-Oder-Verknüpfung über die Längen aller Systemdatenbausteine + /// + public UInt16 ken_ver1_hw + { + get { return _ken_ver1_hw; } + set { _ken_ver1_hw = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _ken_ver2_hw; + /// + /// Prüfsumme 2 der Hardwarekonfiguration (Intel-Format):Exklusiv-Oder-Verknüpfung über die Prüfsummen aller Systemdatenbausteine + /// + public UInt16 ken_ver2_hw + { + get { return _ken_ver2_hw; } + set { _ken_ver2_hw = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _ken_ver1_awp; + /// + /// Prüfsumme 1 des Anwenderprogramms (Intel-Format):Exklusiv-Oder-Verknüpfung über die Längen der folgenden Bausteine: OBs, DBs, FBs, FCs + /// + public UInt16 ken_ver1_awp + { + get { return _ken_ver1_awp; } + set { _ken_ver1_awp = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _ken_ver2_awp; + /// + /// Prüfsumme 2 des Anwenderprogramms (Intel-Format):Exklusiv-Oder-Verknüpfung über die Prüfsummen der folgenden Bausteine: OBs, DBs, FBs, FCs + /// + public UInt16 ken_ver2_awp + { + get { return _ken_ver2_awp; } + set { _ken_ver2_awp = value; } + } + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] + private ushort[] _res; + /// + /// Reserviert + /// + public UInt16[] res + { + get { return _res; } + set { _res = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _sfc_req; + /// + /// Anforderung von Schutzstufe 2 bzw. 3 durch SFC 109 (1: Anforderung ist erfolgt) + /// + public UInt16 sfc_req + { + get { return _sfc_req; } + set { _sfc_req = value; } + } + + [Endian(Endianness.BigEndian)] + private ushort _sfc_act; + /// + /// Aktivierung von Schutzstufe 2 bzw. 3 durch SFC 109 (1: Aktivierung ist erfolgt) + /// + public UInt16 sfc_act + { + get { return _sfc_act; } + set { _sfc_act = value; } + } + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + private ushort[] _res2; + /// + /// Reserviert + /// + public UInt16[] res2 + { + get { return _res2; } + set { _res2 = value; } + } + } + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public class xy32_8Dataset : SZLDataset { diff --git a/LibNoDaveConnectionLibrary/DataTypes/NCK_Area.cs b/LibNoDaveConnectionLibrary/DataTypes/NCK_Area.cs new file mode 100644 index 00000000..455fba4b --- /dev/null +++ b/LibNoDaveConnectionLibrary/DataTypes/NCK_Area.cs @@ -0,0 +1,43 @@ + +namespace DotNetSiemensPLCToolBoxLibrary.DataTypes +{ + public enum NCK_Area : byte + { + /// + /// N: NC Daten + /// + AreaNCK = 0, + /// + /// B: Daten Betriebsartengruppe + /// + AreaBag = 1, + /// + /// C: Kanalzugeordnete Daten + /// + AreaChannel = 2, + /// + /// A: Achsspezifische Grundeinstellungen + /// + AreaAxis = 3, + /// + /// T: Wergzeugdaten + /// + AreaTool = 4, + /// + /// V: Vorschubantrieb + /// + AreaFeedDrive = 5, + /// + /// H: Hauptantrieb + /// + AreaMainDrive = 6, + /// + /// M: MMC-Daten + /// + AreaMMC = 7, + /// + /// ?: Unbekannt + /// + AreaUnknown = 255, + } +} diff --git a/LibNoDaveConnectionLibrary/DataTypes/Projectfolders/ExportFormat.cs b/LibNoDaveConnectionLibrary/DataTypes/Projectfolders/ExportFormat.cs index 66464efd..5ec3e075 100644 --- a/LibNoDaveConnectionLibrary/DataTypes/Projectfolders/ExportFormat.cs +++ b/LibNoDaveConnectionLibrary/DataTypes/Projectfolders/ExportFormat.cs @@ -1,14 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders -{ - public enum ExportFormat - { - Default, - Text, - Xml, - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders +{ + public enum ExportFormat + { + Default, + Text, + Xml, + } +} diff --git a/LibNoDaveConnectionLibrary/DotNetSiemensPLCToolBoxLibrary.csproj b/LibNoDaveConnectionLibrary/DotNetSiemensPLCToolBoxLibrary.csproj index 53e43ec4..6ce1150f 100644 --- a/LibNoDaveConnectionLibrary/DotNetSiemensPLCToolBoxLibrary.csproj +++ b/LibNoDaveConnectionLibrary/DotNetSiemensPLCToolBoxLibrary.csproj @@ -60,7 +60,6 @@ S7ReachablePLCDialog.cs - @@ -133,6 +132,7 @@ + diff --git a/ReadTools/App.config b/ReadTools/App.config new file mode 100644 index 00000000..8e156463 --- /dev/null +++ b/ReadTools/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ReadTools/Enums.cs b/ReadTools/Enums.cs new file mode 100644 index 00000000..4a425396 --- /dev/null +++ b/ReadTools/Enums.cs @@ -0,0 +1,396 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ToolReader +{ + public enum NCK_Block + { + // Summary: + // Systemdaten + BlockY = 16, + // + // Summary: + // NCK-Anweisungsgruppen + BlockYNCFL = 17, + // + // Summary: + // Einstellbare Nullpunktverschiebung + BlockFU = 18, + // + // Summary: + // Aktive Nullpunktverschiebung + BlockFA = 19, + // + // Summary: + // Schneidendaten, Korrekturdaten + BlockTO = 20, + // + // Summary: + // Rechenparameter + BlockRP = 21, + // + // Summary: + // Settingdaten + BlockSE = 22, + // + // Summary: + // SGUD-Block + BlockSGUD = 23, + // + // Summary: + // Lokale Benutzerdaten + BlockLUD = 24, + // + // Summary: + // Werkzeugträger-Parameter + BlockTC = 25, + // + // Summary: + // Maschinendaten + BlockM = 26, + BlockWAL = 28, + // + // Summary: + // Diagnosedaten, die nur für entwicklungsinterne Zwecke + BlockDIAG = 30, + BlockCC = 31, + // + // Summary: + // Externe Nullpunktverschiebung + BlockFE = 32, + // + // Summary: + // Werkzeugdaten, allgemeine Daten + BlockTD = 33, + // + // Summary: + // Schneidendaten, Ãœberwachungsdaten + BlockTS = 34, + // + // Summary: + // Werkzeugdaten, schleifspezifische Daten + BlockTG = 35, + // + // Summary: + // Werkzeugdaten, anwenderdefinierte Daten + BlockTU = 36, + // + // Summary: + // Schneidendaten, anwenderdefinierte Daten + BlockTUE = 37, + // + // Summary: + // Werkzeugdaten, Verzeichnis + BlockTV = 38, + // + // Summary: + // Magazindaten, allgemeine Daten + BlockTM = 39, + // + // Summary: + // Magazindaten, Platzdaten + BlockTP = 40, + // + // Summary: + // Magazindaten, Mehrfachzuordnung von Platzdaten + BlockTPM = 41, + // + // Summary: + // Magazindaten, Platztypen + BlockTT = 42, + // + // Summary: + // Magazindaten, Verzeichnis + BlockTMV = 43, + // + // Summary: + // Magazindaten, Konfigurationsdaten + BlockTMC = 44, + // + // Summary: + // MGUD-Block + BlockMGUD = 45, + // + // Summary: + // UGUD-Block + BlockUGUD = 46, + // + // Summary: + // GUD4-Block + BlockGUD4 = 47, + // + // Summary: + // GUD5-Block + BlockGUD5 = 48, + // + // Summary: + // GUD6-Block + BlockGUD6 = 49, + // + // Summary: + // GUD7-Block + BlockGUD7 = 50, + // + // Summary: + // GUD8-Block + BlockGUD8 = 51, + // + // Summary: + // GUD9-Block + BlockGUD9 = 52, + // + // Summary: + // Schutzbereiche + BlockPA = 53, + BlockGD1 = 54, + // + // Summary: + // Nibbling + BlockNIB = 55, + // + // Summary: + // Event-Typen + BlockETP = 56, + // + // Summary: + // Datenlisten für die Protokollierung + BlockETPD = 57, + // + // Summary: + // Kanalspezifische Synchronaktionen + BlockSYNACT = 58, + // + // Summary: + // Diagnosebaustein + BlockDIAGN = 59, + // + // Summary: + // NCK-spezifische Anwendervariablen für Synchronaktion + BlockVSYN = 60, + // + // Summary: + // Ãœberwachungsanwenderdaten + BlockTUS = 61, + // + // Summary: + // Magazin-Anwenderdaten + BlockTUM = 62, + // + // Summary: + // Magazinplatz-Anwenderdaten + BlockTUP = 63, + BlockTF = 64, + // + // Summary: + // Basisframe: einstellbarer Frame, der immer wirkt + BlockFB = 65, + // + // Summary: + // Spindelzustandsdaten bei Spindelumsetzung + BlockSSP2 = 66, + // + // Summary: + // Programmglobale Benutzerdaten + BlockPUD = 67, + // + // Summary: + // Schneidenbezogene ortsabhängige Summenkorrekturen + BlockTOS = 68, + // + // Summary: + // Schneidenbezogene ortsabhängige Summenkorrekturen + BlockTOST = 69, + // + // Summary: + // Schneidenbezogene ortsabhängige Summenkorrekturen + BlockTOE = 70, + // + // Summary: + // Schneidenbezogene ortsabhängige Summenkorrekturen + BlockTOET = 71, + // + // Summary: + // Adapterdaten + BlockAD = 72, + // + // Summary: + // Schneidendaten, transformierte Korrekturdaten + BlockTOT = 73, + // + // Summary: + // Arbeitskorrekturen: Verzeichnis + BlockAEV = 74, + // + // Summary: + // NCK-Anweisungsgruppen Fanuc + BlockYFAFL = 75, + // + // Summary: + // System-Frame + BlockFS = 76, + // + // Summary: + // Servo-Daten + BlockSD = 77, + // + // Summary: + // Applikationsspezifische Daten + BlockTAD = 78, + // + // Summary: + // Applikationsspezifische Schneidendaten + BlockTAO = 79, + // + // Summary: + // Applikationsspezifische Ãœberwachungsdaten + BlockTAS = 80, + // + // Summary: + // Applikationsspezifische Magazindaten + BlockTAM = 81, + // + // Summary: + // Applikationsspezifische Magazinplatzdaten + BlockTAP = 82, + BlockMEM = 83, + // + // Summary: + // Alarm-Ereignisse, ältestes zuerst + BlockSALAC = 84, + // + // Summary: + // Aktive Hilfsfunktionen + BlockAUXFU = 85, + BlockTDC = 86, + BlockCP = 87, + eBCK_BlockSDME = 110, + // + // Summary: + // Programmzeiger bei Unterbrechung + BlockSPARPI = 111, + // + // Summary: + // erweiterte Zustandsdaten im WKS + BlockSEGA = 112, + // + // Summary: + // Erweiterte Zustandsdaten im MKS + BlockSEMA = 113, + // + // Summary: + // Zustandsdaten Spindel + BlockSSP = 114, + // + // Summary: + // Zustandsdaten im WKS + BlockSGA = 115, + // + // Summary: + // Zustandsdaten im MKS + BlockSMA = 116, + // + // Summary: + // Letzter Alarm + BlockSALAL = 117, + // + // Summary: + // Alarm mit top Priorität + BlockSALAP = 118, + BlockSALA = 119, + // + // Summary: + // Synchronaktionen + BlockSSYNAC = 120, + // + // Summary: + // Programmzeiger für Satzsuchlauf + BlockSPARPF = 121, + // + // Summary: + // Programmzeiger im Automatikbetrieb + BlockSPARPP = 122, + // + // Summary: + // Aktive G-Funktionen + BlockSNCF = 123, + // + // Summary: + // Teileprogramminformation + BlockSPARP = 125, + // + // Summary: + // Teileprogrammspezifische Zustandsdaten + BlockSINF = 126, + // + // Summary: + // Zustandsdaten + BlockS = 127, + Block0x80 = 128, + Block0x81 = 129, + Block0x82 = 130, + Block0x83 = 131, + Block0x84 = 132, + Block0x85 = 133, + // + // Summary: + // Intern + BlockO = 253, + // + // Summary: + // Unbekannt + BlockUnknown = 255, + } + + public enum NCK_BlockTVColumn + { + One=1, + Two=2, + TNo = 3, + Identnumber = 4, + Duplo = 5, + Edges= 6, + Depot = 7, + Place = 8, + Nine= 9 + } + + public enum NCK_BlockTSLine + { ///////////////////////////////////////////////////////// + ////// Beginn mit Datenbaustein TS (Schneidedaten, Korrekturdaten): + + // //P1 = Vorwarngrenze Standzeit in Minuten ($TC_MOP1) + // //P2 = Verbleibende Standzeit in Minuten ($TC_MOP2) + // //P3 = Vorwarngrenze Stückzahl ($TC_MOP3) + // //P4 = verbleibende Stückzahl ($TC_MOP4) + // //P5 = Sollstandzeit ($TC_MOP11) + // //P6 = Sollstückzahl ($TC_MOP13) + // //P7 = Vorwarngrenze Verschleiß (Vorwarngrenze) (ab SW 5.1) ($TC_MOP5) + // // Dieser Parameter kann nur gesetzt werden, wenn Bit 5 von Maschinendatum $MN_MM_TOOL_MANAGEMENT_MASK entsprechend gesetzt ist. + // //P8 = verbleibender Verschleiß (Istwert) (ab SW 5.1) ($TC_MOP6) nicht schreibbar + // //P9 = Sollwert Verschleiß (ab SW 5.1) ($TC_MOP15) + ThreshholdTime = 1, + Resdurabillity = 2, + ThreshholdParts = 3, + RestParts = 4, + SetTime = 5, + SetParts = 6, + ThreshholdWastage = 7, + RestWastage = 8, + SetWastage = 9 + } + //public enum NCK_BlockTSLine + //{ + // One = 1, + // Two = 2, + // TNo = 3, + // Four = 4, + // Five = 5, + // Six = 6, + // Seven = 7, + // Eight = 8, + // Nine = 9 + //} +} diff --git a/ReadTools/Form1.Designer.cs b/ReadTools/Form1.Designer.cs new file mode 100644 index 00000000..cb2b61b0 --- /dev/null +++ b/ReadTools/Form1.Designer.cs @@ -0,0 +1,343 @@ +namespace ToolReader +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.cmdConnect = new System.Windows.Forms.Button(); + this.cmdConfig = new System.Windows.Forms.Button(); + this.lblStatus = new System.Windows.Forms.Label(); + this.btnReadTools = new System.Windows.Forms.Button(); + this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.lblActualParts = new System.Windows.Forms.Label(); + this.lblTotalParts = new System.Windows.Forms.Label(); + this.lblCounter = new System.Windows.Forms.Label(); + this.lblCycleTime = new System.Windows.Forms.Label(); + this.idDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.toolIdentNumberDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.duploDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.edgesDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.internalToolNumberDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.depotDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.placeDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.lockedDataGridViewCheckBoxColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn(); + this.lastRestDurabilityDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.restDurabilityTotalMinutesDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.maxTimeTotalMinutesDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.statusDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.maxTimeDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.currTimeDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.restDurabilityDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.toolDataBindingSource = new System.Windows.Forms.BindingSource(this.components); + this.lblOldProgNetTime = new System.Windows.Forms.Label(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.toolDataBindingSource)).BeginInit(); + this.SuspendLayout(); + // + // cmdConnect + // + this.cmdConnect.Location = new System.Drawing.Point(251, 12); + this.cmdConnect.Name = "cmdConnect"; + this.cmdConnect.Size = new System.Drawing.Size(227, 27); + this.cmdConnect.TabIndex = 2; + this.cmdConnect.Text = "Connect"; + this.cmdConnect.UseVisualStyleBackColor = true; + this.cmdConnect.Click += new System.EventHandler(this.cmdConnect_Click); + // + // cmdConfig + // + this.cmdConfig.Location = new System.Drawing.Point(12, 12); + this.cmdConfig.Name = "cmdConfig"; + this.cmdConfig.Size = new System.Drawing.Size(227, 27); + this.cmdConfig.TabIndex = 3; + this.cmdConfig.Text = "Config"; + this.cmdConfig.UseVisualStyleBackColor = true; + this.cmdConfig.Click += new System.EventHandler(this.cmdConfig_Click); + // + // lblStatus + // + this.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.lblStatus.Location = new System.Drawing.Point(12, 42); + this.lblStatus.Name = "lblStatus"; + this.lblStatus.Size = new System.Drawing.Size(466, 36); + this.lblStatus.TabIndex = 4; + this.lblStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // btnReadTools + // + this.btnReadTools.Location = new System.Drawing.Point(12, 81); + this.btnReadTools.Name = "btnReadTools"; + this.btnReadTools.Size = new System.Drawing.Size(466, 27); + this.btnReadTools.TabIndex = 5; + this.btnReadTools.Text = "Read"; + this.btnReadTools.UseVisualStyleBackColor = true; + this.btnReadTools.Click += new System.EventHandler(this.btnReadTools_Click); + // + // dataGridView1 + // + this.dataGridView1.AllowUserToAddRows = false; + this.dataGridView1.AllowUserToDeleteRows = false; + this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.dataGridView1.AutoGenerateColumns = false; + this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.idDataGridViewTextBoxColumn, + this.toolIdentNumberDataGridViewTextBoxColumn, + this.duploDataGridViewTextBoxColumn, + this.edgesDataGridViewTextBoxColumn, + this.internalToolNumberDataGridViewTextBoxColumn, + this.depotDataGridViewTextBoxColumn, + this.placeDataGridViewTextBoxColumn, + this.lockedDataGridViewCheckBoxColumn, + this.lastRestDurabilityDataGridViewTextBoxColumn, + this.restDurabilityTotalMinutesDataGridViewTextBoxColumn, + this.maxTimeTotalMinutesDataGridViewTextBoxColumn, + this.statusDataGridViewTextBoxColumn, + this.maxTimeDataGridViewTextBoxColumn, + this.currTimeDataGridViewTextBoxColumn, + this.restDurabilityDataGridViewTextBoxColumn}); + this.dataGridView1.DataSource = this.toolDataBindingSource; + this.dataGridView1.Location = new System.Drawing.Point(12, 126); + this.dataGridView1.Name = "dataGridView1"; + this.dataGridView1.ReadOnly = true; + this.dataGridView1.Size = new System.Drawing.Size(1206, 330); + this.dataGridView1.TabIndex = 6; + // + // lblActualParts + // + this.lblActualParts.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.lblActualParts.Location = new System.Drawing.Point(641, 7); + this.lblActualParts.Name = "lblActualParts"; + this.lblActualParts.Size = new System.Drawing.Size(180, 36); + this.lblActualParts.TabIndex = 7; + this.lblActualParts.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // lblTotalParts + // + this.lblTotalParts.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.lblTotalParts.Location = new System.Drawing.Point(641, 72); + this.lblTotalParts.Name = "lblTotalParts"; + this.lblTotalParts.Size = new System.Drawing.Size(180, 36); + this.lblTotalParts.TabIndex = 8; + this.lblTotalParts.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // lblCounter + // + this.lblCounter.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.lblCounter.Location = new System.Drawing.Point(858, 7); + this.lblCounter.Name = "lblCounter"; + this.lblCounter.Size = new System.Drawing.Size(180, 36); + this.lblCounter.TabIndex = 9; + this.lblCounter.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // lblCycleTime + // + this.lblCycleTime.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.lblCycleTime.Location = new System.Drawing.Point(858, 72); + this.lblCycleTime.Name = "lblCycleTime"; + this.lblCycleTime.Size = new System.Drawing.Size(180, 36); + this.lblCycleTime.TabIndex = 10; + this.lblCycleTime.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // idDataGridViewTextBoxColumn + // + this.idDataGridViewTextBoxColumn.DataPropertyName = "Id"; + this.idDataGridViewTextBoxColumn.HeaderText = "Id"; + this.idDataGridViewTextBoxColumn.Name = "idDataGridViewTextBoxColumn"; + this.idDataGridViewTextBoxColumn.ReadOnly = true; + // + // toolIdentNumberDataGridViewTextBoxColumn + // + this.toolIdentNumberDataGridViewTextBoxColumn.DataPropertyName = "ToolIdentNumber"; + this.toolIdentNumberDataGridViewTextBoxColumn.HeaderText = "ToolIdentNumber"; + this.toolIdentNumberDataGridViewTextBoxColumn.Name = "toolIdentNumberDataGridViewTextBoxColumn"; + this.toolIdentNumberDataGridViewTextBoxColumn.ReadOnly = true; + // + // duploDataGridViewTextBoxColumn + // + this.duploDataGridViewTextBoxColumn.DataPropertyName = "Duplo"; + this.duploDataGridViewTextBoxColumn.HeaderText = "Duplo"; + this.duploDataGridViewTextBoxColumn.Name = "duploDataGridViewTextBoxColumn"; + this.duploDataGridViewTextBoxColumn.ReadOnly = true; + // + // edgesDataGridViewTextBoxColumn + // + this.edgesDataGridViewTextBoxColumn.DataPropertyName = "Edges"; + this.edgesDataGridViewTextBoxColumn.HeaderText = "Edges"; + this.edgesDataGridViewTextBoxColumn.Name = "edgesDataGridViewTextBoxColumn"; + this.edgesDataGridViewTextBoxColumn.ReadOnly = true; + // + // internalToolNumberDataGridViewTextBoxColumn + // + this.internalToolNumberDataGridViewTextBoxColumn.DataPropertyName = "InternalToolNumber"; + this.internalToolNumberDataGridViewTextBoxColumn.HeaderText = "InternalToolNumber"; + this.internalToolNumberDataGridViewTextBoxColumn.Name = "internalToolNumberDataGridViewTextBoxColumn"; + this.internalToolNumberDataGridViewTextBoxColumn.ReadOnly = true; + // + // depotDataGridViewTextBoxColumn + // + this.depotDataGridViewTextBoxColumn.DataPropertyName = "Depot"; + this.depotDataGridViewTextBoxColumn.HeaderText = "Depot"; + this.depotDataGridViewTextBoxColumn.Name = "depotDataGridViewTextBoxColumn"; + this.depotDataGridViewTextBoxColumn.ReadOnly = true; + // + // placeDataGridViewTextBoxColumn + // + this.placeDataGridViewTextBoxColumn.DataPropertyName = "Place"; + this.placeDataGridViewTextBoxColumn.HeaderText = "Place"; + this.placeDataGridViewTextBoxColumn.Name = "placeDataGridViewTextBoxColumn"; + this.placeDataGridViewTextBoxColumn.ReadOnly = true; + // + // lockedDataGridViewCheckBoxColumn + // + this.lockedDataGridViewCheckBoxColumn.DataPropertyName = "Locked"; + this.lockedDataGridViewCheckBoxColumn.HeaderText = "Locked"; + this.lockedDataGridViewCheckBoxColumn.Name = "lockedDataGridViewCheckBoxColumn"; + this.lockedDataGridViewCheckBoxColumn.ReadOnly = true; + // + // lastRestDurabilityDataGridViewTextBoxColumn + // + this.lastRestDurabilityDataGridViewTextBoxColumn.DataPropertyName = "LastRestDurability"; + this.lastRestDurabilityDataGridViewTextBoxColumn.HeaderText = "LastRestDurability"; + this.lastRestDurabilityDataGridViewTextBoxColumn.Name = "lastRestDurabilityDataGridViewTextBoxColumn"; + this.lastRestDurabilityDataGridViewTextBoxColumn.ReadOnly = true; + // + // restDurabilityTotalMinutesDataGridViewTextBoxColumn + // + this.restDurabilityTotalMinutesDataGridViewTextBoxColumn.DataPropertyName = "RestDurabilityTotalMinutes"; + this.restDurabilityTotalMinutesDataGridViewTextBoxColumn.HeaderText = "RestDurabilityTotalMinutes"; + this.restDurabilityTotalMinutesDataGridViewTextBoxColumn.Name = "restDurabilityTotalMinutesDataGridViewTextBoxColumn"; + this.restDurabilityTotalMinutesDataGridViewTextBoxColumn.ReadOnly = true; + // + // maxTimeTotalMinutesDataGridViewTextBoxColumn + // + this.maxTimeTotalMinutesDataGridViewTextBoxColumn.DataPropertyName = "MaxTimeTotalMinutes"; + this.maxTimeTotalMinutesDataGridViewTextBoxColumn.HeaderText = "MaxTimeTotalMinutes"; + this.maxTimeTotalMinutesDataGridViewTextBoxColumn.Name = "maxTimeTotalMinutesDataGridViewTextBoxColumn"; + this.maxTimeTotalMinutesDataGridViewTextBoxColumn.ReadOnly = true; + // + // statusDataGridViewTextBoxColumn + // + this.statusDataGridViewTextBoxColumn.DataPropertyName = "Status"; + this.statusDataGridViewTextBoxColumn.HeaderText = "Status"; + this.statusDataGridViewTextBoxColumn.Name = "statusDataGridViewTextBoxColumn"; + this.statusDataGridViewTextBoxColumn.ReadOnly = true; + // + // maxTimeDataGridViewTextBoxColumn + // + this.maxTimeDataGridViewTextBoxColumn.DataPropertyName = "MaxTime"; + this.maxTimeDataGridViewTextBoxColumn.HeaderText = "MaxTime"; + this.maxTimeDataGridViewTextBoxColumn.Name = "maxTimeDataGridViewTextBoxColumn"; + this.maxTimeDataGridViewTextBoxColumn.ReadOnly = true; + // + // currTimeDataGridViewTextBoxColumn + // + this.currTimeDataGridViewTextBoxColumn.DataPropertyName = "CurrTime"; + this.currTimeDataGridViewTextBoxColumn.HeaderText = "CurrTime"; + this.currTimeDataGridViewTextBoxColumn.Name = "currTimeDataGridViewTextBoxColumn"; + this.currTimeDataGridViewTextBoxColumn.ReadOnly = true; + // + // restDurabilityDataGridViewTextBoxColumn + // + this.restDurabilityDataGridViewTextBoxColumn.DataPropertyName = "RestDurability"; + this.restDurabilityDataGridViewTextBoxColumn.HeaderText = "RestDurability"; + this.restDurabilityDataGridViewTextBoxColumn.Name = "restDurabilityDataGridViewTextBoxColumn"; + this.restDurabilityDataGridViewTextBoxColumn.ReadOnly = true; + // + // toolDataBindingSource + // + this.toolDataBindingSource.DataSource = typeof(ToolReader.ToolData); + // + // lblOldProgNetTime + // + this.lblOldProgNetTime.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.lblOldProgNetTime.Location = new System.Drawing.Point(1044, 72); + this.lblOldProgNetTime.Name = "lblOldProgNetTime"; + this.lblOldProgNetTime.Size = new System.Drawing.Size(180, 36); + this.lblOldProgNetTime.TabIndex = 11; + this.lblOldProgNetTime.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1220, 523); + this.Controls.Add(this.lblOldProgNetTime); + this.Controls.Add(this.lblCycleTime); + this.Controls.Add(this.lblCounter); + this.Controls.Add(this.lblTotalParts); + this.Controls.Add(this.lblActualParts); + this.Controls.Add(this.dataGridView1); + this.Controls.Add(this.btnReadTools); + this.Controls.Add(this.lblStatus); + this.Controls.Add(this.cmdConnect); + this.Controls.Add(this.cmdConfig); + this.Name = "Form1"; + this.Text = "Form1"; + ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.toolDataBindingSource)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Button cmdConnect; + private System.Windows.Forms.Button cmdConfig; + private System.Windows.Forms.Label lblStatus; + private System.Windows.Forms.Button btnReadTools; + private System.Windows.Forms.DataGridView dataGridView1; + private System.Windows.Forms.DataGridViewTextBoxColumn idDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn machineIdDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewCheckBoxColumn newAddedDataGridViewCheckBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn toolIdentNumberDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn duploDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn edgesDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn alternativeToolIdentNumberDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn internalToolNumberDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn depotDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn placeDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewCheckBoxColumn lockedDataGridViewCheckBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn lastRestDurabilityDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn restDurabilityTotalMinutesDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn maxTimeTotalMinutesDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn statusDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn maxTimeDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn currTimeDataGridViewTextBoxColumn; + private System.Windows.Forms.DataGridViewTextBoxColumn restDurabilityDataGridViewTextBoxColumn; + private System.Windows.Forms.BindingSource toolDataBindingSource; + private System.Windows.Forms.Label lblActualParts; + private System.Windows.Forms.Label lblTotalParts; + private System.Windows.Forms.Label lblCounter; + private System.Windows.Forms.Label lblCycleTime; + private System.Windows.Forms.Label lblOldProgNetTime; + } +} + diff --git a/ReadTools/Form1.cs b/ReadTools/Form1.cs new file mode 100644 index 00000000..b70a3c7f --- /dev/null +++ b/ReadTools/Form1.cs @@ -0,0 +1,438 @@ +using DotNetSiemensPLCToolBoxLibrary.Communication; +using DotNetSiemensPLCToolBoxLibrary.DataTypes; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Diagnostics; +using System.Drawing; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ToolReader +{ + public partial class Form1 : Form + { + public Form1() + { + InitializeComponent(); + } + + private void cmdConfig_Click(object sender, EventArgs e) + { + DotNetSiemensPLCToolBoxLibrary.Communication.Configuration.ShowConfiguration("Toolreader", true); + } + + private DotNetSiemensPLCToolBoxLibrary.Communication.PLCConnection myConn = null; + private void cmdConnect_Click(object sender, EventArgs e) + { + DoConnect(); + } + + private void DoConnect() + { + try + { + lblStatus.Text = ""; + myConn = new PLCConnection("Toolreader"); + myConn.Connect(); + lblStatus.Text = "Connected!"; + } + catch (Exception ex) + { + lblStatus.Text = ex.Message; + } + } + + + public List GetToolList() + { + //toolData = new List(); + bool error = false; + + + Stopwatch sw = new Stopwatch(); + sw.Start(); + if (myConn == null) + DoConnect(); + // Meherere Datenbausteine müssen eingelesen werden + + ///////////////////////////////////////////////////// + // Beginn mit Datenbaustein TP (MagazinPlatzdaten): + + + + List toolData = new List(); + + PLCNckTag tag = new PLCNckTag(); + + // Anzahl der Werkzeuge + tag.NckArea = NCK_Area.AreaTool; + tag.NckModule = (int)NCK_Block.BlockTV; + + tag.NckLinecount = 1; + tag.NckUnit = 1; + + tag.NckLine = 1; + tag.NckColumn = 1; + tag.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Word; + + + myConn.ReadValue(tag); + ushort numberOfTools = 0; + object o = new object(); + numberOfTools = (UInt16)tag.Value; + + + List tagList = new List(); + + int line = 1; + for (int i = 0; i < numberOfTools; i++) + { + try + { + //--------------------- InternalNumber Tno ------------------------------- + PLCNckTag internalToolNumber = new PLCNckTag(); + internalToolNumber.NckArea = NCK_Area.AreaTool; + internalToolNumber.NckModule = (int)NCK_Block.BlockTV; + + internalToolNumber.NckLinecount = 1; + internalToolNumber.NckUnit = 1; + + internalToolNumber.NckLine = line; + internalToolNumber.NckColumn = (int)NCK_BlockTVColumn.TNo; + internalToolNumber.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Word; + internalToolNumber.Tag = "InternalToolNumber"; + tagList.Add(internalToolNumber); + + + //--------------------- IdentNumber ------------------------------- + + PLCNckTag identNumber = new PLCNckTag(); + identNumber.NckArea = NCK_Area.AreaTool; + identNumber.NckModule = (int)NCK_Block.BlockTV; + + identNumber.NckLinecount = 1; + identNumber.NckUnit = 1; + + identNumber.NckLine = line; + identNumber.NckColumn = (int)NCK_BlockTVColumn.Identnumber; + + identNumber.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.CharArray; + identNumber.ArraySize = 32; + identNumber.Tag = "ToolIdentNumber"; + tagList.Add(identNumber); + //--------------------- Duplo ------------------------------- + PLCNckTag duploNumber = new PLCNckTag(); + duploNumber.NckArea = NCK_Area.AreaTool; + duploNumber.NckModule = (int)NCK_Block.BlockTV; + + duploNumber.NckLinecount = 1; + duploNumber.NckUnit = 1; + + duploNumber.NckLine = line; + duploNumber.NckColumn = (int)NCK_BlockTVColumn.Duplo; + duploNumber.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Word; + duploNumber.Tag = "Duplo"; + tagList.Add(duploNumber); + + //--------------------- Edges ------------------------------- + + PLCNckTag edges = new PLCNckTag(); + edges.NckArea = NCK_Area.AreaTool; + edges.NckModule = (int)NCK_Block.BlockTV; + + edges.NckLinecount = 1; + edges.NckUnit = 1; + + edges.NckLine = line; + edges.NckColumn = (int)NCK_BlockTVColumn.Edges; + edges.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Word; + edges.Tag = "Edges"; + tagList.Add(edges); + + + //--------------------- Depot ------------------------------- + + PLCNckTag depot = new PLCNckTag(); + depot.NckArea = NCK_Area.AreaTool; + depot.NckModule = (int)NCK_Block.BlockTV; + + depot.NckLinecount = 1; + depot.NckUnit = 1; + + depot.NckLine = line; + depot.NckColumn = (int)NCK_BlockTVColumn.Depot; + depot.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Word; + depot.Tag = "Depot"; + tagList.Add(depot); + + + + //--------------------- Place ------------------------------- + PLCNckTag place = new PLCNckTag(); + place.NckArea = NCK_Area.AreaTool; + place.NckModule = (int)NCK_Block.BlockTV; + + place.NckLinecount = 1; + place.NckUnit = 1; + + place.NckLine = line; + place.NckColumn = (int)NCK_BlockTVColumn.Place; + place.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Word; + place.Tag = "Place"; + tagList.Add(place); + + + + ++line; + } + catch (Exception ex) + { + + } + } + + //PLCNckTag p = new PLCNckTag(); + //p.NckArea = (int)NCK_Area.AreaTool; + //p.NckModule = (int)NCK_Block.BlockTV; + + //p.NckLinecount = 1; + //p.NckUnit = 1; + + //p.NckLine = 1; + //p.NckColumn = (int)NCK_BlockTVColumn.Place; + //p.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Word; + //p.Tag = "Place"; + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + //tagList.Add(p); + + myConn.ReadValues(tagList); + + + foreach (var nckTag in tagList) + { + if (nckTag.Tag != null) + { + + + ToolData td = toolData.Where(w => w.Id == nckTag.NckLine).FirstOrDefault(); + if (td == null) + { + td = new ToolData(); + td.Id = nckTag.NckLine; + toolData.Add(td); + } + + PropertyInfo prop = typeof(ToolData).GetProperty(nckTag.Tag.ToString(), BindingFlags.Public | BindingFlags.Instance); + if (null != prop && prop.CanWrite) + { + try + { + prop.SetValue(td, Convert.ChangeType(nckTag.Value, prop.PropertyType), null); + }catch{} + } + + + } + } + tagList = new List(); + foreach (var td in toolData) + { + + ///////////////////////////////////////////////////////// + ////// Beginn mit Datenbaustein TS (Schneidedaten, Korrekturdaten): + + // //P1 = Vorwarngrenze Standzeit in Minuten ($TC_MOP1) + // //P2 = Verbleibende Standzeit in Minuten ($TC_MOP2) + // //P3 = Vorwarngrenze Stückzahl ($TC_MOP3) + // //P4 = verbleibende Stückzahl ($TC_MOP4) + // //P5 = Sollstandzeit ($TC_MOP11) + // //P6 = Sollstückzahl ($TC_MOP13) + // //P7 = Vorwarngrenze Verschleiß (Vorwarngrenze) (ab SW 5.1) ($TC_MOP5) + // // Dieser Parameter kann nur gesetzt werden, wenn Bit 5 von Maschinendatum $MN_MM_TOOL_MANAGEMENT_MASK entsprechend gesetzt ist. + // //P8 = verbleibender Verschleiß (Istwert) (ab SW 5.1) ($TC_MOP6) nicht schreibbar + // //P9 = Sollwert Verschleiß (ab SW 5.1) ($TC_MOP15) + // // Dieser Parameter kann nur gesetzt werden, wenn Bit 5 von Maschinendatum + // // $MN_MM_TOOL_MANAGEMENT_MASK entsprechend gesetzt ist. + PLCNckTag restdurabillity = new PLCNckTag(); + restdurabillity.NckArea = NCK_Area.AreaTool; + restdurabillity.NckModule = (int)NCK_Block.BlockTS; + + restdurabillity.NckLinecount = 1; + restdurabillity.NckUnit = 1; + + restdurabillity.NckLine = (int)NCK_BlockTSLine.Resdurabillity; + restdurabillity.NckColumn = int.Parse(td.InternalToolNumber); + restdurabillity.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.LReal; + restdurabillity.Tag = "RestDurability"; + tagList.Add(restdurabillity); + + PLCNckTag setTime = new PLCNckTag(); + setTime.NckArea = NCK_Area.AreaTool; + setTime.NckModule = (int)NCK_Block.BlockTS; + + setTime.NckLinecount = 1; + setTime.NckUnit = 1; + + setTime.NckLine = (int)NCK_BlockTSLine.SetTime; + setTime.NckColumn = int.Parse(td.InternalToolNumber); + setTime.TagDataType = DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.LReal; + setTime.Tag = "MaxTime"; + tagList.Add(setTime); + + } + + + myConn.ReadValues(tagList); + + foreach (var nckTag in tagList) + { + if (nckTag.Tag != null) + { + ToolData td = toolData.Where(w => w.InternalToolNumber == nckTag.NckColumn.ToString()).FirstOrDefault(); + if (td != null) + { + PropertyInfo prop = typeof(ToolData).GetProperty(nckTag.Tag.ToString(), BindingFlags.Public | BindingFlags.Instance); + if (null != prop && prop.CanWrite) + { + try + { + if (prop.PropertyType == typeof(TimeSpan)) + { + TimeSpan ts = TimeSpan.FromSeconds(double.Parse(nckTag.Value.ToString())); + prop.SetValue(td, ts, null); + } + else + { + prop.SetValue(td, Convert.ChangeType(nckTag.Value, prop.PropertyType), null); + } + } + catch + { + + } + } + + } + + } + } + + if (error) + Console.WriteLine("Error GetTools in Aglink happened"); + + + sw.Stop(); + + lblStatus.Text = sw.ElapsedMilliseconds.ToString() + " ms"; + if (toolData != null) + { + dataGridView1.DataSource = toolData; + } + return toolData; + } + + private void btnReadTools_Click(object sender, EventArgs e) + { + GetToolList(); + GetParts(); + } + + private void GetParts() + { + Stopwatch sw = new Stopwatch(); + sw.Start(); + if (myConn == null) + DoConnect(); + + List tagList = new List(); + + try + { + //--------------------- ActualParts ------------------------------- + + NC_Var actualParts = new NC_Var(); + actualParts.Bereich_u_einheit = 0x40; + actualParts.Spalte = 0x79; + actualParts.Zeile = 0x1; + actualParts.Bausteintyp = 0x7F; + actualParts.ZEILENANZAHL = 0x1; + actualParts.Typ = 0xF; + actualParts.Laenge = 0x8; + + + + PLCNckTag actualPartsTag = actualParts.GetNckTag(1, 0); + actualPartsTag.Tag = "ActualParts"; + tagList.Add(actualPartsTag); + + //--------------------- TotalParts ------------------------------- + + NC_Var totalParts = new NC_Var(); + totalParts.Bereich_u_einheit = 0x40; + totalParts.Spalte = 0x78; + totalParts.Zeile = 0x1; + totalParts.Bausteintyp = 0x7F; + totalParts.ZEILENANZAHL = 0x1; + totalParts.Typ = 0xF; + totalParts.Laenge = 0x8; + + PLCNckTag totalPartsTag = totalParts.GetNckTag(1, 0); + totalPartsTag.Tag = "TotalParts"; + tagList.Add(totalPartsTag); + + + + NC_Var partsCounter = new NC_Var(0x82, 0x40, 0x6CE8, 0x1, 0x1A, 0x1, 0x7, 0x4); // soll 272 sein, dann ists richtig aktiviert + PLCNckTag counterPartsTag = partsCounter.GetNckTag(1, 0); + tagList.Add(counterPartsTag); + + + NC_Var cycleTime = new NC_Var(0x82, 0x40, 0x9, 0x1, 0x3B, 0x1, 0xF, 0x8); + PLCNckTag cycleTimeTag = cycleTime.GetNckTag(1, 0); + tagList.Add(cycleTimeTag); + + NC_Var oldProgNetTime = new NC_Var(0x82, 0x40, 0x12A, 0x1, 0x7F, 0x1, 0xF, 0x8); + PLCNckTag oldProgNetTimeTag = oldProgNetTime.GetNckTag(1, 0); + tagList.Add(oldProgNetTimeTag); + + oldProgNetTimeTag.Value = 12.3; + myConn.WriteValue(oldProgNetTimeTag); + + myConn.ReadValues(tagList); + + lblActualParts.Text = String.Format("Actual: {0}", actualPartsTag.Value.ToString()); + lblTotalParts.Text = String.Format("Total: {0}", totalPartsTag.Value.ToString()); + lblCounter.Text = String.Format("Counter: {0}", counterPartsTag.Value.ToString()); + lblCycleTime.Text = String.Format("CycleTime: {0}", cycleTimeTag.Value.ToString()); + lblOldProgNetTime.Text = String.Format("OldProgNetTime: {0}", oldProgNetTimeTag.Value.ToString()); + + } + catch + { + + } + + sw.Stop(); + + lblStatus.Text += " "+ sw.ElapsedMilliseconds.ToString() + " ms"; + } + + } +} diff --git a/ReadTools/Form1.resx b/ReadTools/Form1.resx new file mode 100644 index 00000000..c15a11d9 --- /dev/null +++ b/ReadTools/Form1.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/ReadTools/Program.cs b/ReadTools/Program.cs new file mode 100644 index 00000000..1408a2ae --- /dev/null +++ b/ReadTools/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ToolReader +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/ReadTools/Properties/AssemblyInfo.cs b/ReadTools/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..c507192c --- /dev/null +++ b/ReadTools/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ReadTools")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ReadTools")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("4db7bd11-740a-44b1-983b-998b8846fcfa")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ReadTools/Properties/DataSources/ToolData.datasource b/ReadTools/Properties/DataSources/ToolData.datasource new file mode 100644 index 00000000..2b711417 --- /dev/null +++ b/ReadTools/Properties/DataSources/ToolData.datasource @@ -0,0 +1,10 @@ + + + + ToolReader.ToolData, ToolReader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + \ No newline at end of file diff --git a/ReadTools/Properties/Resources.Designer.cs b/ReadTools/Properties/Resources.Designer.cs new file mode 100644 index 00000000..7be71f7c --- /dev/null +++ b/ReadTools/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ToolReader.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ToolReader.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/ReadTools/Properties/Resources.resx b/ReadTools/Properties/Resources.resx new file mode 100644 index 00000000..af7dbebb --- /dev/null +++ b/ReadTools/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ReadTools/Properties/Settings.Designer.cs b/ReadTools/Properties/Settings.Designer.cs new file mode 100644 index 00000000..28c23655 --- /dev/null +++ b/ReadTools/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ToolReader.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "12.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/ReadTools/Properties/Settings.settings b/ReadTools/Properties/Settings.settings new file mode 100644 index 00000000..39645652 --- /dev/null +++ b/ReadTools/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/ReadTools/ToolData.cs b/ReadTools/ToolData.cs new file mode 100644 index 00000000..aa068323 --- /dev/null +++ b/ReadTools/ToolData.cs @@ -0,0 +1,195 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Serialization; + +namespace ToolReader +{ + public class ToolData : IDisposable + { + + + #region CTOR + + public ToolData() + { + RestDurability = TimeSpan.MinValue; + } + + public ToolData(string depot, string place) + : this() + { + SetLocation(depot, place); + } + + #endregion + + + public int Id { get; set; } + private string _ToolIdentNumber; + + /// + /// Tool Number called from withing NcProgram + /// + public string ToolIdentNumber + { + get + { + return _ToolIdentNumber; + } + set + { + if (_ToolIdentNumber != value) + { + _ToolIdentNumber = value; + } + } + } + + /// + /// Sister tool number. + /// + public int Duplo { get; set; } + public int Edges { get; set; } + + /// + /// Unique number of tool (Siemens T-Number). + /// + public string InternalToolNumber { get; set; } + + public string Depot { get; set; } + + public string Place { get; set; } + + private bool _Locked = false; + + public bool Locked + { + get { return _Locked; } + set + { + + if (_Locked != value) + { + _Locked = value; + } + } + } + + public TimeSpan LastRestDurability { get; set; } + + private TimeSpan _MaxTime = TimeSpan.MinValue; + + public double RestDurabilityTotalMinutes + { + get + { + return RestDurability.TotalMinutes; + } + } + + public double MaxTimeTotalMinutes + { + get + { + return MaxTime.TotalMinutes; + } + } + + + public string Status + { + get + { + return Locked ? "gesperrt" : "ok"; // Todo: Ãœbersetzung, weitere Statusse + } + } + + public TimeSpan MaxTime + { + get + { + return _MaxTime; + } + set + { + if (_MaxTime != value) + { + _MaxTime = value; + } + } + } + + public TimeSpan CurrTime { get; set; } + + + TimeSpan _RestDurability = TimeSpan.MinValue; + + /// + /// Rest durability. + /// + public TimeSpan RestDurability + { + get { return _RestDurability; } + set + { + _RestDurability = value; + + } + } + public void SetLocation(string depot, string place) + { + Depot = depot; + Place = place; + } + + public override string ToString() + { + try + { + return String.Format("Ident:{0} Internal:{1} RestDura:{2} Location:{3}/{4}", ToolIdentNumber, InternalToolNumber, RestDurability.TotalSeconds, Depot, Place); + } + catch + { + return base.ToString(); + } + } + + public override bool Equals(object obj) + { + ToolData comp = obj as ToolData; + bool result = false; + try + { + if (comp == null && obj != null) + { + result = true; + } + else + { + if ((comp == null && obj != null) || (comp != null && obj == null)) + result = false; + else + result = this.ToolIdentNumber == comp.ToolIdentNumber && this.Duplo == comp.Duplo && this.InternalToolNumber == comp.InternalToolNumber; + } + } + catch + { + } + return result; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } + + public void Dispose() + { + } + } + +} diff --git a/ReadTools/ToolReader.csproj b/ReadTools/ToolReader.csproj new file mode 100644 index 00000000..92542f11 --- /dev/null +++ b/ReadTools/ToolReader.csproj @@ -0,0 +1,102 @@ + + + + + Debug + AnyCPU + {C0C1142F-592C-4F26-9655-A737C74DAF7E} + WinExe + Properties + ToolReader + ToolReader + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + True + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + {e3ed87e8-b550-46ac-9196-9688d30efd29} + DotNetSiemensPLCToolBoxLibrary + + + + + copy /Y "$(SolutionDir)externalDlls\libnodave\libnodave_jfkmod.dll" "$(TargetDir)libnodave_jfkmod.dll" +copy /Y "$(SolutionDir)externalDlls\libnodave\libnodave_jfkmod64.dll" "$(TargetDir)libnodave_jfkmod64.dll" + + + \ No newline at end of file diff --git a/TiaGitHandler/App.config b/TiaGitHandler/App.config index fad249e4..8e156463 100644 --- a/TiaGitHandler/App.config +++ b/TiaGitHandler/App.config @@ -1,6 +1,6 @@ - - - - - + + + + + \ No newline at end of file diff --git a/TiaGitHandler/Program.cs b/TiaGitHandler/Program.cs index 87a10c04..599eb94d 100644 --- a/TiaGitHandler/Program.cs +++ b/TiaGitHandler/Program.cs @@ -1,270 +1,270 @@ -using System; -using System.IO; -using System.Linq; -using System.Text; -using System.Windows.Forms; -using System.Xml; -using DotNetSiemensPLCToolBoxLibrary.DataTypes; -using DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders; -using DotNetSiemensPLCToolBoxLibrary.Projectfiles; - -namespace TiaGitHandler -{ - class Program - { - private static string folder = ""; - - [STAThread] - static void Main(string[] args) - { - - string file = ""; - string exportPath = ""; - - if (args.Count() < 1) - { - OpenFileDialog op = new OpenFileDialog(); - op.Filter = "TIA-Portal Project|*.ap13;*.ap14"; - op.CheckFileExists = false; - op.ValidateNames = false; - var ret = op.ShowDialog(); - if (ret == DialogResult.OK) - { - file = op.FileName; - } - else - { - Console.WriteLine("Bitte S7 projekt als Parameter angeben!"); - return; - } - - exportPath = Path.GetDirectoryName(file); - exportPath = Path.GetFullPath(Path.Combine(exportPath, "..\\Export")); - if (Directory.Exists(exportPath)) - { - if ( - MessageBox.Show(exportPath + " wird gelöscht. Möchten Sie fortfahren?", "Sicherheitsabfrage", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) - { - Directory.Delete(exportPath, true); - } - else - { - Environment.Exit(-1); - } - - } - Directory.CreateDirectory(exportPath); - - } - else - { - file = args[0]; - } - - var prj = Projects.LoadProject(file, false); - - ParseFolder(prj.ProjectStructure, exportPath); - - //Console.ReadLine(); - } - - private class EncodingStringWriter : StringWriter - { - private readonly Encoding _encoding; - - public EncodingStringWriter(StringBuilder builder, Encoding encoding) : base(builder) - { - _encoding = encoding; - } - - public override Encoding Encoding - { - get { return _encoding; } - } - } - - private static void ParseFolder(ProjectFolder folder, string dir) - { - //Directory.CreateDirectory(dir); - var path = Path.Combine(dir, NormalizeFolderName(folder.Name)); - - foreach (var projectFolder in folder.SubItems) - { - ParseFolder(projectFolder, path); - } - - if (folder is IBlocksFolder) - { - var blkFld = folder as IBlocksFolder; - - foreach (var projectBlockInfo in blkFld.BlockInfos) - { - try - { - var src = projectBlockInfo.Export(ExportFormat.Default); - string xml = null; - if (src != null) - { - var ext = "xml"; - if (projectBlockInfo.BlockLanguage == PLCLanguage.DB && projectBlockInfo.BlockType == PLCBlockType.DB) - { - ext = "db"; - xml = projectBlockInfo.Export(ExportFormat.Xml); - } - else if (projectBlockInfo.BlockLanguage == PLCLanguage.SCL) - { - ext = "scl"; - xml = projectBlockInfo.Export(ExportFormat.Xml); - } - else if (projectBlockInfo.BlockLanguage == PLCLanguage.KOP) - { - ext = "xml"; - } - else if (projectBlockInfo.BlockLanguage == PLCLanguage.FUP) - { - ext = "xml"; - } - else if (projectBlockInfo.BlockLanguage == PLCLanguage.AWL) - { - ext = "awl"; - xml = projectBlockInfo.Export(ExportFormat.Xml); - } - else if (projectBlockInfo.BlockType == PLCBlockType.UDT) - { - ext = "udt"; - } - var file = Path.Combine(path, projectBlockInfo.Name.Replace("\\", "_").Replace("/", "_") + "." + ext); - var xmlfile = Path.Combine(path, projectBlockInfo.Name.Replace("\\", "_").Replace("/", "_") + ".xml"); - - var xmlValid = false; - XmlDocument xmlDoc = new XmlDocument(); - try - { - xmlDoc.LoadXml(src); - xmlValid = true; - } - catch - { - xmlValid = false; - } - - if (xmlValid) - { - try - { - var nodes = xmlDoc.SelectNodes("//Created"); - var node = nodes[0]; - node.ParentNode.RemoveChild(node); - } - catch - { - } - try - { - var nodes = xmlDoc.SelectNodes("//DocumentInfo"); - var node = nodes[0]; - node.ParentNode.RemoveChild(node); - } - catch - { - } - - StringBuilder sb = new StringBuilder(); - XmlWriterSettings settings = new XmlWriterSettings - { - Indent = true, - IndentChars = " ", - NewLineChars = "\r\n", - NewLineHandling = NewLineHandling.Replace - }; - using (TextWriter writer = new EncodingStringWriter(sb, Encoding.UTF8)) - { - xmlDoc.Save(writer); - } - src = sb.ToString(); - } - - if (src != null && ext != "db") - { - Directory.CreateDirectory(path); - File.WriteAllText(file, src, new UTF8Encoding(true)); - } - - if (xml != null) - { - var xmlValid2 = false; - XmlDocument xmlDoc2 = new XmlDocument(); - try - { - xmlDoc2.LoadXml(xml); - xmlValid2 = true; - } - catch - { - xmlValid2 = false; - } - - if (xmlValid2) - { - try - { - var nodes = xmlDoc2.SelectNodes("//Created"); - var node = nodes[0]; - node.ParentNode.RemoveChild(node); - } - catch - { - } - try - { - var nodes = xmlDoc2.SelectNodes("//DocumentInfo"); - var node = nodes[0]; - node.ParentNode.RemoveChild(node); - } - catch - { - } - - StringBuilder sb = new StringBuilder(); - XmlWriterSettings settings = new XmlWriterSettings - { - Indent = true, - IndentChars = " ", - NewLineChars = "\r\n", - NewLineHandling = NewLineHandling.Replace - }; - using (TextWriter writer = new EncodingStringWriter(sb, Encoding.UTF8)) - { - xmlDoc2.Save(writer); - } - - xml = sb.ToString(); - - xml = xml.Replace("SCL", "STL"); - } - - Directory.CreateDirectory(path); - File.WriteAllText(xmlfile, xml, new UTF8Encoding(true)); - - } - } - else - { - Console.WriteLine("Skipping Block (null)" + projectBlockInfo.Name); - } - } - catch (Exception ex) - { - Console.WriteLine("Skipping Block: \"" + projectBlockInfo.Name + "\" Exception: " + ex.Message); - } - } - } - } - - private static string NormalizeFolderName(string name) - { - return name.Replace("-", "").Replace(".", "").Replace(" ", ""); - } - } -} +using System; +using System.IO; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using System.Xml; +using DotNetSiemensPLCToolBoxLibrary.DataTypes; +using DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders; +using DotNetSiemensPLCToolBoxLibrary.Projectfiles; + +namespace TiaGitHandler +{ + class Program + { + private static string folder = ""; + + [STAThread] + static void Main(string[] args) + { + + string file = ""; + string exportPath = ""; + + if (args.Count() < 1) + { + OpenFileDialog op = new OpenFileDialog(); + op.Filter = "TIA-Portal Project|*.ap13;*.ap14"; + op.CheckFileExists = false; + op.ValidateNames = false; + var ret = op.ShowDialog(); + if (ret == DialogResult.OK) + { + file = op.FileName; + } + else + { + Console.WriteLine("Bitte S7 projekt als Parameter angeben!"); + return; + } + + exportPath = Path.GetDirectoryName(file); + exportPath = Path.GetFullPath(Path.Combine(exportPath, "..\\Export")); + if (Directory.Exists(exportPath)) + { + if ( + MessageBox.Show(exportPath + " wird gelöscht. Möchten Sie fortfahren?", "Sicherheitsabfrage", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + Directory.Delete(exportPath, true); + } + else + { + Environment.Exit(-1); + } + + } + Directory.CreateDirectory(exportPath); + + } + else + { + file = args[0]; + } + + var prj = Projects.LoadProject(file, false); + + ParseFolder(prj.ProjectStructure, exportPath); + + //Console.ReadLine(); + } + + private class EncodingStringWriter : StringWriter + { + private readonly Encoding _encoding; + + public EncodingStringWriter(StringBuilder builder, Encoding encoding) : base(builder) + { + _encoding = encoding; + } + + public override Encoding Encoding + { + get { return _encoding; } + } + } + + private static void ParseFolder(ProjectFolder folder, string dir) + { + //Directory.CreateDirectory(dir); + var path = Path.Combine(dir, NormalizeFolderName(folder.Name)); + + foreach (var projectFolder in folder.SubItems) + { + ParseFolder(projectFolder, path); + } + + if (folder is IBlocksFolder) + { + var blkFld = folder as IBlocksFolder; + + foreach (var projectBlockInfo in blkFld.BlockInfos) + { + try + { + var src = projectBlockInfo.Export(ExportFormat.Default); + string xml = null; + if (src != null) + { + var ext = "xml"; + if (projectBlockInfo.BlockLanguage == PLCLanguage.DB && projectBlockInfo.BlockType == PLCBlockType.DB) + { + ext = "db"; + xml = projectBlockInfo.Export(ExportFormat.Xml); + } + else if (projectBlockInfo.BlockLanguage == PLCLanguage.SCL) + { + ext = "scl"; + xml = projectBlockInfo.Export(ExportFormat.Xml); + } + else if (projectBlockInfo.BlockLanguage == PLCLanguage.KOP) + { + ext = "xml"; + } + else if (projectBlockInfo.BlockLanguage == PLCLanguage.FUP) + { + ext = "xml"; + } + else if (projectBlockInfo.BlockLanguage == PLCLanguage.AWL) + { + ext = "awl"; + xml = projectBlockInfo.Export(ExportFormat.Xml); + } + else if (projectBlockInfo.BlockType == PLCBlockType.UDT) + { + ext = "udt"; + } + var file = Path.Combine(path, projectBlockInfo.Name.Replace("\\", "_").Replace("/", "_") + "." + ext); + var xmlfile = Path.Combine(path, projectBlockInfo.Name.Replace("\\", "_").Replace("/", "_") + ".xml"); + + var xmlValid = false; + XmlDocument xmlDoc = new XmlDocument(); + try + { + xmlDoc.LoadXml(src); + xmlValid = true; + } + catch + { + xmlValid = false; + } + + if (xmlValid) + { + try + { + var nodes = xmlDoc.SelectNodes("//Created"); + var node = nodes[0]; + node.ParentNode.RemoveChild(node); + } + catch + { + } + try + { + var nodes = xmlDoc.SelectNodes("//DocumentInfo"); + var node = nodes[0]; + node.ParentNode.RemoveChild(node); + } + catch + { + } + + StringBuilder sb = new StringBuilder(); + XmlWriterSettings settings = new XmlWriterSettings + { + Indent = true, + IndentChars = " ", + NewLineChars = "\r\n", + NewLineHandling = NewLineHandling.Replace + }; + using (TextWriter writer = new EncodingStringWriter(sb, Encoding.UTF8)) + { + xmlDoc.Save(writer); + } + src = sb.ToString(); + } + + if (src != null && ext != "db") + { + Directory.CreateDirectory(path); + File.WriteAllText(file, src, new UTF8Encoding(true)); + } + + if (xml != null) + { + var xmlValid2 = false; + XmlDocument xmlDoc2 = new XmlDocument(); + try + { + xmlDoc2.LoadXml(xml); + xmlValid2 = true; + } + catch + { + xmlValid2 = false; + } + + if (xmlValid2) + { + try + { + var nodes = xmlDoc2.SelectNodes("//Created"); + var node = nodes[0]; + node.ParentNode.RemoveChild(node); + } + catch + { + } + try + { + var nodes = xmlDoc2.SelectNodes("//DocumentInfo"); + var node = nodes[0]; + node.ParentNode.RemoveChild(node); + } + catch + { + } + + StringBuilder sb = new StringBuilder(); + XmlWriterSettings settings = new XmlWriterSettings + { + Indent = true, + IndentChars = " ", + NewLineChars = "\r\n", + NewLineHandling = NewLineHandling.Replace + }; + using (TextWriter writer = new EncodingStringWriter(sb, Encoding.UTF8)) + { + xmlDoc2.Save(writer); + } + + xml = sb.ToString(); + + xml = xml.Replace("SCL", "STL"); + } + + Directory.CreateDirectory(path); + File.WriteAllText(xmlfile, xml, new UTF8Encoding(true)); + + } + } + else + { + Console.WriteLine("Skipping Block (null)" + projectBlockInfo.Name); + } + } + catch (Exception ex) + { + Console.WriteLine("Skipping Block: \"" + projectBlockInfo.Name + "\" Exception: " + ex.Message); + } + } + } + } + + private static string NormalizeFolderName(string name) + { + return name.Replace("-", "").Replace(".", "").Replace(" ", ""); + } + } +} diff --git a/TiaGitHandler/Properties/AssemblyInfo.cs b/TiaGitHandler/Properties/AssemblyInfo.cs index 20b27843..29c9d58a 100644 --- a/TiaGitHandler/Properties/AssemblyInfo.cs +++ b/TiaGitHandler/Properties/AssemblyInfo.cs @@ -1,36 +1,36 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// Allgemeine Informationen über eine Assembly werden über die folgenden -// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, -// die einer Assembly zugeordnet sind. -[assembly: AssemblyTitle("TiaGitHandler")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("TiaGitHandler")] -[assembly: AssemblyCopyright("Copyright © 2016")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar -// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von -// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. -[assembly: ComVisible(false)] - -// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird -[assembly: Guid("147cd31f-f449-4568-af62-56b987851d26")] - -// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: -// -// Hauptversion -// Nebenversion -// Buildnummer -// Revision -// -// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern -// übernehmen, indem Sie "*" eingeben: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("TiaGitHandler")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TiaGitHandler")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("147cd31f-f449-4568-af62-56b987851d26")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TiaGitHandler/TiaGitHandler.csproj b/TiaGitHandler/TiaGitHandler.csproj index 3ca818b9..a1e6b308 100644 --- a/TiaGitHandler/TiaGitHandler.csproj +++ b/TiaGitHandler/TiaGitHandler.csproj @@ -1,66 +1,66 @@ - - - - - Debug - AnyCPU - {147CD31F-F449-4568-AF62-56B987851D26} - Exe - Properties - TiaGitHandler - TiaGitHandler - v4.5 - 512 - - - AnyCPU - true - full - false - ..\compiled\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - {e3ed87e8-b550-46ac-9196-9688d30efd29} - DotNetSiemensPLCToolBoxLibrary - - - - + + + + + Debug + AnyCPU + {147CD31F-F449-4568-AF62-56B987851D26} + Exe + Properties + TiaGitHandler + TiaGitHandler + v4.5 + 512 + + + AnyCPU + true + full + false + ..\compiled\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + {e3ed87e8-b550-46ac-9196-9688d30efd29} + DotNetSiemensPLCToolBoxLibrary + + + + \ No newline at end of file diff --git a/ToolBoxLibUnitTests/ConnectionWrapper.cs b/ToolBoxLibUnitTests/ConnectionWrapper.cs index 47bbba63..9e8d92c2 100644 --- a/ToolBoxLibUnitTests/ConnectionWrapper.cs +++ b/ToolBoxLibUnitTests/ConnectionWrapper.cs @@ -229,11 +229,26 @@ public int daveGetNCProgram(string filename, byte[] buffer, ref int length) throw new NotImplementedException(); } + public int daveGetNcFile(string filename, byte[] buffer, ref int length) + { + throw new NotImplementedException(); + } + + public int daveGetNcFileSize(string filename, ref int length) + { + throw new NotImplementedException(); + } + public int davePutNCProgram(string filename, string path, string ts, byte[] buffer, int length) { throw new NotImplementedException(); } + public int alarmQueryAlarm_S(byte[] buffer, int length, ref int alarmCount) + { + throw new NotImplementedException(); + } + public int daveReadPLCTime(out DateTime dateTime) { throw new NotImplementedException(); diff --git a/ToolBoxLibUnitTests/TestCRC.cs b/ToolBoxLibUnitTests/TestCRC.cs index ee815c5e..1810db0e 100644 --- a/ToolBoxLibUnitTests/TestCRC.cs +++ b/ToolBoxLibUnitTests/TestCRC.cs @@ -1,7 +1,7 @@ using DotNetSiemensPLCToolBoxLibrary.General; using NUnit.Framework; -namespace ToolBoxLibUnitTests +namespace ToolBoxLibUnitTests { [TestFixture] public class TestCRC @@ -10,9 +10,9 @@ public class TestCRC public void TestCRC16BlockSum() { //see: https://www.sps-forum.de/hochsprachen-opc/56412-upload-von-block-zur-s7-2.html - var bytes = new byte[] { 0x0C, 0x00, 0x0E, 0xC0, 0x00, 0xC1, 0x00, 0xC3, 0x00, 0xC4, 0x00, 0xC5, 0x00, 0xD8, 0x80, 0x65, 0x00 }; + var bytes = new byte[] { 0x0C, 0x00, 0x0E, 0xC0, 0x00, 0xC1, 0x00, 0xC3, 0x00, 0xC4, 0x00, 0xC5, 0x00, 0xD8, 0x80, 0x65, 0x00 }; var crc = CrcHelper.GetCrc16(bytes); Assert.AreEqual(crc, 0x7822); } - } -} + } +} diff --git a/externalDlls/libnodave/libnodave_jfkmod.dll b/externalDlls/libnodave/libnodave_jfkmod.dll new file mode 100644 index 00000000..44f366be Binary files /dev/null and b/externalDlls/libnodave/libnodave_jfkmod.dll differ diff --git a/externalDlls/libnodave/libnodave_jfkmod.exp b/externalDlls/libnodave/libnodave_jfkmod.exp new file mode 100644 index 00000000..f7a08af5 Binary files /dev/null and b/externalDlls/libnodave/libnodave_jfkmod.exp differ diff --git a/externalDlls/libnodave/libnodave_jfkmod.lib b/externalDlls/libnodave/libnodave_jfkmod.lib new file mode 100644 index 00000000..48b46f57 Binary files /dev/null and b/externalDlls/libnodave/libnodave_jfkmod.lib differ diff --git a/externalDlls/libnodave/libnodave_jfkmod64.dll b/externalDlls/libnodave/libnodave_jfkmod64.dll new file mode 100644 index 00000000..3f21da4b Binary files /dev/null and b/externalDlls/libnodave/libnodave_jfkmod64.dll differ diff --git a/externalDlls/libnodave/libnodave_jfkmod64.exp b/externalDlls/libnodave/libnodave_jfkmod64.exp new file mode 100644 index 00000000..c966fc2f Binary files /dev/null and b/externalDlls/libnodave/libnodave_jfkmod64.exp differ diff --git a/externalDlls/libnodave/libnodave_jfkmod64.lib b/externalDlls/libnodave/libnodave_jfkmod64.lib new file mode 100644 index 00000000..44e4530c Binary files /dev/null and b/externalDlls/libnodave/libnodave_jfkmod64.lib differ diff --git a/externalDlls/libnodave/makefile.64 b/externalDlls/libnodave/makefile.64 index 3ed55282..05122d0e 100644 --- a/externalDlls/libnodave/makefile.64 +++ b/externalDlls/libnodave/makefile.64 @@ -17,13 +17,20 @@ CFLAGS= -I"$(VCPATH)\include" -I"$(SDKPATH)\include" -c -DBCCWIN -DDAVE_LITTLE_E LFLAGS= /LIBPATH:"$(VCPATH)"\lib\x64,"$(SDKPATH)"\lib\x64 LLFLAGS = /LIBPATH:"$(VCPATH)\lib\x64" /DEF:libnodave.DEF /MACHINE:X64 +PROGRAMS=testISO_TCP.exe +# PROGRAMS=testMPI.exe testPPI.exe testAS511.exe\ +# testPPI_IBH.exe testPPI_IBHload.exe testPPIload.exe \ +# testMPIload.exe testISO_TCP.exe testISO_TCPload.exe testIBH.exe testMPI_IBHload.exe \ +# testNLpro.exe testS7online.exe LIBRARIES=libnodave_jfkmod64.dll -all: $(LIBRARIES) +all: $(PROGRAMS) $(LIBRARIES) dynamic: $(DYNAMIC_PROGRAMS) +testISO_TCP.exe: nodave.obj openSocketw.obj testISO_TCP.obj + $(LL) $(LFLAGS) testISO_TCP.obj openSocketw.obj nodave.obj "$(SDKPATH)"\lib\x64\ws2_32.lib /OUT:testISO_TCP.exe libnodave_jfkmod64.dll: nodave.obj setportw.obj openSocketw.obj openS7online.obj $(LL) $(LLFLAGS) /DLL nodave.obj setportw.obj openSocketw.obj openS7online.obj "$(SDKPATH)"\lib\x64\ws2_32.lib /OUT:libnodave_jfkmod64.dll diff --git a/externalDlls/libnodave/nodave.c b/externalDlls/libnodave/nodave.c index 29c0172c..35a0e582 100644 --- a/externalDlls/libnodave/nodave.c +++ b/externalDlls/libnodave/nodave.c @@ -22,7 +22,7 @@ GNU General Public License for more details. You should have received a copy of the GNU Library General Public License along with Libnodave; see the file COPYING. If not, write to -the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "nodave.h" #include @@ -67,7 +67,7 @@ Library specific: #include #endif -int daveDebug=0; +int daveDebug = 0; #ifdef BCCWIN #include @@ -76,18 +76,18 @@ int daveDebug=0; void setTimeOut(daveInterface * di, int tmo) { COMMTIMEOUTS cto; #ifdef DEBUG_CALLS - LOG3("setTimeOut(di:%p, time:%d)\n", di,tmo); + LOG3("setTimeOut(di:%p, time:%d)\n", di, tmo); FLUSH; -#endif - // if(di->fd.connectionType==daveSerialConnection) { +#endif + // if(di->fd.connectionType==daveSerialConnection) { GetCommTimeouts(di->fd.rfd, &cto); - cto.ReadIntervalTimeout=0; - cto.ReadTotalTimeoutMultiplier=0; - cto.ReadTotalTimeoutConstant=tmo/1000; - SetCommTimeouts(di->fd.rfd,&cto); - // } else if(di->fd.connectionType==daveTcpConnection) { - // } -} + cto.ReadIntervalTimeout = 0; + cto.ReadTotalTimeoutMultiplier = 0; + cto.ReadTotalTimeoutConstant = tmo / 1000; + SetCommTimeouts(di->fd.rfd, &cto); + //} else if(di->fd.connectionType==daveTcpConnection) { + //} +} #endif #ifdef AVR_NOOS @@ -95,7 +95,7 @@ int DECL2 stdwrite(daveInterface * di, char * buffer, int length) { return 0; } -int DECL2 stdread(daveInterface * di, char * buffer, int length) { +int DECL2 stdread(daveInterface * di, char * buffer, int length) { return 0; } #endif @@ -105,22 +105,22 @@ int DECL2 stdread(daveInterface * di, char * buffer, int length) { int DECL2 stdwrite(daveInterface * di, char * buffer, int length) { if (daveDebug & daveDebugByte) _daveDump("I send", (uc*)buffer, length); - return write(di->fd.wfd, buffer,length); + return write(di->fd.wfd, buffer, length); } int DECL2 stdread(daveInterface * di, char * buffer, int length) { - fd_set FDS; + fd_set FDS; struct timeval t; int i; t.tv_sec = di->timeout / 1000000; t.tv_usec = (di->timeout % 1000000); FD_ZERO(&FDS); FD_SET(di->fd.rfd, &FDS); - i=0; - if(select(di->fd.rfd + 1, &FDS, NULL, NULL, &t)>0) { - i=read(di->fd.rfd, buffer, length); + i = 0; + if (select(di->fd.rfd + 1, &FDS, NULL, NULL, &t) > 0) { + i = read(di->fd.rfd, buffer, length); } - // if (daveDebug & daveDebugByte) + // if (daveDebug & daveDebugByte) // _daveDump("got",buffer,i); return i; } @@ -130,168 +130,168 @@ int DECL2 stdread(daveInterface * di, char * buffer, int length) { #ifdef BCCWIN int DECL2 stdread(daveInterface * di, - char * buffer, + char * buffer, int length) { - unsigned long i; - ReadFile(di->fd.rfd, buffer, length, &i, NULL); - // if (daveDebug & daveDebugByte) - // _daveDump("got",buffer,i); - return i; + unsigned long i; + ReadFile(di->fd.rfd, buffer, length, &i, NULL); + // if (daveDebug & daveDebugByte) + // _daveDump("got",buffer,i); + return i; } int DECL2 stdwrite(daveInterface * di, char * buffer, int length) { unsigned long i; if (daveDebug & daveDebugByte) - _daveDump("I send",buffer,length); - // EscapeCommFunction(di->fd.rfd, CLRRTS); // patch from Keith Harris. He says: + _daveDump("I send", buffer, length); + // EscapeCommFunction(di->fd.rfd, CLRRTS); // patch from Keith Harris. He says: //******* this is what microwin does (needed for usb-serial) - WriteFile(di->fd.rfd, buffer, length, &i,NULL); + WriteFile(di->fd.rfd, buffer, length, &i, NULL); // patch from Andrea. He says: // In this way the PC Adapter connected to CPU313C hangs waiting for RTS line before answering back. // Added the following to regain answers: - // EscapeCommFunction(di->fd.rfd, SETRTS); + // EscapeCommFunction(di->fd.rfd, SETRTS); return i; } #endif -/* +/* Setup a new interface structure from an initialized serial interface's handle and a name. */ daveInterface * DECL2 daveNewInterface(_daveOSserialType nfd, char * nname, int localMPI, int protocol, int speed){ - daveInterface * di=(daveInterface *) calloc(1, sizeof(daveInterface)); + daveInterface * di = (daveInterface *)calloc(1, sizeof(daveInterface)); #ifdef DEBUG_CALLS LOG7("daveNewInterface(fd.rfd:%d fd.wfd:%d name:%s local MPI:%d protocol:%d PB speed:%d)\n", - nfd.rfd,nfd.wfd,nname, localMPI, protocol, speed); - FLUSH; -#endif + nfd.rfd, nfd.wfd, nname, localMPI, protocol, speed); + FLUSH; +#endif if (di) { // di->name=nname; - strncpy(di->realName,nname,20); - di->name=di->realName; - di->fd=nfd; - di->localMPI=localMPI; - di->protocol=protocol; - di->timeout=2500000; /* 2.5 second */ - di->nextConnection=0x14; - di->speed=speed; + strncpy(di->realName, nname, 20); + di->name = di->realName; + di->fd = nfd; + di->localMPI = localMPI; + di->protocol = protocol; + di->timeout = 2500000; /* 2.5 second */ + di->nextConnection = 0x14; + di->speed = speed; #ifndef AVR_NOOS - di->getResponse=_daveGetResponseISO_TCP; -#endif - di->ifread=stdread; - di->ifwrite=stdwrite; - di->initAdapter=_daveReturnOkDummy; - di->connectPLC=_daveReturnOkDummy2; - di->disconnectPLC=_daveReturnOkDummy2; - di->disconnectAdapter=_daveReturnOkDummy; - di->listReachablePartners=_daveListReachablePartnersDummy; + di->getResponse = _daveGetResponseISO_TCP; +#endif + di->ifread = stdread; + di->ifwrite = stdwrite; + di->initAdapter = _daveReturnOkDummy; + di->connectPLC = _daveReturnOkDummy2; + di->disconnectPLC = _daveReturnOkDummy2; + di->disconnectAdapter = _daveReturnOkDummy; + di->listReachablePartners = _daveListReachablePartnersDummy; switch (protocol) { case daveProtoMPI: - di->initAdapter=_daveInitAdapterMPI1; - di->connectPLC=_daveConnectPLCMPI1; - di->disconnectPLC=_daveDisconnectPLCMPI; - di->disconnectAdapter=_daveDisconnectAdapterMPI; - di->exchange=_daveExchangeMPI; - di->sendMessage=_daveSendMessageMPI; - di->getResponse=_daveGetResponseMPI; - di->listReachablePartners=_daveListReachablePartnersMPI; - break; + di->initAdapter = _daveInitAdapterMPI1; + di->connectPLC = _daveConnectPLCMPI1; + di->disconnectPLC = _daveDisconnectPLCMPI; + di->disconnectAdapter = _daveDisconnectAdapterMPI; + di->exchange = _daveExchangeMPI; + di->sendMessage = _daveSendMessageMPI; + di->getResponse = _daveGetResponseMPI; + di->listReachablePartners = _daveListReachablePartnersMPI; + break; case daveProtoMPI2: case daveProtoMPI4: - di->initAdapter=_daveInitAdapterMPI2; - di->connectPLC=_daveConnectPLCMPI2; - di->disconnectPLC=_daveDisconnectPLCMPI; - di->disconnectAdapter=_daveDisconnectAdapterMPI; - di->exchange=_daveExchangeMPI; - di->sendMessage=_daveSendMessageMPI; - di->getResponse=_daveGetResponseMPI; - di->listReachablePartners=_daveListReachablePartnersMPI; - di->nextConnection=0x3; + di->initAdapter = _daveInitAdapterMPI2; + di->connectPLC = _daveConnectPLCMPI2; + di->disconnectPLC = _daveDisconnectPLCMPI; + di->disconnectAdapter = _daveDisconnectAdapterMPI; + di->exchange = _daveExchangeMPI; + di->sendMessage = _daveSendMessageMPI; + di->getResponse = _daveGetResponseMPI; + di->listReachablePartners = _daveListReachablePartnersMPI; + di->nextConnection = 0x3; break; case daveProtoMPI3: - di->initAdapter=_daveInitAdapterMPI3; - di->connectPLC=_daveConnectPLCMPI3; - di->disconnectPLC=_daveDisconnectPLCMPI3; - di->disconnectAdapter=_daveDisconnectAdapterMPI3; - di->exchange=_daveExchangeMPI3; - di->sendMessage=_daveSendMessageMPI3; - di->getResponse=_daveGetResponseMPI3; - di->listReachablePartners=_daveListReachablePartnersMPI3; - di->nextConnection=0x3; - break; + di->initAdapter = _daveInitAdapterMPI3; + di->connectPLC = _daveConnectPLCMPI3; + di->disconnectPLC = _daveDisconnectPLCMPI3; + di->disconnectAdapter = _daveDisconnectAdapterMPI3; + di->exchange = _daveExchangeMPI3; + di->sendMessage = _daveSendMessageMPI3; + di->getResponse = _daveGetResponseMPI3; + di->listReachablePartners = _daveListReachablePartnersMPI3; + di->nextConnection = 0x3; + break; #ifndef AVR_NOOS case daveProtoISOTCP: case daveProtoISOTCP243: //case daveProtoISOTCPR: // routing over MPI network - di->getResponse=_daveGetResponseISO_TCP; - di->connectPLC=_daveConnectPLCTCP; - di->exchange=_daveExchangeTCP; - break; + di->getResponse = _daveGetResponseISO_TCP; + di->connectPLC = _daveConnectPLCTCP; + di->exchange = _daveExchangeTCP; + break; case daveProtoPPI: - di->getResponse=_daveGetResponsePPI; - di->exchange=_daveExchangePPI; - di->connectPLC=_daveConnectPLCPPI; - di->timeout=150000; /* 0.15 seconds */ + di->getResponse = _daveGetResponsePPI; + di->exchange = _daveExchangePPI; + di->connectPLC = _daveConnectPLCPPI; + di->timeout = 150000; /* 0.15 seconds */ break; case daveProtoMPI_IBH: - di->exchange=_daveExchangeIBH; - di->connectPLC=_daveConnectPLC_IBH; - di->disconnectPLC=_daveDisconnectPLC_IBH; - di->sendMessage=_daveSendMessageMPI_IBH; - di->getResponse=_daveGetResponseMPI_IBH; - di->listReachablePartners=_daveListReachablePartnersMPI_IBH; - break; + di->exchange = _daveExchangeIBH; + di->connectPLC = _daveConnectPLC_IBH; + di->disconnectPLC = _daveDisconnectPLC_IBH; + di->sendMessage = _daveSendMessageMPI_IBH; + di->getResponse = _daveGetResponseMPI_IBH; + di->listReachablePartners = _daveListReachablePartnersMPI_IBH; + break; case daveProtoPPI_IBH: - di->exchange=_daveExchangePPI_IBH; - di->connectPLC=_daveConnectPLCPPI; - di->sendMessage=_daveSendMessageMPI_IBH; - di->getResponse=_daveGetResponsePPI_IBH; - di->listReachablePartners=_daveListReachablePartnersMPI_IBH; - break; + di->exchange = _daveExchangePPI_IBH; + di->connectPLC = _daveConnectPLCPPI; + di->sendMessage = _daveSendMessageMPI_IBH; + di->getResponse = _daveGetResponsePPI_IBH; + di->listReachablePartners = _daveListReachablePartnersMPI_IBH; + break; case daveProtoS7online: - di->exchange=_daveExchangeS7online; - di->connectPLC=_daveConnectPLCS7online; - di->sendMessage=_daveSendMessageS7online; - di->getResponse=_daveGetResponseS7online; - di->listReachablePartners=_daveListReachablePartnersS7online; - di->disconnectPLC=_daveDisconnectPLCS7online; //JK + di->exchange = _daveExchangeS7online; + di->connectPLC = _daveConnectPLCS7online; + di->sendMessage = _daveSendMessageS7online; + di->getResponse = _daveGetResponseS7online; + di->listReachablePartners = _daveListReachablePartnersS7online; + di->disconnectPLC = _daveDisconnectPLCS7online; //JK // di->disconnectAdapter=_daveDisconnectAdapterS7online; - break; + break; case daveProtoAS511: - di->connectPLC=_daveConnectPLCAS511; - di->disconnectPLC=_daveDisconnectPLCAS511; - di->exchange=_daveFakeExchangeAS511; - di->sendMessage=_daveFakeExchangeAS511; - break; + di->connectPLC = _daveConnectPLCAS511; + di->disconnectPLC = _daveDisconnectPLCAS511; + di->exchange = _daveFakeExchangeAS511; + di->sendMessage = _daveFakeExchangeAS511; + break; case daveProtoNLPro: - di->initAdapter=_daveInitAdapterNLPro; - di->connectPLC=_daveConnectPLCNLPro; - di->disconnectPLC=_daveDisconnectPLCNLPro; - di->disconnectAdapter=_daveDisconnectAdapterNLPro; - di->exchange=_daveExchangeNLPro; - di->sendMessage=_daveSendMessageNLPro; - di->getResponse=_daveGetResponseNLPro; - di->listReachablePartners=_daveListReachablePartnersNLPro; + di->initAdapter = _daveInitAdapterNLPro; + di->connectPLC = _daveConnectPLCNLPro; + di->disconnectPLC = _daveDisconnectPLCNLPro; + di->disconnectAdapter = _daveDisconnectAdapterNLPro; + di->exchange = _daveExchangeNLPro; + di->sendMessage = _daveSendMessageNLPro; + di->getResponse = _daveGetResponseNLPro; + di->listReachablePartners = _daveListReachablePartnersNLPro; break; -#endif +#endif } #ifdef BCCWIN setTimeOut(di, di->timeout); #endif } - return di; + return di; } daveInterface * DECL2 davePascalNewInterface(_daveOSserialType* nfd, char * nname, int localMPI, int protocol, int speed){ #ifdef DEBUG_CALLS LOG7("davePascalNewInterface(fd.rfd:%d fd.wfd:%d name:%s local MPI:%d protocol:%d PB speed:%d)\n", - nfd->rfd,nfd->wfd,nname, localMPI, protocol, speed); - FLUSH; -#endif - return daveNewInterface(*nfd,nname, localMPI, protocol, speed); + nfd->rfd, nfd->wfd, nname, localMPI, protocol, speed); + FLUSH; +#endif + return daveNewInterface(*nfd, nname, localMPI, protocol, speed); } /* @@ -310,17 +310,17 @@ public use. void DECL2 daveSetDebug(int nDebug) { #ifdef DEBUG_CALLS - LOG2("daveSetDebug(%d)\n",nDebug); + LOG2("daveSetDebug(%d)\n", nDebug); FLUSH; -#endif - daveDebug=nDebug; +#endif + daveDebug = nDebug; } int DECL2 daveGetDebug() { #ifdef DEBUG_CALLS LOG1("daveGetDebug()\n"); FLUSH; -#endif +#endif return daveDebug; } /** @@ -328,52 +328,52 @@ C# interoperability: **/ void DECL2 daveSetTimeout(daveInterface * di, int tmo) { #ifdef DEBUG_CALLS - LOG3("daveSetTimeOut(di:%p, time:%d)\n", di,tmo); -#endif - di->timeout=tmo; + LOG3("daveSetTimeOut(di:%p, time:%d)\n", di, tmo); +#endif + di->timeout = tmo; #ifdef BCCWIN - setTimeOut(di,tmo); -#endif + setTimeOut(di, tmo); +#endif } int DECL2 daveGetTimeout(daveInterface * di) { #ifdef DEBUG_CALLS - LOG2("daveGetTimeOut(di:%p)\n",di); + LOG2("daveGetTimeOut(di:%p)\n", di); FLUSH; -#endif +#endif return di->timeout; } char * DECL2 daveGetName(daveInterface * di) { #ifdef DEBUG_CALLS - LOG2("daveGetName(di:%p)\n",di); + LOG2("daveGetName(di:%p)\n", di); FLUSH; -#endif +#endif return di->name; } int DECL2 daveGetMPIAdr(daveConnection * dc) { #ifdef DEBUG_CALLS - LOG2("daveGetMPIAdr(dc:%p)\n",dc); + LOG2("daveGetMPIAdr(dc:%p)\n", dc); FLUSH; -#endif +#endif return dc->MPIAdr; } int DECL2 daveGetAnswLen(daveConnection * dc) { #ifdef DEBUG_CALLS - LOG2("daveGetAnswLen(dc:%p)\n",dc); + LOG2("daveGetAnswLen(dc:%p)\n", dc); FLUSH; -#endif +#endif return dc->AnswLen; } int DECL2 daveGetMaxPDULen(daveConnection * dc) { #ifdef DEBUG_CALLS - LOG2("daveGetMaxPDULen(dc:%p)\n",dc); + LOG2("daveGetMaxPDULen(dc:%p)\n", dc); FLUSH; -#endif +#endif return dc->maxPDUlength; } @@ -385,104 +385,107 @@ PDU handling: set up the header. Needs valid header pointer */ void DECL2 _daveInitPDUheader(PDU * p, int type) { - memset(p->header, 0, sizeof(PDUHeader)); - if (type==2 || type==3) - p->hlen=12; + memset(p->header, 0, sizeof(PDUHeader)); + if (type == 2 || type == 3) + p->hlen = 12; else - p->hlen=10; - p->param=p->header+p->hlen; - ((PDUHeader*)p->header)->P=0x32; - ((PDUHeader*)p->header)->type=type; - p->dlen=0; - p->plen=0; - p->udlen=0; - p->data=NULL; - p->udata=NULL; + p->hlen = 10; + p->param = p->header + p->hlen; + ((PDUHeader*)p->header)->P = 0x32; + ((PDUHeader*)p->header)->type = type; + p->dlen = 0; + p->plen = 0; + p->udlen = 0; + p->data = NULL; + p->udata = NULL; } /* add parameters after header, adjust pointer to data. needs valid header */ -void DECL2 _daveAddParam(PDU * p,uc * param,us len) { +void DECL2 _daveAddParam(PDU * p, uc * param, us len) { #ifdef DEBUG_CALLS - LOG4("_daveAddParam(PDU:%p, param %p, len:%d)\n", p, param, len); - FLUSH; -#endif - p->plen=len; + LOG4("_daveAddParam(PDU:%p, param %p, len:%d)\n", p, param, len); + FLUSH; +#endif + p->plen = len; #ifdef DAVE_HAVE_MEMCPY - memcpy(p->param, param, len); + memcpy(p->param, param, len); #else - int i; - for (i=0;iparam[i]=param[i]; -#endif - ((PDUHeader2*)p->header)->plenHi=len/256; - ((PDUHeader2*)p->header)->plenLo=len%256; -// ((PDUHeader*)p->header)->plen=daveSwapIed_16(len); - p->data=p->param+len; - p->dlen=0; + int i; + for (i = 0; i < len; i++) p->param[i] = param[i]; +#endif + ((PDUHeader2*)p->header)->plenHi = len / 256; + ((PDUHeader2*)p->header)->plenLo = len % 256; + // ((PDUHeader*)p->header)->plen=daveSwapIed_16(len); + p->data = p->param + len; + p->dlen = 0; } /* add data after parameters, set dlen needs valid header,parameters */ -void DECL2 _daveAddData(PDU * p,void * data,int len) { +void DECL2 _daveAddData(PDU * p, void * data, int len) { #ifdef DEBUG_CALLS - LOG4("_daveAddData(PDU:%p, data %p, len:%d)\n", p, data, len); -// _daveDumpPDU(p); - FLUSH; -#endif - uc * dn= p->data+p->dlen; - p->dlen+=len; + LOG4("_daveAddData(PDU:%p, data %p, len:%d)\n", p, data, len); + // _daveDumpPDU(p); + FLUSH; +#endif + uc * dn = p->data + p->dlen; + p->dlen += len; #ifdef DAVE_HAVE_MEMCPY - memcpy(dn, data, len); -#else - int i; uc * d=(uc*)data; - for (i=0;idata[p->dlen+i]=d[i]; -#endif - ((PDUHeader2*)p->header)->dlenHi=p->dlen/256; - ((PDUHeader2*)p->header)->dlenLo=p->dlen%256; -// ((PDUHeader*)p->header)->dlen=daveSwapIed_16(p->dlen); + memcpy(dn, data, len); +#else + int i; uc * d = (uc*)data; + for (i = 0; i < len; i++) p->data[p->dlen + i] = d[i]; +#endif + ((PDUHeader2*)p->header)->dlenHi = p->dlen / 256; + ((PDUHeader2*)p->header)->dlenLo = p->dlen % 256; + // ((PDUHeader*)p->header)->dlen=daveSwapIed_16(p->dlen); } /* add values after value header in data, adjust dlen and data count. needs valid header,parameters,data,dlen */ -void DECL2 _daveAddValue(PDU * p,void * data,int len) { - us dCount; - uc * dtype; +void DECL2 _daveAddValue(PDU * p, void * data, int len) { + us dCount; + uc * dtype; #ifdef DEBUG_CALLS - LOG4("_daveAddValue(PDU:%p, data %p, len:%d)\n", p, data, len); - _daveDumpPDU(p); - FLUSH; + LOG4("_daveAddValue(PDU:%p, data %p, len:%d)\n", p, data, len); + _daveDumpPDU(p); + FLUSH; #endif - dtype=p->data+p->dlen-4+1; /* position of first byte in the 4 byte sequence */ + dtype = p->data + p->dlen - 4 + 1; /* position of first byte in the 4 byte sequence */ - dCount= p->data[p->dlen-4+2+1]; - dCount+= 256*p->data[p->dlen-4+2]; - - if (daveDebug & daveDebugPDU) + dCount = p->data[p->dlen - 4 + 2 + 1]; + dCount += 256 * p->data[p->dlen - 4 + 2]; + + if (daveDebug & daveDebugPDU) LOG2("dCount: %d\n", dCount); - if (*dtype==4) { /* bit data, length is in bits */ - dCount+=8*len; - } else if (*dtype==9) { /* byte data, length is in bytes */ - dCount+=len; - } else if (* dtype==3) { /* bit data, length is in bits */ - dCount+=len; - } else { + if (*dtype == 4) { /* bit data, length is in bits */ + dCount += 8 * len; + } + else if (*dtype == 9) { /* byte data, length is in bytes */ + dCount += len; + } + else if (*dtype == 3) { /* bit data, length is in bits */ + dCount += len; + } + else { if (daveDebug & daveDebugPDU) LOG2("unknown data type/length: %d\n", *dtype); - } - if (p->udata==NULL) p->udata=p->data+4; - p->udlen+=len; + } + if (p->udata == NULL) p->udata = p->data + 4; + p->udlen += len; if (daveDebug & daveDebugPDU) LOG2("dCount: %d\n", dCount); - - p->data[p->dlen-4+2]= dCount/256; - p->data[p->dlen-4+2+1]=dCount%256; + + p->data[p->dlen - 4 + 2] = dCount / 256; + p->data[p->dlen - 4 + 2 + 1] = dCount % 256; _daveAddData(p, data, len); } @@ -491,474 +494,473 @@ void DECL2 _daveAddValue(PDU * p,void * data,int len) { add data in user data. Add a user data header, if not yet present. */ void DECL2 _daveAddUserData(PDU * p, uc * da, int len) { - uc udh[]={0xff,9,0,0}; - if (p->dlen==0) { + uc udh[] = { 0xff, 9, 0, 0 }; + if (p->dlen == 0) { if (daveDebug & daveDebugPDU) - LOG1("adding user data header.\n"); + LOG1("adding user data header.\n"); _daveAddData(p, udh, sizeof(udh)); } _daveAddValue(p, da, len); } void DECL2 davePrepareReadRequest(daveConnection * dc, PDU *p) { - uc pa[]= {daveFuncRead,0}; + uc pa[] = { daveFuncRead, 0 }; #ifdef DEBUG_CALLS LOG3("davePrepareReadRequest(dc:%p PDU:%p)\n", dc, p); FLUSH; -#endif - p->header=dc->msgOut+dc->PDUstartO; - _daveInitPDUheader(p,1); +#endif + p->header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(p, 1); _daveAddParam(p, pa, sizeof(pa)); -} +} PDU * DECL2 daveNewPDU() { PDU * p; - p=(PDU*)malloc(sizeof(PDU)); + p = (PDU*)malloc(sizeof(PDU)); #ifdef DEBUG_CALLS - LOG2("daveNewPDU() = %p\n",p); + LOG2("daveNewPDU() = %p\n", p); FLUSH; -#endif +#endif return p; -} +} void DECL2 davePrepareWriteRequest(daveConnection * dc, PDU *p) { - uc pa[]= {daveFuncWrite, 0}; + uc pa[] = { daveFuncWrite, 0 }; #ifdef DEBUG_CALLS LOG3("davePrepareWriteRequest(dc:%p PDU:%p)\n", dc, p); FLUSH; -#endif - p->header=dc->msgOut+dc->PDUstartO; - _daveInitPDUheader(p,1); +#endif + p->header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(p, 1); _daveAddParam(p, pa, sizeof(pa)); - p->dlen=0; -} + p->dlen = 0; +} void DECL2 daveAddToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount, int isBit) { - uc pa[]= { + uc pa[] = { 0x12, 0x0a, 0x10, 0x02, /* 1=single bit, 2=byte, 4=word */ - 0,0, /* length in bytes */ - 0,0, /* DB number */ + 0, 0, /* length in bytes */ + 0, 0, /* DB number */ 0, /* area code */ - 0,0,0 /* start address in bits */ + 0, 0, 0 /* start address in bits */ }; - if ((area==daveAnaIn) || (area==daveAnaOut) /*|| (area==daveP)*/) { - pa[3]=4; - start*=8; /* bits */ - } else if ((area==daveTimer) || (area==daveCounter)||(area==daveTimer200) || (area==daveCounter200)) { - pa[3]=area; - } else { - if(isBit) { - pa[3]=1; - } else { - start*=8; /* bit address of byte */ - } - } - - pa[4]=byteCount / 256; - pa[5]=byteCount & 0xff; - pa[6]=DBnum / 256; - pa[7]=DBnum & 0xff; - pa[8]=area; - pa[11]=start & 0xff; - pa[10]=(start / 0x100) & 0xff; - pa[9]=start / 0x10000; + if ((area == daveAnaIn) || (area == daveAnaOut) /*|| (area==daveP)*/) { + pa[3] = 4; + start *= 8; /* bits */ + } + else if ((area == daveTimer) || (area == daveCounter) || (area == daveTimer200) || (area == daveCounter200)) { + pa[3] = area; + } + else { + if (isBit) { + pa[3] = 1; + } + else { + start *= 8; /* bit address of byte */ + } + } + + pa[4] = byteCount / 256; + pa[5] = byteCount & 0xff; + pa[6] = DBnum / 256; + pa[7] = DBnum & 0xff; + pa[8] = area; + pa[11] = start & 0xff; + pa[10] = (start / 0x100) & 0xff; + pa[9] = start / 0x10000; p->param[1]++; - memcpy(p->param+p->plen, pa, sizeof(pa)); - p->plen+=sizeof(pa); - - ((PDUHeader2*)p->header)->plenHi=p->plen/256; - ((PDUHeader2*)p->header)->plenLo=p->plen%256; - - p->data=p->param+p->plen; - p->dlen=0; + memcpy(p->param + p->plen, pa, sizeof(pa)); + p->plen += sizeof(pa); + + ((PDUHeader2*)p->header)->plenHi = p->plen / 256; + ((PDUHeader2*)p->header)->plenLo = p->plen % 256; + + p->data = p->param + p->plen; + p->dlen = 0; if (daveDebug & daveDebugPDU) { _daveDumpPDU(p); - } -} + } +} void DECL2 daveAddFillByteToReadRequest(PDU *p) { - uc pa[]= { + uc pa[] = { 0 /* fill byte */ }; - - memcpy(p->param+p->plen, pa, 1); - p->plen+=1; + + memcpy(p->param + p->plen, pa, 1); + p->plen += 1; } void DECL2 daveAddDbRead400ToReadRequest(PDU *p, int DBnum, int offset, int byteCount) { - uc pa[]= { + uc pa[] = { 0x12, 0x07, 0xb0, 0x01, /* */ 0, /* length in bytes */ - 0,0, /* DB number */ - 0,0, /* start address in bits */ + 0, 0, /* DB number */ + 0, 0, /* start address in bits */ }; - + int paSize = 0; - + #ifdef ARM_FIX us tmplen; -#endif +#endif #ifdef DEBUG_CALLS LOG6("daveAddDbRead400ToReadRequest(PDU:%p db:%p offset:%p byteCount:%p)\n", p, DBnum, offset, byteCount); - FLUSH; -#endif - - pa[4]=byteCount; - pa[5]=DBnum / 256; - pa[6]=DBnum & 0xff; - pa[7]=offset / 256; - pa[8]=offset & 0xff; - + FLUSH; +#endif + + pa[4] = byteCount; + pa[5] = DBnum / 256; + pa[6] = DBnum & 0xff; + pa[7] = offset / 256; + pa[8] = offset & 0xff; + p->param[1]++; - - memcpy(p->param+p->plen, pa, sizeof(pa)); - p->plen+=sizeof(pa); -#ifdef ARM_FIX - tmplen=daveSwapIed_16(p->plen); + memcpy(p->param + p->plen, pa, sizeof(pa)); + p->plen += sizeof(pa); + +#ifdef ARM_FIX + tmplen = daveSwapIed_16(p->plen); memcpy(&(((PDUHeader*)p->header)->plen), &tmplen, sizeof(us)); #else - ((PDUHeader*)p->header)->plen=daveSwapIed_16(p->plen); -#endif - p->data=p->param+p->plen; - p->dlen=0; + ((PDUHeader*)p->header)->plen = daveSwapIed_16(p->plen); +#endif + p->data = p->param + p->plen; + p->dlen = 0; if (daveDebug & daveDebugPDU) { _daveDumpPDU(p); - } -} + } +} void DECL2 daveAddSymbolToReadRequest(PDU *p, void * completeSymbol, int completeSymbolLength) { - - uc pa[]= { 0x12, 0x00, 0xb2, 0xff }; - + + uc pa[] = { 0x12, 0x00, 0xb2, 0xff }; + #ifdef ARM_FIX us tmplen; -#endif +#endif + + pa[1] = completeSymbolLength + 4; + - pa[1]=completeSymbolLength + 4; - - p->param[1]++; - memcpy(p->param+p->plen, pa, sizeof(pa)); - memcpy(p->param+p->plen+4, completeSymbol, completeSymbolLength); - p->plen+= pa[1]; + memcpy(p->param + p->plen, pa, sizeof(pa)); + memcpy(p->param + p->plen + 4, completeSymbol, completeSymbolLength); + p->plen += pa[1]; -#ifdef ARM_FIX - tmplen=daveSwapIed_16(p->plen); +#ifdef ARM_FIX + tmplen = daveSwapIed_16(p->plen); memcpy(&(((PDUHeader*)p->header)->plen), &tmplen, sizeof(us)); #else - ((PDUHeader*)p->header)->plen=daveSwapIed_16(p->plen); -#endif - p->data=p->param+p->plen; - p->dlen=0; + ((PDUHeader*)p->header)->plen = daveSwapIed_16(p->plen); +#endif + p->data = p->param + p->plen; + p->dlen = 0; if (daveDebug & daveDebugPDU) { _daveDumpPDU(p); - } -} + } +} void DECL2 daveAddSymbolVarToReadRequest(PDU *p, void * completeSymbol, int completeSymbolLength) { #ifdef DEBUG_CALLS LOG6("daveAddSymbolVarToReadRequest(PDU:%p symbol:%s)\n", p, completeSymbol); - FLUSH; -#endif + FLUSH; +#endif daveAddSymbolToReadRequest(p, completeSymbol, completeSymbolLength); -} +} void DECL2 daveAddVarToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount) { #ifdef DEBUG_CALLS LOG6("daveAddVarToReadRequest(PDU:%p area:%s area number:%d start address:%d byte count:%d)\n", p, daveAreaName(area), DBnum, start, byteCount); - FLUSH; -#endif + FLUSH; +#endif daveAddToReadRequest(p, area, DBnum, start, byteCount, 0); -} +} void DECL2 daveAddBitVarToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount) { daveAddToReadRequest(p, area, DBnum, start, byteCount, 1); -} +} void DECL2 daveAddNCKToReadRequest(PDU *p, int area, int unit, int column, int line, int module, int linecount) { - uc pa[] = { - 0x12, 0x08, 0x82, /* VarSpec, Length, SyntaxId */ - 0x00, /* Area/Unit: 3 Bits Area, 5 Bits unit */ - 0x00, 0x00, /* column */ - 0x00, 0x00, /* line */ - 0x00, /* module */ - 0x01, /* linecount */ - }; - pa[3] = ((area & 0x07) << 5) | (unit & 0x1f); - pa[4] = column / 256; - pa[5] = column & 0xff; - pa[6] = line / 256; - pa[7] = line & 0xff; - pa[8] = module; - pa[9] = linecount; - - p->param[1]++; - memcpy(p->param+p->plen, pa, sizeof(pa)); - p->plen += sizeof(pa); - - ((PDUHeader2*)p->header)->plenHi = p->plen / 256; - ((PDUHeader2*)p->header)->plenLo = p->plen % 256; - - p->data = p->param+p->plen; - p->dlen = 0; - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(p); - } + uc pa[] = { + 0x12, 0x08, 0x82, /* VarSpec, Length, SyntaxId */ + 0x00, /* Area/Unit: 3 Bits Area, 5 Bits unit */ + 0x00, 0x00, /* column */ + 0x00, 0x00, /* line */ + 0x00, /* module */ + 0x01, /* linecount */ + }; + pa[3] = ((area & 0x07) << 5) | (unit & 0x1f); + pa[4] = column / 256; + pa[5] = column & 0xff; + pa[6] = line / 256; + pa[7] = line & 0xff; + pa[8] = module; + pa[9] = linecount; + + p->param[1]++; + memcpy(p->param + p->plen, pa, sizeof(pa)); + p->plen += sizeof(pa); + + ((PDUHeader2*)p->header)->plenHi = p->plen / 256; + ((PDUHeader2*)p->header)->plenLo = p->plen % 256; + + p->data = p->param + p->plen; + p->dlen = 0; + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(p); + } } void DECL2 daveAddNCKToWriteRequest(PDU *p, int area, int unit, int column, int line, int module, int linecount, int byteCount, void * buffer) { - uc saveData[1024]; - //#ifdef ARM_FIX - // us tmplen; - //#endif - - //************************************************************************** - // Transport sizes in data - // - //S7COMM_DATA_TRANSPORT_SIZE_NULL 0 - //S7COMM_DATA_TRANSPORT_SIZE_BBIT 3 /* bit access, len is in bits */ - //S7COMM_DATA_TRANSPORT_SIZE_BBYTE 4 /* byte/word/dword access, len is in bits */ - //S7COMM_DATA_TRANSPORT_SIZE_BINT 5 /* integer access, len is in bits */ - //S7COMM_DATA_TRANSPORT_SIZE_BDINT 6 /* integer access, len is in bytes */ - //S7COMM_DATA_TRANSPORT_SIZE_BREAL 7 /* real access, len is in bytes */ - //S7COMM_DATA_TRANSPORT_SIZE_BSTR 9 /* octet string, len is in bytes */ - //************************************************************************** - uc da[]= {0, //Return Value - 9, //Transport-Size - 0, //Count of the following Data - 0, //Count of the following Data - }; - + uc saveData[1024]; + //#ifdef ARM_FIX + // us tmplen; + //#endif + + //************************************************************************** + // Transport sizes in data + // + //S7COMM_DATA_TRANSPORT_SIZE_NULL 0 + //S7COMM_DATA_TRANSPORT_SIZE_BBIT 3 /* bit access, len is in bits */ + //S7COMM_DATA_TRANSPORT_SIZE_BBYTE 4 /* byte/word/dword access, len is in bits */ + //S7COMM_DATA_TRANSPORT_SIZE_BINT 5 /* integer access, len is in bits */ + //S7COMM_DATA_TRANSPORT_SIZE_BDINT 6 /* integer access, len is in bytes */ + //S7COMM_DATA_TRANSPORT_SIZE_BREAL 7 /* real access, len is in bytes */ + //S7COMM_DATA_TRANSPORT_SIZE_BSTR 9 /* octet string, len is in bytes */ + //************************************************************************** + uc da[] = { 0, //Return Value + 9, //Transport-Size + 0, //Count of the following Data + 0, //Count of the following Data + }; + uc pa[] = { - 0x12, 0x08, 0x82, /* VarSpec, Length, SyntaxId */ - 0x00, /* Area/Unit: 3 Bits Area, 5 Bits unit */ - 0x00, 0x00, /* column */ - 0x00, 0x00, /* line */ - 0x00, /* module */ - 0x01, /* linecount */ - }; - pa[3] = ((area & 0x07) << 5) | (unit & 0x1f); - pa[4] = column / 256; - pa[5] = column & 0xff; - pa[6] = line / 256; - pa[7] = line & 0xff; - pa[8] = module; - pa[9] = linecount; - if(p->dlen%2) { - _daveAddData(p, da, 1); - } - p->param[1]++; - if(p->dlen){ + 0x12, 0x08, 0x82, /* VarSpec, Length, SyntaxId */ + 0x00, /* Area/Unit: 3 Bits Area, 5 Bits unit */ + 0x00, 0x00, /* column */ + 0x00, 0x00, /* line */ + 0x00, /* module */ + 0x01, /* linecount */ + }; + pa[3] = ((area & 0x07) << 5) | (unit & 0x1f); + pa[4] = column / 256; + pa[5] = column & 0xff; + pa[6] = line / 256; + pa[7] = line & 0xff; + pa[8] = module; + pa[9] = linecount; + if (p->dlen % 2) { + _daveAddData(p, da, 1); + } + p->param[1]++; + if (p->dlen){ memcpy(saveData, p->data, p->dlen); - memcpy(p->data+sizeof(pa), saveData, p->dlen); - } - memcpy(p->param+p->plen, pa, sizeof(pa)); - p->plen += sizeof(pa); - -//#ifdef ARM_FIX -// tmplen=daveSwapIed_16(p->plen); -// memcpy(&(((PDUHeader*)p->header)->plen), &tmplen, sizeof(us)); -//#else -// ((PDUHeader*)p->header)->plen=daveSwapIed_16(p->plen); -//#endif - ((PDUHeader2*)p->header)->plenHi = p->plen / 256; - ((PDUHeader2*)p->header)->plenLo = p->plen % 256; - - p->data = p->param+p->plen; + memcpy(p->data + sizeof(pa), saveData, p->dlen); + } + memcpy(p->param + p->plen, pa, sizeof(pa)); + p->plen += sizeof(pa); + + //#ifdef ARM_FIX + // tmplen=daveSwapIed_16(p->plen); + // memcpy(&(((PDUHeader*)p->header)->plen), &tmplen, sizeof(us)); + //#else + // ((PDUHeader*)p->header)->plen=daveSwapIed_16(p->plen); + //#endif + ((PDUHeader2*)p->header)->plenHi = p->plen / 256; + ((PDUHeader2*)p->header)->plenLo = p->plen % 256; + + p->data = p->param + p->plen; _daveAddData(p, da, sizeof(da)); _daveAddValue(p, buffer, byteCount); - //p->dlen = 0; - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(p); - } + //p->dlen = 0; + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(p); + } } int DECL2 davePIstart_nc(daveConnection *dc, const char *piservice, const char *param[], int paramCount) { - int res; - int i; - PDU p, p2; - int paramlen; - int len; - int pos; - uc pa[1024]; + int res; + int i; + PDU p, p2; + int paramlen; + int len; + int pos; + uc pa[1024]; #ifdef DEBUG_CALLS - LOG2("davePIstart_nc(dc:%p)\n", dc); - FLUSH; -#endif - /* Header */ - pa[0] = 0x28; /* Function code for PI-Start*/ - pa[1] = 0x00; /* unknown */ - pa[2] = 0x00; - pa[3] = 0x00; - pa[4] = 0x00; - pa[5] = 0x00; - pa[6] = 0x00; - pa[7] = 0xfd; /* max. string length */ - pa[8] = 0x00; /* Parameterblock length */ - pa[9] = 0x00; /* Parameterblock length */ - - /* Parameterblock */ - pos = 10; /* start of Parameterblock */ - paramlen = 0; - for (i = 0; i < paramCount; i++) { - len = strlen(param[i]); - pa[pos++] = len; /* 1 Byte stringlength */ - memcpy(&pa[pos], param[i], len); /* the string */ - pos += len; - if (len % 2 == 0) { /* stringlength + length header must be even */ - pa[pos++] = '\0'; /* fillbyte */ - paramlen += 1 + len + 1; - } else { - paramlen += 1 + len; - } - } - /* set parameter block length */ - pa[8] = paramlen / 256; - pa[9] = paramlen % 256; - - /* add servicename */ - len = strlen(piservice); - pa[pos++] = len; - memcpy(&pa[pos], piservice, len); - pos += len; - - /* length of complete parameter part */ - len = 10 + paramlen + 1 + strlen(piservice); - - p.header=dc->msgOut+dc->PDUstartO; - _daveInitPDUheader(&p, 1); - _daveAddParam(&p, pa, len); - - ((PDUHeader2*)p.header)->plenHi = p.plen / 256; - ((PDUHeader2*)p.header)->plenLo = p.plen % 256; - - res =_daveExchange(dc, &p); - if (res == daveResOK) { - res =_daveSetupReceivedPDU(dc, &p2); - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(&p2); - } - } - return res; -} - -void DECL2 daveAddToWriteRequest(PDU *p, int area, int DBnum, int start, int byteCount, - void * buffer, - uc * da, - int dasize, - uc * pa, - int pasize - ) { - uc saveData[1024]; + LOG2("davePIstart_nc(dc:%p)\n", dc); + FLUSH; +#endif + /* Header */ + pa[0] = 0x28; /* Function code for PI-Start*/ + pa[1] = 0x00; /* unknown */ + pa[2] = 0x00; + pa[3] = 0x00; + pa[4] = 0x00; + pa[5] = 0x00; + pa[6] = 0x00; + pa[7] = 0xfd; /* max. string length */ + pa[8] = 0x00; /* Parameterblock length */ + pa[9] = 0x00; /* Parameterblock length */ + + /* Parameterblock */ + pos = 10; /* start of Parameterblock */ + paramlen = 0; + for (i = 0; i < paramCount; i++) { + len = strlen(param[i]); + pa[pos++] = len; /* 1 Byte stringlength */ + memcpy(&pa[pos], param[i], len); /* the string */ + pos += len; + if (len % 2 == 0) { /* stringlength + length header must be even */ + pa[pos++] = '\0'; /* fillbyte */ + paramlen += 1 + len + 1; + } + else { + paramlen += 1 + len; + } + } + /* set parameter block length */ + pa[8] = paramlen / 256; + pa[9] = paramlen % 256; + + /* add servicename */ + len = strlen(piservice); + pa[pos++] = len; + memcpy(&pa[pos], piservice, len); + pos += len; + + /* length of complete parameter part */ + len = 10 + paramlen + 1 + strlen(piservice); + + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 1); + _daveAddParam(&p, pa, len); + + ((PDUHeader2*)p.header)->plenHi = p.plen / 256; + ((PDUHeader2*)p.header)->plenLo = p.plen % 256; + + res = _daveExchange(dc, &p); + if (res == daveResOK) { + res = _daveSetupReceivedPDU(dc, &p2); + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(&p2); + } + } + return res; +} + +void DECL2 daveAddToWriteRequest(PDU *p, int area, int DBnum, int start, int byteCount, void * buffer, uc * da, int dasize, uc * pa, int pasize) { + uc saveData[1024]; #ifdef DEBUG_CALLS - LOG7("daveAddToWriteRequest(PDU:%p area:%s area number:%d start address:%d byte count:%d buffer:%p)\n", - p, daveAreaName(area), DBnum, start, byteCount, buffer); -// _daveDumpPDU(p); - FLUSH; -#endif - if ((area==daveTimer) || (area==daveCounter)||(area==daveTimer200) || (area==daveCounter200)) { - pa[3]=area; - pa[4]=((byteCount+1)/2) / 0x100; - pa[5]=((byteCount+1)/2) & 0xff; - } else if ((area==daveAnaIn) || (area==daveAnaOut)) { - pa[3]=4; - pa[4]=((byteCount+1)/2) / 0x100; - pa[5]=((byteCount+1)/2) & 0xff; - } else { - pa[4]=byteCount / 0x100; - pa[5]=byteCount & 0xff; - } - pa[6]=DBnum / 256; - pa[7]=DBnum & 0xff; - pa[8]=area; - pa[11]=start & 0xff; - pa[10]=(start / 0x100) & 0xff; - pa[9]=start / 0x10000; - if(p->dlen%2) { - _daveAddData(p, da, 1); - } - p->param[1]++; + LOG7("daveAddToWriteRequest(PDU:%p area:%s area number:%d start address:%d byte count:%d buffer:%p)\n", + p, daveAreaName(area), DBnum, start, byteCount, buffer); + // _daveDumpPDU(p); + FLUSH; +#endif + if ((area == daveTimer) || (area == daveCounter) || (area == daveTimer200) || (area == daveCounter200)) { + pa[3] = area; + pa[4] = ((byteCount + 1) / 2) / 0x100; + pa[5] = ((byteCount + 1) / 2) & 0xff; + } + else if ((area == daveAnaIn) || (area == daveAnaOut)) { + pa[3] = 4; + pa[4] = ((byteCount + 1) / 2) / 0x100; + pa[5] = ((byteCount + 1) / 2) & 0xff; + } + else { + pa[4] = byteCount / 0x100; + pa[5] = byteCount & 0xff; + } + pa[6] = DBnum / 256; + pa[7] = DBnum & 0xff; + pa[8] = area; + pa[11] = start & 0xff; + pa[10] = (start / 0x100) & 0xff; + pa[9] = start / 0x10000; + if (p->dlen % 2) { + _daveAddData(p, da, 1); + } + p->param[1]++; #ifdef DAVE_HAVE_MEMCPY - if(p->dlen){ - memcpy(saveData, p->data, p->dlen); - memcpy(p->data+pasize, saveData, p->dlen); - } - memcpy(p->param+p->plen, pa, pasize); + if (p->dlen){ + memcpy(saveData, p->data, p->dlen); + memcpy(p->data + pasize, saveData, p->dlen); + } + memcpy(p->param + p->plen, pa, pasize); #else - int i; - if(p->dlen){ - for (i=0; idlen; i++) saveData[i]=p->data[i]; - for (i=0; idlen; i++) p->data[i+pasize]=saveData[i]; - } - for (i=0; iparam[i+p->plen]=pa[i]; -#endif - p->plen+=pasize; - - ((PDUHeader2*)p->header)->plenHi=p->plen/256; - ((PDUHeader2*)p->header)->plenLo=p->plen%256; - - p->data=p->param+p->plen; - _daveAddData(p, da, dasize); - _daveAddValue(p, buffer, byteCount); - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(p); - } -} + int i; + if (p->dlen){ + for (i = 0; i < p->dlen; i++) saveData[i] = p->data[i]; + for (i = 0; i < p->dlen; i++) p->data[i + pasize] = saveData[i]; + } + for (i = 0; i < pasize; i++) p->param[i + p->plen] = pa[i]; +#endif + p->plen += pasize; + + ((PDUHeader2*)p->header)->plenHi = p->plen / 256; + ((PDUHeader2*)p->header)->plenLo = p->plen % 256; + + p->data = p->param + p->plen; + _daveAddData(p, da, dasize); + _daveAddValue(p, buffer, byteCount); + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(p); + } +} void DECL2 daveAddVarToWriteRequest(PDU *p, int area, int DBnum, int start, int byteCount, void * buffer) { - uc da[]= {0, //Return Value - 4, //Transport-Size - 0, //Count of the following Data - 0, //Count of the following Data - }; - - uc pa[]= { + uc da[] = { 0, //Return Value + 4, //Transport-Size + 0, //Count of the following Data + 0, //Count of the following Data + }; + + uc pa[] = { 0x12, 0x0a, 0x10, 0x02, /* unit (for count?, for consistency?) byte */ - 0,0, /* length in bytes */ - 0,0, /* DB number */ + 0, 0, /* length in bytes */ + 0, 0, /* DB number */ 0, /* area code */ - 0,0,0 /* start address in bits */ + 0, 0, 0 /* start address in bits */ }; #ifdef DEBUG_CALLS LOG7("daveAddVarToWriteRequest(PDU:%p area:%s area number:%d start address:%d byte count:%d buffer:%p)\n", p, daveAreaName(area), DBnum, start, byteCount, buffer); - FLUSH; -#endif + FLUSH; +#endif - daveAddToWriteRequest(p, area, DBnum, 8*start, byteCount,buffer,da,sizeof(da),pa,sizeof(pa)); + daveAddToWriteRequest(p, area, DBnum, 8 * start, byteCount, buffer, da, sizeof(da), pa, sizeof(pa)); } - void DECL2 daveAddBitVarToWriteRequest(PDU *p, int area, int DBnum, int start, int byteCount, void * buffer) { - uc da[]= {0,3,0,0,}; - uc pa[]= { + uc da[] = { 0, 3, 0, 0, }; + uc pa[] = { 0x12, 0x0a, 0x10, 0x01, /* single bit */ - 0,0, /* insert length in bytes here */ - 0,0, /* insert DB number here */ + 0, 0, /* insert length in bytes here */ + 0, 0, /* insert DB number here */ 0, /* change this to real area code */ - 0,0,0 /* insert start address in bits */ + 0, 0, 0 /* insert start address in bits */ }; #ifdef DEBUG_CALLS LOG7("daveAddBitVarToWriteRequest(PDU:%p area:%s area number:%d start address:%d byte count:%d buffer:%p)\n", p, daveAreaName(area), DBnum, start, byteCount, buffer); - FLUSH; -#endif + FLUSH; +#endif - daveAddToWriteRequest(p, area, DBnum, start, byteCount,buffer,da,sizeof(da),pa,sizeof(pa)); -} + daveAddToWriteRequest(p, area, DBnum, start, byteCount, buffer, da, sizeof(da), pa, sizeof(pa)); +} /* Get the eror code from a PDU, if one. @@ -967,136 +969,151 @@ int DECL2 daveGetPDUerror(PDU * p) { #ifdef DEBUG_CALLS LOG2("daveGetPDUerror(PDU:%p\n", p); FLUSH; -#endif - if (p->header[1]==2 || p->header[1]==3) { - return daveGetU16from(p->header+10); - } else +#endif + if (p->header[1] == 2 || p->header[1] == 3) { + return daveGetU16from(p->header + 10); + } + else return 0; } /* -Sets up pointers to the fields of a received message. +Sets up pointers to the fields of a received message. */ int DECL2 _daveSetupReceivedPDU(daveConnection * dc, PDU * p) { int res; /* = daveResCannotEvaluatePDU; */ - p->header=dc->msgIn+dc->PDUstartI; - res=0; - if (p->header[1]==2 || p->header[1]==3) { - p->hlen=12; - res=256*p->header[10]+p->header[11]; - } else { - p->hlen=10; - } - p->param=p->header+p->hlen; - - p->plen=256*p->header[6] + p->header[7]; - p->data=p->param+p->plen; - p->dlen=256*p->header[8] + p->header[9]; - p->udlen=0; - p->udata=NULL; + p->header = dc->msgIn + dc->PDUstartI; + res = 0; + if (p->header[1] == 2 || p->header[1] == 3) { + p->hlen = 12; + res = 256 * p->header[10] + p->header[11]; + } + else { + p->hlen = 10; + } + p->param = p->header + p->hlen; + + p->plen = 256 * p->header[6] + p->header[7]; + p->data = p->param + p->plen; + p->dlen = 256 * p->header[8] + p->header[9]; + p->udlen = 0; + p->udata = NULL; if (daveDebug & daveDebugPDU) - _daveDumpPDU(p); + _daveDumpPDU(p); return res; -} +} -int DECL2 _daveTestResultData(PDU * p) { +int DECL2 _daveTestResultData(PDU * p) { int res; /*=daveResCannotEvaluatePDU;*/ - if ((p->data[0]==255)&&(p->dlen>4)) + if ((p->data[0] == 255) && (p->dlen > 4)) { - res=daveResOK; - p->udata=p->data+4; - p->udlen=p->data[2]*0x100+p->data[3]; - if (p->data[1]==4) { - p->udlen>>=3; /* len is in bits, adjust */ - } else if (p->data[1]==9) { + res = daveResOK; + p->udata = p->data + 4; + p->udlen = p->data[2] * 0x100 + p->data[3]; + if (p->data[1] == 4) { + p->udlen >>= 3; /* len is in bits, adjust */ + } + else if (p->data[1] == 9) { /* len is already in bytes, ok */ - } else if (p->data[1]==3) { + } + else if (p->data[1] == 3) { /* len is in bits, but there is a byte per result bit, ok */ - } else if (p->data[1]==5) { /* Fehlenden Size-Type INTEGER ergänzt */ - p->udlen>>=3; /* len is in bits, adjust */ - } else if (p->data[1]==7) { /* Fehlenden Size-Type REAL ergänzt */ + } + else if (p->data[1] == 5) { /* Fehlenden Size-Type INTEGER ergänzt */ + p->udlen >>= 3; /* len is in bits, adjust */ + } + else if (p->data[1] == 7) { /* Fehlenden Size-Type REAL ergänzt */ /* len is already in bytes, ok */ - } else if (p->data[1]==6) { + } + else if (p->data[1] == 6) { /* integer access, len is in bytes */ - } else { + } + else { if (daveDebug & daveDebugPDU) - LOG2("fixme: what to do with data type %d?\n",p->data[1]); + LOG2("fixme: what to do with data type %d?\n", p->data[1]); res = daveResUnknownDataUnitSize; //res = 0; - } + } } else { - res=p->data[0]; + res = p->data[0]; } - return res; + return res; } int DECL2 _daveTestReadResult(PDU * p) { if (daveDebug & daveDebugPDU) - LOG2("dave test result: p-param[0]: %d\n",p->param[0]); - if (p->param[0]!=daveFuncRead) return daveResUnexpectedFunc; + LOG2("dave test result: p-param[0]: %d\n", p->param[0]); + if (p->param[0] != daveFuncRead) return daveResUnexpectedFunc; return _daveTestResultData(p); } -int DECL2 _daveTestResultDataMulti(PDU * p) { +int DECL2 _daveTestResultDataMulti(PDU * p) { int res; /*=daveResCannotEvaluatePDU;*/ - if ((p->data[0]==255)&&(p->dlen>4)) + if ((p->data[0] == 255) && (p->dlen > 4)) { - res=daveResOK; - p->udata=p->data+4; - p->udlen=p->data[2]*0x100+p->data[3]; - if (p->data[1]==4) { - p->udlen>>=3; /* len is in bits, adjust */ - } else if (p->data[1]==9) { + res = daveResOK; + p->udata = p->data + 4; + p->udlen = p->data[2] * 0x100 + p->data[3]; + if (p->data[1] == 4) { + p->udlen >>= 3; /* len is in bits, adjust */ + } + else if (p->data[1] == 9) { /* len is already in bytes, ok */ - } else if (p->data[1]==3) { + } + else if (p->data[1] == 3) { /* len is in bits, but there is a byte per result bit, ok */ - } else if (p->data[1]==5) { /* Fehlenden Size-Type INTEGER ergänzt */ - p->udlen>>=3; /* len is in bits, adjust */ - } else if (p->data[1]==7) { /* Fehlenden Size-Type REAL ergänzt */ + } + else if (p->data[1] == 5) { /* Fehlenden Size-Type INTEGER ergänzt */ + p->udlen >>= 3; /* len is in bits, adjust */ + } + else if (p->data[1] == 7) { /* Fehlenden Size-Type REAL ergänzt */ /* len is already in bytes, ok */ - } else if (p->data[1]==6) { - /* integer access, len is in bytes */ - } else { + } + else if (p->data[1] == 6) { + /* integer access, len is in bytes */ + } + else { if (daveDebug & daveDebugPDU) - LOG2("fixme: what to do with data type %d?\n",p->data[1]); + LOG2("fixme: what to do with data type %d?\n", p->data[1]); res = daveResUnknownDataUnitSize; //res = 0; - } + } } - else if (p->data[0]==10 || p->data[0]==5) + else if (p->data[0] == 10 || p->data[0] == 5) { //This Section returns ok, even if nothing was read, //because with the multiple read we get the error in (daveUseResult) - res = daveResOK; + res = daveResOK; } else { - res=p->data[0]; + res = p->data[0]; } - return res; + return res; } int DECL2 _daveTestReadResultMulti(PDU * p) { if (daveDebug & daveDebugPDU) - LOG2("dave test result: p-param[0]: %d\n",p->param[0]); - if (p->param[0]!=daveFuncRead) return daveResUnexpectedFunc; + LOG2("dave test result: p-param[0]: %d\n", p->param[0]); + if (p->param[0] != daveFuncRead) return daveResUnexpectedFunc; return _daveTestResultDataMulti(p); } -int DECL2 _daveTestPGReadResult(PDU * p) { - int pres=0; - if (p->param[0]!=0) return daveResUnexpectedFunc; - if (p->plen==12) pres=(256*p->param[10]+p->param[11]); - if (pres==0)return _daveTestResultData(p); else return pres; +int DECL2 _daveTestPGReadResult(PDU * p) { + int pres = 0; + if (p->param[0] != 0) return daveResUnexpectedFunc; + if (p->plen == 12) pres = (256 * p->param[10] + p->param[11]); + if (pres == 0)return _daveTestResultData(p); else return pres; } int DECL2 _daveTestWriteResult(PDU * p) { int res;/* =daveResCannotEvaluatePDU; */ - if (p->param[0]!=daveFuncWrite) return daveResUnexpectedFunc; - if ((p->data[0]==255)) { - res=daveResOK; - } else - res=p->data[0]; + if (p->param[0] != daveFuncWrite) return daveResUnexpectedFunc; + if ((p->data[0] == 255)) { + res = daveResOK; + } + else + res = p->data[0]; if (daveDebug & daveDebugPDU) { _daveDumpPDU(p); } @@ -1107,20 +1124,20 @@ int DECL2 _daveTestWriteResult(PDU * p) { Utilities: ****/ /* -This is an extended memory compare routine. It can handle don't care and stop flags +This is an extended memory compare routine. It can handle don't care and stop flags in the sample data. A stop flag lets it return success. */ int DECL2 _daveMemcmp(us * a, uc *b, size_t len) { unsigned int i; - us * a1=(us*)a; - uc * b1=(uc*)b; - for (i=0;idaveMaxRawLen) len=daveMaxRawLen; /* this will avoid to dump zillions of chars */ - if (len>DUMPLIMIT) len=DUMPLIMIT; /* this will avoid large dumps */ - for (j=0; j daveMaxRawLen) len = daveMaxRawLen; /* this will avoid to dump zillions of chars */ + if (len > DUMPLIMIT) len = DUMPLIMIT; /* this will avoid large dumps */ + for (j = 0; j < len; j++){ + if ((j & 0xf) == 0) LOG_2("\n %x:", j); + LOG_2("0x%02X,", ((uc*)(b))[j]); + } + LOG_1("\n"); } /* Hex dump PDU: */ void DECL2 _daveDumpPDU(PDU * p) { - int i,dl; + int i, dl; uc * pd; _daveDump("PDU header", p->header, p->hlen); - LOG3("plen: %d dlen: %d\n",p->plen, p->dlen); - if(p->plen>0) _daveDump("Parameter",p->param,p->plen); - if(p->dlen>0) _daveDump("Data ",p->data,p->dlen); - if ((p->plen==2)&&(p->param[0]==daveFuncRead)) { - pd=p->data; - for (i=0;iparam[1];i++) { - _daveDump("Data hdr ",pd,4); - - dl=0x100*pd[2]+pd[3]; - if (pd[1]==4) dl/=8; - pd+=4; - _daveDump("Data ",pd,dl); - if(iparam[1]-1) dl=dl+(dl%2); // the PLC places extra bytes at the end of all + LOG3("plen: %d dlen: %d\n", p->plen, p->dlen); + if (p->plen > 0) _daveDump("Parameter", p->param, p->plen); + if (p->dlen > 0) _daveDump("Data ", p->data, p->dlen); + if ((p->plen == 2) && (p->param[0] == daveFuncRead)) { + pd = p->data; + for (i = 0; i < p->param[1]; i++) { + _daveDump("Data hdr ", pd, 4); + + dl = 0x100 * pd[2] + pd[3]; + if (pd[1] == 4) dl /= 8; + pd += 4; + _daveDump("Data ", pd, dl); + if (i < p->param[1] - 1) dl = dl + (dl % 2); // the PLC places extra bytes at the end of all // but last result, if length is not a multiple // of 2 - pd+=dl; - } - } else if ((p->header[1]==1)&&/*(p->plen==2)&&*/(p->param[0]==daveFuncWrite)) { - pd=p->data; - for (i=0;iparam[1];i++) { - _daveDump("Write Data hdr ",pd,4); - - dl=0x100*pd[2]+pd[3]; - if (pd[1]==4) dl/=8; - pd+=4; - _daveDump("Data ",pd,dl); - if(iparam[1]-1) dl=dl+(dl%2); // the PLC places extra bytes at the end of all + pd += dl; + } + } + else if ((p->header[1] == 1) &&/*(p->plen==2)&&*/(p->param[0] == daveFuncWrite)) { + pd = p->data; + for (i = 0; i < p->param[1]; i++) { + _daveDump("Write Data hdr ", pd, 4); + + dl = 0x100 * pd[2] + pd[3]; + if (pd[1] == 4) dl /= 8; + pd += 4; + _daveDump("Data ", pd, dl); + if (i < p->param[1] - 1) dl = dl + (dl % 2); // the PLC places extra bytes at the end of all // but last result, if length is not a multiple // of 2 - pd+=dl; + pd += dl; } - } else { - /* + } + else { + /* if(p->dlen>0) { if(p->udlen==0) - _daveDump("Data ",p->data,p->dlen); + _daveDump("Data ",p->data,p->dlen); else _daveDump("Data hdr ",p->data,4); - } + } if(p->udlen>0) _daveDump("result Data ",p->udata,p->udlen); - */ + */ + } + if ((p->header[1] == 2) || (p->header[1] == 3)) { + LOG2("error: %s\n", daveStrerror(daveGetPDUerror(p))); } - if ((p->header[1]==2)||(p->header[1]==3)) { - LOG2("error: %s\n",daveStrerror(daveGetPDUerror(p))); - } } /* @@ -1204,8 +1223,8 @@ char * DECL2 daveBlockName(uc bn) { #ifdef DEBUG_CALLS LOG2("daveBlockName(bn:%d)\n", bn); FLUSH; -#endif - switch(bn) { +#endif + switch (bn) { case daveBlockType_OB: return "OB"; case daveBlockType_DB: return "DB"; case daveBlockType_SDB: return "SDB"; @@ -1214,14 +1233,14 @@ char * DECL2 daveBlockName(uc bn) { case daveBlockType_FB: return "FB"; case daveBlockType_SFB: return "SFB"; default:return "unknown block type!"; - } + } } char * DECL2 daveAreaName(uc n) { #ifdef DEBUG_CALLS LOG2("daveAreaName(n:%d)\n", n); FLUSH; -#endif +#endif switch (n) { case daveSysInfo: return "System info mem.area of 200 family"; case daveSysFlags: return "System flags of 200 family"; @@ -1241,131 +1260,131 @@ char * DECL2 daveAreaName(uc n) { case daveCounter200: return "IEC counters"; case daveTimer200: return "IEC timers"; default:return "unknown area!"; - } + } } /* Functions to load blocks from PLC: */ -void DECL2 _daveConstructUpload(PDU *p,char blockType, int blockNr) { - uc pa[]= {0x1d, - 0,0,0,0,0,0,0,9,0x5f,0x30,0x41,48,48,48,48,49,65}; - pa[11]=blockType; - sprintf((char*)(pa+12),"%05d",blockNr); - pa[17]='A'; - _daveInitPDUheader(p,1); +void DECL2 _daveConstructUpload(PDU *p, char blockType, int blockNr) { + uc pa[] = { 0x1d, + 0, 0, 0, 0, 0, 0, 0, 9, 0x5f, 0x30, 0x41, 48, 48, 48, 48, 49, 65 }; + pa[11] = blockType; + sprintf((char*)(pa + 12), "%05d", blockNr); + pa[17] = 'A'; + _daveInitPDUheader(p, 1); _daveAddParam(p, pa, sizeof(pa)); if (daveDebug & daveDebugPDU) { _daveDumpPDU(p); - } + } } void DECL2 _daveConstructDoUpload(PDU * p, uc* uploadID) { - uc pa[]= {0x1e,0,0,0,0,0,0,1}; + uc pa[] = { 0x1e, 0, 0, 0, 0, 0, 0, 1 }; pa[4] = uploadID[0]; pa[5] = uploadID[1]; pa[6] = uploadID[2]; pa[7] = uploadID[3]; - _daveInitPDUheader(p,1); + _daveInitPDUheader(p, 1); _daveAddParam(p, pa, sizeof(pa)); if (daveDebug & daveDebugPDU) { _daveDumpPDU(p); - } -} + } +} void DECL2 _daveConstructEndUpload(PDU * p, uc* uploadID) { - uc pa[]= {0x1f,0,0,0,0,0,0,1}; + uc pa[] = { 0x1f, 0, 0, 0, 0, 0, 0, 1 }; pa[4] = uploadID[0]; pa[5] = uploadID[1]; pa[6] = uploadID[2]; pa[7] = uploadID[3]; - _daveInitPDUheader(p,1); + _daveInitPDUheader(p, 1); _daveAddParam(p, pa, sizeof(pa)); if (daveDebug & daveDebugPDU) { _daveDumpPDU(p); - } -} + } +} /* Functions to load files from NC: */ void DECL2 _daveConstructUploadNC(PDU *p, const char *filename) { - uc pa[260]; - memset(pa, 0, sizeof(pa)); - pa[0] = 0x1d; /* Function code for start upload*/ - pa[8] = strlen(filename); - memcpy(&pa[9], filename, strlen(filename)); - _daveInitPDUheader(p, 1); - _daveAddParam(p, pa, strlen(filename) + 9); - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(p); - } + uc pa[260]; + memset(pa, 0, sizeof(pa)); + pa[0] = 0x1d; /* Function code for start upload*/ + pa[8] = strlen(filename); + memcpy(&pa[9], filename, strlen(filename)); + _daveInitPDUheader(p, 1); + _daveAddParam(p, pa, strlen(filename) + 9); + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(p); + } } void DECL2 _daveConstructDoUploadNC(PDU *p, uc *uploadID) { - uc pa[] = {0x1e,0,0,0,0,0,0,1}; - pa[4] = uploadID[0]; - pa[5] = uploadID[1]; - pa[6] = uploadID[2]; - pa[7] = uploadID[3]; - _daveInitPDUheader(p, 1); - _daveAddParam(p, pa, sizeof(pa)); - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(p); - } + uc pa[] = { 0x1e, 0, 0, 0, 0, 0, 0, 1 }; + pa[4] = uploadID[0]; + pa[5] = uploadID[1]; + pa[6] = uploadID[2]; + pa[7] = uploadID[3]; + _daveInitPDUheader(p, 1); + _daveAddParam(p, pa, sizeof(pa)); + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(p); + } } void DECL2 _daveConstructEndUploadNC(PDU * p, uc *uploadID) { - uc pa[] = {0x1f,0,0,0,0,0,0,1}; - pa[4] = uploadID[0]; - pa[5] = uploadID[1]; - pa[6] = uploadID[2]; - pa[7] = uploadID[3]; - _daveInitPDUheader(p,1); - _daveAddParam(p, pa, sizeof(pa)); - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(p); - } -} - -uc paInsert[]= { // sended after transmission of a complete block, + uc pa[] = { 0x1f, 0, 0, 0, 0, 0, 0, 1 }; + pa[4] = uploadID[0]; + pa[5] = uploadID[1]; + pa[6] = uploadID[2]; + pa[7] = uploadID[3]; + _daveInitPDUheader(p, 1); + _daveAddParam(p, pa, sizeof(pa)); + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(p); + } +} + +uc paInsert[] = { // sended after transmission of a complete block, // I guess this makes the CPU link the block into a program. - 0x28,0,0,0,0,0,0,0xFD,0,0x0A,1,0,0x30,0x42,0x30,0x30,0x30,0x30,0x34,0x50, // block type code and number - 0x05,'_','I','N','S','E', + 0x28, 0, 0, 0, 0, 0, 0, 0xFD, 0, 0x0A, 1, 0, 0x30, 0x42, 0x30, 0x30, 0x30, 0x30, 0x34, 0x50, // block type code and number + 0x05, '_', 'I', 'N', 'S', 'E', }; -uc paMakeRun[]= { - 0x28,0,0,0,0,0,0,0xFD,0,0x00,9,'P','_','P','R','O','G','R','A','M' +uc paMakeRun[] = { + 0x28, 0, 0, 0, 0, 0, 0, 0xFD, 0, 0x00, 9, 'P', '_', 'P', 'R', 'O', 'G', 'R', 'A', 'M' }; -uc paCompress[]= { - 0x28,0,0,0,0,0,0,0xFD,0,0x00,5,'_','G','A','R','B' +uc paCompress[] = { + 0x28, 0, 0, 0, 0, 0, 0, 0xFD, 0, 0x00, 5, '_', 'G', 'A', 'R', 'B' }; -uc paMakeStop[]= { - 0x29,0,0,0,0,0,9,'P','_','P','R','O','G','R','A','M' +uc paMakeStop[] = { + 0x29, 0, 0, 0, 0, 0, 9, 'P', '_', 'P', 'R', 'O', 'G', 'R', 'A', 'M' }; -uc paCopyRAMtoROM[]= { - 0x28,0,0,0,0,0,0,0xfd,0,2,'E','P',5,'_','M','O','D','U' +uc paCopyRAMtoROM[] = { + 0x28, 0, 0, 0, 0, 0, 0, 0xfd, 0, 2, 'E', 'P', 5, '_', 'M', 'O', 'D', 'U' }; int DECL2 daveStop(daveConnection * dc) { int res; - PDU p,p2; + PDU p, p2; #ifdef DEBUG_CALLS LOG2("daveStop(dc:%p)\n", dc); FLUSH; -#endif - if (dc->iface->protocol==daveProtoAS511) { +#endif + if (dc->iface->protocol == daveProtoAS511) { return daveStopS5(dc); - } - p.header=dc->msgOut+dc->PDUstartO; + } + p.header = dc->msgOut + dc->PDUstartO; _daveInitPDUheader(&p, 1); _daveAddParam(&p, paMakeStop, sizeof(paMakeStop)); - res=_daveExchange(dc, &p); - if (res==daveResOK) { - res=_daveSetupReceivedPDU(dc,&p2); + res = _daveExchange(dc, &p); + if (res == daveResOK) { + res = _daveSetupReceivedPDU(dc, &p2); if (daveDebug & daveDebugPDU) { _daveDumpPDU(&p2); } @@ -1375,20 +1394,20 @@ int DECL2 daveStop(daveConnection * dc) { int DECL2 daveStart(daveConnection*dc) { int res; - PDU p,p2; + PDU p, p2; #ifdef DEBUG_CALLS LOG2("daveStart(dc:%p)\n", dc); FLUSH; -#endif - if (dc->iface->protocol==daveProtoAS511) { +#endif + if (dc->iface->protocol == daveProtoAS511) { return daveStartS5(dc); - } - p.header=dc->msgOut+dc->PDUstartO; + } + p.header = dc->msgOut + dc->PDUstartO; _daveInitPDUheader(&p, 1); _daveAddParam(&p, paMakeRun, sizeof(paMakeRun)); - res=_daveExchange(dc, &p); - if (res==daveResOK) { - res=_daveSetupReceivedPDU(dc, &p2); + res = _daveExchange(dc, &p); + if (res == daveResOK) { + res = _daveSetupReceivedPDU(dc, &p2); if (daveDebug & daveDebugPDU) { _daveDumpPDU(&p2); } @@ -1398,17 +1417,17 @@ int DECL2 daveStart(daveConnection*dc) { int DECL2 daveCopyRAMtoROM(daveConnection * dc) { int res; - PDU p,p2; + PDU p, p2; #ifdef DEBUG_CALLS LOG2("davecopyRAMtoROM(dc:%p)\n", dc); FLUSH; -#endif - p.header=dc->msgOut+dc->PDUstartO; +#endif + p.header = dc->msgOut + dc->PDUstartO; _daveInitPDUheader(&p, 1); _daveAddParam(&p, paCopyRAMtoROM, sizeof(paCopyRAMtoROM)); - res=_daveExchange(dc, &p); - if (res==daveResOK) { - res=_daveSetupReceivedPDU(dc,&p2); /* possible problem, Timeout */ + res = _daveExchange(dc, &p); + if (res == daveResOK) { + res = _daveSetupReceivedPDU(dc, &p2); /* possible problem, Timeout */ if (daveDebug & daveDebugPDU) { _daveDumpPDU(&p2); } @@ -1419,41 +1438,41 @@ int DECL2 daveCopyRAMtoROM(daveConnection * dc) { /* Build a PDU with user data ud, send it and prepare received PDU. */ -int DECL2 daveBuildAndSendPDU(daveConnection * dc, PDU*p2,uc *pa,int psize, uc *ud,int usize) { +int DECL2 daveBuildAndSendPDU(daveConnection * dc, PDU*p2, uc *pa, int psize, uc *ud, int usize) { int res; PDU p; - uc nullData[]={0x0a,0,0,0}; - p.header=dc->msgOut+dc->PDUstartO; + uc nullData[] = { 0x0a, 0, 0, 0 }; + p.header = dc->msgOut + dc->PDUstartO; _daveInitPDUheader(&p, 7); _daveAddParam(&p, pa, psize); - if (ud!=NULL) _daveAddUserData(&p, ud, usize); else - if (usize!=0) _daveAddData(&p, nullData, 4); + if (ud != NULL) _daveAddUserData(&p, ud, usize); else + if (usize != 0) _daveAddData(&p, nullData, 4); if (daveDebug & daveDebugPDU) { _daveDumpPDU(&p); - } - res=_daveExchange(dc, &p); + } + res = _daveExchange(dc, &p); if (daveDebug & daveDebugErrorReporting) - LOG2("*** res of _daveExchange(): %d\n",res); - if (res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc,p2); + LOG2("*** res of _daveExchange(): %d\n", res); + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, p2); if (daveDebug & daveDebugPDU) { _daveDumpPDU(p2); } if (daveDebug & daveDebugErrorReporting) - LOG2("*** res of _daveSetupReceivedPDU(): %04X\n",res); - if (res!=daveResOK) return res; - res=_daveTestPGReadResult(p2); + LOG2("*** res of _daveSetupReceivedPDU(): %04X\n", res); + if (res != daveResOK) return res; + res = _daveTestPGReadResult(p2); if (daveDebug & daveDebugErrorReporting) - LOG2("*** res of _daveTestPGReadResult(): %04X\n",res); + LOG2("*** res of _daveTestPGReadResult(): %04X\n", res); return res; -} +} /* Get the PDU Data to a ByteBuffer */ -int DECL2 daveGetPDUData(daveConnection * dc, PDU*p2, uc* data, int* ldata, uc* param, int* lparam) +int DECL2 daveGetPDUData(daveConnection * dc, PDU*p2, uc* data, int* ldata, uc* param, int* lparam) { - int res=0; + int res = 0; memcpy(data, p2->data, p2->dlen); *ldata = p2->dlen; memcpy(param, p2->param, p2->plen); @@ -1462,277 +1481,277 @@ int DECL2 daveGetPDUData(daveConnection * dc, PDU*p2, uc* data, int* ldata, uc* return res; } -int DECL2 daveListBlocksOfType(daveConnection * dc,uc type,daveBlockEntry * buf) { - int res,i, len; +int DECL2 daveListBlocksOfType(daveConnection * dc, uc type, daveBlockEntry * buf) { + int res, i, len; PDU p2; - uc * buffer=(uc*)buf; - uc pa[]={0,1,18,4,17,67,2,0}; - uc da[]={'0','0'}; - uc pam[]={0,1,18,8,0x12,0x43,2,1,0,0,0,0}; + uc * buffer = (uc*)buf; + uc pa[] = { 0, 1, 18, 4, 17, 67, 2, 0 }; + uc da[] = { '0', '0' }; + uc pam[] = { 0, 1, 18, 8, 0x12, 0x43, 2, 1, 0, 0, 0, 0 }; #ifdef DEBUG_CALLS LOG4("ListBlocksOfType(dc:%p type:%d buf:%p)\n", dc, type, buf); FLUSH; -#endif - da[1]=type; - res=daveBuildAndSendPDU(dc, &p2,pa, sizeof(pa), da, sizeof(da)); - if (res!=daveResOK) return -res; - len=0; - while (p2.param[9]!=0) { - if (buffer!=NULL) memcpy(buffer+len,p2.udata,p2.udlen); - dc->resultPointer=p2.udata; - dc->_resultPointer=p2.udata; - len+=p2.udlen; +#endif + da[1] = type; + res = daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), da, sizeof(da)); + if (res != daveResOK) return -res; + len = 0; + while (p2.param[9] != 0) { + if (buffer != NULL) memcpy(buffer + len, p2.udata, p2.udlen); + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + len += p2.udlen; printf("more data\n"); - pam[7]=p2.param[7]; - res=daveBuildAndSendPDU(dc, &p2,pam, sizeof(pam), NULL, 1); - if (res==0xa) break; - if (res==0xd20e) break; //hopefully fixes: http://www.sps-forum.de/hochsprachen-opc/76710-libnodave-davelistblocksoftype-error.html#post542914 - if (res!=daveResOK) return res; // bugfix from Natalie Kather + pam[7] = p2.param[7]; + res = daveBuildAndSendPDU(dc, &p2, pam, sizeof(pam), NULL, 1); + if (res == 0xa) break; + if (res == 0xd20e) break; //hopefully fixes: http://www.sps-forum.de/hochsprachen-opc/76710-libnodave-davelistblocksoftype-error.html#post542914 + if (res != daveResOK) return res; // bugfix from Natalie Kather } //if (res==daveResOK) { - if (buffer!=NULL) memcpy(buffer+len,p2.udata,p2.udlen); - dc->resultPointer=p2.udata; - dc->_resultPointer=p2.udata; - len+=p2.udlen; + if (buffer != NULL) memcpy(buffer + len, p2.udata, p2.udlen); + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + len += p2.udlen; //} else { // if(daveDebug & daveDebugPrintErrors) // LOG3("daveListBlocksOfType: %d=%s\n",res, daveStrerror(res)); //} - dc->AnswLen=len; - res=len/sizeof(daveBlockEntry); - for (i=0; iAnswLen = len; + res = len / sizeof(daveBlockEntry); + for (i = 0; i < res; i++) { + buf[i].number = daveSwapIed_16(buf[i].number); } return res; -} +} /* doesn't work on S7-200 */ -int DECL2 daveGetOrderCode(daveConnection * dc,char * buf) { - int res=0; +int DECL2 daveGetOrderCode(daveConnection * dc, char * buf) { + int res = 0; PDU p2; - uc pa[]={0,1,18,4,17,68,1,0}; - uc da[]={1,17,0,1}; /* SZL-ID 0x111 index 1 */ + uc pa[] = { 0, 1, 18, 4, 17, 68, 1, 0 }; + uc da[] = { 1, 17, 0, 1 }; /* SZL-ID 0x111 index 1 */ #ifdef DEBUG_CALLS LOG3("daveGetOrderCode(dc:%p buf:%p)\n", dc, buf); FLUSH; -#endif - res=daveBuildAndSendPDU(dc, &p2,pa, sizeof(pa), da, sizeof(da)); - if (res!=daveResOK) return res; // bugfix from Natalie Kather +#endif + res = daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), da, sizeof(da)); + if (res != daveResOK) return res; // bugfix from Natalie Kather if (buf) { - memcpy(buf, p2.udata+10, daveOrderCodeSize); - buf[daveOrderCodeSize]=0; - } + memcpy(buf, p2.udata + 10, daveOrderCodeSize); + buf[daveOrderCodeSize] = 0; + } return res; } int DECL2 daveReadSZL(daveConnection * dc, int ID, int index, void * buffer, int buflen) { - int res,len,cpylen; + int res, len, cpylen; int pa7; - // int pa6; + // int pa6; PDU p2; - uc pa[]={0,1,18,4,17,68,1,0}; - uc da[]={1,17,0,1}; + uc pa[] = { 0, 1, 18, 4, 17, 68, 1, 0 }; + uc da[] = { 1, 17, 0, 1 }; - uc pam[]={0,1,18,8,18,68,1,1,0,0,0,0}; - // uc dam[]={10,0,0,0}; + uc pam[] = { 0, 1, 18, 8, 18, 68, 1, 1, 0, 0, 0, 0 }; + // uc dam[]={10,0,0,0}; #ifdef DEBUG_CALLS LOG5("daveReadSZL(dc:%p, ID:%d, index:%d, buffer:%p)\n", dc, ID, index, buffer); FLUSH; -#endif - da[0]=ID / 0x100; - da[1]=ID % 0x100; - da[2]=index / 0x100; - da[3]=index % 0x100; - res=daveBuildAndSendPDU(dc, &p2,pa, sizeof(pa), da, sizeof(da)); - if (res!=daveResOK) return res; // bugfix from Natalie Kather - - len=0; - pa7=p2.param[7]; - // pa6=p2.param[6]; - while (p2.param[9]!=0) { - if (buffer!=NULL) { +#endif + da[0] = ID / 0x100; + da[1] = ID % 0x100; + da[2] = index / 0x100; + da[3] = index % 0x100; + res = daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), da, sizeof(da)); + if (res != daveResOK) return res; // bugfix from Natalie Kather + + len = 0; + pa7 = p2.param[7]; + // pa6=p2.param[6]; + while (p2.param[9] != 0) { + if (buffer != NULL) { cpylen = p2.udlen; if (len + cpylen > buflen) cpylen = buflen - len; - if (cpylen > 0) memcpy((uc *)buffer+len,p2.udata,cpylen); + if (cpylen > 0) memcpy((uc *)buffer + len, p2.udata, cpylen); } - dc->resultPointer=p2.udata; - dc->_resultPointer=p2.udata; - len+=p2.udlen; - pam[7]=pa7; + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + len += p2.udlen; + pam[7] = pa7; // res=daveBuildAndSendPDU(dc, &p2,pam, sizeof(pam), NULL, sizeof(dam)); - res=daveBuildAndSendPDU(dc, &p2,pam, sizeof(pam), NULL, 1); - if (res!=daveResOK) return res; // bugfix from Natalie Kather + res = daveBuildAndSendPDU(dc, &p2, pam, sizeof(pam), NULL, 1); + if (res != daveResOK) return res; // bugfix from Natalie Kather } - if (res==daveResOK) { - if (buffer!=NULL) { + if (res == daveResOK) { + if (buffer != NULL) { cpylen = p2.udlen; if (len + cpylen > buflen) cpylen = buflen - len; - if (cpylen > 0) memcpy((uc *)buffer+len,p2.udata,cpylen); + if (cpylen > 0) memcpy((uc *)buffer + len, p2.udata, cpylen); } - dc->resultPointer=p2.udata; - dc->_resultPointer=p2.udata; - len+=p2.udlen; - } - dc->AnswLen=len; + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + len += p2.udlen; + } + dc->AnswLen = len; return res; } int DECL2 daveGetBlockInfo(daveConnection * dc, daveBlockInfo *dbi, uc type, int number) { int res; - uc pa[]={0,1,18,4,17,67,3,0}; /* param */ - uc da[]={'0',0,'0','0','0','1','0','A'}; + uc pa[] = { 0, 1, 18, 4, 17, 67, 3, 0 }; /* param */ + uc da[] = { '0', 0, '0', '0', '0', '1', '0', 'A' }; PDU p2; - sprintf((char*)(da+2),"%05d",number); - da[1]=type; - da[7]='A'; - res=daveBuildAndSendPDU(dc, &p2,pa, sizeof(pa), da, sizeof(da)); - if (res!=daveResOK) return res; // bugfix from Natalie Kather - if ((dbi!=NULL) && (p2.udlen==sizeof(daveBlockInfo))) { + sprintf((char*)(da + 2), "%05d", number); + da[1] = type; + da[7] = 'A'; + res = daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), da, sizeof(da)); + if (res != daveResOK) return res; // bugfix from Natalie Kather + if ((dbi != NULL) && (p2.udlen == sizeof(daveBlockInfo))) { memcpy(dbi, p2.udata, p2.udlen); - dbi->number=daveSwapIed_16(dbi->number); - dbi->length=daveSwapIed_16(dbi->length); + dbi->number = daveSwapIed_16(dbi->number); + dbi->length = daveSwapIed_16(dbi->length); } - return res; + return res; } -int DECL2 daveListBlocks(daveConnection * dc,daveBlockTypeEntry * buf) { - int res,i; +int DECL2 daveListBlocks(daveConnection * dc, daveBlockTypeEntry * buf) { + int res, i; PDU p2; - uc pa[]={0,1,18,4,17,67,1,0}; - res=daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), NULL, 1/*da, sizeof(da)*/); - if (res!=daveResOK) return res; // bugfix from Natalie Kather - res=p2.udlen/sizeof(daveBlockTypeEntry); + uc pa[] = { 0, 1, 18, 4, 17, 67, 1, 0 }; + res = daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), NULL, 1/*da, sizeof(da)*/); + if (res != daveResOK) return res; // bugfix from Natalie Kather + res = p2.udlen / sizeof(daveBlockTypeEntry); if (buf) { memcpy(buf, p2.udata, p2.udlen); - for (i=0; i0) { - if (len>dc->maxPDUlength-18) readLen=dc->maxPDUlength-18; else readLen=len; - res=daveReadBytes(dc,area, DBnum, start, readLen, pbuf); - if (res!=0) return res; - len-=readLen; - start+=readLen; - pbuf+=readLen; - } + pos = 0; + if (buffer == NULL) return daveResNoBuffer; + pbuf = (uc*)buffer; + res = daveResInvalidLength; //the only chance to return this is when len<=0 + while (len > 0) { + if (len > dc->maxPDUlength - 18) readLen = dc->maxPDUlength - 18; else readLen = len; + res = daveReadBytes(dc, area, DBnum, start, readLen, pbuf); + if (res != 0) return res; + len -= readLen; + start += readLen; + pbuf += readLen; + } return res; } /* -Read len bytes from PLC memory area "area", data block DBnum. +Read len bytes from PLC memory area "area", data block DBnum. Return the Number of bytes read. If a buffer pointer is provided, data will be copied into this buffer. If it's NULL you can get your data from the resultPointer in daveConnection long as you do not send further requests. */ -int DECL2 daveReadBytes(daveConnection * dc,int area, int DBnum, int start,int len, void * buffer){ - PDU p1,p2; +int DECL2 daveReadBytes(daveConnection * dc, int area, int DBnum, int start, int len, void * buffer){ + PDU p1, p2; int res; #ifdef DEBUG_CALLS LOG7("daveReadBytes(dc:%p area:%s area number:%d start address:%d byte count:%d buffer:%p)\n", - dc, daveAreaName(area), DBnum, start,len, buffer); - FLUSH; + dc, daveAreaName(area), DBnum, start, len, buffer); + FLUSH; #endif - if (dc->iface->protocol==daveProtoAS511) { + if (dc->iface->protocol == daveProtoAS511) { return daveReadS5Bytes(dc, area, DBnum, start, len/*, buffer*/); } - dc->AnswLen=0; // 03/12/05 - dc->resultPointer=NULL; - dc->_resultPointer=NULL; - p1.header=dc->msgOut+dc->PDUstartO; + dc->AnswLen = 0; // 03/12/05 + dc->resultPointer = NULL; + dc->_resultPointer = NULL; + p1.header = dc->msgOut + dc->PDUstartO; davePrepareReadRequest(dc, &p1); daveAddVarToReadRequest(&p1, area, DBnum, start, len); - res=_daveExchange(dc, &p1); - if (res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); + res = _daveExchange(dc, &p1); + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); if (daveDebug & daveDebugPDU) - LOG3("_daveSetupReceivedPDU() returned: %d=%s\n", res,daveStrerror(res)); - if (res!=daveResOK) return res; + LOG3("_daveSetupReceivedPDU() returned: %d=%s\n", res, daveStrerror(res)); + if (res != daveResOK) return res; - res=_daveTestReadResult(&p2); + res = _daveTestReadResult(&p2); if (daveDebug & daveDebugPDU) - LOG3("_daveTestReadResult() returned: %d=%s\n", res,daveStrerror(res)); - if (res!=daveResOK) return res; + LOG3("_daveTestReadResult() returned: %d=%s\n", res, daveStrerror(res)); + if (res != daveResOK) return res; - if (p2.udlen==0) { - return daveResCPUNoData; - } + if (p2.udlen == 0) { + return daveResCPUNoData; + } /* copy to user buffer and setup internal buffer pointers: - */ - if (buffer!=NULL) memcpy(buffer,p2.udata,p2.udlen); - dc->resultPointer=p2.udata; - dc->_resultPointer=p2.udata; - dc->AnswLen=p2.udlen; + */ + if (buffer != NULL) memcpy(buffer, p2.udata, p2.udlen); + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + dc->AnswLen = p2.udlen; return res; } /* -Read len BITS from PLC memory area "area", data block DBnum. +Read len BITS from PLC memory area "area", data block DBnum. Return the Number of bytes read. If a buffer pointer is provided, data will be copied into this buffer. If it's NULL you can get your data from the resultPointer in daveConnection long as you do not send further requests. */ -int DECL2 daveReadBits(daveConnection * dc,int area, int DBnum, int start,int len, void * buffer){ - PDU p1,p2; +int DECL2 daveReadBits(daveConnection * dc, int area, int DBnum, int start, int len, void * buffer){ + PDU p1, p2; int res; #ifdef DEBUG_CALLS LOG7("daveReadBits(dc:%p area:%s area number:%d start address:%d byte count:%d buffer:%p)\n", - dc, daveAreaName(area), DBnum, start,len,buffer); - FLUSH; -#endif - dc->resultPointer=NULL; - dc->_resultPointer=NULL; - dc->AnswLen=0; - p1.header=dc->msgOut+dc->PDUstartO; + dc, daveAreaName(area), DBnum, start, len, buffer); + FLUSH; +#endif + dc->resultPointer = NULL; + dc->_resultPointer = NULL; + dc->AnswLen = 0; + p1.header = dc->msgOut + dc->PDUstartO; davePrepareReadRequest(dc, &p1); daveAddBitVarToReadRequest(&p1, area, DBnum, start, len); - res=_daveExchange(dc, &p1); - if (res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); + res = _daveExchange(dc, &p1); + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); if (daveDebug & daveDebugPDU) - LOG3("_daveSetupReceivedPDU() returned: %d=%s\n", res,daveStrerror(res)); - if (res!=daveResOK) return res; + LOG3("_daveSetupReceivedPDU() returned: %d=%s\n", res, daveStrerror(res)); + if (res != daveResOK) return res; - res=_daveTestReadResult(&p2); + res = _daveTestReadResult(&p2); if (daveDebug & daveDebugPDU) - LOG3("_daveTestReadResult() returned: %d=%s\n", res,daveStrerror(res)); - if (res!=daveResOK) return res; + LOG3("_daveTestReadResult() returned: %d=%s\n", res, daveStrerror(res)); + if (res != daveResOK) return res; if (daveDebug & daveDebugPDU) LOG2("got %d bytes of data\n", p2.udlen); - if (p2.udlen==0) { - return daveResCPUNoData; - } - if (buffer!=NULL) { + if (p2.udlen == 0) { + return daveResCPUNoData; + } + if (buffer != NULL) { if (daveDebug & daveDebugPDU) LOG2("copy %d bytes to buffer\n", p2.udlen); - memcpy(buffer,p2.udata,p2.udlen); - } - dc->resultPointer=p2.udata; - dc->_resultPointer=p2.udata; - dc->AnswLen=p2.udlen; + memcpy(buffer, p2.udata, p2.udlen); + } + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + dc->AnswLen = p2.udlen; return res; } @@ -1744,74 +1763,93 @@ int DECL2 daveExecReadRequest(daveConnection * dc, PDU *p, daveResultSet* rl){ uc * q; daveResult * cr, *c2; int res, i, len, rlen; -#ifdef DEBUG_CALLS - LOG4("daveExecReadRequest(dc:%p, PDU:%p, rl:%p\n", dc, p, rl); - FLUSH; -#endif - dc->AnswLen=0; // 03/12/05 - dc->resultPointer=NULL; - dc->_resultPointer=NULL; - res=_daveExchange(dc, p); - if (res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); - if (res!=daveResOK) return res; - res=_daveTestReadResultMulti(&p2); - if (res!=daveResOK) return res; - i=0; - if (rl!=NULL) { - cr=(daveResult*)calloc(p2.param[1], sizeof(daveResult)); - rl->numResults=p2.param[1]; - rl->results=cr; - c2=cr; - q=p2.data; - rlen=p2.dlen; - while (i4)) { - len=q[2]*0x100+q[3]; - if (q[1]==4) { - len>>=3; /* len is in bits, adjust */ - } else if (q[1]==5) { /* Fehlenden Size-Type INTEGER ergänzt */ - len>>=3; /* len is in bits, adjust */ - } else if (q[1]==7) { /* Fehlenden Size-Type REAL ergänzt */ - /* len is already in bytes, ok */ - } else if (q[1]==9) { + if (daveDebug & daveDebugPDU) + { + LOG4("daveExecReadRequest(dc:%p, PDU:%p, rl:%p\n", dc, p, rl); + FLUSH; + } + dc->AnswLen = 0; // 03/12/05 + dc->resultPointer = NULL; + dc->_resultPointer = NULL; + res = _daveExchange(dc, p); + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + if (res != daveResOK) return res; + res = _daveTestReadResultMulti(&p2); + if (res != daveResOK) return res; + i = 0; + if (rl != NULL) { + cr = (daveResult*)calloc(p2.param[1], sizeof(daveResult)); + rl->numResults = p2.param[1]; + rl->results = cr; + c2 = cr; + q = p2.data; + rlen = p2.dlen; + while (i < p2.param[1]) { + /* printf("result %d: %d %d %d %d\n",i, *q,q[1],q[2],q[3]); */ + if (daveDebug & daveDebugPDU) + { + LOG2("daveExecReadRequest result %d: %d %d %d %d\n", i, *q, q[1], q[2], q[3]); + FLUSH; + } + if ((*q == 255) && (rlen>4)) { + len = q[2] * 0x100 + q[3]; + if (q[1] == 4) { + len >>= 3; /* len is in bits, adjust */ + } + else if (q[1] == 5) { /* Fehlenden Size-Type INTEGER ergänzt */ + //len>>=3; /* len is in bits, adjust */ + } + else if (q[1] == 7) { /* Fehlenden Size-Type REAL ergänzt */ + /* len is already in bytes, ok */ + } + else if (q[1] == 9) { /* len is already in bytes, ok */ - } else if (q[1]==3) { + } + else if (q[1] == 3) { /* len is in bits, but there is a byte per result bit, ok */ - } else if (q[1]==6) { + } + else if (q[1] == 6) { /* integer access, len is in bytes */ - } else { + } + else { if (daveDebug & daveDebugPDU) - LOG2("fixme: what to do with data type %d?\n",q[1]); + LOG2("fixme: what to do with data type %d?\n", q[1]); } - } else { - len=0; - } - /* printf("Store result %d length:%d\n", i, len); */ - c2->length=len; - if(len>0){ - c2->bytes=(uc*)malloc(len); - memcpy(c2->bytes, q+4, len); - } - c2->error=daveUnknownError; - - if (q[0]==0xFF) { - c2->error=daveResOK; - } else - c2->error=q[0]; - - /* printf("Error %d\n", c2->error); */ - q+=len+4; - rlen-=len; - if ((len % 2)==1) { + } + else { + len = 0; + } + /* printf("Store result %d length:%d\n", i, len); */ + if (daveDebug & daveDebugPDU) + { + LOG2("Store result %d length:%d\n", i, len); + FLUSH; + } + c2->length = len; + if (len > 0){ + c2->bytes = (uc*)malloc(len); + memcpy(c2->bytes, q + 4, len); + } + c2->error = daveUnknownError; + + if (q[0] == 0xFF) { + c2->error = daveResOK; + } + else + c2->error = q[0]; + + /* printf("Error %d\n", c2->error); */ + q += len + 4; + rlen -= len; + if ((len % 2) == 1) { q++; rlen--; - } + } c2++; i++; } - } + } return res; } @@ -1826,94 +1864,112 @@ int DECL2 daveExecWriteRequest(daveConnection * dc, PDU *p, daveResultSet* rl){ #ifdef DEBUG_CALLS LOG4("daveExecWriteRequest(dc:%p, PDU:%p, rl:%p\n", dc, p, rl); FLUSH; -#endif - res=_daveExchange(dc, p); - if(res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); - if(res!=daveResOK) return res; - res=_daveTestWriteResult(&p2); - if(res!=daveResOK) return res; - if (rl!=NULL) { - cr=(daveResult*)calloc(p2.param[1], sizeof(daveResult)); - rl->numResults=p2.param[1]; - rl->results=cr; - c2=cr; - q=p2.data; - i=0; - while (inumResults = p2.param[1]; + rl->results = cr; + c2 = cr; + q = p2.data; + i = 0; + while (i < p2.param[1]) { /* printf("result %d: %d %d %d %d\n",i, *q,q[1],q[2],q[3]); */ - c2->error=daveUnknownError; - if (q[0]==0x0A) { /* 300 and 400 families */ - c2->error=daveResItemNotAvailable; - } else if (q[0]==0x03) { /* 200 family */ - c2->error=daveResItemNotAvailable; - } else if (q[0]==0x05) { - c2->error=daveAddressOutOfRange; - } else if (q[0]==0xFF) { - c2->error=daveResOK; - } else if (q[0]==0x07) { - c2->error=daveWriteDataSizeMismatch; - } - /* printf("Error %d\n", c2->error); */ + c2->error = daveUnknownError; + if (q[0] == 0x0A) { /* 300 and 400 families */ + c2->error = daveResItemNotAvailable; + } + else if (q[0] == 0x03) { /* 200 family */ + c2->error = daveResItemNotAvailable; + } + else if (q[0] == 0x05) { + c2->error = daveAddressOutOfRange; + } + else if (q[0] == 0xFF) { + c2->error = daveResOK; + } + else if (q[0] == 0x07) { + c2->error = daveWriteDataSizeMismatch; + } + /* printf("Error %d\n", c2->error); */ q++; c2++; i++; - } + } } return res; } int DECL2 daveUseResult(daveConnection * dc, daveResultSet * rl, int n, void * buffer){ daveResult * dr; -#ifdef DEBUG_CALLS - LOG4("daveUseResult(dc:%p, result set:%p, number:%d)\n", dc, rl, n); -#endif - if (rl==NULL) { -#ifdef DEBUG_CALLS - LOG1("invalid resultSet \n"); - FLUSH; -#endif + if (daveDebug & daveDebugAll) + { + LOG4("daveUseResult(dc:%p, result set:%p, number:%d)\n", dc, rl, n); + } + if (rl == NULL) + { + if (daveDebug & daveDebugUpload) + { + LOG1("invalid resultSet \n"); + FLUSH; + } return daveEmptyResultSetError; - } -#ifdef DEBUG_CALLS - LOG2("result set has %d results\n",rl->numResults); - FLUSH; -#endif - if (rl->numResults==0) return daveEmptyResultSetError; - if (n>=rl->numResults) return daveEmptyResultSetError; + } + if (daveDebug & daveDebugAll) + { + LOG2("result set has %d results\n", rl->numResults); + FLUSH; + } + if (rl->numResults == 0) return daveEmptyResultSetError; + if (n >= rl->numResults) return daveEmptyResultSetError; dr = &(rl->results[n]); - if (dr->error!=0) return dr->error; - if (dr->length<=0) return daveEmptyResultError; - - if (buffer!=NULL) memcpy(buffer,dr->bytes,dr->length); - dc->resultPointer=dr->bytes; - dc->_resultPointer=dr->bytes; + if (daveDebug & daveDebugAll) + { + LOG2("result error: %d\n", dr->error); + LOG2("result length: %d\n", dr->length); + FLUSH; + } + if (dr->error != 0) return dr->error; + if (dr->length <= 0) return daveEmptyResultError; + + if (buffer != NULL) memcpy(buffer, dr->bytes, dr->length); + dc->resultPointer = dr->bytes; + dc->_resultPointer = dr->bytes; return 0; } int DECL2 daveUseResultBuffer(daveResultSet * rl, int n, void * buffer){ daveResult * dr; -#ifdef DEBUG_CALLS - LOG4("daveUseResult(result set:%p, number:%d)\n", rl, n); -#endif - if (rl==NULL) { -#ifdef DEBUG_CALLS - LOG1("invalid resultSet \n"); - FLUSH; -#endif + if (daveDebug & daveDebugAll) + { + LOG2("daveUseResultBuffer(result set:%p, number:%d)\n", rl, n); + } + if (rl == NULL) + { + if (daveDebug & daveDebugUpload) + { + LOG1("invalid resultSet \n"); + FLUSH; + } return daveEmptyResultSetError; - } -#ifdef DEBUG_CALLS - LOG2("result set has %d results\n",rl->numResults); - FLUSH; -#endif - if (rl->numResults==0) return daveEmptyResultSetError; - if (n>=rl->numResults) return daveEmptyResultSetError; + } + if (daveDebug & daveDebugAll) + { + LOG2("result set has %d results\n", rl->numResults); + FLUSH; + } + if (rl->numResults == 0) return daveEmptyResultSetError; + if (n >= rl->numResults) return daveEmptyResultSetError; dr = &(rl->results[n]); - if (dr->error!=0) return dr->error; - if (dr->length<=0) return daveEmptyResultError; - - if (buffer!=NULL) memcpy(buffer,dr->bytes,dr->length); + if (dr->error != 0) return dr->error; + if (dr->length <= 0) return daveEmptyResultError; + + if (buffer != NULL) memcpy(buffer, dr->bytes, dr->length); return 0; } @@ -1921,26 +1977,26 @@ void DECL2 daveFreeResults(daveResultSet * rl){ daveResult * r; int i; #ifdef DEBUG_CALLS - LOG2("daveFreeResults(%p)",rl); -#endif - if (rl==NULL) { + LOG2("daveFreeResults(%p)", rl); +#endif + if (rl == NULL) { #ifdef DEBUG_CALLS LOG1("no Results,ready\n"); -#endif +#endif return; // make it NULL safe - } - /* printf("result set: %p\n",rl); */ - for (i=0; inumResults; i++) { - r=&(rl->results[i]); + } + /* printf("result set: %p\n",rl); */ + for (i = 0; i < rl->numResults; i++) { + r = &(rl->results[i]); /* printf("result: %p bytes at:%p\n",r,r->bytes); */ - if (r->bytes!=NULL) free(r->bytes); + if (r->bytes != NULL) free(r->bytes); } #ifdef DEBUG_CALLS - LOG2(" free'd %d results\n",rl->numResults); -#endif - free(rl->results); // fix from Renato Gartmann - rl->numResults=0; - /* free(rl); */ /* This is NOT malloc'd by library but in the application's memory space! */ + LOG2(" free'd %d results\n", rl->numResults); +#endif + free(rl->results); // fix from Renato Gartmann + rl->numResults = 0; + /* free(rl); */ /* This is NOT malloc'd by library but in the application's memory space! */ } int DECL2 daveGetErrorOfResult(daveResultSet *rs, int number) { @@ -1948,85 +2004,85 @@ int DECL2 daveGetErrorOfResult(daveResultSet *rs, int number) { } -daveConnection * DECL2 daveNewExtendedConnection(daveInterface * di, void * Destination, int DestinationIsIP, int rack, int slot, int routing, int routingSubnetFirst, int routingSubnetSecond, int routingRack, int routingSlot, void * routingDestination, int routingDestinationIsIP, int ConnectionType, int routingConnectionType) { +daveConnection * DECL2 daveNewExtendedConnection(daveInterface * di, void * Destination, int DestinationIsIP, int rack, int slot, int routing, int routingSubnetFirst, int routingSubnetSecond, int routingRack, int routingSlot, void * routingDestination, int routingDestinationIsIP, int ConnectionType, int routingConnectionType) { - daveConnection * dc=(daveConnection *) calloc(1,sizeof(daveConnection)); + daveConnection * dc = (daveConnection *)calloc(1, sizeof(daveConnection)); if (dc) { uc * pbuf; - pbuf=(uc*) Destination; + pbuf = (uc*)Destination; - dc->DestinationIsIP=DestinationIsIP; + dc->DestinationIsIP = DestinationIsIP; if (DestinationIsIP) { - dc->_DestinationSize=4; - dc->_Destination1=pbuf[0]; - dc->_Destination2=pbuf[1]; - dc->_Destination3=pbuf[2]; - dc->_Destination4=pbuf[3]; + dc->_DestinationSize = 4; + dc->_Destination1 = pbuf[0]; + dc->_Destination2 = pbuf[1]; + dc->_Destination3 = pbuf[2]; + dc->_Destination4 = pbuf[3]; } else { - dc->_DestinationSize=1; - dc->_Destination1=pbuf[0]; - dc->MPIAdr=pbuf[0]; - dc->_Destination2=0; - dc->_Destination3=0; - dc->_Destination4=0; + dc->_DestinationSize = 1; + dc->_Destination1 = pbuf[0]; + dc->MPIAdr = pbuf[0]; + dc->_Destination2 = 0; + dc->_Destination3 = 0; + dc->_Destination4 = 0; } - dc->iface=di; + dc->iface = di; - dc->rack=rack; - dc->slot=slot; - dc->routing=routing; - dc->routingSubnetFirst=routingSubnetFirst; - dc->routingSubnetSecond=routingSubnetSecond; - dc->routingRack=routingRack; - dc->routingSlot=routingSlot; + dc->rack = rack; + dc->slot = slot; + dc->routing = routing; + dc->routingSubnetFirst = routingSubnetFirst; + dc->routingSubnetSecond = routingSubnetSecond; + dc->routingRack = routingRack; + dc->routingSlot = routingSlot; dc->ConnectionType = ConnectionType; dc->routingConnectionType = routingConnectionType; - pbuf=(uc*) routingDestination; + pbuf = (uc*)routingDestination; - dc->routingDestinationIsIP=routingDestinationIsIP; + dc->routingDestinationIsIP = routingDestinationIsIP; if (routingDestinationIsIP) { - dc->_routingDestinationSize=4; - dc->_routingDestination1=pbuf[0]; - dc->_routingDestination2=pbuf[1]; - dc->_routingDestination3=pbuf[2]; - dc->_routingDestination4=pbuf[3]; + dc->_routingDestinationSize = 4; + dc->_routingDestination1 = pbuf[0]; + dc->_routingDestination2 = pbuf[1]; + dc->_routingDestination3 = pbuf[2]; + dc->_routingDestination4 = pbuf[3]; } else { - dc->_routingDestinationSize=1; - dc->_routingDestination1=pbuf[0]; - dc->_routingDestination2=0; - dc->_routingDestination3=0; - dc->_routingDestination4=0; + dc->_routingDestinationSize = 1; + dc->_routingDestination1 = pbuf[0]; + dc->_routingDestination2 = 0; + dc->_routingDestination3 = 0; + dc->_routingDestination4 = 0; } } return _daveNewConnection(dc); } -/* +/* This will setup a new connection structure using an initialized daveInterface and PLC's MPI address. */ daveConnection * DECL2 daveNewConnection(daveInterface * di, int MPI, int rack, int slot) { - daveConnection * dc=(daveConnection *) calloc(1,sizeof(daveConnection)); + daveConnection * dc = (daveConnection *)calloc(1, sizeof(daveConnection)); if (dc) { - dc->iface=di; - dc->MPIAdr=MPI; + dc->iface = di; + dc->MPIAdr = MPI; - dc->rack=rack; - dc->slot=slot; - dc->routing=0; + dc->rack = rack; + dc->slot = slot; + dc->routing = 0; dc->ConnectionType = 1; dc->routingConnectionType = 1; @@ -2035,231 +2091,231 @@ daveConnection * DECL2 daveNewConnection(daveInterface * di, int MPI, int rack, } -daveConnection * DECL2 _daveNewConnection(daveConnection * dc) { +daveConnection * DECL2 _daveNewConnection(daveConnection * dc) { if (dc) { - dc->maxPDUlength=960; // assume an (unreal?) maximum - dc->connectionNumber=dc->iface->nextConnection; // 1/10/05 trying Andrew's patch + dc->maxPDUlength = 960; // assume an (unreal?) maximum + dc->connectionNumber = dc->iface->nextConnection; // 1/10/05 trying Andrew's patch - dc->PDUnumber=0xFFFE; // just a start value; // test! + dc->PDUnumber = 0xFFFE; // just a start value; // test! - dc->messageNumber=0; + dc->messageNumber = 0; switch (dc->iface->protocol) { case daveProtoMPI: /* my first Version of MPI */ - dc->PDUstartO=8; /* position of PDU in outgoing messages */ - dc->PDUstartI=8; /* position of PDU in incoming messages */ - dc->iface->ackPos=6; /* position of 0xB0 in ack packet */ - dc->maxPDUlength=240; /* limit because we still cannot assemble a PDU transported in multiple packets */ + dc->PDUstartO = 8; /* position of PDU in outgoing messages */ + dc->PDUstartI = 8; /* position of PDU in incoming messages */ + dc->iface->ackPos = 6; /* position of 0xB0 in ack packet */ + dc->maxPDUlength = 240; /* limit because we still cannot assemble a PDU transported in multiple packets */ break; case daveProtoMPI3: /* Step 7 Version of MPI */ - dc->PDUstartO=8; /* position of PDU in outgoing messages */ - dc->PDUstartI=12; /* position of PDU in incoming messages */ - dc->iface->ackPos=10; /* position of 0xB0 in ack packet */ - dc->maxPDUlength=240; /* limit because we still cannot assemble a PDU transported in multiple packets */ - break; + dc->PDUstartO = 8; /* position of PDU in outgoing messages */ + dc->PDUstartI = 12; /* position of PDU in incoming messages */ + dc->iface->ackPos = 10; /* position of 0xB0 in ack packet */ + dc->maxPDUlength = 240; /* limit because we still cannot assemble a PDU transported in multiple packets */ + break; case daveProtoMPI2: /* Andrew's Version of MPI */ case daveProtoMPI4: /* Andrew's Version of MPI with extra STX */ - dc->PDUstartO=6; /* position of PDU in outgoing messages */ - dc->PDUstartI=6; /* position of PDU in incoming messages */ - dc->iface->ackPos=4; /* position of 0xB0 in ack packet */ - dc->maxPDUlength=240; /* limit because we still cannot assemble a PDU transported in multiple packets */ - break; - - case daveProtoNLPro: /* Deltalogic NetLink Pro */ - dc->PDUstartO=6; /* position of PDU in outgoing messages */ - dc->PDUstartI=8; /* position of PDU in incoming messages */ - dc->iface->ackPos=4; /* position of 0xB0 in ack packet */ - break; - - case daveProtoNLProFamily: /* Deltalogic NetLink Pro */ - dc->PDUstartO=8; /* position of PDU in outgoing messages */ - dc->PDUstartI=8; /* position of PDU in incoming messages */ - dc->iface->ackPos=4; /* position of 0xB0 in ack packet */ - break; + dc->PDUstartO = 6; /* position of PDU in outgoing messages */ + dc->PDUstartI = 6; /* position of PDU in incoming messages */ + dc->iface->ackPos = 4; /* position of 0xB0 in ack packet */ + dc->maxPDUlength = 240; /* limit because we still cannot assemble a PDU transported in multiple packets */ + break; + + case daveProtoNLPro: /* Deltalogic NetLink Pro */ + dc->PDUstartO = 6; /* position of PDU in outgoing messages */ + dc->PDUstartI = 8; /* position of PDU in incoming messages */ + dc->iface->ackPos = 4; /* position of 0xB0 in ack packet */ + break; + + case daveProtoNLProFamily: /* Deltalogic NetLink Pro */ + dc->PDUstartO = 8; /* position of PDU in outgoing messages */ + dc->PDUstartI = 8; /* position of PDU in incoming messages */ + dc->iface->ackPos = 4; /* position of 0xB0 in ack packet */ + break; case daveProtoPPI: - dc->PDUstartO=3; /* position of PDU in outgoing messages */ - dc->PDUstartI=7; /* position of PDU in incoming messages */ - break; + dc->PDUstartO = 3; /* position of PDU in outgoing messages */ + dc->PDUstartI = 7; /* position of PDU in incoming messages */ + break; case daveProtoISOTCP: case daveProtoISOTCP243: - dc->PDUstartO=7; /* position of PDU in outgoing messages */ - dc->PDUstartI=7; /* position of PDU in incoming messages */ + dc->PDUstartO = 7; /* position of PDU in outgoing messages */ + dc->PDUstartI = 7; /* position of PDU in incoming messages */ //dc->iface->timeout=1500000; - break; - case daveProtoMPI_IBH: -// dc->maxPDUlength=240; // limit for NetLink as reported by AFK. Not needed any more. Now, a PDU can be split into multiple IBH packets. - dc->PDUstartI= sizeof(IBHpacket)+sizeof(MPIheader); - dc->PDUstartO= sizeof(IBHpacket)+sizeof(MPIheader); // 02/01/2005 break; - case daveProtoPPI_IBH: -// dc->maxPDUlength=240; // limit for NetLink as reported by AFK. Not needed any more. Now, a PDU can be split into multiple IBH packets. - dc->PDUstartI=14; // sizeof(IBHpacket)+7; - dc->PDUstartO=13;// sizeof(IBHpacket)+7; // 02/01/2005 - break; - - case daveProtoAS511: - dc->PDUstartI=0; - dc->PDUstartO=0; - break; - - case daveProtoUserTransport: - dc->PDUstartI=0; - dc->PDUstartO=0; - break; - case daveProtoS7online: - dc->PDUstartI=80; - dc->PDUstartO=80; - break; + case daveProtoMPI_IBH: + // dc->maxPDUlength=240; // limit for NetLink as reported by AFK. Not needed any more. Now, a PDU can be split into multiple IBH packets. + dc->PDUstartI = sizeof(IBHpacket) + sizeof(MPIheader); + dc->PDUstartO = sizeof(IBHpacket) + sizeof(MPIheader); // 02/01/2005 + break; + case daveProtoPPI_IBH: + // dc->maxPDUlength=240; // limit for NetLink as reported by AFK. Not needed any more. Now, a PDU can be split into multiple IBH packets. + dc->PDUstartI = 14; // sizeof(IBHpacket)+7; + dc->PDUstartO = 13;// sizeof(IBHpacket)+7; // 02/01/2005 + break; + + case daveProtoAS511: + dc->PDUstartI = 0; + dc->PDUstartO = 0; + break; + + case daveProtoUserTransport: + dc->PDUstartI = 0; + dc->PDUstartO = 0; + break; + case daveProtoS7online: + dc->PDUstartI = 80; + dc->PDUstartO = 80; + break; default: - dc->PDUstartO=8; /* position of PDU in outgoing messages */ - dc->PDUstartI=8; /* position of PDU in incoming messages */ - fprintf(stderr,"Unknown protocol on interface %s\n",dc->iface->name); - } + dc->PDUstartO = 8; /* position of PDU in outgoing messages */ + dc->PDUstartI = 8; /* position of PDU in incoming messages */ + fprintf(stderr, "Unknown protocol on interface %s\n", dc->iface->name); + } #ifdef BCCWIN setTimeOut(dc->iface, dc->iface->timeout); #endif } - return dc; + return dc; } -int DECL2 daveWriteManyBytes(daveConnection * dc,int area, int DBnum, int start,int len, void * buffer){ +int DECL2 daveWriteManyBytes(daveConnection * dc, int area, int DBnum, int start, int len, void * buffer){ int res, pos, writeLen; uc * pbuf; - pos=0; - if (buffer==NULL) return daveResNoBuffer; - pbuf=(uc*) buffer; - res=daveResInvalidLength; //the only chance to return this is when len<=0 - while (len>0) { - if (len>dc->maxPDUlength-28) writeLen=dc->maxPDUlength-28; else writeLen=len; - res=daveWriteBytes(dc,area, DBnum, start, writeLen, pbuf); - if (res!=0) return res; - len-=writeLen; - start+=writeLen; - pbuf+=writeLen; - } + pos = 0; + if (buffer == NULL) return daveResNoBuffer; + pbuf = (uc*)buffer; + res = daveResInvalidLength; //the only chance to return this is when len<=0 + while (len > 0) { + if (len > dc->maxPDUlength - 28) writeLen = dc->maxPDUlength - 28; else writeLen = len; + res = daveWriteBytes(dc, area, DBnum, start, writeLen, pbuf); + if (res != 0) return res; + len -= writeLen; + start += writeLen; + pbuf += writeLen; + } return res; } -int DECL2 daveWriteBytes(daveConnection * dc,int area, int DB, int start, int len, void * buffer) { - PDU p1,p2; +int DECL2 daveWriteBytes(daveConnection * dc, int area, int DB, int start, int len, void * buffer) { + PDU p1, p2; int res; - if (dc->iface->protocol==daveProtoAS511) { + if (dc->iface->protocol == daveProtoAS511) { return daveWriteS5Bytes(dc, area, DB, start, len, buffer); } - p1.header=dc->msgOut+dc->PDUstartO; + p1.header = dc->msgOut + dc->PDUstartO; davePrepareWriteRequest(dc, &p1); daveAddVarToWriteRequest(&p1, area, DB, start, len, buffer); - res=_daveExchange(dc, &p1); - if(res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); - if(res!=daveResOK) return res; - res=_daveTestWriteResult(&p2); + res = _daveExchange(dc, &p1); + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + if (res != daveResOK) return res; + res = _daveTestWriteResult(&p2); return res; } -int DECL2 daveWriteBits(daveConnection * dc,int area, int DB, int start, int len, void * buffer) { - PDU p1,p2; +int DECL2 daveWriteBits(daveConnection * dc, int area, int DB, int start, int len, void * buffer) { + PDU p1, p2; int res; - p1.header=dc->msgOut+dc->PDUstartO; - davePrepareWriteRequest(dc,&p1); + p1.header = dc->msgOut + dc->PDUstartO; + davePrepareWriteRequest(dc, &p1); daveAddBitVarToWriteRequest(&p1, area, DB, start, len, buffer); - res=_daveExchange(dc, &p1); - if(res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); - if (res!=0) return res; - res=_daveTestWriteResult(&p2); + res = _daveExchange(dc, &p1); + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + if (res != 0) return res; + res = _daveTestWriteResult(&p2); return res; } /* Simplified single bit set: */ -int DECL2 daveSetBit(daveConnection * dc,int area, int DB, int byteAdr, int bitAdr) { - int a=1; - return daveWriteBits(dc, area, DB, 8*byteAdr+bitAdr, 1, &a); +int DECL2 daveSetBit(daveConnection * dc, int area, int DB, int byteAdr, int bitAdr) { + int a = 1; + return daveWriteBits(dc, area, DB, 8 * byteAdr + bitAdr, 1, &a); } /* Simplified single bit clear: */ -int DECL2 daveClrBit(daveConnection * dc,int area, int DB, int byteAdr, int bitAdr) { - int a=0; - return daveWriteBits(dc, area, DB, 8*byteAdr+bitAdr, 1, &a); +int DECL2 daveClrBit(daveConnection * dc, int area, int DB, int byteAdr, int bitAdr) { + int a = 0; + return daveWriteBits(dc, area, DB, 8 * byteAdr + bitAdr, 1, &a); } /* PLC program read functions: */ -int DECL2 initUpload(daveConnection * dc,char blockType, int blockNr, uc *uploadID){ - PDU p1,p2; +int DECL2 initUpload(daveConnection * dc, char blockType, int blockNr, uc *uploadID){ + PDU p1, p2; int res; if (daveDebug & daveDebugUpload) { LOG1("****initUpload\n"); - } - p1.header=dc->msgOut+dc->PDUstartO; + } + p1.header = dc->msgOut + dc->PDUstartO; _daveConstructUpload(&p1, blockType, blockNr); - res=_daveExchange(dc, &p1); + res = _daveExchange(dc, &p1); if (daveDebug & daveDebugUpload) { LOG2("error:%d\n", res); FLUSH; - } - if(res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); - if(res!=daveResOK) return res; - + } + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + if (res != daveResOK) return res; + uploadID[0] = p2.param[4]; uploadID[1] = p2.param[5]; uploadID[2] = p2.param[6]; uploadID[3] = p2.param[7]; - + return 0; } int DECL2 doUpload(daveConnection*dc, int * more, uc**buffer, int*len, uc *uploadID){ - PDU p1,p2; + PDU p1, p2; int res, netLen; - p1.header=dc->msgOut+dc->PDUstartO; + p1.header = dc->msgOut + dc->PDUstartO; _daveConstructDoUpload(&p1, uploadID); - res=_daveExchange(dc, &p1); + res = _daveExchange(dc, &p1); if (daveDebug & daveDebugUpload) { LOG2("error:%d\n", res); FLUSH; - } - *more=0; - if(res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); - *more=p2.param[1]; - if(res!=daveResOK) return res; - // netLen=p2.data[1] /* +256*p2.data[0]; */ /* for long PDUs, I guess it is so */; - netLen=p2.data[1]+256*p2.data[0]; /* some user confirmed my guess... */; + } + *more = 0; + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + *more = p2.param[1]; + if (res != daveResOK) return res; + // netLen=p2.data[1] /* +256*p2.data[0]; */ /* for long PDUs, I guess it is so */; + netLen = p2.data[1] + 256 * p2.data[0]; /* some user confirmed my guess... */; if (*buffer) { - memcpy(*buffer,p2.data+4,netLen); - *buffer+=netLen; + memcpy(*buffer, p2.data + 4, netLen); + *buffer += netLen; if (daveDebug & daveDebugUpload) { - LOG2("buffer:%p\n",*buffer); + LOG2("buffer:%p\n", *buffer); FLUSH; - } + } } - *len=netLen; + *len = netLen; return res; } int DECL2 endUpload(daveConnection*dc, uc *uploadID){ - PDU p1,p2; + PDU p1, p2; int res; - p1.header=dc->msgOut+dc->PDUstartO; - _daveConstructEndUpload(&p1,uploadID); + p1.header = dc->msgOut + dc->PDUstartO; + _daveConstructEndUpload(&p1, uploadID); - res=_daveExchange(dc, &p1); + res = _daveExchange(dc, &p1); if (daveDebug & daveDebugUpload) { LOG2("error:%d\n", res); FLUSH; } - if(res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); return res; } @@ -2267,99 +2323,99 @@ int DECL2 endUpload(daveConnection*dc, uc *uploadID){ NC file read functions: */ int DECL2 initUploadNC(daveConnection *dc, const char *filename, uc *uploadID){ - PDU p1,p2; - int res; - if (daveDebug & daveDebugUpload) { - LOG1("****initUploadNC\n"); - } - p1.header = dc->msgOut + dc->PDUstartO; - _daveConstructUploadNC(&p1, filename); - res = _daveExchange(dc, &p1); - if (daveDebug & daveDebugUpload) { - LOG2("error:%d\n", res); - FLUSH; - } - if (res != daveResOK) return res; - res = _daveSetupReceivedPDU(dc, &p2); - if (res != daveResOK) return res; - uploadID[0] = p2.param[4]; - uploadID[1] = p2.param[5]; - uploadID[2] = p2.param[6]; - uploadID[3] = p2.param[7]; - return 0; + PDU p1, p2; + int res; + if (daveDebug & daveDebugUpload) { + LOG1("****initUploadNC\n"); + } + p1.header = dc->msgOut + dc->PDUstartO; + _daveConstructUploadNC(&p1, filename); + res = _daveExchange(dc, &p1); + if (daveDebug & daveDebugUpload) { + LOG2("error:%d\n", res); + FLUSH; + } + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + if (res != daveResOK) return res; + uploadID[0] = p2.param[4]; + uploadID[1] = p2.param[5]; + uploadID[2] = p2.param[6]; + uploadID[3] = p2.param[7]; + return 0; } int DECL2 doUploadNC(daveConnection *dc, int *more, uc **buffer, int *len, uc *uploadID){ - PDU p1,p2; - int res, netLen; - p1.header = dc->msgOut + dc->PDUstartO; - _daveConstructDoUploadNC(&p1, uploadID); - res=_daveExchange(dc, &p1); - if (daveDebug & daveDebugUpload) { - LOG2("error:%d\n", res); - FLUSH; - } - *more = 0; - if (res != daveResOK) return res; - res = _daveSetupReceivedPDU(dc, &p2); - *more = p2.param[1]; - if (res != daveResOK) return res; - netLen = p2.data[1] + 256*p2.data[0]; - if (*buffer) { - memcpy(*buffer, p2.data+4, netLen); - *buffer += netLen; - if (daveDebug & daveDebugUpload) { - LOG2("buffer:%p\n",*buffer); - FLUSH; - } - } - *len=netLen; - return res; + PDU p1, p2; + int res, netLen; + p1.header = dc->msgOut + dc->PDUstartO; + _daveConstructDoUploadNC(&p1, uploadID); + res = _daveExchange(dc, &p1); + if (daveDebug & daveDebugUpload) { + LOG2("error:%d\n", res); + FLUSH; + } + *more = 0; + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + *more = p2.param[1]; + if (res != daveResOK) return res; + netLen = p2.data[1] + 256 * p2.data[0]; + if (*buffer) { + memcpy(*buffer, p2.data + 4, netLen); + *buffer += netLen; + if (daveDebug & daveDebugUpload) { + LOG2("buffer:%p\n", *buffer); + FLUSH; + } + } + *len = netLen; + return res; } int DECL2 doSingleUploadNC(daveConnection *dc, int *more, uc *buffer, int *len, uc *uploadID){ - PDU p1,p2; - int res, netLen; - p1.header = dc->msgOut + dc->PDUstartO; - _daveConstructDoUploadNC(&p1, uploadID); - res=_daveExchange(dc, &p1); - if (daveDebug & daveDebugUpload) { - LOG2("error:%d\n", res); - FLUSH; - } - *more = 0; - if (res != daveResOK) return res; - res = _daveSetupReceivedPDU(dc, &p2); - *more = p2.param[1]; - if (res != daveResOK) return res; - netLen = p2.data[1] + 256*p2.data[0]; + PDU p1, p2; + int res, netLen; + p1.header = dc->msgOut + dc->PDUstartO; + _daveConstructDoUploadNC(&p1, uploadID); + res = _daveExchange(dc, &p1); + if (daveDebug & daveDebugUpload) { + LOG2("error:%d\n", res); + FLUSH; + } + *more = 0; + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + *more = p2.param[1]; + if (res != daveResOK) return res; + netLen = p2.data[1] + 256 * p2.data[0]; if (netLen > 1024) return -1; - //if (*buffer) { - memcpy(buffer, p2.data+4, netLen); - //*buffer += netLen; - if (daveDebug & daveDebugUpload) { - LOG2("buffer:%p\n",*buffer); - FLUSH; - } - //} - *len=netLen; - return res; + //if (*buffer) { + memcpy(buffer, p2.data + 4, netLen); + //*buffer += netLen; + if (daveDebug & daveDebugUpload) { + LOG2("buffer:%p\n", *buffer); + FLUSH; + } + //} + *len = netLen; + return res; } int DECL2 endUploadNC(daveConnection *dc, uc *uploadID){ - PDU p1,p2; - int res; - - p1.header = dc->msgOut + dc->PDUstartO; - _daveConstructEndUploadNC(&p1, uploadID); - res = _daveExchange(dc, &p1); - if (daveDebug & daveDebugUpload) { - LOG2("error:%d\n", res); - FLUSH; - } - if(res != daveResOK) return res; - res = _daveSetupReceivedPDU(dc, &p2); - return res; + PDU p1, p2; + int res; + + p1.header = dc->msgOut + dc->PDUstartO; + _daveConstructEndUploadNC(&p1, uploadID); + res = _daveExchange(dc, &p1); + if (daveDebug & daveDebugUpload) { + LOG2("error:%d\n", res); + FLUSH; + } + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + return res; } /* @@ -2372,9 +2428,9 @@ char * DECL2 daveStrerror(int code) { case daveResItemNotAvailable: return "the desired item is not available in the PLC"; case daveResItemNotAvailable200: return "the desired item is not available in the PLC (200 family)"; case daveAddressOutOfRange: return "the desired address is beyond limit for this PLC"; - case daveResCPUNoData : return "the PLC returned a packet with no result data"; - case daveUnknownError : return "the PLC returned an error code not understood by this library"; - case daveEmptyResultError : return "this result contains no data"; + case daveResCPUNoData: return "the PLC returned a packet with no result data"; + case daveUnknownError: return "the PLC returned an error code not understood by this library"; + case daveEmptyResultError: return "this result contains no data"; case daveEmptyResultSetError: return "cannot work with an undefined result set"; case daveResCannotEvaluatePDU: return "cannot evaluate the received PDU"; case daveWriteDataSizeMismatch: return "Write data size error"; @@ -2400,7 +2456,7 @@ char * DECL2 daveStrerror(int code) { case 0x8402: return "CPU already in RUN or already in STOP ?"; case 0x8404: return "severe error ?"; case 0x8500: return "incorrect PDU size."; - case 0x8702: return "address invalid."; ; + case 0x8702: return "address invalid.";; case 0xd002: return "Step7:variant of command is illegal."; case 0xd004: return "Step7:status for this command is illegal."; case 0xd0A1: return "Step7:function is not allowed in the current protection level."; @@ -2419,7 +2475,7 @@ char * DECL2 daveStrerror(int code) { /* Multiple functions tried to manipulate the same object. Example: a block could not be copied,because it is already present in the target system and - */ + */ case 0xd241: return "Operation not permitted in current protection level."; /**/ case 0xd242: return "protection violation while processing F-blocks. F-blocks can only be processed after password input."; case 0xd401: return "invalid SZL ID."; @@ -2444,7 +2500,7 @@ void DECL2 daveStringCopy(char * intString, char * extString) { /* I'm not quite sure whether this is all correct, but it seems to work for all numbers I tested */ -float DECL2 daveGetKGAt(daveConnection * dc,int pos) { +float DECL2 daveGetKGAt(daveConnection * dc, int pos) { char kgExponent; int sign; union { @@ -2455,62 +2511,62 @@ float DECL2 daveGetKGAt(daveConnection * dc,int pos) { int a; float f; } v; - uc* p=dc->_resultPointer+pos; - kgExponent=*p; + uc* p = dc->_resultPointer + pos; + kgExponent = *p; p++; -#ifdef DAVE_LITTLE_ENDIAN - f.b[3]=0; - f.b[2]=*p; +#ifdef DAVE_LITTLE_ENDIAN + f.b[3] = 0; + f.b[2] = *p; p++; - f.b[1]=*p; + f.b[1] = *p; p++; - f.b[0]=*p; - sign=(f.b[2]& 0x80); - f.b[2]&=0x7f; -#else - f.b[0]=0; - f.b[1]=*p; + f.b[0] = *p; + sign = (f.b[2] & 0x80); + f.b[2] &= 0x7f; +#else + f.b[0] = 0; + f.b[1] = *p; p++; - f.b[2]=*p; + f.b[2] = *p; p++; - f.b[3]=*p; - sign=(f.b[1]& 0x80); - f.b[1]&=0x7f; -#endif + f.b[3] = *p; + sign = (f.b[1] & 0x80); + f.b[1] &= 0x7f; +#endif p++; - LOG3("daveGetKG(dc:%p, mantissa:0x%08X)\n",dc, f.mantissa); - if(sign) { - f.mantissa=f.mantissa ^0xffffffff; - f.mantissa=f.mantissa +0x00800000; - } - v.f=f.mantissa; - if(sign) { - v.f=-v.f; - } - LOG5("daveGetKG(dc:%p, mantissa:0x%08X exponent:0x%02X %0.8f)\n",dc, f.mantissa, kgExponent,v.f); - while (kgExponent>23) { - v.f=v.f*2.0; + LOG3("daveGetKG(dc:%p, mantissa:0x%08X)\n", dc, f.mantissa); + if (sign) { + f.mantissa = f.mantissa ^ 0xffffffff; + f.mantissa = f.mantissa + 0x00800000; + } + v.f = f.mantissa; + if (sign) { + v.f = -v.f; + } + LOG5("daveGetKG(dc:%p, mantissa:0x%08X exponent:0x%02X %0.8f)\n", dc, f.mantissa, kgExponent, v.f); + while (kgExponent > 23) { + v.f = v.f*2.0; kgExponent--; } - while (kgExponent<23) { - v.f=v.f/2.0; + while (kgExponent < 23) { + v.f = v.f / 2.0; kgExponent++; - } - LOG2("daveGetKG(%08X)\n",v.a); - v.f=-v.f; - LOG2("daveGetKG(%08X)\n",v.a); - v.f=-v.f; + } + LOG2("daveGetKG(%08X)\n", v.a); + v.f = -v.f; + LOG2("daveGetKG(%08X)\n", v.a); + v.f = -v.f; #ifdef DEBUG_CALLS - LOG3("daveGetKG(dc:%p, result:%0.6f)\n", dc, v.f); + LOG3("daveGetKG(dc:%p, result:%0.6f)\n", dc, v.f); FLUSH; -#endif +#endif return (v.f); } float DECL2 daveGetKG(daveConnection * dc) { float f; - f=daveGetKGAt(dc, ((int)dc->resultPointer-(int)dc->_resultPointer)); - dc->resultPointer+=4; + f = daveGetKGAt(dc, ((int)dc->resultPointer - (int)dc->_resultPointer)); + dc->resultPointer += 4; return f; } @@ -2519,28 +2575,28 @@ float DECL2 daveGetFloat(daveConnection * dc) { float a; uc b[4]; } f; -#ifdef DAVE_LITTLE_ENDIAN - f.b[3]=*(dc->resultPointer); +#ifdef DAVE_LITTLE_ENDIAN + f.b[3] = *(dc->resultPointer); dc->resultPointer++; - f.b[2]=*(dc->resultPointer); + f.b[2] = *(dc->resultPointer); dc->resultPointer++; - f.b[1]=*(dc->resultPointer); + f.b[1] = *(dc->resultPointer); dc->resultPointer++; - f.b[0]=*(dc->resultPointer); -#else - f.b[0]=*(dc->resultPointer); + f.b[0] = *(dc->resultPointer); +#else + f.b[0] = *(dc->resultPointer); dc->resultPointer++; - f.b[1]=*(dc->resultPointer); + f.b[1] = *(dc->resultPointer); dc->resultPointer++; - f.b[2]=*(dc->resultPointer); + f.b[2] = *(dc->resultPointer); dc->resultPointer++; - f.b[3]=*(dc->resultPointer); -#endif + f.b[3] = *(dc->resultPointer); +#endif dc->resultPointer++; #ifdef DEBUG_CALLS - LOG3("daveGetFloat(dc:%p, result:%0.6f)\n", dc, f.a); + LOG3("daveGetFloat(dc:%p, result:%0.6f)\n", dc, f.a); FLUSH; -#endif +#endif return (f.a); } @@ -2549,51 +2605,51 @@ float DECL2 daveGetFloatAt(daveConnection * dc, int pos) { float a; uc b[4]; } f; - uc* p=(uc*)dc->_resultPointer; - p+=pos; + uc* p = (uc*)dc->_resultPointer; + p += pos; #ifdef DAVE_LITTLE_ENDIAN - f.b[3]=*p;p++; - f.b[2]=*p;p++; - f.b[1]=*p;p++; - f.b[0]=*p; -#else - f.b[0]=*p;p++; - f.b[1]=*p;p++; - f.b[2]=*p;p++; - f.b[3]=*p; -#endif + f.b[3] = *p; p++; + f.b[2] = *p; p++; + f.b[1] = *p; p++; + f.b[0] = *p; +#else + f.b[0] = *p; p++; + f.b[1] = *p; p++; + f.b[2] = *p; p++; + f.b[3] = *p; +#endif return (f.a); } float DECL2 toPLCfloat(float ff) { -#ifdef DAVE_LITTLE_ENDIAN +#ifdef DAVE_LITTLE_ENDIAN union { float a; uc b[4]; } f; uc c; - f.a=ff; - c=f.b[0]; - f.b[0]=f.b[3]; - f.b[3]=c; - c=f.b[1]; - f.b[1]=f.b[2]; - f.b[2]=c; + f.a = ff; + c = f.b[0]; + f.b[0] = f.b[3]; + f.b[3] = c; + c = f.b[1]; + f.b[1] = f.b[2]; + f.b[2] = c; -// f.a=ff; + // f.a=ff; #ifdef DEBUG_CALLS - LOG3("toPLCfloat(%0.6f) = %0.6f\n",ff,f.a); + LOG3("toPLCfloat(%0.6f) = %0.6f\n", ff, f.a); FLUSH; -#endif +#endif return (f.a); -#else +#else #ifdef DEBUG_CALLS - LOG3("toPLCfloat(%0.6f) = %0.6f\n",ff,ff); + LOG3("toPLCfloat(%0.6f) = %0.6f\n", ff, ff); FLUSH; -#endif +#endif return ff; -#endif +#endif } int DECL2 daveToPLCfloat(float ff) { @@ -2602,22 +2658,22 @@ int DECL2 daveToPLCfloat(float ff) { uc b[4]; int c; } f; -#ifdef DAVE_LITTLE_ENDIAN +#ifdef DAVE_LITTLE_ENDIAN uc c; - f.a=ff; - c=f.b[0]; - f.b[0]=f.b[3]; - f.b[3]=c; - c=f.b[1]; - f.b[1]=f.b[2]; - f.b[2]=c; -#else - f.a=ff; -#endif + f.a = ff; + c = f.b[0]; + f.b[0] = f.b[3]; + f.b[3] = c; + c = f.b[1]; + f.b[1] = f.b[2]; + f.b[2] = c; +#else + f.a = ff; +#endif #ifdef DEBUG_CALLS - LOG3("toPLCfloat(%0.6f) = %08x\n",ff,f.c); + LOG3("toPLCfloat(%0.6f) = %08x\n", ff, f.c); FLUSH; -#endif +#endif return (f.c); } @@ -2625,40 +2681,40 @@ int DECL2 daveToKG(float ff) { union { uc b[4]; int c; - } f,f2; - char kgExponent=23; - LOG2("daveToKG(%0.8f)\n",ff); - if (ff==0.0) { - f.c=0; + } f, f2; + char kgExponent = 23; + LOG2("daveToKG(%0.8f)\n", ff); + if (ff == 0.0) { + f.c = 0; return 0; - } - f2.c=(int)ff; // attention! what does this cast? I do want to take the integer part of that float, NOT reinterpret the bit pattern as int! - LOG4("daveToKG(mantissa:0x%08X exponent:0x%02X %0.8f)\n", f2.c, kgExponent,ff); + } + f2.c = (int)ff; // attention! what does this cast? I do want to take the integer part of that float, NOT reinterpret the bit pattern as int! + LOG4("daveToKG(mantissa:0x%08X exponent:0x%02X %0.8f)\n", f2.c, kgExponent, ff); while (f2.c > 0x00400000){ - ff/=2; - f2.c=(int)ff; // attention! what does this cast? I do want to take the integer part of that float, NOT reinterpret the bit pattern as int! + ff /= 2; + f2.c = (int)ff; // attention! what does this cast? I do want to take the integer part of that float, NOT reinterpret the bit pattern as int! kgExponent++; } while (f2.c < 0x00400000){ - ff*=2; - f2.c=(int)ff; // attention! what does this cast? I do want to take the integer part of that float, NOT reinterpret the bit pattern as int! + ff *= 2; + f2.c = (int)ff; // attention! what does this cast? I do want to take the integer part of that float, NOT reinterpret the bit pattern as int! kgExponent--; } - LOG4("daveToKG(mantissa:0x%08X exponent:0x%02X %0.8f)\n", f2.c, kgExponent,ff); - f.b[0]=kgExponent; -#ifdef DAVE_LITTLE_ENDIAN - f.b[1]=f2.b[2]; - f.b[2]=f2.b[1]; - f.b[3]=f2.b[0]; -#else - f.b[3]=f2.b[3]; - f.b[2]=f2.b[2]; - f.b[1]=f2.b[1]; -#endif + LOG4("daveToKG(mantissa:0x%08X exponent:0x%02X %0.8f)\n", f2.c, kgExponent, ff); + f.b[0] = kgExponent; +#ifdef DAVE_LITTLE_ENDIAN + f.b[1] = f2.b[2]; + f.b[2] = f2.b[1]; + f.b[3] = f2.b[0]; +#else + f.b[3] = f2.b[3]; + f.b[2] = f2.b[2]; + f.b[1] = f2.b[1]; +#endif #ifdef DEBUG_CALLS - LOG3("daveToKG(%0.6f) = %08x\n",ff,f.c); + LOG3("daveToKG(%0.6f) = %08x\n", ff, f.c); FLUSH; -#endif +#endif return (f.c); } @@ -2670,15 +2726,15 @@ short DECL2 daveSwapIed_16(short ff) { uc b[2]; } f; uc c; - f.a=ff; - c=f.b[0]; - f.b[0]=f.b[1]; - f.b[1]=c; + f.a = ff; + c = f.b[0]; + f.b[0] = f.b[1]; + f.b[1] = c; return (f.a); #else - // printf("Here we are in BIG ENDIAN!!!\n"); + // printf("Here we are in BIG ENDIAN!!!\n"); return (ff); -#endif +#endif } int DECL2 daveSwapIed_32(int ff) { @@ -2688,91 +2744,91 @@ int DECL2 daveSwapIed_32(int ff) { uc b[4]; } f; uc c; - f.a=ff; - c=f.b[0]; - f.b[0]=f.b[3]; - f.b[3]=c; - c=f.b[1]; - f.b[1]=f.b[2]; - f.b[2]=c; + f.a = ff; + c = f.b[0]; + f.b[0] = f.b[3]; + f.b[3] = c; + c = f.b[1]; + f.b[1] = f.b[2]; + f.b[2] = c; return f.a; #else - // printf("Here we are in BIG ENDIAN!!!\n"); + // printf("Here we are in BIG ENDIAN!!!\n"); return ff; -#endif +#endif } /** Timer and Counter conversion functions: **/ -/* +/* get time in seconds from current read position: */ float DECL2 daveGetSeconds(daveConnection * dc) { - uc b[2],a; + uc b[2], a; float f; - b[1]=*(dc->resultPointer)++; - b[0]=*(dc->resultPointer)++; - f=b[0] & 0xf; - f+=10*((b[0] & 0xf0)>>4); - f+=100*(b[1] & 0xf); - a=((b[1] & 0xf0)>>4); + b[1] = *(dc->resultPointer)++; + b[0] = *(dc->resultPointer)++; + f = b[0] & 0xf; + f += 10 * ((b[0] & 0xf0) >> 4); + f += 100 * (b[1] & 0xf); + a = ((b[1] & 0xf0) >> 4); switch (a) { - case 0: f*=0.01;break; - case 1: f*=0.1;break; - case 3: f*=10.0;break; + case 0: f *= 0.01; break; + case 1: f *= 0.1; break; + case 3: f *= 10.0; break; } - return (f); + return (f); } -/* +/* get time in seconds from random position: */ float DECL2 daveGetSecondsAt(daveConnection * dc, int pos) { float f; - uc b[2],a; - uc* p=(uc*)dc->_resultPointer; - p+=pos; - b[1]=*p; + uc b[2], a; + uc* p = (uc*)dc->_resultPointer; + p += pos; + b[1] = *p; p++; - b[0]=*p; - f=b[0] & 0xf; - f+=10*((b[0] & 0xf0)>>4); - f+=100*(b[1] & 0xf); - a=((b[1] & 0xf0)>>4); + b[0] = *p; + f = b[0] & 0xf; + f += 10 * ((b[0] & 0xf0) >> 4); + f += 100 * (b[1] & 0xf); + a = ((b[1] & 0xf0) >> 4); switch (a) { - case 0: f*=0.01;break; - case 1: f*=0.1;break; - case 3: f*=10.0;break; + case 0: f *= 0.01; break; + case 1: f *= 0.1; break; + case 3: f *= 10.0; break; } return (f); } -/* +/* get counter value from current read position: */ int DECL2 daveGetCounterValue(daveConnection * dc) { uc b[2]; int f; - b[1]=*(dc->resultPointer)++; - b[0]=*(dc->resultPointer)++; - f=b[0] & 0xf; - f+=10*((b[0] & 0xf0)>>4); - f+=100*(b[1] & 0xf); - return (f); -} -/* + b[1] = *(dc->resultPointer)++; + b[0] = *(dc->resultPointer)++; + f = b[0] & 0xf; + f += 10 * ((b[0] & 0xf0) >> 4); + f += 100 * (b[1] & 0xf); + return (f); +} +/* get counter value from random read position: */ -int DECL2 daveGetCounterValueAt(daveConnection * dc,int pos){ +int DECL2 daveGetCounterValueAt(daveConnection * dc, int pos){ int f; uc b[2]; - uc* p=(uc*)dc->_resultPointer; - p+=pos; - b[1]=*p; + uc* p = (uc*)dc->_resultPointer; + p += pos; + b[1] = *p; p++; - b[0]=*p; - f=b[0] & 0xf; - f+=10*((b[0] & 0xf0)>>4); - f+=100*(b[1] & 0xf); + b[0] = *p; + f = b[0] & 0xf; + f += 10 * ((b[0] & 0xf0) >> 4); + f += 100 * (b[1] & 0xf); return (f); } @@ -2788,7 +2844,7 @@ int DECL2 _daveReturnOkDummy2(daveConnection * dc){ return 0; } -int DECL2 _daveListReachablePartnersDummy (daveInterface * di, char * buf) { +int DECL2 _daveListReachablePartnersDummy(daveInterface * di, char * buf) { return 0; } @@ -2796,13 +2852,13 @@ int DECL2 _daveListReachablePartnersDummy (daveInterface * di, char * buf) { MPI specific functions: */ -/* +/* This writes a single chracter to the serial interface: */ void DECL2 _daveSendSingle(daveInterface * di, /* serial interface */ uc c /* chracter to be send */ - ) + ) { di->ifwrite(di, (char*)&c, 1); } @@ -2810,107 +2866,109 @@ void DECL2 _daveSendSingle(daveInterface * di, /* serial interface */ int DECL2 _daveReadSingle(daveInterface * di) { char res; int i; - i=di->ifread(di, &res,1); - if ((daveDebug & daveDebugSpecialChars)!=0) - LOG3("readSingle %d chars. 1st %02X\n",i,res); - if (i==1) return res; + i = di->ifread(di, &res, 1); + if ((daveDebug & daveDebugSpecialChars) != 0) + LOG3("readSingle %d chars. 1st %02X\n", i, res); + if (i == 1) return res; return 0; } int DECL2 _daveReadMPI(daveInterface * di, uc *b) { - int res=0,state=0,nr_read; - uc bcc=0; -rep: - { - nr_read= di->ifread(di, (char*)(b+res), 1); - if (nr_read==0) return 0; - res+=nr_read; - if ((res==1) && (*(b+res-1)==DLE)) { - if ((daveDebug & daveDebugSpecialChars)!=0) + int res = 0, state = 0, nr_read; + uc bcc = 0; +rep: + { + nr_read = di->ifread(di, (char*)(b + res), 1); + if (nr_read == 0) return 0; + res += nr_read; + if ((res == 1) && (*(b + res - 1) == DLE)) { + if ((daveDebug & daveDebugSpecialChars) != 0) LOG1("readMPI single DLE!\n"); return 1; - } - if ((res==1) && (*(b+res-1)==STX)) { - if ((daveDebug & daveDebugSpecialChars)!=0) + } + if ((res == 1) && (*(b + res - 1) == STX)) { + if ((daveDebug & daveDebugSpecialChars) != 0) LOG1("readMPI single STX!\n"); return 1; } - if (*(b+res-1)==DLE) { - if (state==0) { - state=1; - /* if ((daveDebug & daveDebugSpecialChars)!=0) - LOG1("readMPI 1st DLE in data.\n") + if (*(b + res - 1) == DLE) { + if (state == 0) { + state = 1; + /* if ((daveDebug & daveDebugSpecialChars)!=0) + LOG1("readMPI 1st DLE in data.\n") ; - */ - } else if (state==1) { - state=0; + */ + } + else if (state == 1) { + state = 0; res--; /* forget this DLE, it is the second of a pair */ - /* if ((daveDebug & daveDebugSpecialChars)!=0) - LOG1("readMPI 2nd DLE in data.\n") + /* if ((daveDebug & daveDebugSpecialChars)!=0) + LOG1("readMPI 2nd DLE in data.\n") ; - */ + */ } - } - if (state==3) { - if ((daveDebug & daveDebugSpecialChars)!=0) - LOG4("readMPI: packet size %d, got BCC: %x. I calc: %x\n",res,*(b+res-1),bcc); - if ((daveDebug & daveDebugRawRead)!=0) - _daveDump("answer",b,res); - return res; - } else { - bcc=bcc^(*(b+res-1)); - } - - if (*(b+res-1)==ETX) if (state==1) { - state=3; - if ((daveDebug & daveDebugSpecialChars)!=0) + } + if (state == 3) { + if ((daveDebug & daveDebugSpecialChars) != 0) + LOG4("readMPI: packet size %d, got BCC: %x. I calc: %x\n", res, *(b + res - 1), bcc); + if ((daveDebug & daveDebugRawRead) != 0) + _daveDump("answer", b, res); + return res; + } + else { + bcc = bcc ^ (*(b + res - 1)); + } + + if (*(b + res - 1) == ETX) if (state == 1) { + state = 3; + if ((daveDebug & daveDebugSpecialChars) != 0) LOG1("readMPI: DLE ETX,packet end.\n"); - } + } goto rep; - } + } } int DECL2 _daveReadMPI2(daveInterface * di, uc *b) { - uc b6; - uc b2[daveMaxRawLen]; - uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14,5,1,0}; - int res2, re; - int res=_daveReadMPI(di, b); - re=res; - b6=b[6]; + uc b6; + uc b2[daveMaxRawLen]; + uc fix[] = { 04, 0x80, 0x80, 0x0C, 0x03, 0x14, 5, 1, 0 }; + int res2, re; + int res = _daveReadMPI(di, b); + re = res; + b6 = b[6]; again: - if ((re>=7)&&(b6==0xF0)) { - if ((daveDebug & daveDebugRawRead)!=0) - LOG1("follow up expected\n"); -// uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14,0xB0,0,0}; -// uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14,5,1,0}; -/* - uc m[3]; - m[0]=0xB1; - m[1]=0x01; - m[2]=nr; -*/ - fix[8]=b[7]; - fix[1]=b[1]; - _daveSendSingle(di, DLE); - _daveSendSingle(di, STX); - _daveReadSingle(di); - _daveReadSingle(di); - _daveSendWithCRC(di, fix, sizeof(fix)); - _daveReadSingle(di); - _daveSendSingle(di, STX); - _daveSendSingle(di, DLE); -// _daveReadSingle(di); -// _daveReadSingle(di); - res2=_daveReadMPI(di, b2); - b6=b2[6]; - re=res2; - memcpy(b+res-3, b2+6, res2-9); - res+=res2-9; - b[7]++; // increase packet number for ack - goto again; - } - if (res>1) { + if ((re >= 7) && (b6 == 0xF0)) { + if ((daveDebug & daveDebugRawRead) != 0) + LOG1("follow up expected\n"); + // uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14,0xB0,0,0}; + // uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14,5,1,0}; + /* + uc m[3]; + m[0]=0xB1; + m[1]=0x01; + m[2]=nr; + */ + fix[8] = b[7]; + fix[1] = b[1]; + _daveSendSingle(di, DLE); + _daveSendSingle(di, STX); + _daveReadSingle(di); + _daveReadSingle(di); + _daveSendWithCRC(di, fix, sizeof(fix)); + _daveReadSingle(di); + _daveSendSingle(di, STX); + _daveSendSingle(di, DLE); + // _daveReadSingle(di); + // _daveReadSingle(di); + res2 = _daveReadMPI(di, b2); + b6 = b2[6]; + re = res2; + memcpy(b + res - 3, b2 + 6, res2 - 9); + res += res2 - 9; + b[7]++; // increase packet number for ack + goto again; + } + if (res > 1) { _daveSendSingle(di, DLE); _daveSendSingle(di, STX); } @@ -2919,40 +2977,40 @@ int DECL2 _daveReadMPI2(daveInterface * di, uc *b) { } int DECL2 _daveGetAck(daveConnection * dc) { - int res; - daveInterface * di=dc->iface; - int nr=dc->needAckNumber; + int res; + daveInterface * di = dc->iface; + int nr = dc->needAckNumber; uc b1[daveMaxRawLen]; if (daveDebug & daveDebugPacket) LOG2("%s enter getAck ack\n", di->name); res = _daveReadMPI(di, b1); - if (res<0) return res-10; - if (res!=di->ackPos+6) { + if (res < 0) return res - 10; + if (res != di->ackPos + 6) { if (daveDebug & daveDebugPrintErrors) { LOG4("%s *** getAck wrong length %d for ack. Waiting for %d\n dump:", di->name, res, nr); - _daveDump("wrong ack:",b1,res); + _daveDump("wrong ack:", b1, res); } return -1; - } - if (b1[di->ackPos]!=0xB0) { + } + if (b1[di->ackPos] != 0xB0) { if (daveDebug & daveDebugPrintErrors) { - LOG3("%s *** getAck char[6] %x no ack\n", di->name, b1[di->ackPos+2]); - } + LOG3("%s *** getAck char[6] %x no ack\n", di->name, b1[di->ackPos + 2]); + } return -2; - } - if (b1[di->ackPos+2]!=nr) { + } + if (b1[di->ackPos + 2] != nr) { if (daveDebug & daveDebugPrintErrors) { - LOG4("%s *** getAck got: %d need: %d\n", di->name, b1[di->ackPos+2],nr); - } + LOG4("%s *** getAck got: %d need: %d\n", di->name, b1[di->ackPos + 2], nr); + } return -3; - } + } return 0; } #define tmo_normal 95000 -/* +/* This reads up to max chracters when it can get them and returns the number: */ int DECL2 _daveReadChars2(daveInterface * di, /* serial interface */ @@ -2960,10 +3018,10 @@ int DECL2 _daveReadChars2(daveInterface * di, /* serial interface */ int max /* limit */ ) { - return di->ifread(di,(char*)b,max); + return di->ifread(di, (char*)b, max); } -/* +/* This sends a string after doubling DLEs in the String and adding DLE,ETX and bcc. */ @@ -2971,30 +3029,31 @@ int DECL2 _daveSendWithCRC(daveInterface * di, /* serial interface */ uc *b, /* a buffer containing the message */ int size /* the size of the string */ ) -{ - uc target[daveMaxRawLen]; - int i,targetSize=0; - int bcc=DLE^ETX; /* preload */ - for (i=0; ifd.wfd, target, targetSize, wr); + target[targetSize] = DLE; + target[targetSize + 1] = ETX; + target[targetSize + 2] = bcc; + targetSize += 3; + // daveWriteFile(di->fd.wfd, target, targetSize, wr); di->ifwrite(di, (char*)target, targetSize); if (daveDebug & daveDebugPacket) - _daveDump("_daveSendWithCRC",target, targetSize); + _daveDump("_daveSendWithCRC", target, targetSize); return 0; } -/* +/* This adds a prefix to a string and theen sends it after doubling DLEs in the String and adding DLE,ETX and bcc. @@ -3002,74 +3061,75 @@ and adding DLE,ETX and bcc. int DECL2 _daveSendWithPrefix(daveConnection * dc, uc *b, int size) { uc target[daveMaxRawLen]; - uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14}; - uc fix2[]= {0x00,0x0c,0x03,0x03}; - if (dc->iface->protocol==daveProtoMPI2) { - fix2[2]=dc->connectionNumber2; // 1/10/05 trying Andrew's patch - fix2[3]=dc->connectionNumber; // 1/10/05 trying Andrew's patch - memcpy(target,fix2,sizeof(fix2)); - memcpy(target+sizeof(fix2),b,size); - return _daveSendWithCRC(dc->iface,target,size+sizeof(fix2)); - } else { - fix[4]=dc->connectionNumber2; // 1/10/05 trying Andrew's patch - fix[5]=dc->connectionNumber; // 1/10/05 trying Andrew's patch - memcpy(target,fix,sizeof(fix)); - memcpy(target+sizeof(fix),b,size); - target[1]|=dc->MPIAdr; + uc fix[] = { 04, 0x80, 0x80, 0x0C, 0x03, 0x14 }; + uc fix2[] = { 0x00, 0x0c, 0x03, 0x03 }; + if (dc->iface->protocol == daveProtoMPI2) { + fix2[2] = dc->connectionNumber2; // 1/10/05 trying Andrew's patch + fix2[3] = dc->connectionNumber; // 1/10/05 trying Andrew's patch + memcpy(target, fix2, sizeof(fix2)); + memcpy(target + sizeof(fix2), b, size); + return _daveSendWithCRC(dc->iface, target, size + sizeof(fix2)); + } + else { + fix[4] = dc->connectionNumber2; // 1/10/05 trying Andrew's patch + fix[5] = dc->connectionNumber; // 1/10/05 trying Andrew's patch + memcpy(target, fix, sizeof(fix)); + memcpy(target + sizeof(fix), b, size); + target[1] |= dc->MPIAdr; // target[2]|=dc->iface->localMPI; - memcpy(target+sizeof(fix),b,size); - return _daveSendWithCRC(dc->iface,target,size+sizeof(fix)); - } + memcpy(target + sizeof(fix), b, size); + return _daveSendWithCRC(dc->iface, target, size + sizeof(fix)); + } } int DECL2 _daveSendWithPrefix2(daveConnection * dc, int size) { - uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14}; - uc fix2[]= {0x00, 0x0C, 0x03, 0x03}; + uc fix[] = { 04, 0x80, 0x80, 0x0C, 0x03, 0x14 }; + uc fix2[] = { 0x00, 0x0C, 0x03, 0x03 }; - if (dc->iface->protocol==daveProtoMPI2) { - fix2[2]=dc->connectionNumber2; // 1/10/05 trying Andrew's patch - fix2[3]=dc->connectionNumber; // 1/10/05 trying Andrew's patch + if (dc->iface->protocol == daveProtoMPI2) { + fix2[2] = dc->connectionNumber2; // 1/10/05 trying Andrew's patch + fix2[3] = dc->connectionNumber; // 1/10/05 trying Andrew's patch memcpy(dc->msgOut, fix2, sizeof(fix2)); - dc->msgOut[sizeof(fix2)]=0xF1; - return _daveSendWithCRC(dc->iface, dc->msgOut, size+sizeof(fix2)); + dc->msgOut[sizeof(fix2)] = 0xF1; + return _daveSendWithCRC(dc->iface, dc->msgOut, size + sizeof(fix2)); } - else if (dc->iface->protocol==daveProtoMPI) { - fix[4]=dc->connectionNumber2; // 1/10/05 trying Andrew's patch - fix[5]=dc->connectionNumber; // 1/10/05 trying Andrew's patch + else if (dc->iface->protocol == daveProtoMPI) { + fix[4] = dc->connectionNumber2; // 1/10/05 trying Andrew's patch + fix[5] = dc->connectionNumber; // 1/10/05 trying Andrew's patch memcpy(dc->msgOut, fix, sizeof(fix)); - dc->msgOut[1]|=dc->MPIAdr; + dc->msgOut[1] |= dc->MPIAdr; // dc->msgOut[2]|=dc->iface->localMPI; //??? - dc->msgOut[sizeof(fix)]=0xF1; + dc->msgOut[sizeof(fix)] = 0xF1; /* if (daveDebug & daveDebugPacket) _daveDump("_daveSendWithPrefix2",dc->msgOut,size+sizeof(fix)); */ - return _daveSendWithCRC(dc->iface, dc->msgOut, size+sizeof(fix)); + return _daveSendWithCRC(dc->iface, dc->msgOut, size + sizeof(fix)); } return -1; /* shouldn't happen. */ } -/* +/* Sends an ackknowledge message for the message number nr: */ int DECL2 _daveSendAck(daveConnection * dc, int nr) { uc m[3]; if (daveDebug & daveDebugPacket) - LOG3("%s sendAck for message %d \n", dc->iface->name,nr); - m[0]=0xB0; - m[1]=0x01; - m[2]=nr; + LOG3("%s sendAck for message %d \n", dc->iface->name, nr); + m[0] = 0xB0; + m[1] = 0x01; + m[2] = nr; return _daveSendWithPrefix(dc, m, 3); } -/* +/* Handle MPI message numbers in a central place: */ int DECL2 _daveIncMessageNumber(daveConnection * dc) { - int res=dc->messageNumber++; + int res = dc->messageNumber++; if (daveDebug & daveDebugPacket) - LOG2("_daveIncMessageNumber new number %d \n", dc->messageNumber); - if ((dc->messageNumber)==0) dc->messageNumber=1; + LOG2("_daveIncMessageNumber new number %d \n", dc->messageNumber); + if ((dc->messageNumber) == 0) dc->messageNumber = 1; return res; } /* @@ -3077,96 +3137,99 @@ Executes part of the dialog necessary to send a message: */ int DECL2 _daveSendDialog2(daveConnection * dc, int size) { - int a; - _daveSendSingle(dc->iface, STX); - if (_daveReadSingle(dc->iface)!=DLE) { - if (daveDebug & daveDebugPrintErrors) - LOG2("%s *** no DLE before send.\n", dc->iface->name); - _daveSendSingle(dc->iface, DLE); - if (_daveReadSingle(dc->iface)!=DLE) { - if (daveDebug & daveDebugPrintErrors) - LOG2("%s retry*** no DLE before send.\n", dc->iface->name); - return -1; - } - } - if (size>5){ - dc->needAckNumber=dc->messageNumber; - dc->msgOut[dc->iface->ackPos+1]=_daveIncMessageNumber(dc); - } - _daveSendWithPrefix2(dc, size); - a=_daveReadSingle(dc->iface); - if (a!=DLE) { - LOG3("%s *** no DLE after send(1) %02x.\n", dc->iface->name,a); - a=_daveReadSingle(dc->iface); - if (a!=DLE) { - LOG3("%s *** no DLE after send(2) %02x.\n", dc->iface->name,a); - _daveSendWithPrefix2(dc, size); - a=_daveReadSingle(dc->iface); - if (a!=DLE) { - LOG3("%s *** no DLE after resend(3) %02x.\n", dc->iface->name,a); - _daveSendSingle(dc->iface, STX); - a=_daveReadSingle(dc->iface); - if (a!=DLE) { - LOG2("%s *** no DLE before resend.\n", dc->iface->name); - return -1; - } else { - _daveSendWithPrefix2(dc, size); - a=_daveReadSingle(dc->iface); - if (a!=DLE) { - LOG2("%s *** no DLE before resend.\n", dc->iface->name); + int a; + _daveSendSingle(dc->iface, STX); + if (_daveReadSingle(dc->iface) != DLE) { + if (daveDebug & daveDebugPrintErrors) + LOG2("%s *** no DLE before send.\n", dc->iface->name); + _daveSendSingle(dc->iface, DLE); + if (_daveReadSingle(dc->iface) != DLE) { + if (daveDebug & daveDebugPrintErrors) + LOG2("%s retry*** no DLE before send.\n", dc->iface->name); return -1; - } else { - LOG2("%s *** got DLE after repeating whole transmisson.\n", dc->iface->name); - return 0; - } } - } else - LOG3("%s *** got DLE after resend(3) %02x.\n", dc->iface->name,a); - } - - } - return 0; + } + if (size > 5){ + dc->needAckNumber = dc->messageNumber; + dc->msgOut[dc->iface->ackPos + 1] = _daveIncMessageNumber(dc); + } + _daveSendWithPrefix2(dc, size); + a = _daveReadSingle(dc->iface); + if (a != DLE) { + LOG3("%s *** no DLE after send(1) %02x.\n", dc->iface->name, a); + a = _daveReadSingle(dc->iface); + if (a != DLE) { + LOG3("%s *** no DLE after send(2) %02x.\n", dc->iface->name, a); + _daveSendWithPrefix2(dc, size); + a = _daveReadSingle(dc->iface); + if (a != DLE) { + LOG3("%s *** no DLE after resend(3) %02x.\n", dc->iface->name, a); + _daveSendSingle(dc->iface, STX); + a = _daveReadSingle(dc->iface); + if (a != DLE) { + LOG2("%s *** no DLE before resend.\n", dc->iface->name); + return -1; + } + else { + _daveSendWithPrefix2(dc, size); + a = _daveReadSingle(dc->iface); + if (a != DLE) { + LOG2("%s *** no DLE before resend.\n", dc->iface->name); + return -1; + } + else { + LOG2("%s *** got DLE after repeating whole transmisson.\n", dc->iface->name); + return 0; + } + } + } + else + LOG3("%s *** got DLE after resend(3) %02x.\n", dc->iface->name, a); + } + + } + return 0; } int DECL2 _daveGetResponseMPI(daveConnection *dc) { int res; - res= _daveReadSingle(dc->iface); - if (res!=STX) { + res = _daveReadSingle(dc->iface); + if (res != STX) { if (daveDebug & daveDebugPrintErrors) { - LOG2("%s *** _daveGetResponseMPI no STX before answer data.\n", dc->iface->name); - } - res= _daveReadSingle(dc->iface); + LOG2("%s *** _daveGetResponseMPI no STX before answer data.\n", dc->iface->name); + } + res = _daveReadSingle(dc->iface); } - _daveSendSingle(dc->iface,DLE); + _daveSendSingle(dc->iface, DLE); if (daveDebug & daveDebugExchange) { - LOG2("%s _daveGetResponseMPI receive message.\n", dc->iface->name); - } - res = _daveReadMPI2(dc->iface,dc->msgIn); + LOG2("%s _daveGetResponseMPI receive message.\n", dc->iface->name); + } + res = _daveReadMPI2(dc->iface, dc->msgIn); /* LOG3("%s *** _daveExchange read result %d.\n", dc->iface->name, res); */ - if (res<=0) { + if (res <= 0) { if (daveDebug & daveDebugPrintErrors) { - LOG2("%s *** _daveGetResponseMPI no answer data.\n", dc->iface->name); - } + LOG2("%s *** _daveGetResponseMPI no answer data.\n", dc->iface->name); + } return -3; - } - /* This is NONSENSE! + } + /* This is NONSENSE! if (daveDebug & daveDebugExchange) { - LOG3("%s _daveGetResponseMPI got %d bytes\n", dc->iface->name, dc->AnswLen); - } - */ - if (_daveReadSingle(dc->iface)!=DLE) { + LOG3("%s _daveGetResponseMPI got %d bytes\n", dc->iface->name, dc->AnswLen); + } + */ + if (_daveReadSingle(dc->iface) != DLE) { if (daveDebug & daveDebugPrintErrors) { - LOG2("%s *** _daveGetResponseMPI: no DLE.\n", dc->iface->name); - } + LOG2("%s *** _daveGetResponseMPI: no DLE.\n", dc->iface->name); + } return -5; - } - _daveSendAck(dc, dc->msgIn[dc->iface->ackPos+1]); - if (_daveReadSingle(dc->iface)!=DLE) { + } + _daveSendAck(dc, dc->msgIn[dc->iface->ackPos + 1]); + if (_daveReadSingle(dc->iface) != DLE) { if (daveDebug & daveDebugPrintErrors) { - LOG2("%s *** _daveGetResponseMPI: no DLE after Ack.\n", dc->iface->name); + LOG2("%s *** _daveGetResponseMPI: no DLE after Ack.\n", dc->iface->name); } return -6; - } + } return 0; } @@ -3175,402 +3238,416 @@ Sends a message and gets ackknowledge: */ int DECL2 _daveSendMessageMPI(daveConnection * dc, PDU * p) { if (daveDebug & daveDebugExchange) { - LOG2("%s enter _daveSendMessageMPI\n", dc->iface->name); - } - if (_daveSendDialog2(dc, 2+p->hlen+p->plen+p->dlen)) { - LOG2("%s *** _daveSendMessageMPI error in _daveSendDialog.\n",dc->iface->name); + LOG2("%s enter _daveSendMessageMPI\n", dc->iface->name); + } + if (_daveSendDialog2(dc, 2 + p->hlen + p->plen + p->dlen)) { + LOG2("%s *** _daveSendMessageMPI error in _daveSendDialog.\n", dc->iface->name); // return -1; - } + } if (daveDebug & daveDebugExchange) { - LOG3("%s _daveSendMessageMPI send done. needAck %x\n", dc->iface->name,dc->needAckNumber); - } + LOG3("%s _daveSendMessageMPI send done. needAck %x\n", dc->iface->name, dc->needAckNumber); + } - if (_daveReadSingle(dc->iface)!=STX) { + if (_daveReadSingle(dc->iface) != STX) { if (daveDebug & daveDebugPrintErrors) { - LOG2("%s *** _daveSendMessageMPI no STX after _daveSendDialog.\n",dc->iface->name); - } - if ( _daveReadSingle(dc->iface)!=STX) { + LOG2("%s *** _daveSendMessageMPI no STX after _daveSendDialog.\n", dc->iface->name); + } + if (_daveReadSingle(dc->iface) != STX) { if (daveDebug & daveDebugPrintErrors) { - LOG2("%s *** _daveSendMessageMPI no STX after _daveSendDialog.\n",dc->iface->name); - } + LOG2("%s *** _daveSendMessageMPI no STX after _daveSendDialog.\n", dc->iface->name); + } return -2; - } else { + } + else { if (daveDebug & daveDebugPrintErrors) { - LOG2("%s *** _daveSendMessageMPI got STX after retry.\n",dc->iface->name); + LOG2("%s *** _daveSendMessageMPI got STX after retry.\n", dc->iface->name); } - } + } } - _daveSendSingle(dc->iface,DLE); + _daveSendSingle(dc->iface, DLE); _daveGetAck(dc); - _daveSendSingle(dc->iface,DLE); + _daveSendSingle(dc->iface, DLE); return 0; } int DECL2 _daveExchangeMPI(daveConnection * dc, PDU * p) { _daveSendMessageMPI(dc, p); - dc->AnswLen=0; + dc->AnswLen = 0; return _daveGetResponseMPI(dc); } -/* +/* Send a string of init data to the MPI adapter. */ int DECL2 _daveInitStep(daveInterface * di, int nr, uc *fix, int len, char * caller) { _daveSendSingle(di, STX); - if (_daveReadSingle(di)!=DLE){ + if (_daveReadSingle(di) != DLE){ if (daveDebug & daveDebugInitAdapter) LOG3("%s %s no answer (DLE) from adapter.\n", di->name, caller); - if (_daveReadSingle(di)!=DLE){ + if (_daveReadSingle(di) != DLE){ if (daveDebug & daveDebugInitAdapter) LOG3("%s %s no answer (DLE) from adapter.\n", di->name, caller); return nr; - } - } + } + } if (daveDebug & daveDebugInitAdapter) LOG4("%s %s step %d.\n", di->name, caller, nr); _daveSendWithCRC(di, fix, len); - if (_daveReadSingle(di)!=DLE) return nr+1; + if (_daveReadSingle(di) != DLE) return nr + 1; if (daveDebug & daveDebugInitAdapter) - LOG4("%s %s step %d.\n", di->name, caller,nr+1); - if (_daveReadSingle(di)!=STX) return nr+2; + LOG4("%s %s step %d.\n", di->name, caller, nr + 1); + if (_daveReadSingle(di) != STX) return nr + 2; if (daveDebug & daveDebugInitAdapter) - LOG4("%s %s step %d.\n", di->name, caller,nr+2); + LOG4("%s %s step %d.\n", di->name, caller, nr + 2); _daveSendSingle(di, DLE); return 0; -} +} -/* +/* This initializes the MPI adapter. Andrew's version. */ int DECL2 _daveInitAdapterMPI2(daveInterface * di) /* serial interface */ { - uc b3[]={ - 0x01,0x03,0x02,0x17, 0x00,0x9F,0x01,0x3C, - 0x00,0x90,0x01,0x14, 0x00, /* ^^^ MaxTsdr */ - 0x00,0x5, + uc b3[] = { + 0x01, 0x03, 0x02, 0x17, 0x00, 0x9F, 0x01, 0x3C, + 0x00, 0x90, 0x01, 0x14, 0x00, /* ^^^ MaxTsdr */ + 0x00, 0x5, 0x02,/* Bus speed */ - 0x00,0x0F,0x05,0x01,0x01,0x03,0x80,/* from topserverdemo */ + 0x00, 0x0F, 0x05, 0x01, 0x01, 0x03, 0x80,/* from topserverdemo */ /*^^ - Local mpi */ - }; + }; int res; uc b1[daveMaxRawLen]; - b3[16]=di->localMPI; - if (di->speed==daveSpeed500k) - b3[7]=0x64; - if (di->speed==daveSpeed1500k) - b3[7]=0x96; - b3[15]=di->speed; + b3[16] = di->localMPI; + if (di->speed == daveSpeed500k) + b3[7] = 0x64; + if (di->speed == daveSpeed1500k) + b3[7] = 0x96; + b3[15] = di->speed; - res=_daveInitStep(di, 1, b3, sizeof(b3),"initAdapter()"); + res = _daveInitStep(di, 1, b3, sizeof(b3), "initAdapter()"); - res= _daveReadMPI(di, b1); - if (daveDebug & daveDebugInitAdapter) + res = _daveReadMPI(di, b1); + if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() success.\n", di->name); - _daveSendSingle(di,DLE); - di->users=0; /* there cannot be any connections now */ + _daveSendSingle(di, DLE); + di->users = 0; /* there cannot be any connections now */ return 0; } -/* +/* Initializes the MPI adapter. */ int DECL2 _daveInitAdapterMPI1(daveInterface * di) { - uc b2[]={ - 0x01,0x0D,0x02, + uc b2[] = { + 0x01, 0x0D, 0x02, }; // us answ1[]={0x01,0x0D,0x20,'V','0','0','.','8','3'}; // us adapter0330[]={0x01,0x03,0x20,'E','=','0','3','3','0'}; // us answ2[]={0x01,0x03,0x20,'V','0','0','.','8','3'}; - us answ1[]={0x01,0x10D,0x20,'V','0','0','.',0x138,0x133}; - us adapter0330[]={0x01,0x03,0x20,'E','=','0','3',0x133,0x130}; + us answ1[] = { 0x01, 0x10D, 0x20, 'V', '0', '0', '.', 0x138, 0x133 }; + us adapter0330[] = { 0x01, 0x03, 0x20, 'E', '=', '0', '3', 0x133, 0x130 }; - uc b3[]={ - 0x01,0x03,0x02,0x27, 0x00,0x9F,0x01,0x3C, - 0x00,0x90,0x01,0x14, 0x00, - 0x00,0x05, + uc b3[] = { + 0x01, 0x03, 0x02, 0x27, 0x00, 0x9F, 0x01, 0x3C, + 0x00, 0x90, 0x01, 0x14, 0x00, + 0x00, 0x05, 0x02, - 0x00,0x1F,0x02,0x01,0x01,0x03,0x80, + 0x00, 0x1F, 0x02, 0x01, 0x01, 0x03, 0x80, // ^localMPI - }; - uc v1[]={ - 0x01,0x0C,0x02, + }; + uc v1[] = { + 0x01, 0x0C, 0x02, }; int res; uc b1[daveMaxRawLen]; if (daveDebug & daveDebugInitAdapter) LOG2("%s enter initAdapter(1).\n", di->name); - res=_daveInitStep(di, 1, b2, sizeof(b2),"initAdapter()"); + res = _daveInitStep(di, 1, b2, sizeof(b2), "initAdapter()"); if (res) { if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() fails.\n", di->name); - return -44; - } + return -44; + } - res= _daveReadMPI(di, b1); - _daveSendSingle(di,DLE); + res = _daveReadMPI(di, b1); + _daveSendSingle(di, DLE); - if (_daveMemcmp(answ1, b1, sizeof(answ1)/2)) return 4; + if (_daveMemcmp(answ1, b1, sizeof(answ1) / 2)) return 4; - b3[16]=di->localMPI; + b3[16] = di->localMPI; - if (di->speed==daveSpeed500k) - b3[7]=0x64; - if (di->speed==daveSpeed1500k) - b3[7]=0x96; - b3[15]=di->speed; - res=_daveInitStep(di, 4, b3, sizeof(b3),"initAdapter()"); + if (di->speed == daveSpeed500k) + b3[7] = 0x64; + if (di->speed == daveSpeed1500k) + b3[7] = 0x96; + b3[15] = di->speed; + res = _daveInitStep(di, 4, b3, sizeof(b3), "initAdapter()"); if (res) { if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() fails.\n", di->name); - return -54; + return -54; } /* - The following extra lines seem to be necessary for + The following extra lines seem to be necessary for TS adapter 6ES7 972-0CA33-0XAC: - */ - res= _daveReadMPI(di, b1); - _daveSendSingle(di,DLE); - if (!_daveMemcmp(adapter0330, b1, sizeof(adapter0330)/2)) { + */ + res = _daveReadMPI(di, b1); + _daveSendSingle(di, DLE); + if (!_daveMemcmp(adapter0330, b1, sizeof(adapter0330) / 2)) { if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() found Adapter E=0330.\n", di->name); - _daveSendSingle(di,STX); - res= _daveReadMPI2(di, b1); - _daveSendWithCRC(di, v1, sizeof(v1)); + _daveSendSingle(di, STX); + res = _daveReadMPI2(di, b1); + _daveSendWithCRC(di, v1, sizeof(v1)); if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() Adapter E=0330 step 7.\n", di->name); - if (_daveReadSingle(di)!=DLE) return 8; + if (_daveReadSingle(di) != DLE) return 8; if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() Adapter E=0330 step 8.\n", di->name); - res= _daveReadMPI(di, b1); - if (res!=1 || b1[0]!=STX) return 9; + res = _daveReadMPI(di, b1); + if (res != 1 || b1[0] != STX) return 9; if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() Adapter E=0330 step 9.\n", di->name); - _daveSendSingle(di,DLE); - /* This needed the exact Adapter version: */ - /* instead, just read and waste it */ - res= _daveReadMPI(di, b1); + _daveSendSingle(di, DLE); + /* This needed the exact Adapter version: */ + /* instead, just read and waste it */ + res = _daveReadMPI(di, b1); if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() Adapter E=0330 step 10.\n", di->name); - _daveSendSingle(di,DLE); - return 0; + _daveSendSingle(di, DLE); + return 0; - } else if (!_daveMemcmp(answ1, b1, sizeof(answ1)/2)) { + } + else if (!_daveMemcmp(answ1, b1, sizeof(answ1) / 2)) { if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() success.\n", di->name); - di->users=0; /* there cannot be any connections now */ + di->users = 0; /* there cannot be any connections now */ return 0; - } else { + } + else { if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() failed.\n", di->name); - return -56; + return -56; } } -us ccrc(uc *b,int size) { +us ccrc(uc *b, int size) { us sum; - int i,j,m,lll; -//initialize for crc - lll=0xcf87; - sum=0x7e; - for(j=2;j<=size;j++) { - for(m=0;m<=7;m++) { - if((lll&0x8000)!=0) { - lll=lll^0x8408; - lll=lll<<1; - lll=lll+1; - } else { - lll=lll<<1; - } - } - sum=sum^lll; - } - for(j=0;j>1; - sum=sum^0x8408; - } else { - sum=sum>>1; - } - } + int i, j, m, lll; + //initialize for crc + lll = 0xcf87; + sum = 0x7e; + for (j = 2; j <= size; j++) { + for (m = 0; m <= 7; m++) { + if ((lll & 0x8000) != 0) { + lll = lll ^ 0x8408; + lll = lll << 1; + lll = lll + 1; + } + else { + lll = lll << 1; + } + } + sum = sum^lll; + } + for (j = 0; j < size; j++) { + sum = sum ^ b[j]; + for (i = 0; i <= 7; i++) { + if (sum & 0x01) { + sum = sum >> 1; + sum = sum ^ 0x8408; + } + else { + sum = sum >> 1; + } + } } return sum; } -int daveSendWithCRC3(daveInterface * di, uc* buffer,int length) { +int daveSendWithCRC3(daveInterface * di, uc* buffer, int length) { uc target[daveMaxRawLen]; us crc; - memcpy(target+4,buffer,length); - target[0]=0x7e; - if (target[10]==0xB0) { - target[1]=di->seqNumber+1; - } else { - di->seqNumber+=0x11; - if (di->seqNumber>=0x88) di->seqNumber=0; - target[1]=di->seqNumber; - } - target[2]=(length); - target[3]=0xff-(length); - // crc=ccrc(target,length+4,startTab[length]); - crc=ccrc(target,length+4); - target[4+length]=crc % 256; - target[5+length]=crc / 256; - target[6+length]=0x7e; - di->ifwrite(di, (char*)target, length+7); + memcpy(target + 4, buffer, length); + target[0] = 0x7e; + if (target[10] == 0xB0) { + target[1] = di->seqNumber + 1; + } + else { + di->seqNumber += 0x11; + if (di->seqNumber >= 0x88) di->seqNumber = 0; + target[1] = di->seqNumber; + } + target[2] = (length); + target[3] = 0xff - (length); + // crc=ccrc(target,length+4,startTab[length]); + crc = ccrc(target, length + 4); + target[4 + length] = crc % 256; + target[5 + length] = crc / 256; + target[6 + length] = 0x7e; + di->ifwrite(di, (char*)target, length + 7); return 0; } int read1(daveInterface * di, uc* b) { - int len,res; + int len, res; if (daveDebug & daveDebugByte) LOG1("enter read1\n"); - len=0; -again: - res=di->ifread(di, (char*)b, 5); - if (res==5) { - if(b[4]==0x7e) goto again; - if(b[2]==255-b[3]) { - len=b[2]+7; - // LOG2("need length %d\n",len); - while (resifread(di, (char*)(b+res), len-res); + len = 0; +again: + res = di->ifread(di, (char*)b, 5); + if (res == 5) { + if (b[4] == 0x7e) goto again; + if (b[2] == 255 - b[3]) { + len = b[2] + 7; + // LOG2("need length %d\n",len); + while (res < len) { + res += di->ifread(di, (char*)(b + res), len - res); } } } - // LOG3("need length %d got %d\n",len,res); + // LOG3("need length %d got %d\n",len,res); if (daveDebug & daveDebugByte) - _daveDump("got",b,res); + _daveDump("got", b, res); return res; } -/* +/* This initializes the MPI adapter. Step 7 version. */ -int DECL2 _daveInitAdapterMPI3(daveInterface * di) +int DECL2 _daveInitAdapterMPI3(daveInterface * di) { - uc b2[]={0x7E,0xFC,0x9B,0xCD,0x7E}; - us adapter0330[]={0x01,0x03,0x20,'E','=','0','3','3','0'}; - uc v1[]={0x01,0x0C,0x02}; - - uc b3[]={ - 0x01,0x03,0x02,0x17, 0x00,0x9F,0x01,0x3C, - 0x00,0x90,0x01,0x14, 0x00, /* ^^^ MaxTsdr */ - 0x00,0x5, + uc b2[] = { 0x7E, 0xFC, 0x9B, 0xCD, 0x7E }; + us adapter0330[] = { 0x01, 0x03, 0x20, 'E', '=', '0', '3', '3', '0' }; + uc v1[] = { 0x01, 0x0C, 0x02 }; + + uc b3[] = { + 0x01, 0x03, 0x02, 0x17, 0x00, 0x9F, 0x01, 0x3C, + 0x00, 0x90, 0x01, 0x14, 0x00, /* ^^^ MaxTsdr */ + 0x00, 0x5, 0x02,/* Bus speed */ - 0x00,0x1F,0x05,0x01,0x01,0x03,0x80,/* from topserverdemo */ + 0x00, 0x1F, 0x05, 0x01, 0x01, 0x03, 0x80,/* from topserverdemo */ /*^^ - Local mpi */ - }; - uc m4[]={0x7e,0xca,0x2e,0x99,0x7e}; - uc b55[]={0x01,0x08,0x02}; + }; + uc m4[] = { 0x7e, 0xca, 0x2e, 0x99, 0x7e }; + uc b55[] = { 0x01, 0x08, 0x02 }; uc b1[daveMaxRawLen]; - int res,count; + int res, count; - b3[16]=di->localMPI; - if (di->speed==daveSpeed500k) - b3[7]=0x64; - if (di->speed==daveSpeed1500k) - b3[7]=0x96; - b3[15]=di->speed; - count=0; -again: + b3[16] = di->localMPI; + if (di->speed == daveSpeed500k) + b3[7] = 0x64; + if (di->speed == daveSpeed1500k) + b3[7] = 0x96; + b3[15] = di->speed; + count = 0; +again: count++; - if (count>20) return -2; - di->seqNumber=0x77; + if (count > 20) return -2; + di->seqNumber = 0x77; di->ifwrite(di, (char*)b2, sizeof(b2)); - res=di->ifread(di, (char*)b1, 5); - if (res==0) { + res = di->ifread(di, (char*)b1, 5); + if (res == 0) { + di->ifwrite(di, (char*)b2, sizeof(b2)); + res = di->ifread(di, (char*)b1, 5); + } + if (res == 0) { di->ifwrite(di, (char*)b2, sizeof(b2)); - res=di->ifread(di, (char*)b1, 5); + res = di->ifread(di, (char*)b1, 5); } - if (res==0) { - di->ifwrite(di, (char*)b2,sizeof(b2)); - res=di->ifread(di, (char*)b1, 5); - } if (daveDebug & daveDebugByte) _daveDump("got", b1, res); - if (res==5) { - if (b1[1]==0xCE) { + if (res == 5) { + if (b1[1] == 0xCE) { if (daveDebug & daveDebugInitAdapter) LOG1("ok, I begin sequence\n"); - di->seqNumber=0x77; - } else if (b1[1]==0xCA) { + di->seqNumber = 0x77; + } + else if (b1[1] == 0xCA) { if (daveDebug & daveDebugInitAdapter) LOG1("refused.\n"); goto again; - // res=di->ifread(di, b1, 100); //certainly nonsense after a jump - } else if (b1[1]==0xF8) { + // res=di->ifread(di, b1, 100); //certainly nonsense after a jump + } + else if (b1[1] == 0xF8) { if (daveDebug & daveDebugInitAdapter) LOG1("refused.\n"); di->ifwrite(di, (char*)m4, sizeof(m4)); - res=di->ifread(di, (char*)b1, 100); + res = di->ifread(di, (char*)b1, 100); goto again; - } else if (b1[1]==0x8a) { + } + else if (b1[1] == 0x8a) { if (daveDebug & daveDebugInitAdapter) LOG1("in sequence. set to 0x11\n"); - di->seqNumber=0x0; - } else if (b1[1]==0x8b) { + di->seqNumber = 0x0; + } + else if (b1[1] == 0x8b) { if (daveDebug & daveDebugInitAdapter) LOG1("in sequence. set to 0x22\n"); - di->seqNumber=0x22; - } else if (b1[1]==0x8c) { + di->seqNumber = 0x22; + } + else if (b1[1] == 0x8c) { if (daveDebug & daveDebugInitAdapter) LOG1("in sequence. set to 0x33\n"); - di->seqNumber=0x33; - } else if (b1[1]==0x8d) { + di->seqNumber = 0x33; + } + else if (b1[1] == 0x8d) { if (daveDebug & daveDebugInitAdapter) LOG1("in sequence. set to 0x44\n"); - di->seqNumber=0x44; + di->seqNumber = 0x44; } - } else return -1; - daveSendWithCRC3(di,b3,sizeof(b3)); + } + else return -1; + daveSendWithCRC3(di, b3, sizeof(b3)); read1(di, b1); - if (!_daveMemcmp(adapter0330, b1+4, sizeof(adapter0330)/2)) { + if (!_daveMemcmp(adapter0330, b1 + 4, sizeof(adapter0330) / 2)) { if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() found Adapter E=0330.\n", di->name); - daveSendWithCRC3(di,v1,sizeof(v1)); + daveSendWithCRC3(di, v1, sizeof(v1)); read1(di, b1); return 0; - } - daveSendWithCRC3(di,b55,sizeof(b55)); + } + daveSendWithCRC3(di, b55, sizeof(b55)); read1(di, b1); - // daveSendWithCRC3(di,b66,sizeof(b66)); - // read1(di, b1); + // daveSendWithCRC3(di,b66,sizeof(b66)); + // read1(di, b1); return 0; } int DECL2 _daveSendWithPrefix32(daveConnection * dc, int size) { - uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14}; - fix[4]=dc->connectionNumber2; // 1/10/05 trying Andrew's patch - fix[5]=dc->connectionNumber; // 1/10/05 trying Andrew's patch + uc fix[] = { 04, 0x80, 0x80, 0x0C, 0x03, 0x14 }; + fix[4] = dc->connectionNumber2; // 1/10/05 trying Andrew's patch + fix[5] = dc->connectionNumber; // 1/10/05 trying Andrew's patch memcpy(dc->msgOut, fix, sizeof(fix)); - dc->msgOut[1]|=dc->MPIAdr; - dc->msgOut[sizeof(fix)]=0xF1; - return daveSendWithCRC3(dc->iface, dc->msgOut, size+sizeof(fix)); + dc->msgOut[1] |= dc->MPIAdr; + dc->msgOut[sizeof(fix)] = 0xF1; + return daveSendWithCRC3(dc->iface, dc->msgOut, size + sizeof(fix)); } -int DECL2 _daveListReachablePartnersMPI3(daveInterface * di,char * buf) { +int DECL2 _daveListReachablePartnersMPI3(daveInterface * di, char * buf) { uc b1[daveMaxRawLen]; - uc m1[]={1,7,2}; + uc m1[] = { 1, 7, 2 }; int res, len; - daveSendWithCRC3(di,m1,sizeof(m1)); - res=read1(di, b1); + daveSendWithCRC3(di, m1, sizeof(m1)); + res = read1(di, b1); if (daveDebug & daveDebugInitAdapter) - LOG2("res:%d\n",res); - if(140==res){ - memcpy(buf,b1+10,126); + LOG2("res:%d\n", res); + if (140 == res){ + memcpy(buf, b1 + 10, 126); return 126; - } else - return 0; -} + } + else + return 0; +} -/* +/* Open connection to a PLC. This assumes that dc is initialized by daveNewConnection and is not yet used. (or reused for the same PLC ?) @@ -3581,66 +3658,66 @@ int DECL2 _daveConnectPLCMPI3(daveConnection * dc) { PDU p1; uc b1[daveMaxRawLen]; - uc e18[]={0x04,0x82,0x00, - 0x0d,0x00,0x14,0xe0,0x04,0x00,0x80, - 0x00,0x02,0x00,0x02, + uc e18[] = { 0x04, 0x82, 0x00, + 0x0d, 0x00, 0x14, 0xe0, 0x04, 0x00, 0x80, + 0x00, 0x02, 0x00, 0x02, 0x01, 0x00, - 0x01,0x00, - // 0x02,0x03,0x01,0x00 + 0x01, 0x00, + // 0x02,0x03,0x01,0x00 }; - uc b4[]={ - 0x00,0x0d,0x00,0x03,0xe0,0x04,0x00,0x80, - 0x00,0x02,0x01,0x06, + uc b4[] = { + 0x00, 0x0d, 0x00, 0x03, 0xe0, 0x04, 0x00, 0x80, + 0x00, 0x02, 0x01, 0x06, 0x01, 0x00, - 0x00,0x01, - 0x02,0x03,0x01,0x00 + 0x00, 0x01, + 0x02, 0x03, 0x01, 0x00 /*^^ MPI ADDR */ }; - us t4[]={ - 0x00,0x0c,0x103,0x103,0xd0,0x04,0x00,0x80, - 0x01,0x06, - 0x00,0x02,0x00,0x01,0x02, - 0x03,0x01,0x00, - 0x01,0x00,0x10,0x03,0x4d + us t4[] = { + 0x00, 0x0c, 0x103, 0x103, 0xd0, 0x04, 0x00, 0x80, + 0x01, 0x06, + 0x00, 0x02, 0x00, 0x01, 0x02, + 0x03, 0x01, 0x00, + 0x01, 0x00, 0x10, 0x03, 0x4d }; - uc b5[]={ - 0x05,0x01, + uc b5[] = { + 0x05, 0x01, }; - b4[3]=dc->connectionNumber; // 1/10/05 trying Andrew's patch - b4[sizeof(b4)-3]=dc->MPIAdr; - t4[15]=dc->MPIAdr; - t4[sizeof(t4)/2-1]^=dc->MPIAdr; /* 'patch' the checksum */ - mpi=dc->MPIAdr; - // dc->MPIAdr=2; - // e18[sizeof(e18)-3]=dc->MPIAdr; - e18[1]|=dc->MPIAdr; - daveSendWithCRC3(dc->iface,e18,sizeof(e18)); + b4[3] = dc->connectionNumber; // 1/10/05 trying Andrew's patch + b4[sizeof(b4) - 3] = dc->MPIAdr; + t4[15] = dc->MPIAdr; + t4[sizeof(t4) / 2 - 1] ^= dc->MPIAdr; /* 'patch' the checksum */ + mpi = dc->MPIAdr; + // dc->MPIAdr=2; + // e18[sizeof(e18)-3]=dc->MPIAdr; + e18[1] |= dc->MPIAdr; + daveSendWithCRC3(dc->iface, e18, sizeof(e18)); read1(dc->iface, b1); - // dc->connectionNumber2=b1[3]; // 1/10/05 trying Andrew's patch - dc->connectionNumber2=b1[9]; - dc->connectionNumber=0x14; + // dc->connectionNumber2=b1[3]; // 1/10/05 trying Andrew's patch + dc->connectionNumber2 = b1[9]; + dc->connectionNumber = 0x14; - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 3.\n", dc->iface->name); - // res=_daveReadMPI(dc->iface,b1); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 3.\n", dc->iface->name); + // res=_daveReadMPI(dc->iface,b1); - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 4.\n", dc->iface->name); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 4.\n", dc->iface->name); - _daveSendWithPrefix31(dc, b5, sizeof(b5)); + _daveSendWithPrefix31(dc, b5, sizeof(b5)); read1(dc->iface, b1); - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); - res= _daveNegPDUlengthRequest(dc, &p1); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); + res = _daveNegPDUlengthRequest(dc, &p1); return 0; } -/* +/* This adds a prefix to a string and theen sends it after doubling DLEs in the String and adding DLE,ETX and bcc. @@ -3648,14 +3725,14 @@ and adding DLE,ETX and bcc. int DECL2 _daveSendWithPrefix31(daveConnection * dc, uc *b, int size) { uc target[daveMaxRawLen]; - uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14}; - fix[4]=dc->connectionNumber2; // 1/10/05 trying Andrew's patch - fix[5]=dc->connectionNumber; // 1/10/05 trying Andrew's patch - memcpy(target,fix,sizeof(fix)); - memcpy(target+sizeof(fix),b,size); - target[1]|=dc->MPIAdr; - memcpy(target+sizeof(fix),b,size); - return daveSendWithCRC3(dc->iface,target,size+sizeof(fix)); + uc fix[] = { 04, 0x80, 0x80, 0x0C, 0x03, 0x14 }; + fix[4] = dc->connectionNumber2; // 1/10/05 trying Andrew's patch + fix[5] = dc->connectionNumber; // 1/10/05 trying Andrew's patch + memcpy(target, fix, sizeof(fix)); + memcpy(target + sizeof(fix), b, size); + target[1] |= dc->MPIAdr; + memcpy(target + sizeof(fix), b, size); + return daveSendWithCRC3(dc->iface, target, size + sizeof(fix)); } /* @@ -3663,39 +3740,39 @@ Executes part of the dialog necessary to send a message: */ int DECL2 _daveSendDialog3(daveConnection * dc, int size) { - if (size>5){ - dc->needAckNumber=dc->messageNumber; - dc->msgOut[dc->iface->ackPos-dc->PDUstartI+dc->PDUstartO+1]=_daveIncMessageNumber(dc); - } + if (size > 5){ + dc->needAckNumber = dc->messageNumber; + dc->msgOut[dc->iface->ackPos - dc->PDUstartI + dc->PDUstartO + 1] = _daveIncMessageNumber(dc); + } _daveSendWithPrefix32(dc, size); return 0; } int DECL2 _daveSendMessageMPI3(daveConnection * dc, PDU * p) { if (daveDebug & daveDebugExchange) { - LOG2("%s enter _daveSendMessageMPI3\n", dc->iface->name); - } - if (_daveSendDialog3(dc, 2+p->hlen+p->plen+p->dlen)) { - LOG2("%s *** _daveSendMessageMPI3 error in _daveSendDialog.\n",dc->iface->name); + LOG2("%s enter _daveSendMessageMPI3\n", dc->iface->name); + } + if (_daveSendDialog3(dc, 2 + p->hlen + p->plen + p->dlen)) { + LOG2("%s *** _daveSendMessageMPI3 error in _daveSendDialog.\n", dc->iface->name); // return -1; - } + } if (daveDebug & daveDebugExchange) { - LOG3("%s _daveSendMessageMPI send done. needAck %x\n", dc->iface->name,dc->needAckNumber); - } + LOG3("%s _daveSendMessageMPI send done. needAck %x\n", dc->iface->name, dc->needAckNumber); + } return 0; } -/* +/* Sends an ackknowledge message for the message number nr: */ int DECL2 _daveSendAckMPI3(daveConnection * dc, int nr) { uc m[3]; if (daveDebug & daveDebugPacket) - LOG3("%s sendAck for message %d \n", dc->iface->name,nr); - m[0]=0xB0; - m[1]=0x01; - m[2]=nr; + LOG3("%s sendAck for message %d \n", dc->iface->name, nr); + m[0] = 0xB0; + m[1] = 0x01; + m[2] = nr; return _daveSendWithPrefix31(dc, m, 3); } @@ -3708,7 +3785,7 @@ int testcrc(unsigned char *b, int size, int start) unsigned char *b1 = b; sum = start; - for (j = 0; j < size-2; j++) { + for (j = 0; j < size - 2; j++) { // LOG2("I calc: %x.\n", sum); sum = sum ^ (b1[j]); // LOG2("after xor data: %x.\n", sum); @@ -3717,104 +3794,105 @@ int testcrc(unsigned char *b, int size, int start) if (sum & 0x1) { sum = sum >> 1; sum = sum ^ 0x8408; - } else + } + else sum = sum >> 1; - // LOG2("loop: %x.\n", sum); + // LOG2("loop: %x.\n", sum); } } - /* + /* if ( ((sum /256)==b[size-1]) && ((sum %256)==b[size-2]) ) { printf ("found 1 %04x \n",start); return 1; - } - */ + } + */ if ( - ((sum %256)==b[size-2]) && - ((sum /256)==b[size-1]) + ((sum % 256) == b[size - 2]) && + ((sum / 256) == b[size - 1]) ){ - printf ("found 2 %04x %d\n",start, size-6); - startTab[size-6]=start; - return 1; + printf("found 2 %04x %d\n", start, size - 6); + startTab[size - 6] = start; + return 1; } return 0; } #endif int DECL2 _daveGetResponseMPI3(daveConnection *dc) { - int res,count; + int res, count; if (daveDebug & daveDebugExchange) LOG1("enter _daveGetResponseMPI3\n"); - count=0; - dc->msgIn[10]=0; + count = 0; + dc->msgIn[10] = 0; do { // res=dc->iface->ifread(dc->iface, dc->msgIn, 400); - res=read1(dc->iface, dc->msgIn); + res = read1(dc->iface, dc->msgIn); count++; - }while((count<5) && (dc->msgIn[10]!=0xF1)); - if (dc->msgIn[10]==0xF1) { - dc->iface->seqNumber=dc->msgIn[1]; - _daveSendAckMPI3(dc, dc->msgIn[dc->iface->ackPos+1]); + } while ((count < 5) && (dc->msgIn[10] != 0xF1)); + if (dc->msgIn[10] == 0xF1) { + dc->iface->seqNumber = dc->msgIn[1]; + _daveSendAckMPI3(dc, dc->msgIn[dc->iface->ackPos + 1]); #ifdef CRC - if (startTab[res-7]==0) { - for(count=0;count<0xffff;count++) - testcrc(dc->msgIn,res-1, count); - } + if (startTab[res - 7] == 0) { + for (count = 0; count < 0xffff; count++) + testcrc(dc->msgIn, res - 1, count); + } #endif return 0; - } + } return -10; } int DECL2 _daveExchangeMPI3(daveConnection * dc, PDU * p) { _daveSendMessageMPI3(dc, p); - dc->AnswLen=0; + dc->AnswLen = 0; return _daveGetResponseMPI3(dc); } int DECL2 _daveDisconnectPLCMPI3(daveConnection * dc) { - // uc m[]={ - // 0x80 - // }; - uc fix[]= {04,0x82,0x0,0x0C,0x03,0x14,0x80}; - fix[4]=dc->connectionNumber2; // 1/10/05 trying Andrew's patch - fix[5]=dc->connectionNumber; // 1/10/05 trying Andrew's patch - // _daveSendWithPrefix31(dc, m, 1); - fix[1]|=dc->MPIAdr; - daveSendWithCRC3(dc->iface,fix,sizeof(fix)); - read1(dc->iface,dc->msgIn); + // uc m[]={ + // 0x80 + // }; + uc fix[] = { 04, 0x82, 0x0, 0x0C, 0x03, 0x14, 0x80 }; + fix[4] = dc->connectionNumber2; // 1/10/05 trying Andrew's patch + fix[5] = dc->connectionNumber; // 1/10/05 trying Andrew's patch + // _daveSendWithPrefix31(dc, m, 1); + fix[1] |= dc->MPIAdr; + daveSendWithCRC3(dc->iface, fix, sizeof(fix)); + read1(dc->iface, dc->msgIn); return 0; -} +} /* It seems to be better to complete this subroutine even if answers from adapter are not as expected. */ int DECL2 _daveDisconnectAdapterMPI3(daveInterface * di) { - // uc m3[]={ - // 0x80 - // }; - uc m2[]={ - 1,4,2 + // uc m3[]={ + // 0x80 + // }; + uc m2[] = { + 1, 4, 2 }; uc b[daveMaxRawLen]; - // uc m4[]={0x7e,0xca,0x2e,0x99,0x7e}; - // _daveSendWithPrefix31(di, m3, sizeof(m3)); - // read1(di,b); + // uc m4[]={0x7e,0xca,0x2e,0x99,0x7e}; + // _daveSendWithPrefix31(di, m3, sizeof(m3)); + // read1(di,b); daveSendWithCRC3(di, m2, sizeof(m2)); - read1(di,b); + read1(di, b); #ifdef CRC - printf ("\n\n\n\nus startTab[]={"); - for (res=0;res<255;res++) { - printf ("0x%04x , // %d\n",startTab[res],res); + printf("\n\n\n\nus startTab[]={"); + for (res = 0; res < 255; res++) { + printf("0x%04x , // %d\n", startTab[res], res); } - printf ("}\n\n\n\n"); -#endif - // di->ifwrite(di, m4, 5); - return 0; + printf("}\n\n\n\n"); +#endif + // di->ifwrite(di, m4, 5); + return 0; } /* @@ -3823,122 +3901,123 @@ from adapter are not as expected. */ int DECL2 _daveDisconnectAdapterMPI(daveInterface * di) { int res; - uc m2[]={ - 1,4,2 + uc m2[] = { + 1, 4, 2 }; uc b1[daveMaxRawLen]; - if (daveDebug & daveDebugInitAdapter) - LOG2("%s enter DisconnectAdapter()\n", di->name); + if (daveDebug & daveDebugInitAdapter) + LOG2("%s enter DisconnectAdapter()\n", di->name); _daveSendSingle(di, STX); - res=_daveReadMPI(di,b1); - /* if ((res!=1)||(b1[0]!=DLE)) return -1; */ - _daveSendWithCRC(di, m2, sizeof(m2)); - if (daveDebug & daveDebugInitAdapter) - LOG2("%s daveDisconnectAdapter() step 1.\n", di->name); - res=_daveReadMPI(di, b1); - /* if ((res!=1)||(b1[0]!=DLE)) return -2; */ - res=_daveReadMPI(di, b1); - /* if ((res!=1)||(b1[0]!=STX)) return -3; */ - if (daveDebug & daveDebugInitAdapter) - LOG2("%s daveDisconnectAdapter() step 2.\n", di->name); + res = _daveReadMPI(di, b1); + /* if ((res!=1)||(b1[0]!=DLE)) return -1; */ + _daveSendWithCRC(di, m2, sizeof(m2)); + if (daveDebug & daveDebugInitAdapter) + LOG2("%s daveDisconnectAdapter() step 1.\n", di->name); + res = _daveReadMPI(di, b1); + /* if ((res!=1)||(b1[0]!=DLE)) return -2; */ + res = _daveReadMPI(di, b1); + /* if ((res!=1)||(b1[0]!=STX)) return -3; */ + if (daveDebug & daveDebugInitAdapter) + LOG2("%s daveDisconnectAdapter() step 2.\n", di->name); _daveSendSingle(di, DLE); di->ifread(di, b1, daveMaxRawLen); - // _daveReadChars(di, b1, tmo_normal, daveMaxRawLen); + // _daveReadChars(di, b1, tmo_normal, daveMaxRawLen); _daveSendSingle(di, DLE); - if (daveDebug & daveDebugInitAdapter) - _daveDump("got",b1,10); - return 0; + if (daveDebug & daveDebugInitAdapter) + _daveDump("got", b1, 10); + return 0; } /* This doesn't work yet. I'm not sure whether it is possible to get that list after having connected to a PLC. */ -int DECL2 _daveListReachablePartnersMPI(daveInterface * di,char * buf) { +int DECL2 _daveListReachablePartnersMPI(daveInterface * di, char * buf) { uc b1[daveMaxRawLen]; - uc m1[]={1,7,2}; + uc m1[] = { 1, 7, 2 }; int res; - res=_daveInitStep(di, 1, m1, sizeof(m1),"listReachablePartners()"); + res = _daveInitStep(di, 1, m1, sizeof(m1), "listReachablePartners()"); if (res) return 0; - res=_daveReadMPI(di,b1); - // LOG2("res %d\n", res); - if(136==res){ - _daveSendSingle(di,DLE); - memcpy(buf,b1+6,126); + res = _daveReadMPI(di, b1); + // LOG2("res %d\n", res); + if (136 == res){ + _daveSendSingle(di, DLE); + memcpy(buf, b1 + 6, 126); return 126; - } else - return 0; -} + } + else + return 0; +} int DECL2 _daveDisconnectPLCMPI(daveConnection * dc) { int res; - uc m[]={ + uc m[] = { 0x80 }; uc b1[daveMaxRawLen]; _daveSendSingle(dc->iface, STX); - res=_daveReadMPI(dc->iface,b1); - if ((res!=1)||(b1[0]!=DLE)) { + res = _daveReadMPI(dc->iface, b1); + if ((res != 1) || (b1[0] != DLE)) { if (daveDebug & daveDebugPrintErrors) - LOG2("%s *** no DLE before send.\n", dc->iface->name); + LOG2("%s *** no DLE before send.\n", dc->iface->name); return -1; } - _daveSendWithPrefix(dc, m, 1); + _daveSendWithPrefix(dc, m, 1); - res=_daveReadMPI(dc->iface,b1); - if ((res!=1)||(b1[0]!=DLE)) { + res = _daveReadMPI(dc->iface, b1); + if ((res != 1) || (b1[0] != DLE)) { if (daveDebug & daveDebugPrintErrors) - LOG2("%s *** no DLE after send.\n", dc->iface->name); + LOG2("%s *** no DLE after send.\n", dc->iface->name); return -2; - } + } _daveSendSingle(dc->iface, DLE); - res=_daveReadMPI(dc->iface,b1); - if ((res!=1)||(b1[0]!=STX)) return 6; - if (daveDebug & daveDebugConnect) - LOG2("%s daveDisConnectPLC() step 6.\n", dc->iface->name); - res=_daveReadMPI(dc->iface,b1); - if (daveDebug & daveDebugConnect) - _daveDump("got",b1,10); + res = _daveReadMPI(dc->iface, b1); + if ((res != 1) || (b1[0] != STX)) return 6; + if (daveDebug & daveDebugConnect) + LOG2("%s daveDisConnectPLC() step 6.\n", dc->iface->name); + res = _daveReadMPI(dc->iface, b1); + if (daveDebug & daveDebugConnect) + _daveDump("got", b1, 10); _daveSendSingle(dc->iface, DLE); return 0; -} +} /* -build the PDU for a PDU length negotiation +build the PDU for a PDU length negotiation */ int DECL2 _daveNegPDUlengthRequest(daveConnection * dc, PDU *p) { - uc pa[]= {0xF0, 0 ,0, 1, 0, 1, - dc->maxPDUlength / 0x100, //3, - dc->maxPDUlength % 0x100, //0xC0, - }; + uc pa[] = { 0xF0, 0, 0, 1, 0, 1, + dc->maxPDUlength / 0x100, //3, + dc->maxPDUlength % 0x100, //0xC0, + }; int res; int CpuPduLimit; PDU p2; - p->header=dc->msgOut+dc->PDUstartO; - _daveInitPDUheader(p,1); + p->header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(p, 1); _daveAddParam(p, pa, sizeof(pa)); if (daveDebug & daveDebugPDU) { _daveDumpPDU(p); - } - res=_daveExchange(dc, p); - if(res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc, &p2); - if(res!=daveResOK) return res; - CpuPduLimit=daveGetU16from(p2.param+6); + } + res = _daveExchange(dc, p); + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, &p2); + if (res != daveResOK) return res; + CpuPduLimit = daveGetU16from(p2.param + 6); if (dc->maxPDUlength > CpuPduLimit) dc->maxPDUlength = CpuPduLimit; // use lower number as limit if (daveDebug & daveDebugConnect) { - LOG3("\n*** Partner offered PDU length: %d used limit %d\n\n",CpuPduLimit,dc->maxPDUlength); - } + LOG3("\n*** Partner offered PDU length: %d used limit %d\n\n", CpuPduLimit, dc->maxPDUlength); + } return res; -} +} -/* +/* Open connection to a PLC. This assumes that dc is initialized by daveNewConnection and is not yet used. (or reused for the same PLC ?) @@ -3948,78 +4027,78 @@ int DECL2 _daveConnectPLCMPI2(daveConnection * dc) { PDU p1; uc b1[daveMaxRawLen]; - uc b4[]={ - 0x00,0x0d,0x00,0x03,0xe0,0x04,0x00,0x80, - 0x00,0x02,0x01,0x06, + uc b4[] = { + 0x00, 0x0d, 0x00, 0x03, 0xe0, 0x04, 0x00, 0x80, + 0x00, 0x02, 0x01, 0x06, 0x01, 0x00, - 0x00,0x01, - 0x02,0x03,0x01,0x00 + 0x00, 0x01, + 0x02, 0x03, 0x01, 0x00 /*^^ MPI ADDR */ }; - us t4[]={ - 0x00,0x0c,0x103,0x103,0xd0,0x04,0x00,0x80, - 0x01,0x06, - 0x00,0x02,0x00,0x01,0x02, - 0x03,0x01,0x00, - 0x01,0x00,0x10,0x03,0x4d + us t4[] = { + 0x00, 0x0c, 0x103, 0x103, 0xd0, 0x04, 0x00, 0x80, + 0x01, 0x06, + 0x00, 0x02, 0x00, 0x01, 0x02, + 0x03, 0x01, 0x00, + 0x01, 0x00, 0x10, 0x03, 0x4d }; - uc b5[]={ - 0x05,0x01, + uc b5[] = { + 0x05, 0x01, }; - us t5[]={ + us t5[] = { 0x00, 0x0c, - 0x103,0x103,0x05,0x01,0x10,0x03,0x1b + 0x103, 0x103, 0x05, 0x01, 0x10, 0x03, 0x1b }; - b4[3]=dc->connectionNumber; // 1/10/05 trying Andrew's patch - b4[sizeof(b4)-3]=dc->MPIAdr; - t4[15]=dc->MPIAdr; - t4[sizeof(t4)/2-1]^=dc->MPIAdr; /* 'patch' the checksum */ + b4[3] = dc->connectionNumber; // 1/10/05 trying Andrew's patch + b4[sizeof(b4) - 3] = dc->MPIAdr; + t4[15] = dc->MPIAdr; + t4[sizeof(t4) / 2 - 1] ^= dc->MPIAdr; /* 'patch' the checksum */ - _daveInitStep(dc->iface, 1, b4, sizeof(b4),"connectPLC(2)"); - res=_daveReadMPI2(dc->iface,b1); + _daveInitStep(dc->iface, 1, b4, sizeof(b4), "connectPLC(2)"); + res = _daveReadMPI2(dc->iface, b1); if (_daveMemcmp(t4, b1, res)) { - LOG2("%s daveConnectPLC() step 3 ends with 3.\n", dc->iface->name); + LOG2("%s daveConnectPLC() step 3 ends with 3.\n", dc->iface->name); return 3; - } - dc->connectionNumber2=b1[3]; // 1/10/05 trying Andrew's patch + } + dc->connectionNumber2 = b1[3]; // 1/10/05 trying Andrew's patch - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 4.\n", dc->iface->name); - res=_daveReadMPI(dc->iface,b1); - if ((res!=1)||(b1[0]!=DLE)) { - LOG2("%s daveConnectPLC() step 4 ends with 4.\n", dc->iface->name); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 4.\n", dc->iface->name); + res = _daveReadMPI(dc->iface, b1); + if ((res != 1) || (b1[0] != DLE)) { + LOG2("%s daveConnectPLC() step 4 ends with 4.\n", dc->iface->name); return 4; - } + } - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 5.\n", dc->iface->name); - _daveSendWithPrefix(dc, b5, sizeof(b5)); - res=_daveReadMPI(dc->iface,b1); - if ((res!=1)||(b1[0]!=DLE)) return 5; - res=_daveReadMPI(dc->iface,b1); - if ((res!=1)||(b1[0]!=STX)) return 5; + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 5.\n", dc->iface->name); + _daveSendWithPrefix(dc, b5, sizeof(b5)); + res = _daveReadMPI(dc->iface, b1); + if ((res != 1) || (b1[0] != DLE)) return 5; + res = _daveReadMPI(dc->iface, b1); + if ((res != 1) || (b1[0] != STX)) return 5; - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); _daveSendSingle(dc->iface, DLE); - res=_daveReadMPI(dc->iface,b1); + res = _daveReadMPI(dc->iface, b1); _daveSendSingle(dc->iface, DLE); - if (dc->iface->protocol==daveProtoMPI4) _daveSendSingle(dc->iface, STX); + if (dc->iface->protocol == daveProtoMPI4) _daveSendSingle(dc->iface, STX); if (_daveMemcmp(t5, b1, res)) return 6; - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); - res= _daveNegPDUlengthRequest(dc, &p1); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); + res = _daveNegPDUlengthRequest(dc, &p1); return 0; } -/* +/* Open connection to a PLC. This assumes that dc is initialized by daveNewConnection and is not yet used. (or reused for the same PLC ?) @@ -4027,63 +4106,63 @@ daveNewConnection and is not yet used. int DECL2 _daveConnectPLCMPI1(daveConnection * dc) { int res; PDU p1; - uc b4[]={ - 0x04,0x80,0x80,0x0D,0x00,0x14,0xE0,0x04,0x00,0x80,0x00,0x02,0x00,0x02,0x01,0x00,0x01,0x00, + uc b4[] = { + 0x04, 0x80, 0x80, 0x0D, 0x00, 0x14, 0xE0, 0x04, 0x00, 0x80, 0x00, 0x02, 0x00, 0x02, 0x01, 0x00, 0x01, 0x00, }; - uc b4R[]={ - //7E 11 1F E0 04 82 00 0D 00 14 E0 04 00 80 00 02 01 0F 01 00 06 04 02 AA BB 00 00 CC DD C0 A8 02 BC 01 03 18 87 7E - //Step7//7E 00 1F E0 04 86 00 0D 00 14 E0 04 00 80 00 02 01 0F 01 00 06 04 02 AA BB 00 00 CC DD C0 A8 02 BC 01 03 8C 60 7E + uc b4R[] = { + //7E 11 1F E0 04 82 00 0D 00 14 E0 04 00 80 00 02 01 0F 01 00 06 04 02 AA BB 00 00 CC DD C0 A8 02 BC 01 03 18 87 7E + //Step7//7E 00 1F E0 04 86 00 0D 00 14 E0 04 00 80 00 02 01 0F 01 00 06 04 02 AA BB 00 00 CC DD C0 A8 02 BC 01 03 8C 60 7E - // MPI connr SUBNET SUBNET - 0x04,0x80,0x00,0x0D,0x00,0x14,0xE0,0x04,0x00,0x80,0x00,0x02,0x01,0x0F,0x01,0x00,0x06,0x04,0x02,0xAA,0xBB,0x00,0x00,0xCC,0xDD,0xC0,0xA8,0x02,0xBC,0x01,0x03,0x18,0x87,0x7E + // MPI connr SUBNET SUBNET + 0x04, 0x80, 0x00, 0x0D, 0x00, 0x14, 0xE0, 0x04, 0x00, 0x80, 0x00, 0x02, 0x01, 0x0F, 0x01, 0x00, 0x06, 0x04, 0x02, 0xAA, 0xBB, 0x00, 0x00, 0xCC, 0xDD, 0xC0, 0xA8, 0x02, 0xBC, 0x01, 0x03, 0x18, 0x87, 0x7E //0x04,0x80,0x80,0x0D,0x00,0x14,0xE0,0x04,0x00,0x80,0x00,0x02,0x00,0x02,0x01,0x00,0x01,0x00, }; - us t4[]={ - 0x04,0x80,0x180,0x0C,0x114,0x103,0xD0,0x04, // 1/10/05 trying Andrew's patch - 0x00,0x80, - 0x00,0x02,0x00,0x02,0x01, - 0x00,0x01,0x00, + us t4[] = { + 0x04, 0x80, 0x180, 0x0C, 0x114, 0x103, 0xD0, 0x04, // 1/10/05 trying Andrew's patch + 0x00, 0x80, + 0x00, 0x02, 0x00, 0x02, 0x01, + 0x00, 0x01, 0x00, }; - uc b5[]={ - 0x05,0x01, + uc b5[] = { + 0x05, 0x01, }; - us t5[]={ + us t5[] = { 0x04, 0x80, - 0x180,0x0C,0x114,0x103,0x05,0x01, + 0x180, 0x0C, 0x114, 0x103, 0x05, 0x01, }; - b4[1]|=dc->MPIAdr; - b4[5]=dc->connectionNumber; // 1/10/05 trying Andrew's patch + b4[1] |= dc->MPIAdr; + b4[5] = dc->connectionNumber; // 1/10/05 trying Andrew's patch - t4[1]|=dc->MPIAdr; - t5[1]|=dc->MPIAdr; + t4[1] |= dc->MPIAdr; + t5[1] |= dc->MPIAdr; - _daveInitStep(dc->iface, 1, b4, sizeof(b4),"connectPLC(1)"); + _daveInitStep(dc->iface, 1, b4, sizeof(b4), "connectPLC(1)"); - res= _daveReadMPI2(dc->iface,dc->msgIn); - if (_daveMemcmp(t4, dc->msgIn, sizeof(t4)/2)) return 3; - dc->connectionNumber2=dc->msgIn[5]; // 1/10/05 trying Andrew's patch - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC(1) step 4.\n", dc->iface->name); + res = _daveReadMPI2(dc->iface, dc->msgIn); + if (_daveMemcmp(t4, dc->msgIn, sizeof(t4) / 2)) return 3; + dc->connectionNumber2 = dc->msgIn[5]; // 1/10/05 trying Andrew's patch + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC(1) step 4.\n", dc->iface->name); - if (_daveReadSingle(dc->iface)!=DLE) return 4; - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 5.\n", dc->iface->name); - _daveSendWithPrefix(dc, b5, sizeof(b5)); - if (_daveReadSingle(dc->iface)!=DLE) return 5; - if (_daveReadSingle(dc->iface)!=STX) return 5; + if (_daveReadSingle(dc->iface) != DLE) return 4; + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 5.\n", dc->iface->name); + _daveSendWithPrefix(dc, b5, sizeof(b5)); + if (_daveReadSingle(dc->iface) != DLE) return 5; + if (_daveReadSingle(dc->iface) != STX) return 5; - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); _daveSendSingle(dc->iface, DLE); - res= _daveReadMPI2(dc->iface,dc->msgIn); - if (_daveMemcmp(t5, dc->msgIn, sizeof(t5)/2)) return 6; - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); - res= _daveNegPDUlengthRequest(dc, &p1); + res = _daveReadMPI2(dc->iface, dc->msgIn); + if (_daveMemcmp(t5, dc->msgIn, sizeof(t5) / 2)) return 6; + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); + res = _daveNegPDUlengthRequest(dc, &p1); return 0; } @@ -4100,15 +4179,16 @@ int DECL2 _daveReadOne(daveInterface * di, uc *b) { t.tv_sec = di->timeout / 1000000; t.tv_usec = di->timeout % 1000000; - /* if (daveDebug & daveDebugByte) + /* if (daveDebug & daveDebugByte) LOG2("timeout %d\n",di->timeout); */ - if (select(di->fd.rfd + 1, &FDS, NULL, NULL, &t) <= 0) + if (select(di->fd.rfd + 1, &FDS, NULL, NULL, &t) <= 0) { if (daveDebug & daveDebugByte) LOG1("timeout in readOne.\n"); return (0); - } else { + } + else { return read(di->fd.rfd, b, 1); - } + } }; #endif @@ -4116,21 +4196,21 @@ int DECL2 _daveReadOne(daveInterface * di, uc *b) { int DECL2 _daveReadOne(daveInterface * di, uc *b) { unsigned long i; char res; - ReadFile(di->fd.rfd, b, 1, &i,NULL); + ReadFile(di->fd.rfd, b, 1, &i, NULL); return i; } #endif #endif /* - Universal receive with timeout: -*/ + Universal receive with timeout: + */ int DECL2 _daveTimedRecv(daveInterface * di, uc *b, int len){ fd_set FDS; struct timeval t; FD_ZERO(&FDS); #ifdef BCCWIN - FD_SET((SOCKET)(di->fd.rfd), &FDS); + FD_SET((SOCKET)(di->fd.rfd), &FDS); #endif #ifdef LINUX FD_SET(di->fd.rfd, &FDS); @@ -4138,252 +4218,270 @@ int DECL2 _daveTimedRecv(daveInterface * di, uc *b, int len){ t.tv_sec = di->timeout / 1000000; t.tv_usec = di->timeout % 1000000; #ifdef BCCWIN - if (select(1, &FDS, NULL, NULL, &t) <= 0) { - if (daveDebug & daveDebugByte) LOG1("timeout in TCP read.\n"); - return 0; - } else { - return recv((SOCKET)(di->fd.rfd), b, len, 0); - } + if (select(1, &FDS, NULL, NULL, &t) <= 0) { + if (daveDebug & daveDebugByte) LOG1("timeout in TCP read.\n"); + return 0; + } + else { + return recv((SOCKET)(di->fd.rfd), b, len, 0); + } #endif #ifdef LINUX if (select(di->fd.rfd + 1, &FDS, NULL, NULL, &t) <= 0) { - if (daveDebug & daveDebugByte) LOG1("timeout in TCP read.\n"); - return 0; - } else { - return read(di->fd.rfd, b, len); - } + if (daveDebug & daveDebugByte) LOG1("timeout in TCP read.\n"); + return 0; + } + else { + return read(di->fd.rfd, b, len); + } #endif } -int DECL2 _daveReadIBHPacket2(daveInterface * di,uc *b) { - int res, len; - res=_daveTimedRecv(di, b, 3); - if (res<3) { - if (daveDebug & daveDebugByte) { - LOG2("res %d ",res); - _daveDump("readIBHpacket2: short packet", b, res); - } - return (0); /* short packet */ +int DECL2 _daveReadIBHPacket2(daveInterface * di, uc *b) { + int res, len; + res = _daveTimedRecv(di, b, 3); + if (res < 3) { + if (daveDebug & daveDebugByte) { + LOG2("res %d ", res); + _daveDump("readIBHpacket2: short packet", b, res); + } + return (0); /* short packet */ + } + len = b[2] + 8; + res += _daveTimedRecv(di, b + 3, len - 3); + if (daveDebug & daveDebugByte) { + LOG3("readIBHpacket2: %d bytes read, %d needed\n", res, len); + _daveDump("readIBHpacket2: packet", b, res); + } + // _daveDump("readIBHpacket2: packet", b, 8); + return res; +} + + + +uc IBHfollow[] = { + 0, 0, 7, 0xb, + 0, 0, 0x82, 0, + 0, 0, 0, 0, + 2, 5, 1 +}; + + +int DECL2 _daveReadIBHPacket(daveInterface * di, uc *b) { + int res, res2, len2; + uc b2[300]; + res = _daveReadIBHPacket2(di, b); + + if ((res > 15) && (b[15] == 0xf0)) { + again: + // LOG1("FOLLOW UP\n"); + + IBHfollow[0] = b[1]; + IBHfollow[1] = b[0]; + IBHfollow[8] = b[8]; + IBHfollow[9] = b[9]; + IBHfollow[10] = b[10]; + IBHfollow[11] = b[11]; + + // _daveDump("IBHfollow", IBHfollow, 15); + + res2 = send((unsigned int)(di->fd.wfd), IBHfollow, 15, 0); + + // LOG2("send: res2:%d\n",res2); + + res2 = _daveReadIBHPacket2(di, b2); + // LOG2("read: res2:%d\n",res2); + res2 = _daveReadIBHPacket2(di, b2); + // LOG2("read: res2:%d\n",res2); + + // if ((res>15) && (b[15]==0xf0)) + memcpy(b + res, b2 + 17, res2 - 17); + b[16] = b2[16]; + res += res2 - 17; + b[15] = 0xf1; + // LOG2("new b2[15]: %d\n",b2[15]); + if ((res > 15) && (b2[15] == 0xf0)) goto again; } - len=b[2]+8; - res+=_daveTimedRecv(di, b+3, len-3); + if (daveDebug & daveDebugByte) { - LOG3("readIBHpacket2: %d bytes read, %d needed\n",res, len); - _daveDump("readIBHpacket2: packet", b, res); - } -// _daveDump("readIBHpacket2: packet", b, 8); - return res; - } - - - -uc IBHfollow[]={ - 0,0,7,0xb, - 0,0,0x82,0, - 0,0,0,0, - 2,5,1 - }; - - -int DECL2 _daveReadIBHPacket(daveInterface * di,uc *b) { - int res,res2,len2; - uc b2[300]; - res= _daveReadIBHPacket2(di, b); - - if ((res>15) && (b[15]==0xf0)) { -again: -// LOG1("FOLLOW UP\n"); - - IBHfollow[0]=b[1]; - IBHfollow[1]=b[0]; - IBHfollow[8]=b[8]; - IBHfollow[9]=b[9]; - IBHfollow[10]=b[10]; - IBHfollow[11]=b[11]; - -// _daveDump("IBHfollow", IBHfollow, 15); - - res2=send((unsigned int)(di->fd.wfd), IBHfollow, 15, 0); - -// LOG2("send: res2:%d\n",res2); - - res2= _daveReadIBHPacket2(di, b2); -// LOG2("read: res2:%d\n",res2); - res2= _daveReadIBHPacket2(di, b2); -// LOG2("read: res2:%d\n",res2); - -// if ((res>15) && (b[15]==0xf0)) - memcpy(b+res,b2+17,res2-17); - b[16]=b2[16]; - res+=res2-17; - b[15]=0xf1; -// LOG2("new b2[15]: %d\n",b2[15]); - if ((res>15) && (b2[15]==0xf0)) goto again; - } - - if (daveDebug & daveDebugByte) { - LOG2("readIBHpacket: %d bytes read\n",res); -// _daveDump("readIBHpacket: packet", b, res); - } + LOG2("readIBHpacket: %d bytes read\n", res); + // _daveDump("readIBHpacket: packet", b, res); + } return (res); - } +} /* - Read one complete packet. -*/ -int DECL2 _daveReadISOPacket(daveInterface * di,uc *b) { - int res,i,length, follow; + Read one complete packet. + */ +int DECL2 _daveReadISOPacket(daveInterface * di, uc *b) { + int res, i, length, follow; + int remaining; /* FIX */ uc lhdr[7]; - i=_daveTimedRecv(di, b, 4); - if (i<0) return 0; - res=i; - if (res<4) { - if (daveDebug & daveDebugByte) { - LOG2("res %d ",res); - _daveDump("readISOpacket: short packet", b, res); - } - return (0); /* short packet */ - } - length=b[3]+0x100*b[2]; - i=_daveTimedRecv(di, b+4, length-4); - res+=i; - if (daveDebug & daveDebugByte) { - LOG3("readISOpacket: %d bytes read, %d needed\n",res, length); - _daveDump("readISOpacket: packet", b, res); + i = _daveTimedRecv(di, b, 4); + if (i < 0) return 0; + res = i; + if (res < 4) { + if (daveDebug & daveDebugByte) { + LOG2("res %d ", res); + _daveDump("readISOpacket: short packet", b, res); + } + return (0); /* short packet */ } - follow=((b[5]==0xf0)&& ((b[6] & 0x80)==0) ); - while (follow) { - if (daveDebug & daveDebugByte) { - LOG2("readISOpacket: more data follows %d\n",b[6]); - } - i=_daveTimedRecv(di, lhdr, 7); - length=lhdr[3]+0x100*lhdr[2]; + length = b[3] + 0x100 * b[2]; + i = _daveTimedRecv(di, b + 4, length - 4); + res += i; if (daveDebug & daveDebugByte) { - _daveDump("readISOpacket: follow %d %d", lhdr, i); + LOG3("readISOpacket: %d bytes read, %d needed\n", res, length); + _daveDump("readISOpacket: packet", b, res); } - i=_daveTimedRecv(di, b+res, length-7); - if (daveDebug & daveDebugByte) { - _daveDump("readISOpacket: follow %d %d", b+res, i); + /* FIX START: Force to read the complete TPKT if first was not complete */ + remaining = length - res; + while (remaining > 0){ + if (daveDebug & daveDebugByte) { + LOG2("readISOpacket: Trying to read %d remaining bytes of the complete TPKT\n", remaining); + FLUSH; + } + i = _daveTimedRecv(di, b + res, remaining); + if (i < 0) return 0; + res += i; + remaining = length - res; } - res+=i; - follow=((lhdr[5]==0xf0)&& ((lhdr[6] & 0x80)==0) ); + /* FIX END */ + follow = ((b[5] == 0xf0) && ((b[6] & 0x80) == 0)); + while (follow) { + if (daveDebug & daveDebugByte) { + LOG2("readISOpacket: more data follows %d\n", b[6]); + } + i = _daveTimedRecv(di, lhdr, 7); + length = lhdr[3] + 0x100 * lhdr[2]; + if (daveDebug & daveDebugByte) { + _daveDump("readISOpacket: follow %d %d", lhdr, i); + } + i = _daveTimedRecv(di, b + res, length - 7); + if (daveDebug & daveDebugByte) { + _daveDump("readISOpacket: follow %d %d", b + res, i); + } + res += i; + follow = ((lhdr[5] == 0xf0) && ((lhdr[6] & 0x80) == 0)); } return (res); - } +} int DECL2 _daveSendISOPacket(daveConnection * dc, int size) { - unsigned long i; - int res; - size+=4; - *(dc->msgOut+dc->partPos+3)=size % 0x100; //was %0xFF, certainly a bug - *(dc->msgOut+dc->partPos+2)=size / 0x100; - *(dc->msgOut+dc->partPos+1)=0; - *(dc->msgOut+dc->partPos+0)=3; - if (daveDebug & daveDebugByte) - _daveDump("send packet: ",dc->msgOut+dc->partPos,size); + unsigned long i; + int res; + size += 4; + *(dc->msgOut + dc->partPos + 3) = size % 0x100; //was %0xFF, certainly a bug + *(dc->msgOut + dc->partPos + 2) = size / 0x100; + *(dc->msgOut + dc->partPos + 1) = 0; + *(dc->msgOut + dc->partPos + 0) = 3; + if (daveDebug & daveDebugByte) + _daveDump("send packet: ", dc->msgOut + dc->partPos, size); #ifdef HAVE_SELECT - daveWriteFile(dc->iface->fd.wfd, dc->msgOut+dc->partPos, size, i); -#endif + daveWriteFile(dc->iface->fd.wfd, dc->msgOut + dc->partPos, size, i); +#endif #ifdef BCCWIN - res = send((SOCKET)(dc->iface->fd.wfd), dc->msgOut+dc->partPos, size, 0); - if (res==SOCKET_ERROR ) - if (daveDebug & daveDebugPrintErrors) LOG2("_daveSendISOPacket WSAGetLastError: %d \n",WSAGetLastError()); - + res = send((SOCKET)(dc->iface->fd.wfd), dc->msgOut + dc->partPos, size, 0); + if (res == SOCKET_ERROR) + if (daveDebug & daveDebugPrintErrors) LOG2("_daveSendISOPacket WSAGetLastError: %d \n", WSAGetLastError()); + #endif - return 0; + return 0; } #ifndef AVR_NOOS #define ISOTCPminPacketLength 16 int DECL2 _daveGetResponseISO_TCP(daveConnection * dc) { int res; - res=_daveReadISOPacket(dc->iface,dc->msgIn); - if(res==7) { - if (daveDebug & daveDebugByte) + res = _daveReadISOPacket(dc->iface, dc->msgIn); + if (res == 7) { + if (daveDebug & daveDebugByte) LOG1("CPU sends funny 7 byte packets.\n"); - res=_daveReadISOPacket(dc->iface,dc->msgIn); + res = _daveReadISOPacket(dc->iface, dc->msgIn); } - if (res==0) return daveResTimeout; - if (resiface->name); - } - dc->partPos = 0; - totLen = p->hlen + p->plen + p->dlen; - while (totLen) { - if (totLen>dc->TPDUsize) { - sLen = dc->TPDUsize; - *(dc->msgOut + dc->partPos + 6) = 0x00; - } else { - sLen = totLen; - *(dc->msgOut + dc->partPos + 6) = 0x80; - } - *(dc->msgOut + dc->partPos + 5) = 0xf0; - *(dc->msgOut + dc->partPos + 4) = 0x02; - _daveSendISOPacket(dc, 3 + sLen); - totLen -= sLen; - dc->partPos += sLen; - } - return 0; + int res, totLen, sLen; + + if (daveDebug & daveDebugExchange) { + LOG2("%s enter _daveSendTCP\n", dc->iface->name); + } + dc->partPos = 0; + totLen = p->hlen + p->plen + p->dlen; + while (totLen) { + if (totLen > dc->TPDUsize) { + sLen = dc->TPDUsize; + *(dc->msgOut + dc->partPos + 6) = 0x00; + } + else { + sLen = totLen; + *(dc->msgOut + dc->partPos + 6) = 0x80; + } + *(dc->msgOut + dc->partPos + 5) = 0xf0; + *(dc->msgOut + dc->partPos + 4) = 0x02; + _daveSendISOPacket(dc, 3 + sLen); + totLen -= sLen; + dc->partPos += sLen; + } + return 0; } /* Executes the dialog around one message: */ int DECL2 _daveExchangeTCP(daveConnection * dc, PDU * p) { - int res, totLen, sLen; + int res, totLen, sLen; + + if (daveDebug & daveDebugExchange) { + LOG2("%s enter _daveExchangeTCP\n", dc->iface->name); + } - if (daveDebug & daveDebugExchange) { - LOG2("%s enter _daveExchangeTCP\n", dc->iface->name); - } + // _daveSendISOPacket(dc,3+p->hlen+p->plen+p->dlen); -// _daveSendISOPacket(dc,3+p->hlen+p->plen+p->dlen); + dc->partPos = 0; + totLen = p->hlen + p->plen + p->dlen; + while (totLen) { + if (totLen > dc->TPDUsize) { + sLen = dc->TPDUsize; + *(dc->msgOut + dc->partPos + 6) = 0x00; + } + else { + sLen = totLen; + *(dc->msgOut + dc->partPos + 6) = 0x80; + } + *(dc->msgOut + dc->partPos + 5) = 0xf0; + *(dc->msgOut + dc->partPos + 4) = 0x02; + _daveSendISOPacket(dc, 3 + sLen); + totLen -= sLen; + dc->partPos += sLen; + } - dc->partPos=0; - totLen=p->hlen+p->plen+p->dlen; - while(totLen) { - if (totLen>dc->TPDUsize) { - sLen=dc->TPDUsize; - *(dc->msgOut+dc->partPos+6)=0x00; - } else { - sLen=totLen; - *(dc->msgOut+dc->partPos+6)=0x80; - } - *(dc->msgOut+dc->partPos+5)=0xf0; - *(dc->msgOut+dc->partPos+4)=0x02; - _daveSendISOPacket(dc,3+sLen); - totLen-=sLen; - dc->partPos+=sLen; - } - - res=_daveReadISOPacket(dc->iface,dc->msgIn); - if(res==7) { - if (daveDebug & daveDebugByte) - LOG1("CPU sends funny 7 byte packets.\n"); - res=_daveReadISOPacket(dc->iface,dc->msgIn); - } - if (daveDebug & daveDebugExchange) { - LOG3("%s _daveExchangeTCP res from read %d\n", dc->iface->name,res); - } - if (res==0) return daveResTimeout; - if (res<=ISOTCPminPacketLength) return daveResShortPacket; - return 0; + res = _daveReadISOPacket(dc->iface, dc->msgIn); + if (res == 7) { + if (daveDebug & daveDebugByte) + LOG1("CPU sends funny 7 byte packets.\n"); + res = _daveReadISOPacket(dc->iface, dc->msgIn); + } + if (daveDebug & daveDebugExchange) { + LOG3("%s _daveExchangeTCP res from read %d\n", dc->iface->name, res); + } + if (res == 0) return daveResTimeout; + if (res <= ISOTCPminPacketLength) return daveResShortPacket; + return 0; } int DECL2 _daveConnectPLCTCP(daveConnection * dc) { int res, success, retries, i, px; - uc b4[]={ + uc b4[] = { 0x11, //Length 0xE0, // TDPU Type CR = Connection Request (see RFC1006/ISO8073) 0x00, 0x00, // TPDU Destination Reference (unknown) @@ -4402,13 +4500,13 @@ int DECL2 _daveConnectPLCTCP(daveConnection * dc) { 0xa, // requested TPDU-Size 8=256 Bytes, 9=512 Bytes , a=1024 Bytes }; - uc b4R[]={ // for routing + uc b4R[] = { // for routing 6 + 30 + 30 + 3, // Length over all without this byte (fixed // Data 6 Bytes + size of Parameters (3 for C0h,30 for C1h+C2h) 0xE0, // TDPU Type CR = Connection Request (see RFC1006/ISO8073) - 0x00,0x00, // TPDU Destination Reference (unknown) - 0x00,0x01, // TPDU Source-Reference (my own reference, should not be zero) + 0x00, 0x00, // TPDU Destination Reference (unknown) + 0x00, 0x01, // TPDU Source-Reference (my own reference, should not be zero) 0x00, // TPDU Class 0 and no Option 0xC1, // Parameter Source-TSAP @@ -4417,9 +4515,9 @@ int DECL2 _daveConnectPLCTCP(daveConnection * dc) { 0, // Length for S7-Subnet-ID 0, // Length of PLC-Number 2, // Length of Function/Rack/Slot - 0,0,0,0,0,0,0,0, // empty Data - 0,0,0,0,0,0,0,0, - 0,0,0,0,0,0, + 0, 0, 0, 0, 0, 0, 0, 0, // empty Data + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, dc->ConnectionType, // Function (1=PG,2=OP,3=Step7Basic) (dc->slot + dc->rack * 32), // Rack (Bit 7-5) and Slot (Bit 4-0) @@ -4430,10 +4528,10 @@ int DECL2 _daveConnectPLCTCP(daveConnection * dc) { dc->_routingDestinationSize, // Length of PLC-Number - 04 if you use a IP as Destination! 2, // Length of Function/Rack/Slot - (unsigned char) (dc->routingSubnetFirst >> 8), (unsigned char) dc->routingSubnetFirst, // first part of S7-Subnet-ID + (unsigned char)(dc->routingSubnetFirst >> 8), (unsigned char)dc->routingSubnetFirst, // first part of S7-Subnet-ID // (look into the S7Project/Network configuration) - 0x00,0x00, // fix always 0000 (reserved for later use ?) - (unsigned char) (dc->routingSubnetSecond >> 8), (unsigned char) dc->routingSubnetSecond, // second part of S7-Subnet-ID + 0x00, 0x00, // fix always 0000 (reserved for later use ?) + (unsigned char)(dc->routingSubnetSecond >> 8), (unsigned char)dc->routingSubnetSecond, // second part of S7-Subnet-ID // (see S7Project/Network configuration) dc->_routingDestination1, // PLC-Number (0-126) or IP Adress (then 4 Bytes are used) @@ -4441,98 +4539,102 @@ int DECL2 _daveConnectPLCTCP(daveConnection * dc) { dc->_routingDestination3, dc->_routingDestination4, - 0,0,0,0,0, // empty - 0,0,0,0,0,0,0, + 0, 0, 0, 0, 0, // empty + 0, 0, 0, 0, 0, 0, 0, dc->routingConnectionType, // Function (1=PG,2=OP,3=Step7Basic) - (dc->routingSlot + dc->routingRack*32), // Rack (Bit 7-5) and Slot (Bit 4-0) + (dc->routingSlot + dc->routingRack * 32), // Rack (Bit 7-5) and Slot (Bit 4-0) // 0 for slot = let select the plc itself the correct slotnumber 0xC0, // Parameter requested TPDU-Size 1, // Length of this parameter 0xa, // requested TPDU-Size 8=256 Bytes, 9=512 Bytes , a=1024 Bytes - }; - - uc b243[]={ - 0x11,0xE0,0x00, - 0x00,0x00,0x01,0x00, - 0xC1,2,'M','W', - 0xC2,2,'M','W', - 0xC0,1,0xa, }; - PDU p1; - success=0; - retries=0; + uc b243[] = { + 0x11, 0xE0, 0x00, + 0x00, 0x00, 0x01, 0x00, + 0xC1, 2, 'M', 'W', + 0xC2, 2, 'M', 'W', + 0xC0, 1, 0xa, + }; + + PDU p1; + success = 0; + retries = 0; - if (dc->iface->protocol==daveProtoISOTCP243) { - memcpy(dc->msgOut+4, b243, sizeof(b243)); - } else if (dc->iface->protocol==daveProtoISOTCP && !dc->routing) { - memcpy(dc->msgOut+4, b4, sizeof(b4)); + if (dc->iface->protocol == daveProtoISOTCP243) { + memcpy(dc->msgOut + 4, b243, sizeof(b243)); + } + else if (dc->iface->protocol == daveProtoISOTCP && !dc->routing) { + memcpy(dc->msgOut + 4, b4, sizeof(b4)); //printf("******** do inc %d\n",a); //dc->msgOut[17]=dc->rack+1; //dc->msgOut[18]=dc->slot; //dc->msgOut[18]=dc->slot + dc->rack * 32 ; - } else { - memcpy(dc->msgOut+4, b4R, sizeof(b4R)); // with routing over MPI + } + else { + memcpy(dc->msgOut + 4, b4R, sizeof(b4R)); // with routing over MPI //dc->msgOut[17]=dc->rack+1; // this is probably wrong //dc->msgOut[18]=dc->slot; //dc->msgOut[18]=dc->slot + dc->rack * 32 ; - } + } if (!dc->routing) - _daveSendISOPacket(dc, sizeof(b4)); + _daveSendISOPacket(dc, sizeof(b4)); else - _daveSendISOPacket(dc, sizeof(b4R)); + _daveSendISOPacket(dc, sizeof(b4R)); - do { - res=_daveReadISOPacket(dc->iface,dc->msgIn); + do { + res = _daveReadISOPacket(dc->iface, dc->msgIn); if (daveDebug & daveDebugConnect) { - LOG2("%s daveConnectPLC() step 1. ", dc->iface->name); + LOG2("%s daveConnectPLC() step 1. ", dc->iface->name); _daveDump("got packet: ", dc->msgIn, res); } - if ((res==22 && !dc->routing) || ((res==48 || res==74) && dc->routing)) { - success=1; - for (i=6;imsgIn[i]==0xc0) { - dc->TPDUsize=128 << (dc->msgIn[i+2]-7); + if ((res == 22 && !dc->routing) || ((res == 48 || res == 74) && dc->routing)) { + success = 1; + for (i = 6; i < res; i++) { + if (dc->msgIn[i] == 0xc0) { + dc->TPDUsize = 128 << (dc->msgIn[i + 2] - 7); if (daveDebug & daveDebugConnect) { - LOG3("TPDU len %d = %d\n",dc->msgIn[i+2],dc->TPDUsize); + LOG3("TPDU len %d = %d\n", dc->msgIn[i + 2], dc->TPDUsize); } } } - } else { + } + else { if (daveDebug & daveDebugPrintErrors){ - LOG2("%s error in daveConnectPLC() step 1. retrying...", dc->iface->name); - } + LOG2("%s error in daveConnectPLC() step 1. retrying...", dc->iface->name); + } } retries++; - } while ((success==0)&&(retries<3)); - if (success==0) return -1; + } while ((success == 0) && (retries < 3)); + if (success == 0) return -1; - retries=0; + retries = 0; do { - res= _daveNegPDUlengthRequest(dc, &p1); - if (res==0) { + res = _daveNegPDUlengthRequest(dc, &p1); + if (res == 0) { return res; - } else { + } + else { if (daveDebug & daveDebugPrintErrors){ - LOG2("%s error in daveConnectPLC() step 1. retrying...\n", dc->iface->name); - } + LOG2("%s error in daveConnectPLC() step 1. retrying...\n", dc->iface->name); + } } retries++; - } while (retries<3); + } while (retries < 3); return -1; } #endif /* -Changes: +Changes: 07/19/04 removed unused vars. */ /* -Changes: +Changes: 07/19/04 added return values in daveInitStep and daveSendWithPrefix2. 09/09/04 applied patch for variable Profibus speed from Andrew Rostovtsew. */ @@ -4541,41 +4643,41 @@ int DECL2 _daveConnectPLCTCP(daveConnection * dc) { #define tmo_normalPPI 140000 void DECL2 _daveSendLength(daveInterface * di, int len) { - uc c[]={104,0,0,104}; - c[1]=len; - c[2]=len; + uc c[] = { 104, 0, 0, 104 }; + c[1] = len; + c[2] = len; di->ifwrite(di, (char *)c, 4); - if ((daveDebug & daveDebugByte)!=0) { + if ((daveDebug & daveDebugByte) != 0) { _daveDump("I send", c, 4); - } + } } void DECL2 _daveSendIt(daveInterface * di, uc * b, int size) { int i; us sum = 0; - for (i=0;iifwrite(di, (char*)b, size); - if ((daveDebug & daveDebugByte)!=0) { - LOG2("send %d\n",i); + if ((daveDebug & daveDebugByte) != 0) { + LOG2("send %d\n", i); _daveDump("I send", b, size); - } + } } -void DECL2 _daveSendRequestData(daveConnection * dc,int alt) { - uc b[]={DLE,0,0,0x5C,0,0}; - b[1]=dc->MPIAdr; - b[2]=dc->iface->localMPI; - if(alt) b[3]=0x7c; else b[3]=0x5c; +void DECL2 _daveSendRequestData(daveConnection * dc, int alt) { + uc b[] = { DLE, 0, 0, 0x5C, 0, 0 }; + b[1] = dc->MPIAdr; + b[2] = dc->iface->localMPI; + if (alt) b[3] = 0x7c; else b[3] = 0x5c; dc->iface->ifwrite(dc->iface, (char*)b, 1); //cs: - _daveSendIt(dc->iface, b+1, sizeof(b)-3); + _daveSendIt(dc->iface, b + 1, sizeof(b) - 3); } @@ -4585,104 +4687,106 @@ int DECL2 _daveGetResponsePPI(daveConnection *dc) { int res, expectedLen, expectingLength, i, sum, alt; uc * b; res = 0; - expectedLen=6; - expectingLength=1; - b=dc->msgIn; - alt=1; - while ((expectingLength)||(resiface->ifread(dc->iface, dc->msgIn+res, 1); + expectedLen = 6; + expectingLength = 1; + b = dc->msgIn; + alt = 1; + while ((expectingLength) || (res < expectedLen)) { + i = dc->iface->ifread(dc->iface, dc->msgIn + res, 1); res += i; - if ((daveDebug & daveDebugByte)!=0) { - LOG3("i:%d res:%d\n",i,res); + if ((daveDebug & daveDebugByte) != 0) { + LOG3("i:%d res:%d\n", i, res); FLUSH; - } + } if (i == 0) { return daveResTimeout; - } else { - if ( (expectingLength) && (res==1) && (b[0] == 0xE5)) { - if(alt) { - _daveSendRequestData(dc,alt); - res=0; - alt=0; - } else { - _daveSendRequestData(dc,alt); - res=0; - alt=1; + } + else { + if ((expectingLength) && (res == 1) && (b[0] == 0xE5)) { + if (alt) { + _daveSendRequestData(dc, alt); + res = 0; + alt = 0; + } + else { + _daveSendRequestData(dc, alt); + res = 0; + alt = 1; } } - if ( (expectingLength) && (res>=4) && (b[0] == b[3]) && (b[1] == b[2]) ) { - expectedLen=b[1]+6; - expectingLength=0; + if ((expectingLength) && (res >= 4) && (b[0] == b[3]) && (b[1] == b[2])) { + expectedLen = b[1] + 6; + expectingLength = 0; } - } + } } - if ((daveDebug & daveDebugByte)!=0) { - LOG2("res %d testing lastChar\n",res); - } - if (b[res-1]!=SYN) { + if ((daveDebug & daveDebugByte) != 0) { + LOG2("res %d testing lastChar\n", res); + } + if (b[res - 1] != SYN) { LOG1("block format error\n"); return 1024; } - if ((daveDebug & daveDebugByte)!=0) { + if ((daveDebug & daveDebugByte) != 0) { LOG1("testing check sum\n"); - } - sum=0; - for (i=4; imsgOut[0]=dc->MPIAdr; /* address ? */ - dc->msgOut[1]=dc->iface->localMPI; - dc->msgOut[2]=108; - len=3+p1->hlen+p1->plen+p1->dlen; /* The 3 fix bytes + all parts of PDU */ - _daveSendLength(dc->iface, len); +} + +int DECL2 _daveExchangePPI(daveConnection * dc, PDU * p1) { + int i, res = 0, len; + dc->msgOut[0] = dc->MPIAdr; /* address ? */ + dc->msgOut[1] = dc->iface->localMPI; + dc->msgOut[2] = 108; + len = 3 + p1->hlen + p1->plen + p1->dlen; /* The 3 fix bytes + all parts of PDU */ + _daveSendLength(dc->iface, len); _daveSendIt(dc->iface, dc->msgOut, len); - i = dc->iface->ifread(dc->iface, dc->msgIn+res, 1); - if ((daveDebug & daveDebugByte)!=0) { - LOG3("i:%d res:%d\n",i,res); - _daveDump("got",dc->msgIn,i); // 5.1.2004 - } + i = dc->iface->ifread(dc->iface, dc->msgIn + res, 1); + if ((daveDebug & daveDebugByte) != 0) { + LOG3("i:%d res:%d\n", i, res); + _daveDump("got", dc->msgIn, i); // 5.1.2004 + } if (i == 0) { seconds++; - _daveSendLength(dc->iface, len); + _daveSendLength(dc->iface, len); _daveSendIt(dc->iface, dc->msgOut, len); - i = dc->iface->ifread(dc->iface, dc->msgIn+res, 1); + i = dc->iface->ifread(dc->iface, dc->msgIn + res, 1); if (i == 0) { thirds++; - _daveSendLength(dc->iface, len); + _daveSendLength(dc->iface, len); _daveSendIt(dc->iface, dc->msgOut, len); - i = dc->iface->ifread(dc->iface, dc->msgIn+res, 1); + i = dc->iface->ifread(dc->iface, dc->msgIn + res, 1); if (i == 0) { LOG1("timeout in _daveExchangePPI!\n"); FLUSH; return daveResTimeout; - } - } + } + } } - _daveSendRequestData(dc,0); + _daveSendRequestData(dc, 0); return _daveGetResponsePPI(dc); -} +} int DECL2 _daveConnectPLCPPI(daveConnection * dc) { PDU p; - return _daveNegPDUlengthRequest(dc,&p); -} + return _daveNegPDUlengthRequest(dc, &p); +} -/* +/* "generic" functions calling the protocol specific ones (or the dummies) */ int DECL2 daveInitAdapter(daveInterface * di) { @@ -4703,18 +4807,18 @@ int DECL2 daveDisconnectAdapter(daveInterface * di) { int DECL2 _daveExchange(daveConnection * dc, PDU *p) { int res; - if ((p->header[4]==0)&&(p->header[5]==0)) { /* do not number already numbered PDUs 12/10/04 */ + if ((p->header[4] == 0) && (p->header[5] == 0)) { /* do not number already numbered PDUs 12/10/04 */ dc->PDUnumber++; if (daveDebug & daveDebugExchange) { LOG2("_daveExchange PDU number: %d\n", dc->PDUnumber); } - p->header[5]=dc->PDUnumber % 256; // test! - p->header[4]=dc->PDUnumber / 256; // test! + p->header[5] = dc->PDUnumber % 256; // test! + p->header[4] = dc->PDUnumber / 256; // test! + } + res = dc->iface->exchange(dc, p); + if (((daveDebug & daveDebugExchange) != 0) || ((daveDebug & daveDebugErrorReporting) != 0)) { + LOG2("result of exchange: %d\n", res); } - res=dc->iface->exchange(dc, p); - if (((daveDebug & daveDebugExchange)!=0) ||((daveDebug & daveDebugErrorReporting)!=0)) { - LOG2("result of exchange: %d\n",res); - } return res; } @@ -4732,7 +4836,7 @@ int DECL2 daveGetResponse(daveConnection * dc) { /** Newer conversion routines. As the terms WORD, INT, INTEGER etc have different meanings -for users of different programming languages and compilers, I choose to provide a new +for users of different programming languages and compilers, I choose to provide a new set of conversion routines named according to the bit length of the value used. The 'U' or 'S' stands for unsigned or signed. **/ @@ -4741,7 +4845,7 @@ Get a value from the position b points to. B is typically a pointer to a buffer been filled with daveReadBytes: */ int DECL2 daveGetS8from(uc *b) { - char* p=(char*)b; + char* p = (char*)b; return *p; } @@ -4754,14 +4858,14 @@ int DECL2 daveGetS16from(uc *b) { short a; uc b[2]; } u; -#ifdef DAVE_LITTLE_ENDIAN - u.b[1]=*b; +#ifdef DAVE_LITTLE_ENDIAN + u.b[1] = *b; b++; - u.b[0]=*b; + u.b[0] = *b; #else - u.b[0]=*b; + u.b[0] = *b; b++; - u.b[1]=*b; + u.b[1] = *b; #endif return u.a; } @@ -4771,15 +4875,15 @@ int DECL2 daveGetU16from(uc *b) { unsigned short a; uc b[2]; } u; -#ifdef DAVE_LITTLE_ENDIAN - u.b[1]=*b; +#ifdef DAVE_LITTLE_ENDIAN + u.b[1] = *b; b++; - u.b[0]=*b; + u.b[0] = *b; #else - u.b[0]=*b; + u.b[0] = *b; b++; - u.b[1]=*b; -#endif + u.b[1] = *b; +#endif return u.a; } @@ -4789,21 +4893,21 @@ int DECL2 daveGetS32from(uc *b) { uc b[4]; } u; #ifdef DAVE_LITTLE_ENDIAN - u.b[3]=*b; + u.b[3] = *b; b++; - u.b[2]=*b; + u.b[2] = *b; b++; - u.b[1]=*b; + u.b[1] = *b; b++; - u.b[0]=*b; + u.b[0] = *b; #else - u.b[0]=*b; + u.b[0] = *b; b++; - u.b[1]=*b; + u.b[1] = *b; b++; - u.b[2]=*b; + u.b[2] = *b; b++; - u.b[3]=*b; + u.b[3] = *b; #endif return u.a; } @@ -4814,22 +4918,22 @@ unsigned int DECL2 daveGetU32from(uc *b) { uc b[4]; } u; #ifdef DAVE_LITTLE_ENDIAN - u.b[3]=*b; + u.b[3] = *b; b++; - u.b[2]=*b; + u.b[2] = *b; b++; - u.b[1]=*b; + u.b[1] = *b; b++; - u.b[0]=*b; + u.b[0] = *b; #else - u.b[0]=*b; + u.b[0] = *b; b++; - u.b[1]=*b; + u.b[1] = *b; b++; - u.b[2]=*b; + u.b[2] = *b; b++; - u.b[3]=*b; -#endif + u.b[3] = *b; +#endif return u.a; } @@ -4839,22 +4943,22 @@ float DECL2 daveGetFloatfrom(uc *b) { uc b[4]; } u; #ifdef DAVE_LITTLE_ENDIAN - u.b[3]=*b; + u.b[3] = *b; b++; - u.b[2]=*b; + u.b[2] = *b; b++; - u.b[1]=*b; + u.b[1] = *b; b++; - u.b[0]=*b; + u.b[0] = *b; #else - u.b[0]=*b; + u.b[0] = *b; b++; - u.b[1]=*b; + u.b[1] = *b; b++; - u.b[2]=*b; + u.b[2] = *b; b++; - u.b[3]=*b; -#endif + u.b[3] = *b; +#endif return u.a; } @@ -4866,10 +4970,10 @@ following this value. int DECL2 daveGetS8(daveConnection * dc) { char * p; #ifdef DEBUG_CALLS - LOG2("daveGetS8(dc:%p)\n",dc); + LOG2("daveGetS8(dc:%p)\n", dc); FLUSH; -#endif - p=(char *) dc->resultPointer; +#endif + p = (char *)dc->resultPointer; dc->resultPointer++; return *p; } @@ -4877,13 +4981,13 @@ int DECL2 daveGetS8(daveConnection * dc) { int DECL2 daveGetU8(daveConnection * dc) { uc * p; #ifdef DEBUG_CALLS - LOG2("daveGetU8(dc:%p)\n",dc); + LOG2("daveGetU8(dc:%p)\n", dc); FLUSH; -#endif - p=dc->resultPointer; +#endif + p = dc->resultPointer; dc->resultPointer++; return *p; -} +} int DECL2 daveGetS16(daveConnection * dc) { union { @@ -4891,17 +4995,17 @@ int DECL2 daveGetS16(daveConnection * dc) { uc b[2]; } u; #ifdef DEBUG_CALLS - LOG2("daveGetS16(dc:%p)\n",dc); + LOG2("daveGetS16(dc:%p)\n", dc); FLUSH; -#endif +#endif #ifdef DAVE_LITTLE_ENDIAN - u.b[1]=*(dc->resultPointer); + u.b[1] = *(dc->resultPointer); dc->resultPointer++; - u.b[0]=*(dc->resultPointer); + u.b[0] = *(dc->resultPointer); #else - u.b[0]=*(dc->resultPointer); + u.b[0] = *(dc->resultPointer); dc->resultPointer++; - u.b[1]=*(dc->resultPointer); + u.b[1] = *(dc->resultPointer); #endif dc->resultPointer++; return u.a; @@ -4913,18 +5017,18 @@ int DECL2 daveGetU16(daveConnection * dc) { uc b[2]; } u; #ifdef DEBUG_CALLS - LOG2("daveGetU16(dc:%p)\n",dc); + LOG2("daveGetU16(dc:%p)\n", dc); FLUSH; -#endif +#endif #ifdef DAVE_LITTLE_ENDIAN - u.b[1]=*(dc->resultPointer); + u.b[1] = *(dc->resultPointer); dc->resultPointer++; - u.b[0]=*(dc->resultPointer); + u.b[0] = *(dc->resultPointer); #else - u.b[0]=*(dc->resultPointer); + u.b[0] = *(dc->resultPointer); dc->resultPointer++; - u.b[1]=*(dc->resultPointer); -#endif + u.b[1] = *(dc->resultPointer); +#endif dc->resultPointer++; return u.a; } @@ -4935,25 +5039,25 @@ int DECL2 daveGetS32(daveConnection * dc) { uc b[4]; } u; #ifdef DEBUG_CALLS - LOG2("daveGetS32(dc:%p)\n",dc); + LOG2("daveGetS32(dc:%p)\n", dc); FLUSH; -#endif +#endif #ifdef DAVE_LITTLE_ENDIAN - u.b[3]=*(dc->resultPointer); + u.b[3] = *(dc->resultPointer); dc->resultPointer++; - u.b[2]=*(dc->resultPointer); + u.b[2] = *(dc->resultPointer); dc->resultPointer++; - u.b[1]=*(dc->resultPointer); + u.b[1] = *(dc->resultPointer); dc->resultPointer++; - u.b[0]=*(dc->resultPointer); + u.b[0] = *(dc->resultPointer); #else - u.b[0]=*(dc->resultPointer); + u.b[0] = *(dc->resultPointer); dc->resultPointer++; - u.b[1]=*(dc->resultPointer); + u.b[1] = *(dc->resultPointer); dc->resultPointer++; - u.b[2]=*(dc->resultPointer); + u.b[2] = *(dc->resultPointer); dc->resultPointer++; - u.b[3]=*(dc->resultPointer); + u.b[3] = *(dc->resultPointer); #endif dc->resultPointer++; return u.a; @@ -4965,27 +5069,27 @@ unsigned int DECL2 daveGetU32(daveConnection * dc) { uc b[4]; } u; #ifdef DEBUG_CALLS - LOG2("daveGetU32(dc:%p)\n",dc); + LOG2("daveGetU32(dc:%p)\n", dc); FLUSH; -#endif +#endif #ifdef DAVE_LITTLE_ENDIAN - u.b[3]=*(dc->resultPointer); + u.b[3] = *(dc->resultPointer); dc->resultPointer++; - u.b[2]=*(dc->resultPointer); + u.b[2] = *(dc->resultPointer); dc->resultPointer++; - u.b[1]=*(dc->resultPointer); + u.b[1] = *(dc->resultPointer); dc->resultPointer++; - u.b[0]=*(dc->resultPointer); + u.b[0] = *(dc->resultPointer); #else - u.b[0]=*(dc->resultPointer); + u.b[0] = *(dc->resultPointer); dc->resultPointer++; - u.b[1]=*(dc->resultPointer); + u.b[1] = *(dc->resultPointer); dc->resultPointer++; - u.b[2]=*(dc->resultPointer); + u.b[2] = *(dc->resultPointer); dc->resultPointer++; - u.b[3]=*(dc->resultPointer); -#endif + u.b[3] = *(dc->resultPointer); +#endif dc->resultPointer++; return u.a; } @@ -4993,14 +5097,14 @@ unsigned int DECL2 daveGetU32(daveConnection * dc) { Get a value from a given position in the last result read on the connection dc. */ int DECL2 daveGetS8At(daveConnection * dc, int pos) { - char * p=(char *)(dc->_resultPointer); - p+=pos; + char * p = (char *)(dc->_resultPointer); + p += pos; return *p; } int DECL2 daveGetU8At(daveConnection * dc, int pos) { - uc * p=(uc *)(dc->_resultPointer); - p+=pos; + uc * p = (uc *)(dc->_resultPointer); + p += pos; return *p; } @@ -5009,16 +5113,16 @@ int DECL2 daveGetS16At(daveConnection * dc, int pos) { short a; uc b[2]; } u; - uc * p=(uc *)(dc->_resultPointer); - p+=pos; + uc * p = (uc *)(dc->_resultPointer); + p += pos; #ifdef DAVE_LITTLE_ENDIAN - u.b[1]=*p; + u.b[1] = *p; p++; - u.b[0]=*p; + u.b[0] = *p; #else - u.b[0]=*p; + u.b[0] = *p; p++; - u.b[1]=*p; + u.b[1] = *p; #endif return u.a; } @@ -5028,17 +5132,17 @@ int DECL2 daveGetU16At(daveConnection * dc, int pos) { unsigned short a; uc b[2]; } u; - uc * p=(uc *)(dc->_resultPointer); - p+=pos; + uc * p = (uc *)(dc->_resultPointer); + p += pos; #ifdef DAVE_LITTLE_ENDIAN - u.b[1]=*p; + u.b[1] = *p; p++; - u.b[0]=*p; + u.b[0] = *p; #else - u.b[0]=*p; + u.b[0] = *p; p++; - u.b[1]=*p; -#endif + u.b[1] = *p; +#endif return u.a; } @@ -5047,24 +5151,24 @@ int DECL2 daveGetS32At(daveConnection * dc, int pos) { int a; uc b[4]; } u; - uc * p=dc->_resultPointer; - p+=pos; -#ifdef DAVE_LITTLE_ENDIAN - u.b[3]=*p; + uc * p = dc->_resultPointer; + p += pos; +#ifdef DAVE_LITTLE_ENDIAN + u.b[3] = *p; p++; - u.b[2]=*p; + u.b[2] = *p; p++; - u.b[1]=*p; + u.b[1] = *p; p++; - u.b[0]=*p; + u.b[0] = *p; #else - u.b[0]=*p; + u.b[0] = *p; p++; - u.b[1]=*p; + u.b[1] = *p; p++; - u.b[2]=*p; + u.b[2] = *p; p++; - u.b[3]=*p; + u.b[3] = *p; #endif return u.a; } @@ -5074,50 +5178,50 @@ unsigned int DECL2 daveGetU32At(daveConnection * dc, int pos) { unsigned int a; uc b[4]; } u; - uc * p=(uc *)(dc->_resultPointer); - p+=pos; -#ifdef DAVE_LITTLE_ENDIAN - u.b[3]=*p; + uc * p = (uc *)(dc->_resultPointer); + p += pos; +#ifdef DAVE_LITTLE_ENDIAN + u.b[3] = *p; p++; - u.b[2]=*p; + u.b[2] = *p; p++; - u.b[1]=*p; + u.b[1] = *p; p++; - u.b[0]=*p; + u.b[0] = *p; #else - u.b[0]=*p; + u.b[0] = *p; p++; - u.b[1]=*p; + u.b[1] = *p; p++; - u.b[2]=*p; + u.b[2] = *p; p++; - u.b[3]=*p; -#endif + u.b[3] = *p; +#endif return u.a; } /* put one byte into buffer b: */ -uc * DECL2 davePut8(uc *b,int v) { +uc * DECL2 davePut8(uc *b, int v) { *b = v & 0xff; b++; return b; } -uc * DECL2 davePut16(uc *b,int v) { +uc * DECL2 davePut16(uc *b, int v) { union { short a; uc b[2]; } u; - u.a=v; -#ifdef DAVE_LITTLE_ENDIAN - *b=u.b[1]; + u.a = v; +#ifdef DAVE_LITTLE_ENDIAN + *b = u.b[1]; b++; - *b=u.b[0]; -#else - *b=u.b[0]; + *b = u.b[0]; +#else + *b = u.b[0]; b++; - *b=u.b[1]; + *b = u.b[1]; #endif b++; return b; @@ -5128,50 +5232,50 @@ uc * DECL2 davePut32(uc *b, int v) { int a; uc b[2]; } u; - u.a=v; -#ifdef DAVE_LITTLE_ENDIAN - *b=u.b[3]; + u.a = v; +#ifdef DAVE_LITTLE_ENDIAN + *b = u.b[3]; b++; - *b=u.b[2]; + *b = u.b[2]; b++; - *b=u.b[1]; + *b = u.b[1]; b++; - *b=u.b[0]; -#else - *b=u.b[0]; + *b = u.b[0]; +#else + *b = u.b[0]; b++; - *b=u.b[1]; + *b = u.b[1]; b++; - *b=u.b[2]; + *b = u.b[2]; b++; - *b=u.b[3]; -#endif + *b = u.b[3]; +#endif b++; return b; } -uc * DECL2 davePutFloat(uc *b,float v) { +uc * DECL2 davePutFloat(uc *b, float v) { union { float a; uc b[2]; } u; - u.a=v; -#ifdef DAVE_LITTLE_ENDIAN - *b=u.b[3]; + u.a = v; +#ifdef DAVE_LITTLE_ENDIAN + *b = u.b[3]; b++; - *b=u.b[2]; + *b = u.b[2]; b++; - *b=u.b[1]; + *b = u.b[1]; b++; - *b=u.b[0]; -#else - *b=u.b[0]; + *b = u.b[0]; +#else + *b = u.b[0]; b++; - *b=u.b[1]; + *b = u.b[1]; b++; - *b=u.b[2]; + *b = u.b[2]; b++; - *b=u.b[3]; + *b = u.b[3]; #endif b++; return b; @@ -5182,9 +5286,9 @@ void DECL2 davePut8At(uc *b, int pos, int v) { short a; uc b[2]; } u; - u.a=v; - b+=pos; - *b=v & 0xff; + u.a = v; + b += pos; + *b = v & 0xff; } void DECL2 davePut16At(uc *b, int pos, int v) { @@ -5192,17 +5296,17 @@ void DECL2 davePut16At(uc *b, int pos, int v) { short a; uc b[2]; } u; - u.a=v; - b+=pos; -#ifdef DAVE_LITTLE_ENDIAN - *b=u.b[1]; + u.a = v; + b += pos; +#ifdef DAVE_LITTLE_ENDIAN + *b = u.b[1]; b++; - *b=u.b[0]; -#else - *b=u.b[0]; + *b = u.b[0]; +#else + *b = u.b[0]; b++; - *b=u.b[1]; -#endif + *b = u.b[1]; +#endif } void DECL2 davePut32At(uc *b, int pos, int v) { @@ -5210,110 +5314,110 @@ void DECL2 davePut32At(uc *b, int pos, int v) { int a; uc b[2]; } u; - u.a=v; - b+=pos; -#ifdef DAVE_LITTLE_ENDIAN - *b=u.b[3]; + u.a = v; + b += pos; +#ifdef DAVE_LITTLE_ENDIAN + *b = u.b[3]; b++; - *b=u.b[2]; + *b = u.b[2]; b++; - *b=u.b[1]; + *b = u.b[1]; b++; - *b=u.b[0]; -#else - *b=u.b[0]; + *b = u.b[0]; +#else + *b = u.b[0]; b++; - *b=u.b[1]; + *b = u.b[1]; b++; - *b=u.b[2]; + *b = u.b[2]; b++; - *b=u.b[3]; -#endif + *b = u.b[3]; +#endif } -void DECL2 davePutFloatAt(uc *b, int pos,float v) { +void DECL2 davePutFloatAt(uc *b, int pos, float v) { union { float a; uc b[2]; } u; - u.a=v; - b+=pos; -#ifdef DAVE_LITTLE_ENDIAN - *b=u.b[3]; + u.a = v; + b += pos; +#ifdef DAVE_LITTLE_ENDIAN + *b = u.b[3]; b++; - *b=u.b[2]; + *b = u.b[2]; b++; - *b=u.b[1]; + *b = u.b[1]; b++; - *b=u.b[0]; -#else - *b=u.b[0]; + *b = u.b[0]; +#else + *b = u.b[0]; b++; - *b=u.b[1]; + *b = u.b[1]; b++; - *b=u.b[2]; + *b = u.b[2]; b++; - *b=u.b[3]; -#endif + *b = u.b[3]; +#endif } /* "passive mode" functions. Needed to "simulate" an S7 PLC. */ -userReadFunc readCallBack=NULL; -userWriteFunc writeCallBack=NULL; +userReadFunc readCallBack = NULL; +userWriteFunc writeCallBack = NULL; void DECL2 _daveConstructReadResponse(PDU * p) { - uc pa[]={4,1}; - uc da[]={0xFF,4,0,0}; - _daveInitPDUheader(p,3); + uc pa[] = { 4, 1 }; + uc da[] = { 0xFF, 4, 0, 0 }; + _daveInitPDUheader(p, 3); _daveAddParam(p, pa, sizeof(pa)); - _daveAddData(p, da, sizeof(da)); + _daveAddData(p, da, sizeof(da)); } void DECL2 _daveConstructBadReadResponse(PDU * p) { - uc pa[]={4,1}; - uc da[]={0x0A,0,0,0}; - _daveInitPDUheader(p,3); + uc pa[] = { 4, 1 }; + uc da[] = { 0x0A, 0, 0, 0 }; + _daveInitPDUheader(p, 3); _daveAddParam(p, pa, sizeof(pa)); - _daveAddData(p, da, sizeof(da)); + _daveAddData(p, da, sizeof(da)); } void DECL2 _daveConstructWriteResponse(PDU * p) { - uc pa[]={5,1}; - uc da[]={0xFF}; - _daveInitPDUheader(p,3); + uc pa[] = { 5, 1 }; + uc da[] = { 0xFF }; + _daveInitPDUheader(p, 3); _daveAddParam(p, pa, sizeof(pa)); - _daveAddData(p, da, sizeof(da)); + _daveAddData(p, da, sizeof(da)); } -void DECL2 _daveHandleRead(PDU * p1,PDU * p2) { +void DECL2 _daveHandleRead(PDU * p1, PDU * p2) { int result; - uc * userBytes=NULL; //is this really better than reading from a dangling pointer? - int bytes=0x100*p1->param[6]+p1->param[7]; - int DBnumber=0x100*p1->param[8]+p1->param[9]; - int area=p1->param[10]; - int start=0x10000*p1->param[11]+0x100*p1->param[12]+p1->param[13]; + uc * userBytes = NULL; //is this really better than reading from a dangling pointer? + int bytes = 0x100 * p1->param[6] + p1->param[7]; + int DBnumber = 0x100 * p1->param[8] + p1->param[9]; + int area = p1->param[10]; + int start = 0x10000 * p1->param[11] + 0x100 * p1->param[12] + p1->param[13]; LOG5("read %d bytes from %s %d beginning at %d.\n", - bytes, daveAreaName(area),DBnumber,start); - if (readCallBack) - userBytes=readCallBack(area, DBnumber,start, bytes, &result); - _daveConstructReadResponse(p2); + bytes, daveAreaName(area), DBnumber, start); + if (readCallBack) + userBytes = readCallBack(area, DBnumber, start, bytes, &result); + _daveConstructReadResponse(p2); _daveAddValue(p2, userBytes, bytes); _daveDumpPDU(p2); }; -void DECL2 _daveHandleWrite(PDU * p1,PDU * p2) { - int result,bytes=0x100*p1->param[6]+p1->param[7]; - int DBnumber=0x100*p1->param[8]+p1->param[9]; - int area=p1->param[10]; - int start=0x10000*p1->param[11]+0x100*p1->param[12]+p1->param[13]; +void DECL2 _daveHandleWrite(PDU * p1, PDU * p2) { + int result, bytes = 0x100 * p1->param[6] + p1->param[7]; + int DBnumber = 0x100 * p1->param[8] + p1->param[9]; + int area = p1->param[10]; + int start = 0x10000 * p1->param[11] + 0x100 * p1->param[12] + p1->param[13]; LOG5("write %d bytes to %s %d beginning at %d.\n", - bytes, daveAreaName(area),DBnumber,start); - if (writeCallBack) - writeCallBack(area, DBnumber,start, bytes, &result, p1->data+4); + bytes, daveAreaName(area), DBnumber, start); + if (writeCallBack) + writeCallBack(area, DBnumber, start, bytes, &result, p1->data + 4); LOG1("after callback\n"); FLUSH; - _daveConstructWriteResponse(p2); + _daveConstructWriteResponse(p2); LOG1("after ConstructWriteResponse()\n"); FLUSH; _daveDumpPDU(p2); @@ -5332,50 +5436,50 @@ int DECL2 _daveWriteIBH(daveInterface * di, uc * buffer, int len) { //cs: let it int res; if (daveDebug & daveDebugByte) { _daveDump("writeIBH: ", buffer, len); - } -#ifdef HAVE_SELECT - res=write(di->fd.wfd, buffer, len); -#endif + } +#ifdef HAVE_SELECT + res = write(di->fd.wfd, buffer, len); +#endif #ifdef BCCWIN - // res=send((SOCKET)(di->fd.wfd), buffer, ((len+1)/2)*2, 0); - res=send((SOCKET)(di->fd.wfd), buffer, len, 0); + // res=send((SOCKET)(di->fd.wfd), buffer, ((len+1)/2)*2, 0); + res = send((SOCKET)(di->fd.wfd), buffer, len, 0); #endif return res; -} +} -int DECL2 _davePackPDU(daveConnection * dc,PDU *p) { +int DECL2 _davePackPDU(daveConnection * dc, PDU *p) { IBHpacket * ibhp; - MPIheader * hm= (MPIheader*) (dc->msgOut+sizeof(IBHpacket)); // MPI headerPDU begins packet header - hm->MPI=dc->MPIAdr; - hm->localMPI=dc->iface->localMPI; - hm->src_conn=dc->ibhSrcConn; - hm->dst_conn=dc->ibhDstConn; - hm->len=2+p->hlen+p->plen+p->dlen; // set MPI length - hm->func=0xf1; // set MPI "function code" - hm->packetNumber=_daveIncMessageNumber(dc); - ibhp = (IBHpacket*) dc->msgOut; - ibhp->ch1=7; - ibhp->ch2=0xff; - ibhp->len=hm->len+5; - ibhp->packetNumber=dc->packetNumber; + MPIheader * hm = (MPIheader*)(dc->msgOut + sizeof(IBHpacket)); // MPI headerPDU begins packet header + hm->MPI = dc->MPIAdr; + hm->localMPI = dc->iface->localMPI; + hm->src_conn = dc->ibhSrcConn; + hm->dst_conn = dc->ibhDstConn; + hm->len = 2 + p->hlen + p->plen + p->dlen; // set MPI length + hm->func = 0xf1; // set MPI "function code" + hm->packetNumber = _daveIncMessageNumber(dc); + ibhp = (IBHpacket*)dc->msgOut; + ibhp->ch1 = 7; + ibhp->ch2 = 0xff; + ibhp->len = hm->len + 5; + ibhp->packetNumber = dc->packetNumber; dc->packetNumber++; - ibhp->rFlags=0x82; - ibhp->sFlags=0; + ibhp->rFlags = 0x82; + ibhp->sFlags = 0; return 0; -} +} -uc _MPIack[]={ - 0x07,0xff,0x08,0x05,0x00,0x00,0x82,0x00, 0x15,0x14,0x02,0x00,0x03,0xb0,0x01,0x00, +uc _MPIack[] = { + 0x07, 0xff, 0x08, 0x05, 0x00, 0x00, 0x82, 0x00, 0x15, 0x14, 0x02, 0x00, 0x03, 0xb0, 0x01, 0x00, }; void DECL2 _daveSendMPIAck_IBH(daveConnection*dc) { - _MPIack[15]=dc->msgIn[16]; - _MPIack[8]=dc->ibhSrcConn; - _MPIack[9]=dc->ibhDstConn; - _MPIack[10]=dc->MPIAdr; - _daveWriteIBH(dc->iface,_MPIack,sizeof(_MPIack)); + _MPIack[15] = dc->msgIn[16]; + _MPIack[8] = dc->ibhSrcConn; + _MPIack[9] = dc->ibhDstConn; + _MPIack[10] = dc->MPIAdr; + _daveWriteIBH(dc->iface, _MPIack, sizeof(_MPIack)); } /* @@ -5385,17 +5489,17 @@ void DECL2 _daveSendIBHNetAck(daveConnection * dc) { IBHpacket * p; uc ack[13]; memcpy(ack, dc->msgIn, sizeof(ack)); - p= (IBHpacket*) ack; - p->len=sizeof(ack)-sizeof(IBHpacket); - ack[11]=1; - ack[12]=9; - // LOG2("Sending net level ack for number: %d\n",p->packetNumber); - _daveWriteIBH(dc->iface, ack,sizeof(ack)); + p = (IBHpacket*)ack; + p->len = sizeof(ack) - sizeof(IBHpacket); + ack[11] = 1; + ack[12] = 9; + // LOG2("Sending net level ack for number: %d\n",p->packetNumber); + _daveWriteIBH(dc->iface, ack, sizeof(ack)); } -uc _MPIconnectResponse[]={ - 0xff,0x07,0x13,0x00,0x00,0x00,0xc2,0x02, 0x14,0x14,0x03,0x00,0x00,0x22,0x0c,0xd0, - 0x04,0x00,0x80,0x00,0x02,0x00,0x02,0x01, 0x00,0x01,0x00, +uc _MPIconnectResponse[] = { + 0xff, 0x07, 0x13, 0x00, 0x00, 0x00, 0xc2, 0x02, 0x14, 0x14, 0x03, 0x00, 0x00, 0x22, 0x0c, 0xd0, + 0x04, 0x00, 0x80, 0x00, 0x02, 0x00, 0x02, 0x01, 0x00, 0x01, 0x00, }; /* @@ -5407,113 +5511,115 @@ int DECL2 __daveAnalyze(daveConnection * dc) { MPIheader * pm; MPIheader2 * m2; PDU p1; -#ifdef passiveMode +#ifdef passiveMode PDU pr; -#endif +#endif uc resp[2000]; - // if (dc->AnswLen==0) return _davePtEmpty; - haveResp=0; + // if (dc->AnswLen==0) return _davePtEmpty; + haveResp = 0; - p= (IBHpacket*) dc->msgIn; - dc->needAckNumber=-1; // Assume no ack + p = (IBHpacket*)dc->msgIn; + dc->needAckNumber = -1; // Assume no ack if (daveDebug & daveDebugPacket){ - LOG2("Channel: %d\n",p->ch1); - LOG2("Channel: %d\n",p->ch2); - LOG2("Length: %d\n",p->len); - LOG2("Number: %d\n",p->packetNumber); - LOG3("sFlags: %04x rFlags:%04x\n",p->sFlags,p->rFlags); - } - if (p->rFlags==0x82) { - pm= (MPIheader*) (dc->msgIn+sizeof(IBHpacket)); - if (daveDebug & daveDebugMPI){ - LOG2("srcconn: %d\n",pm->src_conn); - LOG2("dstconn: %d\n",pm->dst_conn); + LOG2("Channel: %d\n", p->ch1); + LOG2("Channel: %d\n", p->ch2); + LOG2("Length: %d\n", p->len); + LOG2("Number: %d\n", p->packetNumber); + LOG3("sFlags: %04x rFlags:%04x\n", p->sFlags, p->rFlags); + } + if (p->rFlags == 0x82) { + pm = (MPIheader*)(dc->msgIn + sizeof(IBHpacket)); + if (daveDebug & daveDebugMPI){ + LOG2("srcconn: %d\n", pm->src_conn); + LOG2("dstconn: %d\n", pm->dst_conn); LOG2("MPI: %d\n",pm->MPI); - LOG2("MPI len: %d\n",pm->len); - LOG2("MPI func:%d\n",pm->func); - } - if (pm->func==0xf1) { - if (daveDebug & daveDebugMPI) - LOG2("MPI packet number: %d needs ackknowledge\n",pm->packetNumber); - dc->needAckNumber=pm->packetNumber; + LOG2("MPI len: %d\n", pm->len); + LOG2("MPI func:%d\n", pm->func); + } + if (pm->func == 0xf1) { + if (daveDebug & daveDebugMPI) + LOG2("MPI packet number: %d needs ackknowledge\n", pm->packetNumber); + dc->needAckNumber = pm->packetNumber; _daveSetupReceivedPDU(dc, &p1); #ifdef passiveMode - // construct response: - pr.header=resp+sizeof(IBHpacket)+sizeof(MPIheader2); -#endif - p2= (IBHpacket*) resp; - p2->ch1=p->ch2; - p2->ch2=p->ch1; - p2->packetNumber=0; - p2->sFlags=0; - p2->rFlags=0x2c2; - - m2= (MPIheader2*) (resp+sizeof(IBHpacket)); - m2->src_conn=pm->src_conn; - m2->dst_conn=pm->dst_conn; - m2->MPI=pm->MPI; - m2->xxx1=0; - m2->xxx2=0; - m2->xx22=0x22; - if (p1.param[0]==daveFuncRead) { -#ifdef passiveMode - _daveHandleRead(&p1,&pr); - haveResp=1; - m2->len=pr.hlen+pr.plen+pr.dlen+2; + // construct response: + pr.header = resp + sizeof(IBHpacket) + sizeof(MPIheader2); #endif - p2->len=m2->len+7; - } else if (p1.param[0]==daveFuncWrite) { + p2 = (IBHpacket*)resp; + p2->ch1 = p->ch2; + p2->ch2 = p->ch1; + p2->packetNumber = 0; + p2->sFlags = 0; + p2->rFlags = 0x2c2; + + m2 = (MPIheader2*)(resp + sizeof(IBHpacket)); + m2->src_conn = pm->src_conn; + m2->dst_conn = pm->dst_conn; + m2->MPI = pm->MPI; + m2->xxx1 = 0; + m2->xxx2 = 0; + m2->xx22 = 0x22; + if (p1.param[0] == daveFuncRead) { +#ifdef passiveMode + _daveHandleRead(&p1, &pr); + haveResp = 1; + m2->len = pr.hlen + pr.plen + pr.dlen + 2; +#endif + p2->len = m2->len + 7; + } + else if (p1.param[0] == daveFuncWrite) { #ifdef passiveMode - _daveHandleWrite(&p1,&pr); - haveResp=1; - m2->len=pr.hlen+pr.plen+pr.dlen+2; -#endif - p2->len=m2->len+7; - } else { - if (daveDebug & daveDebugPDU) - LOG2("Unsupported PDU function code: %d\n",p1.param[0]); + _daveHandleWrite(&p1, &pr); + haveResp = 1; + m2->len = pr.hlen + pr.plen + pr.dlen + 2; +#endif + p2->len = m2->len + 7; + } + else { + if (daveDebug & daveDebugPDU) + LOG2("Unsupported PDU function code: %d\n", p1.param[0]); return _davePtUnknownPDUFunc; } } - if (pm->func==0xb0) { - LOG2("Ackknowledge for packet number: %d\n",*(dc->msgIn+15)); + if (pm->func == 0xb0) { + LOG2("Ackknowledge for packet number: %d\n", *(dc->msgIn + 15)); return _davePtMPIAck; } - if (pm->func==0xe0) { - LOG2("Connect to MPI: %d\n",pm->MPI); + if (pm->func == 0xe0) { + LOG2("Connect to MPI: %d\n", pm->MPI); memcpy(resp, _MPIconnectResponse, sizeof(_MPIconnectResponse)); - resp[8]=pm->src_conn; - resp[9]=pm->src_conn; - resp[10]=pm->MPI; - haveResp=1; - } - } - - if (p->rFlags==0x2c2) { - MPIheader2 * pm= (MPIheader2*) (dc->msgIn+sizeof(IBHpacket)); + resp[8] = pm->src_conn; + resp[9] = pm->src_conn; + resp[10] = pm->MPI; + haveResp = 1; + } + } + + if (p->rFlags == 0x2c2) { + MPIheader2 * pm = (MPIheader2*)(dc->msgIn + sizeof(IBHpacket)); if (daveDebug & daveDebugMPI) { - LOG2("srcconn: %d\n",pm->src_conn); - LOG2("dstconn: %d\n",pm->dst_conn); - LOG2("MPI: %d\n",pm->MPI); - LOG2("MPI len: %d\n",pm->len); - LOG2("MPI func:%d\n",pm->func); + LOG2("srcconn: %d\n", pm->src_conn); + LOG2("dstconn: %d\n", pm->dst_conn); + LOG2("MPI: %d\n", pm->MPI); + LOG2("MPI len: %d\n", pm->len); + LOG2("MPI func:%d\n", pm->func); } - if (pm->func==0xf1) { - if (daveDebug & daveDebugMPI) + if (pm->func == 0xf1) { + if (daveDebug & daveDebugMPI) LOG1("analyze 1\n"); - dc->needAckNumber=pm->packetNumber; - dc->PDUstartI= sizeof(IBHpacket)+sizeof(MPIheader2); + dc->needAckNumber = pm->packetNumber; + dc->PDUstartI = sizeof(IBHpacket) + sizeof(MPIheader2); _daveSendMPIAck_IBH(dc); return 55; - /* - if (daveDebug & daveDebugMPI) + /* + if (daveDebug & daveDebugMPI) LOG2("MPI packet number: %d\n",pm->packetNumber); dc->needAckNumber=pm->packetNumber; - // p1.header=((uc*)pm)+sizeof(MPIheader2); + // p1.header=((uc*)pm)+sizeof(MPIheader2); dc->PDUstartI= sizeof(IBHpacket)+sizeof(MPIheader2); _daveSetupReceivedPDU(dc, &p1); @@ -5531,14 +5637,15 @@ int DECL2 __daveAnalyze(daveConnection * dc) { } else { LOG2("Unsupported PDU function code: %d\n",p1.param[0]); } - */ + */ } - if (pm->func==0xb0) { - if (daveDebug & daveDebugMPI) - LOG2("Ackknowledge for packet number: %d\n",pm->packetNumber); - } else { - LOG2("Unsupported MPI function code !!: %d\n",pm->func); + if (pm->func == 0xb0) { + if (daveDebug & daveDebugMPI) + LOG2("Ackknowledge for packet number: %d\n", pm->packetNumber); + } + else { + LOG2("Unsupported MPI function code !!: %d\n", pm->func); _daveSendMPIAck_IBH(dc); } } @@ -5546,44 +5653,44 @@ int DECL2 __daveAnalyze(daveConnection * dc) { Sending IBHNetAck also for packets with sFlags=082 nearly doubles the speed for LINUX and speeds up windows version to the level of LINUX. Thanks to Axel Kinting for this proposal and for his patience finding it out! - */ - if (((p->sFlags==0x82)||(p->sFlags==0x82))&&(p->packetNumber)&&(p->len)) _daveSendIBHNetAck(dc); + */ + if (((p->sFlags == 0x82) || (p->sFlags == 0x82)) && (p->packetNumber) && (p->len)) _daveSendIBHNetAck(dc); if (haveResp) { - _daveWriteIBH(dc->iface, resp, resp[2]+8); - _daveDump("I send response:", resp, resp[2]+8); - } + _daveWriteIBH(dc->iface, resp, resp[2] + 8); + _daveDump("I send response:", resp, resp[2] + 8); + } return 0; }; int DECL2 _daveExchangeIBH(daveConnection * dc, PDU * p) { _daveSendMessageMPI_IBH(dc, p); - dc->AnswLen=0; + dc->AnswLen = 0; return _daveGetResponseMPI_IBH(dc); } int DECL2 _daveSendMessageMPI_IBH(daveConnection * dc, PDU * p) { int res; _davePackPDU(dc, p); - res=_daveWriteIBH(dc->iface, dc->msgOut, dc->msgOut[2]+8); - if (daveDebug & daveDebugPDU) - _daveDump("I send request: ",dc->msgOut, dc->msgOut[2]+8); - return res; -} + res = _daveWriteIBH(dc->iface, dc->msgOut, dc->msgOut[2] + 8); + if (daveDebug & daveDebugPDU) + _daveDump("I send request: ", dc->msgOut, dc->msgOut[2] + 8); + return res; +} int DECL2 _daveGetResponseMPI_IBH(daveConnection * dc) { - int res,count,pt; - count=0; - pt=0; + int res, count, pt; + count = 0; + pt = 0; do { - res=_daveReadIBHPacket(dc->iface, dc->msgIn); + res = _daveReadIBHPacket(dc->iface, dc->msgIn); count++; - if(res>4) - pt=__daveAnalyze(dc); - if (daveDebug & daveDebugExchange) - LOG2("ExchangeIBH packet type:%d\n",pt); - } while ((pt!=55)&&(count<7)); // 05/21/2013 - if(pt!=55) return daveResTimeout; + if (res > 4) + pt = __daveAnalyze(dc); + if (daveDebug & daveDebugExchange) + LOG2("ExchangeIBH packet type:%d\n", pt); + } while ((pt != 55) && (count < 7)); // 05/21/2013 + if (pt != 55) return daveResTimeout; return 0; } @@ -5593,36 +5700,37 @@ This performs initialization steps with sampled byte sequences. If chal is <>NUL it will send this byte sequence. It will then wait for a packet and compare it to the sample. */ -int DECL2 _daveInitStepIBH(daveInterface * iface, uc * chal, int cl, us* resp,int rl, uc*b) { - int res, res2, a=0; - if (daveDebug & daveDebugConnect) +int DECL2 _daveInitStepIBH(daveInterface * iface, uc * chal, int cl, us* resp, int rl, uc*b) { + int res, res2, a = 0; + if (daveDebug & daveDebugConnect) LOG1("_daveInitStepIBH before write.\n"); - if (chal) res=_daveWriteIBH(iface, chal, cl);else res=daveResInvalidParam; - if (daveDebug & daveDebugConnect) - LOG2("_daveInitStepIBH write returned %d.\n",res); - if (res<0) return 100; - res=_daveReadIBHPacket(iface, b); + if (chal) res = _daveWriteIBH(iface, chal, cl); else res = daveResInvalidParam; + if (daveDebug & daveDebugConnect) + LOG2("_daveInitStepIBH write returned %d.\n", res); + if (res < 0) return 100; + res = _daveReadIBHPacket(iface, b); /* We may get a network layer ackknowledge and an MPI layer ackknowledge, which we discard. - So, normally at least the 3rd packet should have the desired response. + So, normally at least the 3rd packet should have the desired response. Waiting for more does: -discard extra packets resulting from last step. -extend effective timeout. - */ - while (a<5) { + */ + while (a < 5) { if (a) { - res=_daveReadIBHPacket(iface, b); - // _daveDump("got:",b,res); - } - if (res>0) { - res2=_daveMemcmp(resp,b,rl/2); - if (0==res2) { - if (daveDebug & daveDebugInitAdapter) - LOG3("*** Got response %d %d\n",res,rl); + res = _daveReadIBHPacket(iface, b); + // _daveDump("got:",b,res); + } + if (res > 0) { + res2 = _daveMemcmp(resp, b, rl / 2); + if (0 == res2) { + if (daveDebug & daveDebugInitAdapter) + LOG3("*** Got response %d %d\n", res, rl); return a; - } else { - if (daveDebug & daveDebugInitAdapter) - LOG2("wrong! %d\n",res2); + } + else { + if (daveDebug & daveDebugInitAdapter) + LOG2("wrong! %d\n", res2); } } else @@ -5639,14 +5747,14 @@ uc chal0[]={ */ /* us resp0[]={ -0xff,0x00,0xfe,0x00, 0x04,0x00,0x00,0x00, +0xff,0x00,0xfe,0x00, 0x04,0x00,0x00,0x00, 0x00,0x07,0x02,0x03,0x1f, 0x100, // MPI address of NetLink 0x02,0x00, 0x103, // 187,5, MPI = 3 // 19,5, PB = 1 0x00,0x00,0x00,0x102, -0x00,0x02,0x3e, +0x00,0x02,0x3e, 0x19f, // 187,5, MPI = 9F // 19,5, PB = 64 0x101, // 187,5, MPI = 1 @@ -5655,16 +5763,16 @@ us resp0[]={ 0x101, // 187,5, MPI = 1 // 19,5, PB = 0 0x00, -0x102, // 187,5, MPI = 1 +0x102, // 187,5, MPI = 1 // 19,5, PB = 2 0x00, -0x116, // 187,5, MPI = 3c +0x116, // 187,5, MPI = 3c // 19,5, PB = 16 0x00, 0x13c, // 187,5, MPI = 90 // 19,5, PB = 3c -0x101, +0x101, 0x110, 0x127,0x100,0x100,0x114,0x101, 0x100,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x0e, @@ -5683,16 +5791,16 @@ us resp2[]={ 0x10,0x00, 0x20, 0x00, -0x01,0x00,0x00,0x00, +0x01,0x00,0x00,0x00, 0x01,0x106,0x120,0x103,0x17,0x00,0x43,0x00, 0x00,0x00, 0x122,0x121, 0x00,0x00,0x00,0x00, 0x49,0x42,0x48,0x53,0x00,0x00,0x00,0x00, -0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, }; */ -/* This is the correct response. I just inserted a "don't care" to make it work with latest +/* This is the correct response. I just inserted a "don't care" to make it work with latest IBH simulator. Better fix the simulator! us resp7[]={ 0xff,0x07,0x13,0x00,0x00,0x00,0xc2,0x02, 0x115,0x114,0x102,0x100,0x00,0x22,0x0c,0xd0, @@ -5708,259 +5816,259 @@ int DECL2 _daveConnectPLC_IBH(daveConnection*dc) { PDU p1; uc b[daveMaxRawLen]; - uc chal3[]={ - 0x07,0xff,0x06,0x01,0x00,0x00,0x97,0x00, - 0x15, //ibhSrcConn - 0xff,0xf0,0xf0,0xf0,0xf0,}; + uc chal3[] = { + 0x07, 0xff, 0x06, 0x01, 0x00, 0x00, 0x97, 0x00, + 0x15, //ibhSrcConn + 0xff, 0xf0, 0xf0, 0xf0, 0xf0, }; //us resp3[]={ //0xff,0x107,0x02,0x01,0x97,0x00,0x00,0x00, 0x114,0x100,}; - us resp3[]={ - 0xff,0x07,0x02,0x01,0x97,0x00,0x00,0x00,0x14,0x00,}; - - uc chal8[]={ - 0x07,0xff,0x11,0x02,0x00,0x00,0x82,0x00, - 0x14, //ibhSrcConn - 0x00, - 0x02, //MPI - 0x01, - 0x0c, //19 when routing to ip, 16 when routing to mpi, 0c without routing (count of following bytes??) - 0xe0, - 0x04, - 0x00, - 0x80, - 0x00, - 0x02, - 0x00, - 0x02, - dc->ConnectionType, //Connection Type (1=PG) was 0x01 - (dc->slot + dc->rack * 32), // Hopefully Rack/Slot is at this position! Need to test! 0x00, - dc->ConnectionType, //Connection Type (1=PG) repeaded (only when not routing??, why? is 6 when routing) - 0x00,}; - - uc chal8R[]={ //Routing - 0x07, - 0xff, - 0x1b, - 0x02, - 0x00, - 0x00, - 0x82, - 0x00, - 0x14, //ibhSrcConn - 0x00, - 0x02, //MPI - 0x01, - 0x16, //19 when routing to ip, 16 when routing to mpi - 0xe0, - 0x04, - 0x00, - 0x80, - 0x00, - 0x02, - 0x01, - 0x0c, - dc->ConnectionType, //Verb art (1=PG) - 0x00, - 0x06, //Connection Type (1=PG) repeaded (only when not routing??, why? is 6 when routing) - 0x01, - 0x02, - 0xaa, //subnet 1 - 0xaa, //subnet 2 - 0x00, - 0x00, - 0xbb, //subnet 3 - 0xbb, //subnet 4 - 0x07, //destination mpi - dc->routingConnectionType, //0x02 //Verb art zu routing cpu - 0x00, //rack/slot + us resp3[] = { + 0xff, 0x07, 0x02, 0x01, 0x97, 0x00, 0x00, 0x00, 0x14, 0x00, }; + + uc chal8[] = { + 0x07, 0xff, 0x11, 0x02, 0x00, 0x00, 0x82, 0x00, + 0x14, //ibhSrcConn + 0x00, + 0x02, //MPI + 0x01, + 0x0c, //19 when routing to ip, 16 when routing to mpi, 0c without routing (count of following bytes??) + 0xe0, + 0x04, + 0x00, + 0x80, + 0x00, + 0x02, + 0x00, + 0x02, + dc->ConnectionType, //Connection Type (1=PG) was 0x01 + (dc->slot + dc->rack * 32), // Hopefully Rack/Slot is at this position! Need to test! 0x00, + dc->ConnectionType, //Connection Type (1=PG) repeaded (only when not routing??, why? is 6 when routing) + 0x00, }; + + uc chal8R[] = { //Routing + 0x07, + 0xff, + 0x1b, + 0x02, + 0x00, + 0x00, + 0x82, + 0x00, + 0x14, //ibhSrcConn + 0x00, + 0x02, //MPI + 0x01, + 0x16, //19 when routing to ip, 16 when routing to mpi + 0xe0, + 0x04, + 0x00, + 0x80, + 0x00, + 0x02, + 0x01, + 0x0c, + dc->ConnectionType, //Verb art (1=PG) + 0x00, + 0x06, //Connection Type (1=PG) repeaded (only when not routing??, why? is 6 when routing) + 0x01, + 0x02, + 0xaa, //subnet 1 + 0xaa, //subnet 2 + 0x00, + 0x00, + 0xbb, //subnet 3 + 0xbb, //subnet 4 + 0x07, //destination mpi + dc->routingConnectionType, //0x02 //Verb art zu routing cpu + 0x00, //rack/slot }; - uc chal8RIP[]={ //Routing IP - 0x07, - 0xff, - 0x1e, - 0x06, - 0x00, - 0x00, - 0x82, - 0x00, - 0x14, //ibhSrcConn - 0x00, - 0x02, //MPI - 0x01, - 0x19, //19 when routing to ip, 16 when routing to mpi - 0xe0, - 0x04, - 0x00, - 0x80, - 0x00, - 0x02, - 0x01, - 0x0f, - dc->ConnectionType, //Verb art (1=PG) - 0x00, - 0x06, //Connection Type (1=PG) repeaded (only when not routing??, why? is 6 when routing) - 0x04, - 0x02, - 0xaa, //subnet 1 - 0xaa, //subnet 2 - 0x00, - 0x00, - 0xbb, //subnet 3 - 0xbb, //subnet 4 - 0x07, //ip1 - 0x07, //ip2 - 0x07, //ip3 - 0x07, //ip4 - dc->routingConnectionType, //Verb art zu routing cpu - 0x00, //rack/slot + uc chal8RIP[] = { //Routing IP + 0x07, + 0xff, + 0x1e, + 0x06, + 0x00, + 0x00, + 0x82, + 0x00, + 0x14, //ibhSrcConn + 0x00, + 0x02, //MPI + 0x01, + 0x19, //19 when routing to ip, 16 when routing to mpi + 0xe0, + 0x04, + 0x00, + 0x80, + 0x00, + 0x02, + 0x01, + 0x0f, + dc->ConnectionType, //Verb art (1=PG) + 0x00, + 0x06, //Connection Type (1=PG) repeaded (only when not routing??, why? is 6 when routing) + 0x04, + 0x02, + 0xaa, //subnet 1 + 0xaa, //subnet 2 + 0x00, + 0x00, + 0xbb, //subnet 3 + 0xbb, //subnet 4 + 0x07, //ip1 + 0x07, //ip2 + 0x07, //ip3 + 0x07, //ip4 + dc->routingConnectionType, //Verb art zu routing cpu + 0x00, //rack/slot }; - us resp7[]={ - 0xff,0x07,0x13,0x00,0x00,0x00,0xc2,0x02, 0x115,0x114,0x102,0x100,0x00,0x22,0x0c,0xd0, - 0x04,0x00,0x80,0x00,0x02,0x00,0x102,0x01, 0x00,0x01,0x00,}; - - uc chal011[]={ - 0x07,0xff,0x07,0x03,0x00,0x00,0x82,0x00, - 0x15, //ibhSrcConn - 0x14, //ibhDstConn - 0x02, //MPI - 0x00,0x02,0x05,0x01,}; - - us resp09[]={ - 0xff,0x07,0x09,0x00,0x00,0x00,0xc2,0x02, 0x115,0x114,0x102,0x100,0x00,0x22,0x02,0x05, - 0x101,}; //A.K. - - dc->iface->timeout=500000; - dc->iface->localMPI=0; - dc->ibhSrcConn=20-1; - dc->ibhDstConn=20-1; - retries=0; + us resp7[] = { + 0xff, 0x07, 0x13, 0x00, 0x00, 0x00, 0xc2, 0x02, 0x115, 0x114, 0x102, 0x100, 0x00, 0x22, 0x0c, 0xd0, + 0x04, 0x00, 0x80, 0x00, 0x02, 0x00, 0x102, 0x01, 0x00, 0x01, 0x00, }; + + uc chal011[] = { + 0x07, 0xff, 0x07, 0x03, 0x00, 0x00, 0x82, 0x00, + 0x15, //ibhSrcConn + 0x14, //ibhDstConn + 0x02, //MPI + 0x00, 0x02, 0x05, 0x01, }; + + us resp09[] = { + 0xff, 0x07, 0x09, 0x00, 0x00, 0x00, 0xc2, 0x02, 0x115, 0x114, 0x102, 0x100, 0x00, 0x22, 0x02, 0x05, + 0x101, }; //A.K. + + dc->iface->timeout = 500000; + dc->iface->localMPI = 0; + dc->ibhSrcConn = 20 - 1; + dc->ibhDstConn = 20 - 1; + retries = 0; do { - if (daveDebug & daveDebugConnect) + if (daveDebug & daveDebugConnect) LOG1("trying next ID:\n"); dc->ibhSrcConn++; - chal3[8]=dc->ibhSrcConn; - a=_daveInitStepIBH(dc->iface, chal3,sizeof(chal3),resp3,sizeof(resp3),b); + chal3[8] = dc->ibhSrcConn; + a = _daveInitStepIBH(dc->iface, chal3, sizeof(chal3), resp3, sizeof(resp3), b); retries++; - } while ((b[9]!=0) && (retries<10)); + } while ((b[9] != 0) && (retries < 10)); - if (daveDebug & daveDebugConnect) - LOG2("_daveInitStepIBH 4:%d\n",a); if (a>3) /* !!! */ return -4;; - chal8[10]=dc->MPIAdr; - chal8R[10]=dc->MPIAdr; - chal8RIP[10]=dc->MPIAdr; + if (daveDebug & daveDebugConnect) + LOG2("_daveInitStepIBH 4:%d\n", a); if (a > 3) /* !!! */ return -4;; + chal8[10] = dc->MPIAdr; + chal8R[10] = dc->MPIAdr; + chal8RIP[10] = dc->MPIAdr; /* Not yet tested: - chal8[10]=dc->CPUConnectiontype; - chal8R[10]=dc->CPUConnectiontype; - chal8RIP[10]=dc->CPUConnectiontype; + chal8[10]=dc->CPUConnectiontype; + chal8R[10]=dc->CPUConnectiontype; + chal8RIP[10]=dc->CPUConnectiontype; */ - // LOG2("setting MPI %d\n",dc->MPIAdr); - chal8[8]=dc->ibhSrcConn; - chal8R[8]=dc->ibhSrcConn; - chal8RIP[8]=dc->ibhSrcConn; + // LOG2("setting MPI %d\n",dc->MPIAdr); + chal8[8] = dc->ibhSrcConn; + chal8R[8] = dc->ibhSrcConn; + chal8RIP[8] = dc->ibhSrcConn; if (!dc->routing) - a=_daveInitStepIBH(dc->iface, chal8,sizeof(chal8),resp7,sizeof(resp7),b); + a = _daveInitStepIBH(dc->iface, chal8, sizeof(chal8), resp7, sizeof(resp7), b); else { if (!dc->routingDestinationIsIP) - { - chal8R[26]=(unsigned char) (dc->routingSubnetFirst >> 8); - chal8R[27]=(unsigned char) (dc->routingSubnetFirst); - chal8R[30]=(unsigned char) (dc->routingSubnetSecond >> 8); - chal8R[31]=(unsigned char) (dc->routingSubnetSecond); - chal8R[32]=(unsigned char) dc->_routingDestination1; - chal8R[34]=(dc->routingSlot + dc->routingRack*32); //--> Routing Rack/Slot - - a=_daveInitStepIBH(dc->iface, chal8R,sizeof(chal8R),resp7,sizeof(resp7),b); + { + chal8R[26] = (unsigned char)(dc->routingSubnetFirst >> 8); + chal8R[27] = (unsigned char)(dc->routingSubnetFirst); + chal8R[30] = (unsigned char)(dc->routingSubnetSecond >> 8); + chal8R[31] = (unsigned char)(dc->routingSubnetSecond); + chal8R[32] = (unsigned char)dc->_routingDestination1; + chal8R[34] = (dc->routingSlot + dc->routingRack * 32); //--> Routing Rack/Slot + + a = _daveInitStepIBH(dc->iface, chal8R, sizeof(chal8R), resp7, sizeof(resp7), b); } else { - chal8RIP[26]=(unsigned char) (dc->routingSubnetFirst >> 8); - chal8RIP[27]=(unsigned char) (dc->routingSubnetFirst); - chal8RIP[30]=(unsigned char) (dc->routingSubnetSecond >> 8); - chal8RIP[31]=(unsigned char) (dc->routingSubnetSecond); - chal8RIP[32]=(unsigned char) dc->_routingDestination1; - chal8RIP[33]=(unsigned char) dc->_routingDestination2; - chal8RIP[34]=(unsigned char) dc->_routingDestination3; - chal8RIP[35]=(unsigned char) dc->_routingDestination4; - chal8RIP[37]=(dc->routingSlot + dc->routingRack*32); //--> Routing Rack/Slot - - a=_daveInitStepIBH(dc->iface, chal8RIP,sizeof(chal8RIP),resp7,sizeof(resp7),b); + chal8RIP[26] = (unsigned char)(dc->routingSubnetFirst >> 8); + chal8RIP[27] = (unsigned char)(dc->routingSubnetFirst); + chal8RIP[30] = (unsigned char)(dc->routingSubnetSecond >> 8); + chal8RIP[31] = (unsigned char)(dc->routingSubnetSecond); + chal8RIP[32] = (unsigned char)dc->_routingDestination1; + chal8RIP[33] = (unsigned char)dc->_routingDestination2; + chal8RIP[34] = (unsigned char)dc->_routingDestination3; + chal8RIP[35] = (unsigned char)dc->_routingDestination4; + chal8RIP[37] = (dc->routingSlot + dc->routingRack * 32); //--> Routing Rack/Slot + + a = _daveInitStepIBH(dc->iface, chal8RIP, sizeof(chal8RIP), resp7, sizeof(resp7), b); } } - dc->ibhDstConn=b[9]; - if (daveDebug & daveDebugConnect) - LOG3("_daveInitStepIBH 5:%d connID: %d\n",a, dc->ibhDstConn); if (a>3) return -5; + dc->ibhDstConn = b[9]; + if (daveDebug & daveDebugConnect) + LOG3("_daveInitStepIBH 5:%d connID: %d\n", a, dc->ibhDstConn); if (a > 3) return -5; - chal011[8]=dc->ibhSrcConn; - chal011[9]=dc->ibhDstConn; - chal011[10]=dc->MPIAdr; //?????? - a=_daveInitStepIBH(dc->iface, chal011,sizeof(chal011),resp09,sizeof(resp09),b); + chal011[8] = dc->ibhSrcConn; + chal011[9] = dc->ibhDstConn; + chal011[10] = dc->MPIAdr; //?????? + a = _daveInitStepIBH(dc->iface, chal011, sizeof(chal011), resp09, sizeof(resp09), b); - dc->ibhDstConn=b[9]; - if (daveDebug & daveDebugConnect) - LOG3("_daveInitStepIBH 5a:%d connID: %d\n",a, dc->ibhDstConn); if (a>3) return -5; + dc->ibhDstConn = b[9]; + if (daveDebug & daveDebugConnect) + LOG3("_daveInitStepIBH 5a:%d connID: %d\n", a, dc->ibhDstConn); if (a > 3) return -5; - dc->packetNumber=4; + dc->packetNumber = 4; return _daveNegPDUlengthRequest(dc, &p1); -} +} /* -Disconnect from a PLC via IBH-NetLink. +Disconnect from a PLC via IBH-NetLink. Returns 0 for success and a negative number on errors. */ int DECL2 _daveDisconnectPLC_IBH(daveConnection*dc) { uc b[daveMaxRawLen]; - uc chal31[]={ - 0x07,0xff,0x06,0x08,0x00,0x00,0x82,0x00, 0x14,0x14,0x02,0x00,0x01,0x80,}; + uc chal31[] = { + 0x07, 0xff, 0x06, 0x08, 0x00, 0x00, 0x82, 0x00, 0x14, 0x14, 0x02, 0x00, 0x01, 0x80, }; - chal31[8]=dc->ibhSrcConn; - chal31[9]=dc->ibhDstConn; - chal31[10]=dc->MPIAdr; + chal31[8] = dc->ibhSrcConn; + chal31[9] = dc->ibhDstConn; + chal31[10] = dc->MPIAdr; _daveWriteIBH(dc->iface, chal31, sizeof(chal31)); _daveReadIBHPacket(dc->iface, b); -#ifdef BCCWIN -#else +#ifdef BCCWIN +#else _daveReadIBHPacket(dc->iface, b); -#endif +#endif return 0; } /* -Disconnect from a PLC via IBH-NetLink. This can be used to free other than your own +Disconnect from a PLC via IBH-NetLink. This can be used to free other than your own connections. Be careful, this may disturb third party programs/devices. */ int DECL2 daveForceDisconnectIBH(daveInterface * di, int src, int dest, int mpi) { uc b[daveMaxRawLen]; - uc chal31[]={ - 0x07,0xff,0x06,0x08,0x00,0x00,0x82,0x00, 0x14,0x14,0x02,0x00,0x01,0x80,}; + uc chal31[] = { + 0x07, 0xff, 0x06, 0x08, 0x00, 0x00, 0x82, 0x00, 0x14, 0x14, 0x02, 0x00, 0x01, 0x80, }; - chal31[8]=src; - chal31[9]=dest; - chal31[10]=mpi; + chal31[8] = src; + chal31[9] = dest; + chal31[10] = mpi; _daveWriteIBH(di, chal31, sizeof(chal31)); _daveReadIBHPacket(di, b); -#ifdef BCCWIN -#else +#ifdef BCCWIN +#else _daveReadIBHPacket(di, b); -#endif +#endif return 0; -} +} /* Resets the IBH-NetLink. Returns 0 for success and a negative number on errors. */ int DECL2 daveResetIBH(daveInterface * di) { - uc chalReset[]={ - 0x00,0xff,0x01,0x00,0x00,0x00,0x01,0x00,0x01 + uc chalReset[] = { + 0x00, 0xff, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01 }; uc b[daveMaxRawLen]; _daveWriteIBH(di, chalReset, sizeof(chalReset)); @@ -5968,7 +6076,7 @@ int DECL2 daveResetIBH(daveInterface * di) { #ifdef BCCWIN #else _daveReadIBHPacket(di, b); -#endif +#endif return 0; } @@ -5976,73 +6084,73 @@ void DECL2 _daveSendMPIAck2(daveConnection *dc) { IBHpacket * p; uc c; uc ack[18]; - memcpy(ack,dc->msgIn,sizeof(ack)); - p= (IBHpacket*) ack; - p->rFlags|=0x240; //Why that? - c=p->ch1; p->ch1=p->ch2; p->ch2=c; - p->len=sizeof(ack)-sizeof(IBHpacket); - p->packetNumber=0; // this may mean: no net work level acknowledge - ack[13]=0x22; - ack[14]=3; - ack[15]=176; - ack[16]=1; - ack[17]=dc->needAckNumber; - _daveDump("send MPI-Ack2",ack,sizeof(ack)); - _daveWriteIBH(dc->iface,ack,sizeof(ack)); + memcpy(ack, dc->msgIn, sizeof(ack)); + p = (IBHpacket*)ack; + p->rFlags |= 0x240; //Why that? + c = p->ch1; p->ch1 = p->ch2; p->ch2 = c; + p->len = sizeof(ack) - sizeof(IBHpacket); + p->packetNumber = 0; // this may mean: no net work level acknowledge + ack[13] = 0x22; + ack[14] = 3; + ack[15] = 176; + ack[16] = 1; + ack[17] = dc->needAckNumber; + _daveDump("send MPI-Ack2", ack, sizeof(ack)); + _daveWriteIBH(dc->iface, ack, sizeof(ack)); }; -int DECL2 _davePackPDU_PPI(daveConnection * dc,PDU *p) { +int DECL2 _davePackPDU_PPI(daveConnection * dc, PDU *p) { IBHpacket * ibhp; - uc IBHPPIHeader[]={0xff,0xff,2,0,8}; - IBHPPIHeader[2]=dc->MPIAdr; - memcpy(dc->msgOut+sizeof(IBHpacket), &IBHPPIHeader, sizeof(IBHPPIHeader)); - - ibhp = (IBHpacket*) dc->msgOut; - ibhp->ch1=7; - ibhp->ch2=0xff; - ibhp->len=p->hlen+p->plen+p->dlen+5; // set MPI length - // dc->msgOut[3]=2; - *(dc->msgOut+sizeof(IBHpacket)+4)=p->hlen+p->plen+p->dlen+0; - ibhp->packetNumber=dc->packetNumber; - ibhp->rFlags=0x80; - ibhp->sFlags=0; - // dc->msgOut[3]=dc->packetNumber; + uc IBHPPIHeader[] = { 0xff, 0xff, 2, 0, 8 }; + IBHPPIHeader[2] = dc->MPIAdr; + memcpy(dc->msgOut + sizeof(IBHpacket), &IBHPPIHeader, sizeof(IBHPPIHeader)); + + ibhp = (IBHpacket*)dc->msgOut; + ibhp->ch1 = 7; + ibhp->ch2 = 0xff; + ibhp->len = p->hlen + p->plen + p->dlen + 5; // set MPI length + // dc->msgOut[3]=2; + *(dc->msgOut + sizeof(IBHpacket) + 4) = p->hlen + p->plen + p->dlen + 0; + ibhp->packetNumber = dc->packetNumber; + ibhp->rFlags = 0x80; + ibhp->sFlags = 0; + // dc->msgOut[3]=dc->packetNumber; dc->packetNumber++; - dc->msgOut[6]=0x82; - dc->msgOut[3]=2; + dc->msgOut[6] = 0x82; + dc->msgOut[3] = 2; return 0; -} +} /* send a network level ackknowledge */ void DECL2 _daveSendIBHNetAckPPI(daveConnection * dc) { - uc ack[]={7,0xff,5,3,0,0,0x82,0,0xff,0xff,2,0,0}; - ack[10]=dc->MPIAdr; - ack[3]=_daveIncMessageNumber(dc); + uc ack[] = { 7, 0xff, 5, 3, 0, 0, 0x82, 0, 0xff, 0xff, 2, 0, 0 }; + ack[10] = dc->MPIAdr; + ack[3] = _daveIncMessageNumber(dc); _daveWriteIBH(dc->iface, ack, sizeof(ack)); } int DECL2 _daveListReachablePartnersMPI_IBH(daveInterface * di, char * buf) { int a, i; - uc b[2*daveMaxRawLen]; + uc b[2 * daveMaxRawLen]; - uc chal1[]={ - 0x07,0xff,0x08,0x01,0x00,0x00,0x96,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x0a,0x01,}; + uc chal1[] = { + 0x07, 0xff, 0x08, 0x01, 0x00, 0x00, 0x96, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x01, }; - us resp1[]={ - 0xff,0x07,0x87,0x01,0x96,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x7f,0x0a,0x02,}; + us resp1[] = { + 0xff, 0x07, 0x87, 0x01, 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x0a, 0x02, }; - a=_daveInitStepIBH(di, chal1,sizeof(chal1),resp1,16,b); - if (daveDebug & daveDebugListReachables) - LOG2("_daveListReachablePartnersMPI_IBH:%d\n",a); - for (i=0;i<126;i++) { - if (b[i+16]==0xFF) buf[i]=0x10; else buf[i]=0x30; + a = _daveInitStepIBH(di, chal1, sizeof(chal1), resp1, 16, b); + if (daveDebug & daveDebugListReachables) + LOG2("_daveListReachablePartnersMPI_IBH:%d\n", a); + for (i = 0; i < 126; i++) { + if (b[i + 16] == 0xFF) buf[i] = 0x10; else buf[i] = 0x30; // LOG3(" %d %d\n",i, b[i+16]); - } + } return 126; -} +} /* packet analysis. mixes all levels. @@ -6050,21 +6158,22 @@ packet analysis. mixes all levels. int DECL2 __daveAnalyzePPI(daveConnection * dc, uc sa) { IBHpacket* p; - p= (IBHpacket*) dc->msgIn; + p = (IBHpacket*)dc->msgIn; if (daveDebug & daveDebugPacket){ - LOG2("Channel: %d\n",p->ch1); - LOG2("Channel: %d\n",p->ch2); - LOG2("Length: %d\n",p->len); - LOG2("Number: %d\n",p->packetNumber); - LOG3("sFlags: %04x rFlags:%04x\n",p->sFlags,p->rFlags); - } - if (p->sFlags==0x82) { - if(p->len<=5) { - if(sa) _daveSendIBHNetAckPPI(dc); - } else - if ((p->len>=7) && (dc->msgIn[14]==0x32)) + LOG2("Channel: %d\n", p->ch1); + LOG2("Channel: %d\n", p->ch2); + LOG2("Length: %d\n", p->len); + LOG2("Number: %d\n", p->packetNumber); + LOG3("sFlags: %04x rFlags:%04x\n", p->sFlags, p->rFlags); + } + if (p->sFlags == 0x82) { + if (p->len <= 5) { + if (sa) _daveSendIBHNetAckPPI(dc); + } + else + if ((p->len >= 7) && (dc->msgIn[14] == 0x32)) return 55; - } + } return 0; }; @@ -6072,671 +6181,1045 @@ int DECL2 _daveExchangePPI_IBH(daveConnection * dc, PDU * p) { int res, count, pt; _davePackPDU_PPI(dc, p); - res=_daveWriteIBH(dc->iface, dc->msgOut, dc->msgOut[2]+8); - if (daveDebug & daveDebugExchange) - _daveDump("I send request: ",dc->msgOut, dc->msgOut[2]+8); - // _daveSendIBHNetAckPPI(dc); - count=0; + res = _daveWriteIBH(dc->iface, dc->msgOut, dc->msgOut[2] + 8); + if (daveDebug & daveDebugExchange) + _daveDump("I send request: ", dc->msgOut, dc->msgOut[2] + 8); + // _daveSendIBHNetAckPPI(dc); + count = 0; do { - res=_daveReadIBHPacket(dc->iface, dc->msgIn); + res = _daveReadIBHPacket(dc->iface, dc->msgIn); count++; - if (res>0) - pt=__daveAnalyzePPI(dc,1); - else - pt=0; - if (daveDebug & daveDebugExchange) - LOG2("ExchangeIBH packet type:%d\n",pt); - } while ((pt!=55)&&(count<7)); - if(pt!=55) return daveResTimeout; + if (res > 0) + pt = __daveAnalyzePPI(dc, 1); + else + pt = 0; + if (daveDebug & daveDebugExchange) + LOG2("ExchangeIBH packet type:%d\n", pt); + } while ((pt != 55) && (count < 7)); + if (pt != 55) return daveResTimeout; return 0; } int DECL2 _daveGetResponsePPI_IBH(daveConnection * dc) { int res, count, pt; - count=0; + count = 0; do { _daveSendIBHNetAckPPI(dc); - res=_daveReadIBHPacket(dc->iface, dc->msgIn); - LOG2("_daveReadIBHPacket():%d\n",res); + res = _daveReadIBHPacket(dc->iface, dc->msgIn); + LOG2("_daveReadIBHPacket():%d\n", res); count++; - if (res>0) - pt=__daveAnalyzePPI(dc,0); - else - pt=0; - if (daveDebug & daveDebugExchange) - LOG2("ExchangeIBH packet type:%d\n",pt); - } while ((pt!=55)&&(count<7)); - if(pt!=55) return daveResTimeout; + if (res > 0) + pt = __daveAnalyzePPI(dc, 0); + else + pt = 0; + if (daveDebug & daveDebugExchange) + LOG2("ExchangeIBH packet type:%d\n", pt); + } while ((pt != 55) && (count < 7)); + if (pt != 55) return daveResTimeout; return 0; } #endif /* Build a PDU with data from 2 data blocks. */ -int DECL2 BuildAndSendPDU(daveConnection * dc, PDU*p2,uc *pa,int psize, uc *ud,int usize, - uc *ud2,int usize2) { - int res; - PDU p,*p3; - uc * dn; - p.header=dc->msgOut+dc->PDUstartO; - _daveInitPDUheader(&p, 7); - _daveAddParam(&p, pa, psize); - _daveAddUserData(&p, ud, usize); - // LOG2("*** here we are: %d\n",p.dlen); - p3=&p; - dn= p3->data+p3->dlen; - p3->dlen+=usize2; - memcpy(dn, ud2, usize2); - - ((PDUHeader*)p3->header)->dlen=daveSwapIed_16(p3->dlen); - - LOG2("*** here we are: %d\n",p.dlen); - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(&p); - } - res=_daveExchange(dc, &p); - if (daveDebug & daveDebugErrorReporting) - LOG2("*** res of _daveExchange(): %d\n",res); - if (res!=daveResOK) return res; - res=_daveSetupReceivedPDU(dc,p2); - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(p2); - } - if (daveDebug & daveDebugErrorReporting) - LOG2("*** res of _daveSetupReceivedPDU(): %d\n",res); - if (res!=daveResOK) return res; - res=_daveTestPGReadResult(p2); - if (daveDebug & daveDebugErrorReporting) - LOG2("*** res of _daveTestPGReadResult(): %d\n",res); - return res; -} +int DECL2 BuildAndSendPDU(daveConnection * dc, PDU*p2, uc *pa, int psize, uc *ud, int usize, + uc *ud2, int usize2) { + int res; + PDU p, *p3; + uc * dn; + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 7); + _daveAddParam(&p, pa, psize); + _daveAddUserData(&p, ud, usize); + // LOG2("*** here we are: %d\n",p.dlen); + p3 = &p; + dn = p3->data + p3->dlen; + p3->dlen += usize2; + memcpy(dn, ud2, usize2); + + ((PDUHeader*)p3->header)->dlen = daveSwapIed_16(p3->dlen); -int DECL2 daveForce200(daveConnection * dc,int area, int start, int val) { + LOG2("*** here we are: %d\n", p.dlen); + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(&p); + } + res = _daveExchange(dc, &p); + if (daveDebug & daveDebugErrorReporting) + LOG2("*** res of _daveExchange(): %d\n", res); + if (res != daveResOK) return res; + res = _daveSetupReceivedPDU(dc, p2); + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(p2); + } + if (daveDebug & daveDebugErrorReporting) + LOG2("*** res of _daveSetupReceivedPDU(): %d\n", res); + if (res != daveResOK) return res; + res = _daveTestPGReadResult(p2); + if (daveDebug & daveDebugErrorReporting) + LOG2("*** res of _daveTestPGReadResult(): %d\n", res); + return res; +} + +int DECL2 daveForce200(daveConnection * dc, int area, int start, int val) { int res; PDU p2; - // uc pa[]={0,1,18,4,17,67,2,0}; - // uc da[]={'0','0'}; + // uc pa[]={0,1,18,4,17,67,2,0}; + // uc da[]={'0','0'}; //32,7,0,0,0,0,0,c,0,16, - uc pa[]={0,1,18,8,18,72,14,0,0,0,0,0}; - uc da[]={0,1,0x10,2, - 0,1, - 0,0, + uc pa[] = { 0, 1, 18, 8, 18, 72, 14, 0, 0, 0, 0, 0 }; + uc da[] = { 0, 1, 0x10, 2, + 0, 1, + 0, 0, 0, // area - 0,0,0, // start + 0, 0, 0, // start }; - uc da2[]={0,4,0,8,0,0,}; - // uc da2[]={0,4,0,8,7,0,}; - - if ((area==daveAnaIn) || (area==daveAnaOut) /*|| (area==daveP)*/) { - da[3]=4; - start*=8; /* bits */ - } else if ((area==daveTimer) || (area==daveCounter)||(area==daveTimer200) || (area==daveCounter200)) { - da[3]=area; - } else { - start*=8; + uc da2[] = { 0, 4, 0, 8, 0, 0, }; + // uc da2[]={0,4,0,8,7,0,}; + + if ((area == daveAnaIn) || (area == daveAnaOut) /*|| (area==daveP)*/) { + da[3] = 4; + start *= 8; /* bits */ + } + else if ((area == daveTimer) || (area == daveCounter) || (area == daveTimer200) || (area == daveCounter200)) { + da[3] = area; } - /* else { + else { + start *= 8; + } + /* else { if(isBit) { pa[3]=1; } else { - start*=8; - } + start*=8; + } } - */ - da[8]=area; - da[9]=start / 0x10000; - da[10]=(start / 0x100) & 0xff; - da[11]=start & 0xff; + */ + da[8] = area; + da[9] = start / 0x10000; + da[10] = (start / 0x100) & 0xff; + da[11] = start & 0xff; - da2[4]=val % 0x100; - da2[5]=val / 0x100; - res=BuildAndSendPDU(dc, &p2, pa, sizeof(pa), da, sizeof(da), da2, sizeof(da2)); + da2[4] = val % 0x100; + da2[5] = val / 0x100; + res = BuildAndSendPDU(dc, &p2, pa, sizeof(pa), da, sizeof(da), da2, sizeof(da2)); return res; } daveResultSet * DECL2 daveNewResultSet() { - daveResultSet * p=(daveResultSet*) calloc(1, sizeof(daveResultSet)); + daveResultSet * p = (daveResultSet*)calloc(1, sizeof(daveResultSet)); #ifdef DEBUG_CALLS - LOG2("daveNewResultSet() = %p\n",p); + LOG2("daveNewResultSet() = %p\n", p); FLUSH; -#endif +#endif return p; -} +} void DECL2 daveFree(void * dc) { - // if (dc!=NULL) { // I'm not sure whether freeing a NULL pointer will do no harm on each and + // if (dc!=NULL) { // I'm not sure whether freeing a NULL pointer will do no harm on each and // every system. So for safety, we check and set it to NULL afterwards. free(dc); - // } + // } } int DECL2 daveGetProgramBlock(daveConnection * dc, int blockType, int number, char* buffer, int * length) { int res, len, more, totlen; unsigned char uploadID[4]; - uc *bb=(uc*)buffer; //cs: is this right? - len=0; - totlen=0; - if (dc->iface->protocol==daveProtoAS511) { + uc *bb = (uc*)buffer; //cs: is this right? + len = 0; + totlen = 0; + if (dc->iface->protocol == daveProtoAS511) { return daveGetS5ProgramBlock(dc, blockType, number, buffer, length); } - - res=initUpload(dc, blockType, number, &uploadID); - if (res!=0) return res; - do { - res=doUpload(dc,&more,&bb,&len,uploadID); - totlen+=len; - if (res!=0) return res; - } while (more); - res=endUpload(dc,uploadID); - *length=totlen; + + res = initUpload(dc, blockType, number, &uploadID); + if (res != 0) return res; + do { + res = doUpload(dc, &more, &bb, &len, uploadID); + totlen += len; + if (res != 0) return res; + } while (more); + res = endUpload(dc, uploadID); + *length = totlen; + return res; +} + +int DECL2 davePutProgramBlock(daveConnection * dc, int blockType, int blknumber, char* buffer, int length) { +#define maxPBlockLen 0xDe // real maximum 222 bytes + + int res = 0; + int cnt = 0; + int size = 0; + int blockNumber, rawLen, netLen, blockCont; + int number = 0; + + uc pup[] = { // Load request + 0x1A, 0, 1, 0, 0, 0, 0, 0, 9, + 0x5F, 0x30, 0x42, 0x30, 0x30, 0x30, 0x30, 0x34, 0x50, // block type code and number + // _ 0 B 0 0 0 0 4 P + // SDB + 0x0D, + 0x31, 0x30, 0x30, 0x30, 0x32, 0x30, 0x38, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0 // file length and netto length + // 1 0 0 0 2 0 8 0 0 0 1 1 0 + }; + + PDU p, p2; + + uc pablock[] = { // parameters for parts of a block + 0x1B, 0 + }; + + uc progBlock[maxPBlockLen + 4] = { + 0, maxPBlockLen, 0, 0xFB, // This seems to be a fix prefix for program blocks + }; + + pup[11] = blockType; + paInsert[13] = blockType; + /*pup[12] = number / (10*10*10*10); + pup[13] = (number - (pup[12] * 10*10*10*10 )) / (10*10*10); + pup[14] = (number - (pup[13] * 10*10*10)) / (10*10); + pup[15] = (number - (pup[14] * 10*10)) / (10); + pup[16] = (number - (pup[15] * 10)); + + pup[12] = pup[12] + 0x30; + pup[13] = pup[13] + 0x30; + pup[14] = pup[14] + 0x30; + pup[15] = pup[15] + 0x30; + pup[16] = pup[16] + 0x30;*/ + + int copyLen = maxPBlockLen > length ? length : maxPBlockLen; + memcpy(progBlock + 4, buffer, copyLen); + + progBlock[9] = (blockType + 0x0A - 'A'); //Convert 'A' to 0x0A + if (blockType == '8') progBlock[9] = 0x08; + + progBlock[10] = blknumber / 0x100; + progBlock[11] = blknumber - (progBlock[10] * 0x100); + + + rawLen = daveGetU16from(progBlock + 14); + netLen = daveGetU16from(progBlock + 38); + + sprintf((char*)pup + 19, "1%06d%06d", rawLen, netLen); + + sprintf((char*)pup + 12, "%05d", blknumber); + sprintf((char*)paInsert + 14, "%05d", blknumber); + + pup[17] = 'P'; + paInsert[19] = 'P'; + + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 1); + _daveAddParam(&p, pup, sizeof(pup) - 1); + + res = _daveExchange(dc, &p); + if (res == daveResOK) { + res = _daveSetupReceivedPDU(dc, &p2); + if (daveGetDebug() & daveDebugPDU) { + _daveDumpPDU(&p2); + } + res = daveGetPDUerror(&p2); + + if (res == 0) { + blockCont = 1; + res = daveGetResponse(dc); + res = _daveSetupReceivedPDU(dc, &p2); + + cnt = 0; + + do { + res = 0; + res = _daveSetupReceivedPDU(dc, &p2); + + number = ((PDUHeader*)p2.header)->number; + if (p2.param[0] == 0x1B) { + //READFILE + copyLen = (cnt + 1)*maxPBlockLen > length ? length - (cnt*maxPBlockLen) : maxPBlockLen; + memcpy(progBlock + 4, buffer + (cnt*maxPBlockLen), copyLen); + + if (cnt == 0) + { + progBlock[9] = (blockType + 0x0A - 'A'); //Convert 'A' to 0x0A + if (blockType == '8') progBlock[9] = 0x08; + + progBlock[10] = blknumber / 0x100; + progBlock[11] = blknumber - (progBlock[10] * 0x100); + } + + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 3); + size = maxPBlockLen; + + if (length > ((cnt + 1) * maxPBlockLen)) + pablock[1] = 1; + else + { + size = length - (cnt * maxPBlockLen); + pablock[1] = 0; //last block + blockCont = 0; + } + + progBlock[1] = size; + _daveAddParam(&p, pablock, sizeof(pablock)); + _daveAddData(&p, progBlock, size + 4 /* size of block) */); + ((PDUHeader*)p.header)->number = number; + if (daveGetDebug() & daveDebugPDU) { + _daveDumpPDU(&p); + } + _daveExchange(dc, &p); + } + cnt++; + } while (blockCont); + + res = _daveSetupReceivedPDU(dc, &p2); + if (daveGetDebug() & daveDebugPDU) { + _daveDumpPDU(&p2); + } + number = ((PDUHeader*)p2.header)->number; + if (p2.param[0] == 0x1C) { + p.header = dc->msgOut + dc->PDUstartO; + + _daveInitPDUheader(&p, 3); + _daveAddParam(&p, p2.param, 1); + ((PDUHeader*)p.header)->number = number; + _daveExchange(dc, &p); + + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 1); + _daveAddParam(&p, paInsert, sizeof(paInsert)); + res = _daveExchange(dc, &p); + res = _daveSetupReceivedPDU(dc, &p2); + res = daveGetPDUerror(&p2); + } + } + else { + printf("CPU doesn't accept load request:%04X\n", res); + } + return res; + } + return res; +} + +int DECL2 daveDeleteProgramBlock(daveConnection*dc, int blockType, int number) { + int res; + PDU p, p2; + uc paDelete[] = { + 0x28, 0, 0, 0, 0, 0, 0, 0xFD, 0, + 0x0a, 0x01, 0x00, + '0', 'C', //Block type in ASCII (0C = FC) + '0', '0', '0', '0', '1', //Block Number in ASCII + 'B', //Direction? + 0x05, //Length of Command + '_', 'D', 'E', 'L', 'E' //Command Delete + }; + + paDelete[13] = blockType; + sprintf((char*)(paDelete + 14), "%05d", number); + paDelete[19] = 'B'; //This is overriden by sprintf via 0x00 as String seperator! + + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 1); + _daveAddParam(&p, paDelete, sizeof(paDelete)); + res = _daveExchange(dc, &p); + if (res == daveResOK) { + res = _daveSetupReceivedPDU(dc, &p2); + if (daveDebug & daveDebugPDU) { + _daveDumpPDU(&p2); + } + } + + //Retval of 0x28 in Recieved PDU Parameter Part means delete was sucessfull. + //This needs to be implemneted and also error Codes Like Block does not exist or block is locked and so on... + return res; +} + +/* +Send Receive NC Program: +*/ + +int DECL2 daveGetNCProgram(daveConnection *dc, const char *filename, uc *buffer, int *length) +{ + int res, len, more, totlen; + unsigned char uploadID[4]; + uc *bb = (uc*)buffer; + len = 0; + totlen = 0; + + res = initUploadNC(dc, filename, &uploadID); + if (res != 0) return res; + do { + res = doUploadNC(dc, &more, &bb, &len, uploadID); + totlen += len; + if (res != 0) return res; + } while (more); + res = endUploadNC(dc, uploadID); + *length = totlen; + return res; +} + +#define MAXUNACKED 20 // Anzahl Telegramme die ohne ack angenommen werden = 20 +int DECL2 daveGetNcFile(daveConnection *dc, const char *filename, char *buffer, int *length) +{ + PDU p, p2; + int res = 0; + uc unackcount = 0; + int filename_len = 0; + int tot_len = 0; + int part_len = 0; + + /* Request upload */ + uc req_up_pa[] = { + 0x00, 0x01, 0x12, 0x04, 0x11, 0x7f, 0x06, 0x00 + }; + uc req_up_da[] = { + 0xff, 0x09, 0x00, 32, + /* Anzahl Telegramme die ohne ack angenommen werden = 20 */ + MAXUNACKED, 0x00, + /* Dateiname max. 32 Zeichen */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00 + }; + + /* Continue upload */ + uc cont_up_pa[] = { + 0x00, 0x01, 0x12, 0x04, 0x11, 0x3f, 0x08, 0x00 + }; + uc cont_up_da[] = { + 0xff, 0x09, 0x00, 0x02, + /* Anzahl Telegramme die ohne ack angenommen werden = 20 */ + MAXUNACKED, 0x00 + }; + + *length = 0; + filename_len = strlen(filename); /* max. 32 chars */ + if (filename_len > 32) { + return -1; + } + req_up_da[3] = filename_len + 2; /* + 1 Byte Anzahl Telegramme + 1 Byte unbekannt */ + memcpy(&req_up_da[6], filename, filename_len); + + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 7); + _daveAddParam(&p, req_up_pa, sizeof(req_up_pa)); + _daveAddData(&p, req_up_da, 6 + filename_len); + + res = _daveExchange(dc, &p); + if (res != daveResOK) { + return res; + } + res = _daveSetupReceivedPDU(dc, &p2); + /* Errorcode im Parameterteil prüfen */ + res = daveGetU16from(&p2.param[10]); + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: Response start upload, param.errorcode=0x%04x\n", res); + FLUSH; + } + if (res != 0) { + return -2; + } + cont_up_pa[7] = p2.param[7]; /* Sequenznummer für alle folgenden Continue-Uploads verwenden */ + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: Verwendete Sequenznummer=%d\n", cont_up_pa[7]); + FLUSH; + } + res = _daveTestResultData(&p2); + if (res != daveResOK) { + return res; + } + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: Response start upload, p2.udlen=%d\n", p2.udlen); + FLUSH; + } + /* Vor den eigentlichen Daten sind hier noch 2 Bytes unbekannter Funktion eingeschoben */ + part_len = p2.udlen - 2; + if (part_len <= 0) { + return -3; + } + memcpy(buffer + tot_len, p2.data + 6, part_len); + tot_len += part_len; + /* Wenn ab hier noch nicht alles gelesen, dann werden bis zu 20 Telegramme gesendet die + * nicht bestätigt werden müssen. Dann gibt es ein ack usw. usf. + */ + while (p2.param[9] != 0) { /* 0 = Last data unit */ + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: unackcount=%d\n", unackcount); + FLUSH; + } + /* Nach 20 Telegrammen continue Telegramm senden */ + if (unackcount == MAXUNACKED) { + if (daveDebug & daveDebugUpload) { + LOG1("daveGetNcFile: Sende continue Telegramm\n"); + } + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 7); + _daveAddParam(&p, cont_up_pa, sizeof(cont_up_pa)); + _daveAddData(&p, cont_up_da, sizeof(cont_up_da)); + res = _daveSendTCP(dc, &p); + if (res != daveResOK) { + return res; + } + unackcount = 0; + } + if (daveDebug & daveDebugUpload) { + LOG1("daveGetNcFile: Empfange Upload Push-Telegramm\n"); + } + res = _daveGetResponseISO_TCP(dc); + if (res != daveResOK) { + return res; + } + res = _daveSetupReceivedPDU(dc, &p2); + /* Errorcode im Parameterteil prüfen */ + res = daveGetU16from(&p2.param[10]); + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: continue upload, param.errorcode=0x%04x\n", res); + FLUSH; + } + if (res != 0) { + return -4; + } + res = _daveTestResultData(&p2); + if (res != daveResOK) { + return res; + } + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: Response start upload, p2.udlen=%d\n", p2.udlen); + FLUSH; + } + part_len = p2.udlen - 2; + if (part_len <= 0) { + return -5; + } + memcpy(buffer + tot_len, p2.data + 6, part_len); + tot_len += part_len; + unackcount++; + } + *length = tot_len; + + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: tot_len=%d\n", tot_len); + LOG2("daveGetNcFile: res=%d\n", res); + FLUSH; + } + return res; +} + +int DECL2 daveGetNcFileSize(daveConnection *dc, const char *filename, int *length) +{ + PDU p, p2; + int res = 0; + uc unackcount = 0; + int filename_len = 0; + int tot_len = 0; + int part_len = 0; + + /* Request upload */ + uc req_up_pa[] = { + 0x00, 0x01, 0x12, 0x04, 0x11, 0x7f, 0x06, 0x00 + }; + uc req_up_da[] = { + 0xff, 0x09, 0x00, 32, + /* Anzahl Telegramme die ohne ack angenommen werden = 20 */ + MAXUNACKED, 0x00, + /* Dateiname max. 32 Zeichen */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00 + }; + + /* Continue upload */ + uc cont_up_pa[] = { + 0x00, 0x01, 0x12, 0x04, 0x11, 0x3f, 0x08, 0x00 + }; + uc cont_up_da[] = { + 0xff, 0x09, 0x00, 0x02, + /* Anzahl Telegramme die ohne ack angenommen werden = 20 */ + MAXUNACKED, 0x00 + }; + + *length = 0; + filename_len = strlen(filename); /* max. 32 chars */ + if (filename_len > 32) { + return -1; + } + req_up_da[3] = filename_len + 2; /* + 1 Byte Anzahl Telegramme + 1 Byte unbekannt */ + memcpy(&req_up_da[6], filename, filename_len); + + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 7); + _daveAddParam(&p, req_up_pa, sizeof(req_up_pa)); + _daveAddData(&p, req_up_da, 6 + filename_len); + + res = _daveExchange(dc, &p); + if (res != daveResOK) { + return res; + } + res = _daveSetupReceivedPDU(dc, &p2); + /* Errorcode im Parameterteil prüfen */ + res = daveGetU16from(&p2.param[10]); + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: Response start upload, param.errorcode=0x%04x\n", res); + FLUSH; + } + if (res != 0) { + return -2; + } + cont_up_pa[7] = p2.param[7]; /* Sequenznummer für alle folgenden Continue-Uploads verwenden */ + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: Verwendete Sequenznummer=%d\n", cont_up_pa[7]); + FLUSH; + } + res = _daveTestResultData(&p2); + if (res != daveResOK) { + return res; + } + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: Response start upload, p2.udlen=%d\n", p2.udlen); + FLUSH; + } + /* Vor den eigentlichen Daten sind hier noch 2 Bytes unbekannter Funktion eingeschoben */ + part_len = p2.udlen - 2; + if (part_len <= 0) { + return -3; + } + tot_len += part_len; + /* Wenn ab hier noch nicht alles gelesen, dann werden bis zu 20 Telegramme gesendet die + * nicht bestätigt werden müssen. Dann gibt es ein ack usw. usf. + */ + while (p2.param[9] != 0) { /* 0 = Last data unit */ + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: unackcount=%d\n", unackcount); + FLUSH; + } + /* Nach 20 Telegrammen continue Telegramm senden */ + if (unackcount == MAXUNACKED) { + if (daveDebug & daveDebugUpload) { + LOG1("daveGetNcFile: Sende continue Telegramm\n"); + } + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 7); + _daveAddParam(&p, cont_up_pa, sizeof(cont_up_pa)); + _daveAddData(&p, cont_up_da, sizeof(cont_up_da)); + res = _daveSendTCP(dc, &p); + if (res != daveResOK) { + return res; + } + unackcount = 0; + } + if (daveDebug & daveDebugUpload) { + LOG1("daveGetNcFile: Empfange Upload Push-Telegramm\n"); + } + res = _daveGetResponseISO_TCP(dc); + if (res != daveResOK) { + return res; + } + res = _daveSetupReceivedPDU(dc, &p2); + /* Errorcode im Parameterteil prüfen */ + res = daveGetU16from(&p2.param[10]); + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: continue upload, param.errorcode=0x%04x\n", res); + FLUSH; + } + if (res != 0) { + return -4; + } + res = _daveTestResultData(&p2); + if (res != daveResOK) { + return res; + } + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: Response start upload, p2.udlen=%d\n", p2.udlen); + FLUSH; + } + part_len = p2.udlen - 2; + if (part_len <= 0) { + return -5; + } + tot_len += part_len; + unackcount++; + } + *length = tot_len; + if (daveDebug & daveDebugUpload) { + LOG2("daveGetNcFile: tot_len=%d\n", tot_len); + LOG2("daveGetNcFile: res=%d\n", res); + FLUSH; + } return res; } -int DECL2 davePutProgramBlock(daveConnection * dc, int blockType, int blknumber, char* buffer, int length) { -#define maxPBlockLen 0xDe // real maximum 222 bytes +#ifdef TestXXX +#include +int DECL2 davePutNCProgram(daveConnection *dc, char *filename, char *pathname, struct tm *ts, char *buffer, int length) +{ + char dt[13]; + sprintf(dt, "%02d%02d%02d%02d%02d%02d", ts->tm_year % 100, ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec); + return davePutNCProgram(dc, filename, pathname, dt, buffer, length); +} +#endif + +/* +1. NC Request -> Request Download +-> Antwort auswerten, ob OK, und das erste Byte für die Anzahl n_unack. Sequenc-Number aus Antwort für weitere verwenden +Data-unit-ref aus Antwort +1 +2. Nur Push senden bis Anzahl unackcount gesendet. +3. Antwort abwarten, unackcount neu auslesen +4. Weitersenden. Bei letztem Paket "Last data unit" auf 0x00 setzen +*/ - int res=0; - int cnt=0; - int size=0; - int blockNumber,rawLen,netLen,blockCont; - int number=0; +// Für Testmodus, Antworten von der NC werden nicht ausgewertet +// #define NCTESTMODE - uc pup[]= { // Load request - 0x1A,0,1,0,0,0,0,0,9, - 0x5F,0x30,0x42,0x30,0x30,0x30,0x30,0x34,0x50, // block type code and number - // _ 0 B 0 0 0 0 4 P - // SDB - 0x0D, - 0x31,0x30,0x30,0x30,0x32,0x30,0x38,0x30,0x30,0x30,0x31,0x31,0x30,0 // file length and netto length - // 1 0 0 0 2 0 8 0 0 0 1 1 0 +//DateTime ts Format: yyMMddHHmmss +int DECL2 davePutNCProgram(daveConnection *dc, char *filename, char *pathname, char *ts, char *buffer, int length) +{ + PDU p, p2; + int res = 0; + int size = 0; + uc unackcount = 0; + uc seq_num = 0; + uc dataunitref = 0; + int max_data_len = 0; + int filename_len = 0; + int prefix_len = 0; + int buffer_pos = 0; + int tot_len = 0; + + /* Request download */ + uc req_down_pa[] = { + 0x00, 0x01, 0x12, 0x04, 0x11, 0x7f, 0x01, 0x00 + }; + uc req_down_da[] = { + 0xff, 0x09, 0x00, 32, + /* Dateiname max. 32 Zeichen */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00 }; - PDU p,p2; + /* Download block */ + uc do_down_pa[] = { + 0x00, 0x01, 0x12, 0x08, 0x12, 0x3f, 0x02, + 0x00, /* sequence number */ + 0x00, /* data unit reference */ + 0x01, /* Last data unit: 0=yes, 1=no */ + 0x00, 0x00 /* errorcode */ + }; + /* 1920 ist die von libnodave vorgeschlagene max. PDU-Größe */ + uc do_down_da[1920 + 6] = { + 0xff, 0x09, + 0x00, 0x00, /* block size in bytes */ + 0x00, 0xfb, + }; - uc pablock[]= { // parameters for parts of a block - 0x1B,0 + /* End download */ + uc end_down_pa[] = { + 0x00, 0x01, 0x12, 0x04, 0x11, 0x7f, 0x04, + 0x00 /* sequence number */ }; - - uc progBlock[maxPBlockLen + 4]= { - 0,maxPBlockLen,0,0xFB, // This seems to be a fix prefix for program blocks + uc end_down_da[] = { + 0xff, 0x09, 0x00, 0x02, 0x00, 0x00 }; - pup[11] = blockType; - paInsert[13] = blockType; - /*pup[12] = number / (10*10*10*10); - pup[13] = (number - (pup[12] * 10*10*10*10 )) / (10*10*10); - pup[14] = (number - (pup[13] * 10*10*10)) / (10*10); - pup[15] = (number - (pup[14] * 10*10)) / (10); - pup[16] = (number - (pup[15] * 10)); - - pup[12] = pup[12] + 0x30; - pup[13] = pup[13] + 0x30; - pup[14] = pup[14] + 0x30; - pup[15] = pup[15] + 0x30; - pup[16] = pup[16] + 0x30;*/ - - int copyLen = maxPBlockLen > length ? length : maxPBlockLen; - memcpy(progBlock+4,buffer,copyLen); + filename_len = strlen(filename); /* max. 32 chars */ + if (filename_len > 32) { + return -1; + } + req_down_da[3] = filename_len; + memcpy(&req_down_da[4], filename, filename_len); - progBlock[9] = (blockType + 0x0A - 'A'); //Convert 'A' to 0x0A - if (blockType == '8') progBlock[9] = 0x08; - - progBlock[10] = blknumber / 0x100; - progBlock[11] = blknumber - (progBlock[10] * 0x100); - - - rawLen=daveGetU16from(progBlock+14); - netLen=daveGetU16from(progBlock+38); - - sprintf((char*)pup+19,"1%06d%06d",rawLen,netLen); - - sprintf((char*)pup+12,"%05d",blknumber); - sprintf((char*)paInsert+14,"%05d",blknumber); - - pup[17]='P'; - paInsert[19]='P'; - - p.header=dc->msgOut+dc->PDUstartO; - _daveInitPDUheader(&p, 1); - _daveAddParam(&p, pup, sizeof(pup)-1); + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 7); + _daveAddParam(&p, req_down_pa, sizeof(req_down_pa)); + _daveAddData(&p, req_down_da, 4 + filename_len); - res=_daveExchange(dc, &p); - if (res==daveResOK) { - res=_daveSetupReceivedPDU(dc, &p2); + res = _daveExchange(dc, &p); +#ifdef NCTESTMODE + res = 0; +#endif + if (res == daveResOK) { + res = _daveSetupReceivedPDU(dc, &p2); if (daveGetDebug() & daveDebugPDU) { _daveDumpPDU(&p2); } - res=daveGetPDUerror(&p2); - - if (res==0) { - blockCont=1; - res=daveGetResponse(dc); - res=_daveSetupReceivedPDU(dc, &p2); - - cnt = 0; - - do { - res=0; - res=_daveSetupReceivedPDU(dc, &p2); - - number=((PDUHeader*)p2.header)->number; - if (p2.param[0]==0x1B) { - //READFILE - copyLen = (cnt+1)*maxPBlockLen > length ? length - (cnt*maxPBlockLen) : maxPBlockLen; - memcpy(progBlock+4,buffer+(cnt*maxPBlockLen),copyLen); - - if (cnt == 0) - { - progBlock[9] = (blockType + 0x0A - 'A'); //Convert 'A' to 0x0A - if (blockType == '8') progBlock[9] = 0x08; - - progBlock[10] = blknumber / 0x100; - progBlock[11] = blknumber - (progBlock[10] * 0x100); + /* Errorcode im Parameterteil prüfen */ + res = daveGetU16from(&p2.param[10]); +#ifdef NCTESTMODE + res = 0; +#endif + if (res == 0) { + seq_num = p2.param[7]; /* Diesen Wert für alle folgenden Downloadtelegramme verwenden */ + unackcount = p2.data[4]; /* Anzahl an Paketen die gesendet werden dürfen, ohne auf ein Ack zu warten */ + do_down_pa[7] = seq_num; + /* Max. Länge in datablock = PDUsize - hlen - plen - dheader */ + max_data_len = daveGetMaxPDULen(dc) - 10 - 12 - 6; + do_down_da[6] = '/0'; + sprintf(do_down_da + 6, "%08d%s ;$PATH=%s\n", + length + strlen(pathname) + 8, + ts, + pathname); + prefix_len = strlen(do_down_da + 6); + tot_len = prefix_len + length; + /* Daten senden bis unackcount = 0, dann auf Antwort warten, usw. usf. */ + while (tot_len > 0) { + do_down_pa[8] = ++dataunitref; + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 7); + if (tot_len > max_data_len) { + size = max_data_len; + do_down_pa[9] = 1; // Last data unit: 1=no + } + else { + size = tot_len; + do_down_pa[9] = 0; // Last data unit: 0=yes + } + memcpy(do_down_da + 6 + prefix_len, buffer + buffer_pos, size - prefix_len); + tot_len -= size; + buffer_pos += size - prefix_len; + prefix_len = 0; + do_down_da[2] = (size + 2) / 256; + do_down_da[3] = (size + 2) % 256; + + LOG2("davePutNCProgram: tot_len=%d\n", tot_len); + + _daveAddParam(&p, do_down_pa, sizeof(do_down_pa)); + _daveAddData(&p, do_down_da, size + 6); + if (daveGetDebug() & daveDebugPDU) { + _daveDumpPDU(&p); + } + if (--unackcount > 0 || do_down_pa[9] == 0) { + /* PDU senden ohne auf Antwort zu warten */ + LOG1("davePutNCProgram: Senden ohne auf Antwort zu warten, Aufruf _daveSendTCP...\n"); + res = _daveSendTCP(dc, &p); + if (res != daveResOK || do_down_pa[9] == 0) { + LOG1("davePutNCProgram: Daten sind gesendet, gehe zu 'End Download'\n"); + break; } - - p.header=dc->msgOut+dc->PDUstartO; - _daveInitPDUheader(&p, 3); - size = maxPBlockLen; - - if (length > ((cnt+1) * maxPBlockLen)) - pablock[1]=1; - else - { - size = length - (cnt * maxPBlockLen); - pablock[1]=0; //last block - blockCont=0; + } + else { + /* PDU senden mit Warten auf Antwort von NC */ + LOG2("davePutNCProgram: unackcount=%d, warte auf Antwort von NC um fortzusetzen...\n", unackcount); + res = _daveExchange(dc, &p); + if (res == daveResOK) { + res = _daveSetupReceivedPDU(dc, &p2); + if (daveGetDebug() & daveDebugPDU) { + _daveDumpPDU(&p2); + } + /* Hier weiß ich nicht wie ein Fehler auszuwerten ist, da weder im + * Kopf- noch im Parameterteil ein Errorcode vorhanden ist. + * Bei Erfolg sollte der Datenteil 6 Bytes groß sein, und unackcount > 0 + */ + + /* push-nc continue */ + if (p2.param[5] == 0x3f && p2.param[6] == 0x03 && p2.dlen == 6) { + unackcount = 8; //p2.data[4]; /* Anzahl an Paketen die gesendet werden dürfen, ohne auf ein Ack zu warten */ + if (unackcount == 0) { + LOG2("davePutNCProgram: in continue response unackcount=%d. Exit!\n", unackcount); + res = daveResUnexpectedFunc; + break; + } + } + else { + LOG2("davePutNCProgram: in continue response, falscher Aufbau der Antwort (p2.param[5] = %d). Exit!\n", p2.param[5]); + res = daveResUnexpectedFunc; + break; + } } - - progBlock[1]=size; - _daveAddParam(&p, pablock, sizeof(pablock)); - _daveAddData(&p, progBlock, size + 4 /* size of block) */); - ((PDUHeader*)p.header)->number=number; + else { + break; + } + } + }; + + if (res == daveResOK) { + /* End-download senden */ + LOG1("davePutNCProgram: Sende End download...\n"); + end_down_pa[7] = seq_num; + p.header = dc->msgOut + dc->PDUstartO; + _daveInitPDUheader(&p, 7); + _daveAddParam(&p, end_down_pa, sizeof(end_down_pa)); + _daveAddData(&p, end_down_da, sizeof(end_down_da)); + res = _daveExchange(dc, &p); + LOG2("davePutNCProgram: End download, res=%d\n", res); + /* Antwort auswerten */ + if (res == daveResOK) { + res = _daveSetupReceivedPDU(dc, &p2); if (daveGetDebug() & daveDebugPDU) { - _daveDumpPDU(&p); + _daveDumpPDU(&p2); + } + if (p2.param[5] == 0xbf && p2.param[6] == 0x04) { + /* Errorcode im Parameterteil prüfen */ + res = daveGetU16from(&p2.param[10]); + LOG2("davePutNCProgram: End download, errorcode in parameterteil war res=%d\n", res); + } + else { + LOG2("davePutNCProgram: End download, Fehler Parameter: p2.param[5]=0x%02x\n", p2.param[5]); + LOG2("davePutNCProgram: End download, Fehler Parameter: p2.param[6]=0x%02x\n", p2.param[6]); + res = daveResUnexpectedFunc; } - _daveExchange(dc,&p); } - cnt++; - } while (blockCont); - - res=_daveSetupReceivedPDU(dc, &p2); - if (daveGetDebug() & daveDebugPDU) { - _daveDumpPDU(&p2); - } - number=((PDUHeader*)p2.header)->number; - if (p2.param[0]==0x1C) { - p.header=dc->msgOut+dc->PDUstartO; - - _daveInitPDUheader(&p, 3); - _daveAddParam(&p, p2.param,1); - ((PDUHeader*)p.header)->number=number; - _daveExchange(dc,&p); - - p.header=dc->msgOut+dc->PDUstartO; - _daveInitPDUheader(&p, 1); - _daveAddParam(&p, paInsert, sizeof(paInsert)); - res=_daveExchange(dc, &p); - res=_daveSetupReceivedPDU(dc, &p2); - res=daveGetPDUerror(&p2); } - } else { - printf("CPU doesn't accept load request:%04X\n",res); - } - return res; - } - return res; -} - -int DECL2 daveDeleteProgramBlock(daveConnection*dc, int blockType, int number) { - int res; - PDU p,p2; - uc paDelete[]= { - 0x28,0,0,0,0,0,0,0xFD,0, - 0x0a,0x01,0x00, - '0','C', //Block type in ASCII (0C = FC) - '0','0','0','0','1', //Block Number in ASCII - 'B', //Direction? - 0x05, //Length of Command - '_','D','E','L','E' //Command Delete - }; - - paDelete[13] = blockType; - sprintf((char*)(paDelete+14),"%05d",number); - paDelete[19] = 'B'; //This is overriden by sprintf via 0x00 as String seperator! - - p.header=dc->msgOut+dc->PDUstartO; - _daveInitPDUheader(&p, 1); - _daveAddParam(&p, paDelete, sizeof(paDelete)); - res=_daveExchange(dc, &p); - if (res==daveResOK) { - res=_daveSetupReceivedPDU(dc, &p2); - if (daveDebug & daveDebugPDU) { - _daveDumpPDU(&p2); + } + else { + LOG2("davePutNCProgram: errorcode erster Antwort: %04X\n", res); } } - - //Retval of 0x28 in Recieved PDU Parameter Part means delete was sucessfull. - //This needs to be implemneted and also error Codes Like Block does not exist or block is locked and so on... return res; } /* -Send Receive NC Program: +Receive Alarm query: */ -int DECL2 daveGetNCProgram(daveConnection *dc, const char *filename, uc *buffer, int *length) { - int res, len, more, totlen; - unsigned char uploadID[4]; - uc *bb=(uc*)buffer; - len=0; - totlen=0; +int DECL2 alarmQueryAlarm_S(daveConnection *dc, void *buffer, int buflen, int *number_of_alarms){ + int res, len, cpylen; + int pa7; + PDU p2; - res=initUploadNC(dc, filename, &uploadID); - if (res!=0) return res; - do { - res=doUploadNC(dc, &more, &bb, &len, uploadID); - totlen+=len; - if (res!=0) return res; - } while (more); - res=endUploadNC(dc, uploadID); - *length=totlen; - return res; -} + uc pa[] = { 0x00, 0x01, 0x12, 0x04, 0x11, 0x44, 0x13, 0x00 }; /* Parameter 1. Anfrage */ + uc da[] = { 0x00, 0x01, 0x12, 0x08, 0x1a, 0x00, 0x01, 0x34, 0x00, 0x00, 0x00, 0x04 }; /* Datenteil 1. Anfrage */ + uc pa2[] = { 0x00, 0x01, 0x12, 0x08, 0x12, 0x44, 0x13, 0x01, 0x00, 0x00, 0x00, 0x00 }; /* Parameter weitere Anfragen */ -/* -1. NC Request -> Request Download - -> Antwort auswerten, ob OK, und das erste Byte für die Anzahl n_unack. Sequenc-Number aus Antwort für weitere verwenden - Data-unit-ref aus Antwort +1 -2. Nur Push senden bis Anzahl unackcount gesendet. -3. Antwort abwarten, unackcount neu auslesen -4. Weitersenden. Bei letztem Paket "Last data unit" auf 0x00 setzen -*/ + *number_of_alarms = 0; -// Für Testmodus, Antworten von der NC werden nicht ausgewertet -// #define NCTESTMODE + res = daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), da, sizeof(da)); + if (res != daveResOK) return res; -//DateTime ts Format: yyMMddHHmmss -int DECL2 davePutNCProgram(daveConnection *dc, char *filename, char *pathname, char *ts, char *buffer, int length) -{ - PDU p, p2; - int res = 0; - int size = 0; - uc unackcount = 0; - uc seq_num = 0; - uc dataunitref = 0; - int max_data_len = 0; - int filename_len = 0; - int prefix_len = 0; - int buffer_pos = 0; - int tot_len = 0; - - /* Request download */ - uc req_down_pa[]= { - 0x00, 0x01, 0x12, 0x04, 0x11, 0x7f, 0x01, 0x00 - }; - uc req_down_da[]= { - 0xff, 0x09, 0x00, 32, - /* Dateiname max. 32 Zeichen */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00 - }; - - /* Download block */ - uc do_down_pa[]= { - 0x00, 0x01, 0x12, 0x08, 0x12, 0x3f, 0x02, - 0x00, /* sequence number */ - 0x00, /* data unit reference */ - 0x01, /* Last data unit: 0=yes, 1=no */ - 0x00, 0x00 /* errorcode */ - }; - /* 1920 ist die von libnodave vorgeschlagene max. PDU-Größe */ - uc do_down_da[1920 + 6]= { - 0xff, 0x09, - 0x00, 0x00, /* block size in bytes */ - 0x00, 0xfb, - }; - - /* End download */ - uc end_down_pa[]= { - 0x00, 0x01, 0x12, 0x04, 0x11, 0x7f, 0x04, - 0x00 /* sequence number */ - }; - uc end_down_da[]= { - 0xff, 0x09, 0x00, 0x02, 0x00, 0x00 - }; - - filename_len = strlen(filename); /* max. 32 chars */ - if (filename_len > 32) { - return -1; - } - req_down_da[3] = filename_len; - memcpy(&req_down_da[4], filename, filename_len); - - p.header = dc->msgOut + dc->PDUstartO; - _daveInitPDUheader(&p, 7); - _daveAddParam(&p, req_down_pa, sizeof(req_down_pa)); - _daveAddData(&p, req_down_da, 4 + filename_len); - - res = _daveExchange(dc, &p); - #ifdef NCTESTMODE - res = 0; - #endif - if (res == daveResOK) { - res = _daveSetupReceivedPDU(dc, &p2); - if (daveGetDebug() & daveDebugPDU) { - _daveDumpPDU(&p2); - } - /* Errorcode im Parameterteil prüfen */ - res = daveGetU16from(&p2.param[10]); - #ifdef NCTESTMODE - res = 0; - #endif - if (res == 0) { - seq_num = p2.param[7]; /* Diesen Wert für alle folgenden Downloadtelegramme verwenden */ - unackcount = p2.data[4]; /* Anzahl an Paketen die gesendet werden dürfen, ohne auf ein Ack zu warten */ - do_down_pa[7] = seq_num; - /* Max. Länge in datablock = PDUsize - hlen - plen - dheader */ - max_data_len = daveGetMaxPDULen(dc) - 10 - 12 - 6; - do_down_da[6] = '/0'; - sprintf(do_down_da + 6, "%08d%s ;$PATH=%s\n", - length + strlen(pathname) + 8, - ts, - pathname); - prefix_len = strlen(do_down_da + 6); - tot_len = prefix_len + length; - /* Daten senden bis unackcount = 0, dann auf Antwort warten, usw. usf. */ - while (tot_len > 0) { - do_down_pa[8] = ++dataunitref; - p.header = dc->msgOut + dc->PDUstartO; - _daveInitPDUheader(&p, 7); - if (tot_len > max_data_len) { - size = max_data_len; - do_down_pa[9] = 1; // Last data unit: 1=no - } else { - size = tot_len; - do_down_pa[9] = 0; // Last data unit: 0=yes - } - memcpy(do_down_da + 6 + prefix_len, buffer + buffer_pos, size - prefix_len); - tot_len -= size; - buffer_pos += size - prefix_len; - prefix_len = 0; - do_down_da[2] = (size+2) / 256; - do_down_da[3] = (size+2) % 256; - - LOG2("davePutNCProgram: tot_len=%d\n", tot_len); - - _daveAddParam(&p, do_down_pa, sizeof(do_down_pa)); - _daveAddData(&p, do_down_da, size + 6); - if (daveGetDebug() & daveDebugPDU) { - _daveDumpPDU(&p); - } - if (--unackcount > 0 || do_down_pa[9] == 0) { - /* PDU senden ohne auf Antwort zu warten */ - LOG1("davePutNCProgram: Senden ohne auf Antwort zu warten, Aufruf _daveSendTCP...\n"); - res = _daveSendTCP(dc, &p); - if (res != daveResOK || do_down_pa[9] == 0) { - LOG1("davePutNCProgram: Daten sind gesendet, gehe zu 'End Download'\n"); - break; - } - } else { - /* PDU senden mit Warten auf Antwort von NC */ - LOG2("davePutNCProgram: unackcount=%d, warte auf Antwort von NC um fortzusetzen...\n", unackcount); - res = _daveExchange(dc, &p); - if (res == daveResOK) { - res = _daveSetupReceivedPDU(dc, &p2); - if (daveGetDebug() & daveDebugPDU) { - _daveDumpPDU(&p2); - } - /* Hier weiß ich nicht wie ein Fehler auszuwerten ist, da weder im - * Kopf- noch im Parameterteil ein Errorcode vorhanden ist. - * Bei Erfolg sollte der Datenteil 6 Bytes groß sein, und unackcount > 0 - */ - - /* push-nc continue */ - if (p2.param[5] == 0x3f && p2.param[6] == 0x03 && p2.dlen == 6) { - unackcount = 8; //p2.data[4]; /* Anzahl an Paketen die gesendet werden dürfen, ohne auf ein Ack zu warten */ - if (unackcount == 0) { - LOG2("davePutNCProgram: in continue response unackcount=%d. Exit!\n", unackcount); - res = daveResUnexpectedFunc; - break; - } - } else { - LOG2("davePutNCProgram: in continue response, falscher Aufbau der Antwort (p2.param[5] = %d). Exit!\n", p2.param[5]); - res = daveResUnexpectedFunc; - break; - } - } else { - break; - } - } - }; - - if (res == daveResOK) { - /* End-download senden */ - LOG1("davePutNCProgram: Sende End download...\n"); - end_down_pa[7] = seq_num; - p.header = dc->msgOut + dc->PDUstartO; - _daveInitPDUheader(&p, 7); - _daveAddParam(&p, end_down_pa, sizeof(end_down_pa)); - _daveAddData(&p, end_down_da, sizeof(end_down_da)); - res = _daveExchange(dc, &p); - LOG2("davePutNCProgram: End download, res=%d\n", res); - /* Antwort auswerten */ - if (res == daveResOK) { - res = _daveSetupReceivedPDU(dc, &p2); - if (daveGetDebug() & daveDebugPDU) { - _daveDumpPDU(&p2); - } - if (p2.param[5] == 0xbf && p2.param[6] == 0x04) { - /* Errorcode im Parameterteil prüfen */ - res = daveGetU16from(&p2.param[10]); - LOG2("davePutNCProgram: End download, errorcode in parameterteil war res=%d\n", res); - } else { - LOG2("davePutNCProgram: End download, Fehler Parameter: p2.param[5]=0x%02x\n", p2.param[5]); - LOG2("davePutNCProgram: End download, Fehler Parameter: p2.param[6]=0x%02x\n", p2.param[6]); - res = daveResUnexpectedFunc; - } - } - } - } else { - LOG2("davePutNCProgram: errorcode erster Antwort: %04X\n", res); - } - } - return res; + len = 0; + pa7 = p2.param[7]; /* Sequence number für weitere Abfragen */ + + if (p2.udlen < 2) return daveEmptyResultError; + /* Aufbau des 1. Antworttelegramms ist anders und besitzt noch einen 6 Byte Header */ + if (p2.udlen > 6 && p2.udata[1] == 1 && p2.udata[2] == 0xff) { + *number_of_alarms += 1; + cpylen = p2.udlen - 6; + if (buffer != NULL) { + if (len + cpylen > buflen) cpylen = buflen - len; + if (cpylen > 0) memcpy((uc *)buffer + len, &(p2.udata[6]), cpylen); + len += cpylen; + } + } + + if (p2.param[9] != 0) { + pa2[7] = pa7; + res = daveBuildAndSendPDU(dc, &p2, pa2, sizeof(pa2), NULL, 1); + if (res != daveResOK) return res; + + while (p2.param[9] != 0) { /* Last Data unit 0=Yes*/ + if (p2.udlen > 2) { + *number_of_alarms += 1; + cpylen = p2.udlen; + if (buffer != NULL) { + if (len + cpylen > buflen) cpylen = buflen - len; + if (cpylen > 0) memcpy((uc *)buffer + len, p2.udata, cpylen); + len += cpylen; + } + } + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + pa2[7] = pa7; + res = daveBuildAndSendPDU(dc, &p2, pa2, sizeof(pa2), NULL, 1); + if (res != daveResOK) return res; + } + } + + dc->AnswLen = len; + return res; } int DECL2 daveReadPLCTime(daveConnection * dc) { int res, len; PDU p2; - uc pa[]={0,1,18,4,17,'G',1,0}; + uc pa[] = { 0, 1, 18, 4, 17, 'G', 1, 0 }; #ifdef DEBUG_CALLS LOG2("daveGetTime(dc:%p)\n", dc); FLUSH; -#endif - len=0;res=daveBuildAndSendPDU(dc, &p2,pa, sizeof(pa), NULL, 1); - if (res==daveResOK) { - dc->resultPointer=p2.udata; - dc->_resultPointer=p2.udata; - len=p2.udlen; - } else { - if(daveDebug & daveDebugPrintErrors) - LOG3("daveGetTime: %04X=%s\n",res, daveStrerror(res)); +#endif + len = 0; res = daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), NULL, 1); + if (res == daveResOK) { + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + len = p2.udlen; } - dc->AnswLen=len; + else { + if (daveDebug & daveDebugPrintErrors) + LOG3("daveGetTime: %04X=%s\n", res, daveStrerror(res)); + } + dc->AnswLen = len; return res; -} +} -int DECL2 daveSetPLCTime(daveConnection * dc,uc * ts) { +int DECL2 daveSetPLCTime(daveConnection * dc, uc * ts) { int res, len; PDU p2; - uc pa[]={0,1,18,4,17,'G',2,0}; + uc pa[] = { 0, 1, 18, 4, 17, 'G', 2, 0 }; #ifdef DEBUG_CALLS LOG2("daveSetTime(dc:%p)\n", dc); FLUSH; -#endif - len=0; - res=daveBuildAndSendPDU(dc, &p2,pa, sizeof(pa), ts, 10); - if (res==daveResOK) { - dc->resultPointer=p2.udata; - dc->_resultPointer=p2.udata; - len=p2.udlen; - } else { - if(daveDebug & daveDebugPrintErrors) - LOG3("daveGetTime: %04X=%s\n",res, daveStrerror(res)); +#endif + len = 0; + res = daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), ts, 10); + if (res == daveResOK) { + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + len = p2.udlen; + } + else { + if (daveDebug & daveDebugPrintErrors) + LOG3("daveGetTime: %04X=%s\n", res, daveStrerror(res)); } - dc->AnswLen=len; + dc->AnswLen = len; return res; -} +} uc DECL2 daveToBCD(uc i) { - return 16*(i /10)+(i%10); + return 16 * (i / 10) + (i % 10); } uc DECL2 daveFromBCD(uc i) { - return 10*(i /16)+(i%16); + return 10 * (i / 16) + (i % 16); } int DECL2 daveSetPLCTimeToSystime(daveConnection * dc) { int res, len; PDU p2; - uc pa[]={0,1,18,4,17,'G',2,0}; - uc ts[]={ - 0x00,0x19,0x05,0x08,0x23,0x04,0x10,0x23,0x67,0x83, - }; -#ifdef LINUX + uc pa[] = { 0, 1, 18, 4, 17, 'G', 2, 0 }; + uc ts[] = { + 0x00, 0x19, 0x05, 0x08, 0x23, 0x04, 0x10, 0x23, 0x67, 0x83, + }; +#ifdef LINUX struct tm systime; struct timeval t1; gettimeofday(&t1, NULL); - localtime_r(&(t1.tv_sec),&systime); - t1.tv_usec/=100; //tenth of miliseconds from microseconds - // ts[1]=daveToBCD(systime.tm_year/100+19); - ts[1]=daveToBCD(systime.tm_year / 100); // fix 2010 bug is this line necessary? ok? - ts[2]=daveToBCD(systime.tm_year % 100); - ts[3]=daveToBCD(systime.tm_mon+1); - ts[4]=daveToBCD(systime.tm_mday); - ts[5]=daveToBCD(systime.tm_hour); - ts[6]=daveToBCD(systime.tm_min); - ts[7]=daveToBCD(systime.tm_sec); - ts[8]=daveToBCD(t1.tv_usec/100); - ts[9]=daveToBCD(t1.tv_usec%100); - // _daveDump("timestamp: ",ts,10); - // LOG2("tm.sec: %d\n", systime.tm_sec); - // LOG2("tm.min: %d\n", systime.tm_min); - // LOG2("tm.hour: %d\n", systime.tm_hour); -#endif + localtime_r(&(t1.tv_sec), &systime); + t1.tv_usec /= 100; //tenth of miliseconds from microseconds + // ts[1]=daveToBCD(systime.tm_year/100+19); + ts[1] = daveToBCD(systime.tm_year / 100); // fix 2010 bug is this line necessary? ok? + ts[2] = daveToBCD(systime.tm_year % 100); + ts[3] = daveToBCD(systime.tm_mon + 1); + ts[4] = daveToBCD(systime.tm_mday); + ts[5] = daveToBCD(systime.tm_hour); + ts[6] = daveToBCD(systime.tm_min); + ts[7] = daveToBCD(systime.tm_sec); + ts[8] = daveToBCD(t1.tv_usec / 100); + ts[9] = daveToBCD(t1.tv_usec % 100); + // _daveDump("timestamp: ",ts,10); + // LOG2("tm.sec: %d\n", systime.tm_sec); + // LOG2("tm.min: %d\n", systime.tm_min); + // LOG2("tm.hour: %d\n", systime.tm_hour); +#endif #ifdef BCCWIN SYSTEMTIME t1; - // gettimeofday(&t1, NULL); + // gettimeofday(&t1, NULL); GetLocalTime(&t1); - // tm=localtime(&t1); - // t1.tv_usec/=100; //tenth of miliseconds from microseconds - // WORD wYear; + // tm=localtime(&t1); + // t1.tv_usec/=100; //tenth of miliseconds from microseconds + // WORD wYear; /* WORD wMonth; WORD wDayOfWeek; @@ -6746,119 +7229,121 @@ int DECL2 daveSetPLCTimeToSystime(daveConnection * dc) { WORD wSecond; WORD wMilliseconds; */ - ts[1]=daveToBCD(t1.wYear / 100); // fix 2010 bug is this line necessary? ok? - ts[2]=daveToBCD(t1.wYear % 100); - ts[3]=daveToBCD(t1.wMonth); - ts[4]=daveToBCD(t1.wDay); - ts[5]=daveToBCD(t1.wHour); - ts[6]=daveToBCD(t1.wMinute); - ts[7]=daveToBCD(t1.wSecond); - ts[8]=daveToBCD(t1.wMilliseconds/10); - ts[9]=daveToBCD((t1.wMilliseconds%10)*10); - // _daveDump("timestamp: ",ts,10); - // LOG2("tm.sec: %d\n", t1.wSecond); - // LOG2("tm.min: %d\n", t1.wMinute); - // LOG2("tm.hour: %d\n", t1.wHour); -#endif + ts[1] = daveToBCD(t1.wYear / 100); // fix 2010 bug is this line necessary? ok? + ts[2] = daveToBCD(t1.wYear % 100); + ts[3] = daveToBCD(t1.wMonth); + ts[4] = daveToBCD(t1.wDay); + ts[5] = daveToBCD(t1.wHour); + ts[6] = daveToBCD(t1.wMinute); + ts[7] = daveToBCD(t1.wSecond); + ts[8] = daveToBCD(t1.wMilliseconds / 10); + ts[9] = daveToBCD((t1.wMilliseconds % 10) * 10); + // _daveDump("timestamp: ",ts,10); + // LOG2("tm.sec: %d\n", t1.wSecond); + // LOG2("tm.min: %d\n", t1.wMinute); + // LOG2("tm.hour: %d\n", t1.wHour); +#endif #ifdef DEBUG_CALLS LOG2("SetPLCTimeToSystime(dc:%p)\n", dc); FLUSH; -#endif - len=0; - res=daveBuildAndSendPDU(dc, &p2,pa, sizeof(pa), ts, sizeof(ts)); - if (res==daveResOK) { - dc->resultPointer=p2.udata; - dc->_resultPointer=p2.udata; - len=p2.udlen; - } else { - if(daveDebug & daveDebugPrintErrors) - LOG3("daveGetTime: %04X=%s\n",res, daveStrerror(res)); +#endif + len = 0; + res = daveBuildAndSendPDU(dc, &p2, pa, sizeof(pa), ts, sizeof(ts)); + if (res == daveResOK) { + dc->resultPointer = p2.udata; + dc->_resultPointer = p2.udata; + len = p2.udlen; + } + else { + if (daveDebug & daveDebugPrintErrors) + LOG3("daveGetTime: %04X=%s\n", res, daveStrerror(res)); } - dc->AnswLen=len; + dc->AnswLen = len; return res; -} +} /*************** Simatic S5: ****************/ -uc __davet2[]={STX}; -uc __davet10[]={DLE}; -char __davet1006[]={DLE,ACK}; // cs: this is only sent by system functions, so let it be char -us __daveT1006[]={DLE,ACK}; -uc __davet121003[]={0x12,DLE,ETX}; -us __daveT121003[]={0x12,DLE,ETX}; -uc __davet161003[]={0x16,DLE,ETX}; -us __daveT161003[]={0x16,DLE,ETX}; +uc __davet2[] = { STX }; +uc __davet10[] = { DLE }; +char __davet1006[] = { DLE, ACK }; // cs: this is only sent by system functions, so let it be char +us __daveT1006[] = { DLE, ACK }; +uc __davet121003[] = { 0x12, DLE, ETX }; +us __daveT121003[] = { 0x12, DLE, ETX }; +uc __davet161003[] = { 0x16, DLE, ETX }; +us __daveT161003[] = { 0x16, DLE, ETX }; /* -Reads bytes from area with offset , -that can be readed with daveGetInteger etc. You can read bytes from +Reads bytes from area with offset , +that can be readed with daveGetInteger etc. You can read bytes from PBs & FBs too, but use daveReadBlock for this: */ int DECL2 daveReadS5Bytes(daveConnection * dc, uc area, uc BlockN, int offset, int count) { - int res,datastart,dataend; + int res, datastart, dataend; daveS5AreaInfo ai; uc b1[daveMaxRawLen]; - // if (_daveIsS5BlockArea(area)==0) { - if (area==daveDB) { - res=_daveReadS5BlockAddress(dc,area,BlockN,&ai);//TODO make address cache + // if (_daveIsS5BlockArea(area)==0) { + if (area == daveDB) { + res = _daveReadS5BlockAddress(dc, area, BlockN, &ai);//TODO make address cache if (res<0) { LOG2("%s *** Error in ReadS5Bytes.BlockAddr request.\n", dc->iface->name); - return res-50; + return res - 50; } - datastart=ai.address; - } else { + datastart = ai.address; + } + else { switch (area) { - case daveRawMemoryS5: datastart=0; break; - case daveInputs: datastart=dc->cache->PAE; break; - case daveOutputs: datastart=dc->cache->PAA; break; - case daveFlags: datastart=dc->cache->flags; break; - case daveTimer: datastart=dc->cache->timers; break; - case daveCounter: datastart=dc->cache->counters; break; - case daveSysDataS5: datastart=dc->cache->systemData; break; + case daveRawMemoryS5: datastart = 0; break; + case daveInputs: datastart = dc->cache->PAE; break; + case daveOutputs: datastart = dc->cache->PAA; break; + case daveFlags: datastart = dc->cache->flags; break; + case daveTimer: datastart = dc->cache->timers; break; + case daveCounter: datastart = dc->cache->counters; break; + case daveSysDataS5: datastart = dc->cache->systemData; break; default: LOG2("%s *** Unknown area in ReadS5Bytes request.\n", dc->iface->name); return -1; - } + } } //It's difficult to convert Intel-Motorola so I will use arithmetic: - if ((count>daveMaxRawLen) - // ||(offset+count>ai.len) + if ((count > daveMaxRawLen) + // ||(offset+count>ai.len) ) { - LOG2("%s *** readS5Bytes: Requested data is out-of-range.\n", dc->iface->name); - return -1; + LOG2("%s *** readS5Bytes: Requested data is out-of-range.\n", dc->iface->name); + return -1; } - datastart+=offset; - dataend=datastart+count-1; - b1[0]=datastart/256; - b1[1]=datastart%256; - b1[2]=dataend/256; - b1[3]=dataend%256; - res=_daveExchangeAS511(dc,b1,4,2*count+7,0x04); - if (res<0) { + datastart += offset; + dataend = datastart + count - 1; + b1[0] = datastart / 256; + b1[1] = datastart % 256; + b1[2] = dataend / 256; + b1[3] = dataend % 256; + res = _daveExchangeAS511(dc, b1, 4, 2 * count + 7, 0x04); + if (res < 0) { LOG2("%s *** Error in ReadS5Bytes.Exchange sequence.\n", dc->iface->name); - return res-10; + return res - 10; } - if (dc->AnswLeniface->name,dc->AnswLen); + if (dc->AnswLen < count + 7) { + LOG3("%s *** Too few chars (%d) in ReadS5Bytes data.\n", dc->iface->name, dc->AnswLen); return -5; } - if ((dc->msgIn[0]!=0)||(dc->msgIn[1]!=0)||(dc->msgIn[2]!=0)||(dc->msgIn[3]!=0)||(dc->msgIn[4]!=0)) { + if ((dc->msgIn[0] != 0) || (dc->msgIn[1] != 0) || (dc->msgIn[2] != 0) || (dc->msgIn[3] != 0) || (dc->msgIn[4] != 0)) { LOG2("%s *** Wrong ReadS5Bytes data signature.\n", dc->iface->name); return -6; } - dc->resultPointer=dc->msgIn+5; - dc->_resultPointer=dc->resultPointer; + dc->resultPointer = dc->msgIn + 5; + dc->_resultPointer = dc->resultPointer; - // dc->dlen=dc->AnswLen-7; - dc->AnswLen-=7; + // dc->dlen=dc->AnswLen-7; + dc->AnswLen -= 7; return 0; } -/* +/* Write DLE,ACK to the serial interface: */ void DECL2 _daveSendDLEACK(daveInterface * di) // serial interface @@ -6866,36 +7351,36 @@ void DECL2 _daveSendDLEACK(daveInterface * di) // serial interface di->ifwrite(di, __davet1006, 2); } -/* +/* Sends a sequence of characters after doubling DLEs and adding DLE,EOT. */ int DECL2 _daveSendWithDLEDup(daveInterface * di, // serial interface - uc *b, // a buffer containing the message - int size // the size of the string + uc *b, // a buffer containing the message + int size // the size of the string ) -{ - uc target[daveMaxRawLen]; +{ + uc target[daveMaxRawLen]; int res; - int targetSize=0,i; //preload - if(daveDebug & daveDebugExchange) + int targetSize = 0, i; //preload + if (daveDebug & daveDebugExchange) LOG1("SendWithDLEDup: \n"); - if(daveDebug & daveDebugExchange) - _daveDump("I send",b,size); - for (i=0; iifwrite(di, (char*)target, targetSize); - if(daveDebug & daveDebugExchange) - LOG2("send: res:%d\n",res); + res = di->ifwrite(di, (char*)target, targetSize); + if (daveDebug & daveDebugExchange) + LOG2("send: res:%d\n", res); return 0; } @@ -6909,25 +7394,25 @@ int DECL2 _daveReqTrans(daveConnection * dc, uc trN) if (daveDebug & daveDebugExchange) LOG3("%s daveReqTrans %d\n", dc->iface->name, trN); _daveSendSingle(dc->iface, STX); - res=dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT1006)/2); + res = dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT1006) / 2); if (daveDebug & daveDebugByte) - _daveDump("2got",b1, res); - if (_daveMemcmp(__daveT1006, b1, sizeof(__daveT1006)/2)) { + _daveDump("2got", b1, res); + if (_daveMemcmp(__daveT1006, b1, sizeof(__daveT1006) / 2)) { if (daveDebug & daveDebugPrintErrors) LOG3("%s daveReqTrans %d *** no DLE,ACK before send.\n", dc->iface->name, trN); return -1; } _daveSendSingle(dc->iface, trN); - if (_daveReadSingle(dc->iface)!=STX) { + if (_daveReadSingle(dc->iface) != STX) { if (daveDebug & daveDebugPrintErrors) LOG3("%s daveReqTrans %d *** no STX before send.\n", dc->iface->name, trN); return -2; } _daveSendDLEACK(dc->iface); - dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT161003)/2); - if (daveDebug & daveDebugByte) - _daveDump("1got",b1, res); - if (_daveMemcmp(__daveT161003, b1, sizeof(__daveT161003)/2)) { + dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT161003) / 2); + if (daveDebug & daveDebugByte) + _daveDump("1got", b1, res); + if (_daveMemcmp(__daveT161003, b1, sizeof(__daveT161003) / 2)) { if (daveDebug & daveDebugPrintErrors) LOG3("%s daveReqTrans %d *** no accept0 from plc.\n", dc->iface->name, trN); return -3; @@ -6945,18 +7430,18 @@ int DECL2 _daveEndTrans(daveConnection * dc) uc b1[3]; if (daveDebug & daveDebugExchange) LOG2("%s daveEndTrans\n", dc->iface->name); - if (_daveReadSingle(dc->iface)!=STX) { + if (_daveReadSingle(dc->iface) != STX) { LOG2("%s daveEndTrans *** no STX at eot sequense.\n", dc->iface->name); // return -1; } _daveSendDLEACK(dc->iface); - res=dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT121003)/2); + res = dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT121003) / 2); if (daveDebug & daveDebugByte) - _daveDump("3got",b1, res); - if (_daveMemcmp(__daveT121003, b1, sizeof(__daveT121003)/2)) { + _daveDump("3got", b1, res); + if (_daveMemcmp(__daveT121003, b1, sizeof(__daveT121003) / 2)) { LOG2("%s daveEndTrans *** no accept of eot/ETX from plc.\n", dc->iface->name); return -2; - } + } _daveSendDLEACK(dc->iface); return 0; } @@ -6965,76 +7450,77 @@ int DECL2 _daveEndTrans(daveConnection * dc) Remove the DLE doubling: */ int DECL2 _daveDLEDeDup(daveConnection * dc, uc* rawBuf, int rawLen) { - int j=0,k; - for (k=0;kmsgIn[j]=rawBuf[k]; j++; - if (DLE==rawBuf[k]){ - if (DLE!=rawBuf[k+1]) return -1;//Bad doubling found + int j = 0, k; + for (k = 0; k < rawLen - 2; k++){ + dc->msgIn[j] = rawBuf[k]; j++; + if (DLE == rawBuf[k]){ + if (DLE != rawBuf[k + 1]) return -1;//Bad doubling found k++; } } - dc->msgIn[j]=rawBuf[k];//Copy 2 last chars (DLE,ETX) - j++;k++; - dc->msgIn[j]=rawBuf[k]; - dc->AnswLen=j+1; + dc->msgIn[j] = rawBuf[k];//Copy 2 last chars (DLE,ETX) + j++; k++; + dc->msgIn[j] = rawBuf[k]; + dc->AnswLen = j + 1; return 0; } int DECL2 _daveExchangeAS511(daveConnection * dc, uc * b, int len, int maxlen, int trN) { int res, i; uc b1[3]; - res=_daveReqTrans(dc, trN); - if (res<0) { + res = _daveReqTrans(dc, trN); + if (res < 0) { LOG2("%s *** Error in Exchange.ReqTrans request.\n", dc->iface->name); - return res-10; + return res - 10; } - if (trN==8) { //Block write functions have advanced syntax + if (trN == 8) { //Block write functions have advanced syntax LOG1("trN 8\n"); - _daveSendWithDLEDup(dc->iface,b,4); + _daveSendWithDLEDup(dc->iface, b, 4); LOG1("trN 8 done\n"); - } else { + } + else { if (daveDebug & daveDebugExchange) - LOG3("trN %d len %d\n",trN,len); - _daveSendWithDLEDup(dc->iface,b,len); + LOG3("trN %d len %d\n", trN, len); + _daveSendWithDLEDup(dc->iface, b, len); if (daveDebug & daveDebugExchange) - LOG2("trN %d done\n",trN); - } - // _daveSendDLEACK(dc->iface); - // res=dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ 2000 /*sizeof(__daveT1006)/2*/); - res=dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT1006)/2); - if (daveDebug & daveDebugByte) - _daveDump("4 got:",b1, res); - if (_daveMemcmp(__daveT1006, b1, sizeof(__daveT1006)/2)) { + LOG2("trN %d done\n", trN); + } + // _daveSendDLEACK(dc->iface); + // res=dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ 2000 /*sizeof(__daveT1006)/2*/); + res = dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT1006) / 2); + if (daveDebug & daveDebugByte) + _daveDump("4 got:", b1, res); + if (_daveMemcmp(__daveT1006, b1, sizeof(__daveT1006) / 2)) { LOG2("%s *** no DLE,ACK in Exchange request.\n", dc->iface->name); return -1; } - if ((trN!=3)&&(trN!=7)&&(trN!=9)) {//write bytes, compress & delblk - if (!_daveReadSingle(dc->iface)==STX) { + if ((trN != 3) && (trN != 7) && (trN != 9)) {//write bytes, compress & delblk + if (!_daveReadSingle(dc->iface) == STX) { LOG2("%s *** no STX in Exchange request.\n", dc->iface->name); return -2; } // usleep(500000); _daveSendDLEACK(dc->iface); - res=0; + res = 0; do { -// i=dc->iface->ifread(dc->iface, dc->msgIn+res, /*100*dc->iface->timeout,*/ daveMaxRawLen-res); - i=dc->iface->ifread(dc->iface, dc->msgIn+res, /*100*dc->iface->timeout,*/ 1); - res+=i; + // i=dc->iface->ifread(dc->iface, dc->msgIn+res, /*100*dc->iface->timeout,*/ daveMaxRawLen-res); + i = dc->iface->ifread(dc->iface, dc->msgIn + res, /*100*dc->iface->timeout,*/ 1); + res += i; if (daveDebug & daveDebugByte) - _daveDump("5 got:",dc->msgIn, res); - } while((i>0)&& ( (dc->msgIn[res-2]!=DLE) || (dc->msgIn[res-1]!=ETX))); + _daveDump("5 got:", dc->msgIn, res); + } while ((i > 0) && ((dc->msgIn[res - 2] != DLE) || (dc->msgIn[res - 1] != ETX))); if (daveDebug & daveDebugByte) - LOG3("%s *** got %d bytes.\n", dc->iface->name,res); - if (res<0) { + LOG3("%s *** got %d bytes.\n", dc->iface->name, res); + if (res < 0) { LOG2("%s *** Error in Exchange.ReadChars request.\n", dc->iface->name); - return res-20; + return res - 20; } - if ((dc->msgIn[res-2]!=DLE)||(dc->msgIn[res-1]!=ETX)) { + if ((dc->msgIn[res - 2] != DLE) || (dc->msgIn[res - 1] != ETX)) { LOG2("%s *** No DLE,ETX in Exchange data.\n", dc->iface->name); return -4; } - if (_daveDLEDeDup(dc,dc->msgIn,res)<0) { + if (_daveDLEDeDup(dc, dc->msgIn, res) < 0) { LOG2("%s *** Error in Exchange rawdata.\n", dc->iface->name); return -3; } @@ -7042,35 +7528,35 @@ int DECL2 _daveExchangeAS511(daveConnection * dc, uc * b, int len, int maxlen, i // usleep(500000); _daveSendDLEACK(dc->iface); } - if (trN==8) { //Write requests have more differences from others ;( - if (dc->msgIn[0]!=9) { + if (trN == 8) { //Write requests have more differences from others ;( + if (dc->msgIn[0] != 9) { LOG2("%s 8 *** No 0x09 in special Exchange request.\n", dc->iface->name); return -5; } - _daveSendSingle(dc->iface,STX); - res=dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT1006)/2); - _daveDump("got:",b1, res); - if (_daveMemcmp(__daveT1006, b1, sizeof(__daveT1006)/2)) { + _daveSendSingle(dc->iface, STX); + res = dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT1006) / 2); + _daveDump("got:", b1, res); + if (_daveMemcmp(__daveT1006, b1, sizeof(__daveT1006) / 2)) { LOG2("%s 8 *** no DLE,ACK in special Exchange request.\n", dc->iface->name); return -6; } - _daveSendWithDLEDup(dc->iface,b+4,len); + _daveSendWithDLEDup(dc->iface, b + 4, len); - res=dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT1006)/2); - _daveDump("got:",b1, res); - if (_daveMemcmp(__daveT1006, b1, sizeof(__daveT1006)/2)) { - // if (!_daveTestChars(dc->iface, __davet1006, 2)) { + res = dc->iface->ifread(dc->iface, b1, /*dc->iface->timeout,*/ sizeof(__daveT1006) / 2); + _daveDump("got:", b1, res); + if (_daveMemcmp(__daveT1006, b1, sizeof(__daveT1006) / 2)) { + // if (!_daveTestChars(dc->iface, __davet1006, 2)) { LOG2("%s 8 *** no DLE,ACK after transfer in Exchange.\n", dc->iface->name); return -7; } } - if (trN==7) { - // usleep(450000); + if (trN == 7) { + // usleep(450000); }//TODO: check compression time - res=_daveEndTrans(dc); - if (res<0) { + res = _daveEndTrans(dc); + if (res < 0) { LOG2("%s *** Error in Exchange.EndTrans request.\n", dc->iface->name); - return res-30; + return res - 30; } return 0; } @@ -7078,28 +7564,28 @@ int DECL2 _daveExchangeAS511(daveConnection * dc, uc * b, int len, int maxlen, i /* In S7, we need to tell the PLC what memory area we want to read from or write to. The PLC behaves as if each area were a different physical memory starting with an offset of 0. -In S5, everything is in common 64k of memory. For different areas, we have to add start -offsets of areas or objects. +In S5, everything is in common 64k of memory. For different areas, we have to add start +offsets of areas or objects. The following is needed to make memory access as S7-compatible as possible. */ int areaFromBlockType(int area){ - switch(area) { + switch (area) { case daveS5BlockType_DB: // S5 block type case daveBlockType_DB: // S7 block type case daveDB: // S7 area type return daveS5BlockType_DB; case daveS5BlockType_OB: - case daveBlockType_OB: + case daveBlockType_OB: return daveS5BlockType_OB; case daveS5BlockType_FB: - case daveBlockType_FB: - return daveS5BlockType_FB; - // s5 only: + case daveBlockType_FB: + return daveS5BlockType_FB; + // s5 only: case daveS5BlockType_PB: return daveS5BlockType_PB; case daveS5BlockType_SB: return daveS5BlockType_SB; - default: return area; + default: return area; } } /* @@ -7108,43 +7594,43 @@ them to ai structure: */ int DECL2 _daveReadS5BlockAddress(daveConnection * dc, uc area, uc BlockN, daveS5AreaInfo * ai) { - int res,dbaddr,dblen, s5Area; + int res, dbaddr, dblen, s5Area; uc b1[24]; //15 + some Dups - // if (_daveIsS5BlockArea(area)<0) { - // printf("%s *** Not block area .\n", dc->iface->name); - // return -1; - // } - - // b1[0]=area; - - s5Area=areaFromBlockType(area); - b1[0]=s5Area; - b1[1]=BlockN; - res=_daveExchangeAS511(dc,b1,2,24,0x1A); - if (res<0) { + // if (_daveIsS5BlockArea(area)<0) { + // printf("%s *** Not block area .\n", dc->iface->name); + // return -1; + // } + + // b1[0]=area; + + s5Area = areaFromBlockType(area); + b1[0] = s5Area; + b1[1] = BlockN; + res = _daveExchangeAS511(dc, b1, 2, 24, 0x1A); + if (res < 0) { printf("%s *** Error in BlockAddr.Exchange sequense.\n", dc->iface->name); - return res-10; + return res - 10; } - if (dc->AnswLen<15) { - printf("%s *** Too few chars (%d) in BlockAddr data.\n", dc->iface->name,dc->AnswLen); + if (dc->AnswLen < 15) { + printf("%s *** Too few chars (%d) in BlockAddr data.\n", dc->iface->name, dc->AnswLen); return -2; } - if ((dc->msgIn[0]!=0) - ||(dc->msgIn[3]!=0x70) - ||(dc->msgIn[4]!=0x70) - ||(dc->msgIn[5]!=0x40+s5Area)||(dc->msgIn[6]!=BlockN)) { - printf("%s *** Wrong BlockAddr data signature.\n", dc->iface->name); - return -3; + if ((dc->msgIn[0] != 0) + || (dc->msgIn[3] != 0x70) + || (dc->msgIn[4] != 0x70) + || (dc->msgIn[5] != 0x40 + s5Area) || (dc->msgIn[6] != BlockN)) { + printf("%s *** Wrong BlockAddr data signature.\n", dc->iface->name); + return -3; } - dbaddr=dc->msgIn[1]; - dbaddr=dbaddr*256+dc->msgIn[2];//Let make shift operations to compiler's optimizer - dblen=dc->msgIn[11]; - dblen=(dblen*256+dc->msgIn[12]-5)*2; //PLC returns dblen in words including + dbaddr = dc->msgIn[1]; + dbaddr = dbaddr * 256 + dc->msgIn[2];//Let make shift operations to compiler's optimizer + dblen = dc->msgIn[11]; + dblen = (dblen * 256 + dc->msgIn[12] - 5) * 2; //PLC returns dblen in words including //5 word header (but returnes the //start address after the header) so //dblen is length of block body - ai->address=dbaddr; - ai->len=dblen; + ai->address = dbaddr; + ai->len = dblen; return 0; } @@ -7152,22 +7638,22 @@ int DECL2 _daveIsS5BlockArea(uc area) { if ( // (area!=daveBlockType_S5DB)&& - (area!=daveS5BlockType_SB)&& - (area!=daveS5BlockType_PB)&& - (area!=daveS5BlockType_FX)&& - (area!=daveS5BlockType_FB)&& - (area!=daveS5BlockType_DX)&& - (area!=daveS5BlockType_OB)) { - return -1; + (area != daveS5BlockType_SB) && + (area != daveS5BlockType_PB) && + (area != daveS5BlockType_FX) && + (area != daveS5BlockType_FB) && + (area != daveS5BlockType_DX) && + (area != daveS5BlockType_OB)) { + return -1; } return 0; } int DECL2 _daveIsS5DBlockArea(uc area) { - if (area!=daveDB) { - // (area!=daveBlockType_S5DX)) - // (area!=daveBlockType_S5DX)) { + if (area != daveDB) { + // (area!=daveBlockType_S5DX)) + // (area!=daveBlockType_S5DX)) { return -1; } return 0; @@ -7175,10 +7661,10 @@ int DECL2 _daveIsS5DBlockArea(uc area) #define maxSysinfoLen 87 /* -This is a trick which will intercept all functions not available for S5 AS511. It works +This is a trick which will intercept all functions not available for S5 AS511. It works this way: -A function for S7 forms a packet for S7 communication and then calls daveExchange which +A function for S7 forms a packet for S7 communication and then calls daveExchange which will send the packet and return the answer. If a function is also vailable for S5, it must check whether the protocol is AS511. If so, the function calls it's S5 counterpart and returns the result of it. @@ -7189,7 +7675,7 @@ the S7 packet and call daveExchange, which will point hereto. This fake function allways returns a specific error code so the user knows the function is not available in the S5 protocol. -The advantage of this mechanism is that additional functions for S7 can be added at any +The advantage of this mechanism is that additional functions for S7 can be added at any time without caring about S5: If no special handling is provided, they end up here. */ int DECL2 _daveFakeExchangeAS511(daveConnection * dc, PDU *p){ @@ -7202,11 +7688,11 @@ The reason why we provide a daveConnect() is this: From an S5 CPU, you don't read inputs, outputs,flags or any other memory area but simply bytes from global memory. There are addresses of input image area, output image area, flags, timers etc. These depend -on CPU model. Next, there are start addresses of the data blocks. These addresses change +on CPU model. Next, there are start addresses of the data blocks. These addresses change whenever a data block is created or changed in size or modified by programming device. In both cases, we could read the adresses from the PLC before reading the data. To save -time and gain efficiency, we read them once in connectPLC. We rely on users following the +time and gain efficiency, we read them once in connectPLC. We rely on users following the S7 scheme: connect to a PLC before reading from it !! If we would read addresses each time, you could do something you cannot with S7: pull the @@ -7215,14 +7701,14 @@ plug from one PLC, connect to another PLC and the program still works. Here, you CANNOT do that. You have to call connectPLC again after changing to the new PLC. Another thing are data block addresses. We could fetch all 256 possible addresses in connectPLC, -too. But that would use 256 entries that must exist while the program might not use data -blocks at all. So we don't. We add data block addresses to the PLC address cache when they +too. But that would use 256 entries that must exist while the program might not use data +blocks at all. So we don't. We add data block addresses to the PLC address cache when they are used for the first time. There are S5 programs that create data blocks dynamically. Hence cached addresses get invalid. -If you have a PLC with such a program use +If you have a PLC with such a program use daveSetNonCacheable(dc, DBnumber); -If you suspect somebody could pull the plug, connect a programming device, modify data blocks -and reconnect your application program, use +If you suspect somebody could pull the plug, connect a programming device, modify data blocks +and reconnect your application program, use daveSetNonCacheable(dc, allDBs); In this case, the actual address will be fetched before each read or write from/to the related data blocks (which will slow down your application). @@ -7231,39 +7717,39 @@ data blocks (which will slow down your application). int DECL2 _daveConnectPLCAS511(daveConnection * dc){ int res; uc b1[maxSysinfoLen]; //20 words + some Dups - // dc->maxPDUlength=1000; - dc->maxPDUlength=240; - dc->cache=(daveS5cache*)calloc(1,sizeof(daveS5cache)); + // dc->maxPDUlength=1000; + dc->maxPDUlength = 240; + dc->cache = (daveS5cache*)calloc(1, sizeof(daveS5cache)); - res=_daveExchangeAS511(dc,b1,0,maxSysinfoLen,0x18); - if (res<0) { + res = _daveExchangeAS511(dc, b1, 0, maxSysinfoLen, 0x18); + if (res < 0) { LOG2("%s *** Error in ImageAddr.Exchange sequence.\n", dc->iface->name); - return res-10; + return res - 10; } - if (dc->AnswLen<47) { - LOG3("%s *** Too few chars (%d) in ImageAddr data.\n", dc->iface->name,dc->AnswLen); + if (dc->AnswLen < 47) { + LOG3("%s *** Too few chars (%d) in ImageAddr data.\n", dc->iface->name, dc->AnswLen); return -2; } - _daveDump("connect:",dc->msgIn, 47); - dc->cache->PAE=daveGetU16from(dc->msgIn+5); // start of inputs; - dc->cache->PAA=daveGetU16from(dc->msgIn+7); // start of outputs; - dc->cache->flags=daveGetU16from(dc->msgIn+9); // start of flag (marker) memory; - dc->cache->timers=daveGetU16from(dc->msgIn+11); // start of timer memory; - dc->cache->counters=daveGetU16from(dc->msgIn+13); // start of counter memory - dc->cache->systemData=daveGetU16from(dc->msgIn+15); // start of system data - dc->cache->first=NULL; - LOG2("start of inputs in memory %04x\n",dc->cache->PAE); - LOG2("start of outputs in memory %04x\n",dc->cache->PAA); - LOG2("start of flags in memory %04x\n",dc->cache->flags); - LOG2("start of timers in memory %04x\n",dc->cache->timers); - LOG2("start of counters in memory %04x\n",dc->cache->counters); - LOG2("start of system data in memory %04x\n",dc->cache->systemData); + _daveDump("connect:", dc->msgIn, 47); + dc->cache->PAE = daveGetU16from(dc->msgIn + 5); // start of inputs; + dc->cache->PAA = daveGetU16from(dc->msgIn + 7); // start of outputs; + dc->cache->flags = daveGetU16from(dc->msgIn + 9); // start of flag (marker) memory; + dc->cache->timers = daveGetU16from(dc->msgIn + 11); // start of timer memory; + dc->cache->counters = daveGetU16from(dc->msgIn + 13); // start of counter memory + dc->cache->systemData = daveGetU16from(dc->msgIn + 15); // start of system data + dc->cache->first = NULL; + LOG2("start of inputs in memory %04x\n", dc->cache->PAE); + LOG2("start of outputs in memory %04x\n", dc->cache->PAA); + LOG2("start of flags in memory %04x\n", dc->cache->flags); + LOG2("start of timers in memory %04x\n", dc->cache->timers); + LOG2("start of counters in memory %04x\n", dc->cache->counters); + LOG2("start of system data in memory %04x\n", dc->cache->systemData); return 0; } int DECL2 _daveDisconnectPLCAS511(daveConnection * dc){ free(dc->cache); - dc->cache=0; + dc->cache = 0; return 0; } @@ -7274,64 +7760,65 @@ with PLC cycle. For this purposes use daveWriteBlock: */ int DECL2 daveWriteS5Bytes(daveConnection * dc, uc area, uc BlockN, int offset, int count, void * buf) { - int res,datastart; + int res, datastart; daveS5AreaInfo ai; uc b1[daveMaxRawLen]; - // if (_daveIsS5DBlockArea(area)==0) { - if (area==daveDB) { + // if (_daveIsS5DBlockArea(area)==0) { + if (area == daveDB) { // LOG1("_daveIsS5DBlockArea\n"); - res=_daveReadS5BlockAddress(dc,area,BlockN,&ai); + res = _daveReadS5BlockAddress(dc, area, BlockN, &ai); if (res<0) { LOG2("%s *** Error in WriteS5Bytes.BlockAddr request.\n", dc->iface->name); - return res-50; + return res - 50; } - datastart=ai.address; - } else { + datastart = ai.address; + } + else { switch (area) { - case daveRawMemoryS5: datastart=0; break; - case daveInputs: datastart=dc->cache->PAE; break; - case daveOutputs: datastart=dc->cache->PAA; break; - case daveFlags: datastart=dc->cache->flags; break; - case daveTimer: datastart=dc->cache->timers; break; - case daveCounter: datastart=dc->cache->counters; break; - case daveSysDataS5: datastart=dc->cache->systemData; break; + case daveRawMemoryS5: datastart = 0; break; + case daveInputs: datastart = dc->cache->PAE; break; + case daveOutputs: datastart = dc->cache->PAA; break; + case daveFlags: datastart = dc->cache->flags; break; + case daveTimer: datastart = dc->cache->timers; break; + case daveCounter: datastart = dc->cache->counters; break; + case daveSysDataS5: datastart = dc->cache->systemData; break; default: LOG2("%s *** Unknown area in WriteS5Bytes request.\n", dc->iface->name); return -1; } } - if ((count>daveMaxRawLen)||(offset+count>ai.len)) { + if ((count>daveMaxRawLen) || (offset + count > ai.len)) { LOG2("%s writeS5Bytes *** Requested data is out-of-range.\n", dc->iface->name); return -1; } - // datastart=ai.address+offset; - LOG2("area start is %04x, ",datastart); - datastart+=offset; - LOG2("data start is %04x\n",datastart); - b1[0]=datastart/256; - b1[1]=datastart%256; - memcpy(&b1[2],buf,count); - res=_daveExchangeAS511(dc,b1,2+count,0,0x03); - if (res<0) { + // datastart=ai.address+offset; + LOG2("area start is %04x, ", datastart); + datastart += offset; + LOG2("data start is %04x\n", datastart); + b1[0] = datastart / 256; + b1[1] = datastart % 256; + memcpy(&b1[2], buf, count); + res = _daveExchangeAS511(dc, b1, 2 + count, 0, 0x03); + if (res < 0) { LOG2("%s *** Error in WriteS5Bytes.Exchange sequense.\n", dc->iface->name); - return res-10; + return res - 10; } return 0; } int DECL2 daveStopS5(daveConnection * dc) { - uc b1[]={0x88,0x04}; // I don't know what this mean - return daveWriteBytes(dc,daveSysDataS5,0,0x0c,2,b1); + uc b1[] = { 0x88, 0x04 }; // I don't know what this mean + return daveWriteBytes(dc, daveSysDataS5, 0, 0x0c, 2, b1); } int DECL2 daveStartS5(daveConnection * dc) { - uc b1[]={0x68,0x00}; // I don't know what this mean - return daveWriteBytes(dc,daveSysDataS5,0,0x0c,2,b1); + uc b1[] = { 0x68, 0x00 }; // I don't know what this mean + return daveWriteBytes(dc, daveSysDataS5, 0, 0x0c, 2, b1); } int DECL2 daveGetS5ProgramBlock(daveConnection * dc, int blockType, int number, char* buffer, int * length) { - // int totlen,res; - // *length=totlen; + // int totlen,res; + // *length=totlen; return daveResNotYetImplemented; } @@ -7350,14 +7837,14 @@ be good for in the future... /* fill some standard fields and pass it to SCP-send: */ -int DECL2 _daveSCP_send(int fd, uc * reqBlock) { +int DECL2 _daveSCP_send(int fd, uc * reqBlock) { S7OexchangeBlock* fdr; - fdr=(S7OexchangeBlock*)reqBlock; + fdr = (S7OexchangeBlock*)reqBlock; fdr->headerlength = 80; //Length of the Header (always 80) (but the 4 first unkown bytes are not count) fdr->rb_type = 2; //rb_type is always 2 - fdr->offset_1= 80; //Offset of the Begin of userdata (but the 4 first unkown bytes are not count) + fdr->offset_1 = 80; //Offset of the Begin of userdata (but the 4 first unkown bytes are not count) - if (fdr->application_block_subsystem == 0xE4) //Fix for PLCSim + if (fdr->application_block_subsystem == 0xE4) //Fix for PLCSim Sleep(50); //Fix for PLCSim return SCP_send(fd, fdr->seg_length_1 + fdr->headerlength, reqBlock); @@ -7366,26 +7853,26 @@ int DECL2 _daveSCP_send(int fd, uc * reqBlock) { int daveSCP_receive(int h, uc * buffer) { int res, datalen; S7OexchangeBlock * fdr; - fdr=(S7OexchangeBlock*) buffer; + fdr = (S7OexchangeBlock*)buffer; - if (fdr->application_block_subsystem == 0xE4) //Fix for PLCSim + if (fdr->application_block_subsystem == 0xE4) //Fix for PLCSim Sleep(50); //Fix for PLCSim - res=SCP_receive(h, 0xFFFF, &datalen, sizeof(S7OexchangeBlock), buffer); + res = SCP_receive(h, 0xFFFF, &datalen, sizeof(S7OexchangeBlock), buffer); if (daveDebug & daveDebugByte) { - _daveDump("header:",buffer, 80); - _daveDump("data:",buffer+80, fdr->seg_length_1); - } - return res; -} + _daveDump("header:", buffer, 80); + _daveDump("data:", buffer + 80, fdr->seg_length_1); + } + return res; +} /* -* +* */ -int DECL2 _daveConnectPLCS7online (daveConnection * dc) { +int DECL2 _daveConnectPLCS7online(daveConnection * dc) { - int res=0; - uc p2[]={ + int res = 0; + uc p2[] = { 0x00, 0x02, 0x01, @@ -7423,36 +7910,36 @@ int DECL2 _daveConnectPLCS7online (daveConnection * dc) { }; - uc pa[]= {0xF0, 0 ,0, 1, 0, 1, 3, 0xc0,}; + uc pa[] = { 0xF0, 0, 0, 1, 0, 1, 3, 0xc0, }; - PDU pu2,pu1, *p; + PDU pu2, pu1, *p; - int a,b; + int a, b; S7OexchangeBlock * fdr; S7OexchangeBlock * rec; - fdr=(S7OexchangeBlock*)(dc->msgOut); - rec=(S7OexchangeBlock*)(dc->msgIn); + fdr = (S7OexchangeBlock*)(dc->msgOut); + rec = (S7OexchangeBlock*)(dc->msgIn); - dc->PDUstartI=80; + dc->PDUstartI = 80; //This 2 telegramms are only send when not using TCP/IP (maybe for initialising the Adapter?) if (!dc->DestinationIsIP) { - fdr->subsystem= 0x22; - fdr->response= 0xFF; - fdr->user= 0xFF; - fdr->seg_length_1= 0x80; - fdr->priority= 1; - fdr->application_block_service= 0x1A; - - a= _daveSCP_send(((int)dc->iface->fd.wfd), dc->msgOut); + fdr->subsystem = 0x22; + fdr->response = 0xFF; + fdr->user = 0xFF; + fdr->seg_length_1 = 0x80; + fdr->priority = 1; + fdr->application_block_service = 0x1A; + + a = _daveSCP_send(((int)dc->iface->fd.wfd), dc->msgOut); daveSCP_receive((int)(dc->iface->fd.rfd), dc->msgIn); - fdr->seg_length_1= 0xF2; - fdr->application_block_service= 0xB; + fdr->seg_length_1 = 0xF2; + fdr->application_block_service = 0xB; - a= _daveSCP_send(((int)dc->iface->fd.wfd), dc->msgOut); + a = _daveSCP_send(((int)dc->iface->fd.wfd), dc->msgOut); daveSCP_receive((int)(dc->iface->fd.rfd), dc->msgIn); } @@ -7460,68 +7947,68 @@ int DECL2 _daveConnectPLCS7online (daveConnection * dc) { //Telegramms for both Types (TCP/IP and MPI) //3rd telegramm / TCP(1st) - memset(fdr,0,80); - fdr->response= 255; - fdr->subsystem= 0x40; - a= _daveSCP_send(((int)dc->iface->fd.wfd), dc->msgOut); + memset(fdr, 0, 80); + fdr->response = 255; + fdr->subsystem = 0x40; + a = _daveSCP_send(((int)dc->iface->fd.wfd), dc->msgOut); daveSCP_receive((int)(dc->iface->fd.rfd), dc->msgIn); //4th Telegramm / TCP(2nd) - memset(fdr,0,206); - fdr->user= 111; - fdr->subsystem= 64; - fdr->opcode= 1; - fdr->response= 255; - fdr->fill_length_1= 126; - fdr->seg_length_1= 126; - dc->connectionNumber=rec->application_block_opcode; - fdr->application_block_opcode= dc->connectionNumber; - fdr->application_block_ssap= 2; - fdr->application_block_remote_address_station= 114; //I think this should be the local MPI - + memset(fdr, 0, 206); + fdr->user = 111; + fdr->subsystem = 64; + fdr->opcode = 1; + fdr->response = 255; + fdr->fill_length_1 = 126; + fdr->seg_length_1 = 126; + dc->connectionNumber = rec->application_block_opcode; + fdr->application_block_opcode = dc->connectionNumber; + fdr->application_block_ssap = 2; + fdr->application_block_remote_address_station = 114; //I think this should be the local MPI + fdr->application_block_subsystem = rec->application_block_subsystem; //When this is One it is a MP Connection, zero means TCP Connection! - dc->application_block_subsystem = rec->application_block_subsystem; + dc->application_block_subsystem = rec->application_block_subsystem; //Maybe we remove the destination is IP Parameter and use the upper bit - - p2[19]=(dc->slot + dc->rack*32); + + p2[19] = (dc->slot + dc->rack * 32); //Destination IP or MPI if (dc->DestinationIsIP) { - p2[9]=dc->_Destination1; - p2[10]=dc->_Destination2; - p2[11]=dc->_Destination3; - p2[12]=dc->_Destination4; + p2[9] = dc->_Destination1; + p2[10] = dc->_Destination2; + p2[11] = dc->_Destination3; + p2[12] = dc->_Destination4; //p2[19]=(dc->slot + dc->rack*32); } else - p2[9]=dc->MPIAdr; + p2[9] = dc->MPIAdr; if (dc->routing) { - p2[0]=1; //JK (Routing enabled???) - p2[19]=(dc->routingSlot + dc->routingRack*32); //--> Routing Rack/Slot - p2[21]=9; - p2[22]=6; - p2[23]=(unsigned char) (dc->routingSubnetFirst >> 8); - p2[24]=(unsigned char) (dc->routingSubnetFirst); - p2[27]=(unsigned char) (dc->routingSubnetSecond >> 8); - p2[28]=(unsigned char) (dc->routingSubnetSecond); - p2[29]=1; //Maybe 4 when Routing to an IP??? - p2[30]=(unsigned char) dc->_routingDestination1; - p2[31]=(unsigned char) dc->_routingDestination2; - p2[32]=(unsigned char) dc->_routingDestination3; - p2[33]=(unsigned char) dc->_routingDestination4; + p2[0] = 1; //JK (Routing enabled???) + p2[19] = (dc->routingSlot + dc->routingRack * 32); //--> Routing Rack/Slot + p2[21] = 9; + p2[22] = 6; + p2[23] = (unsigned char)(dc->routingSubnetFirst >> 8); + p2[24] = (unsigned char)(dc->routingSubnetFirst); + p2[27] = (unsigned char)(dc->routingSubnetSecond >> 8); + p2[28] = (unsigned char)(dc->routingSubnetSecond); + p2[29] = 1; //Maybe 4 when Routing to an IP??? + p2[30] = (unsigned char)dc->_routingDestination1; + p2[31] = (unsigned char)dc->_routingDestination2; + p2[32] = (unsigned char)dc->_routingDestination3; + p2[33] = (unsigned char)dc->_routingDestination4; } //Destination Routing if (dc->routing && dc->routingDestinationIsIP) { - p2[21]=12; // 3 Chars more ??? - p2[29]=4; // 4 when Routing to an IP??? + p2[21] = 12; // 3 Chars more ??? + p2[29] = 4; // 4 when Routing to an IP??? } - memcpy(&(fdr->user_data_1),p2,sizeof(p2)); - a=_daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); - b=daveSCP_receive((int)(dc->iface->fd.rfd), dc->msgIn); + memcpy(&(fdr->user_data_1), p2, sizeof(p2)); + a = _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); + b = daveSCP_receive((int)(dc->iface->fd.rfd), dc->msgIn); //Connection error??? @@ -7529,42 +8016,42 @@ int DECL2 _daveConnectPLCS7online (daveConnection * dc) { return -1; //5th Telegramm / TCP(3rd) - memset(fdr,0,98); - fdr->subsystem= 64; - fdr->opcode= 6; - fdr->response= 255; - fdr->fill_length_1= 18; - fdr->seg_length_1= 18; - fdr->application_block_opcode= dc->connectionNumber; + memset(fdr, 0, 98); + fdr->subsystem = 64; + fdr->opcode = 6; + fdr->response = 255; + fdr->fill_length_1 = 18; + fdr->seg_length_1 = 18; + fdr->application_block_opcode = dc->connectionNumber; if (!dc->DestinationIsIP) - fdr->application_block_subsystem= 1; - p=&pu1; + fdr->application_block_subsystem = 1; + p = &pu1; //p->header=dc->msgOut+dc->PDUstartO; - p->header=fdr->user_data_1; - _daveInitPDUheader(p,1); + p->header = fdr->user_data_1; + _daveInitPDUheader(p, 1); _daveAddParam(p, pa, sizeof(pa)); if (daveGetDebug() & daveDebugPDU) _daveDumpPDU(p); fdr->application_block_subsystem = dc->application_block_subsystem; //Test - a= _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); + a = _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); daveSCP_receive((int)(dc->iface->fd.rfd), dc->msgIn); - pu2.header=dc->msgIn+80; + pu2.header = dc->msgIn + 80; _daveSetupReceivedPDU(dc, &pu2); if (daveGetDebug() & daveDebugPDU) _daveDumpPDU(&pu2); //6th Telegramm (this get's the PDU size) / TCP(4th) - memset(fdr,0,560); - fdr->user= 0; - fdr->subsystem= 64; - fdr->opcode= 7; - fdr->response= 16642; - fdr->seg_length_1= 480; - fdr->application_block_opcode= dc->connectionNumber; - + memset(fdr, 0, 560); + fdr->user = 0; + fdr->subsystem = 64; + fdr->opcode = 7; + fdr->response = 16642; + fdr->seg_length_1 = 480; + fdr->application_block_opcode = dc->connectionNumber; + if (!dc->DestinationIsIP) - fdr->application_block_subsystem= 1; + fdr->application_block_subsystem = 1; fdr->application_block_subsystem = dc->application_block_subsystem; //Test //PLCSIM... @@ -7572,21 +8059,21 @@ int DECL2 _daveConnectPLCS7online (daveConnection * dc) { //fdr->seg_length_1= 1700; //fdr->application_block_subsystem= 0xe4; - a= _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); + a = _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); daveSCP_receive((int)(dc->iface->fd.rfd), dc->msgIn); _daveSetupReceivedPDU(dc, &pu2); if (daveGetDebug() & daveDebugPDU) _daveDumpPDU(&pu2); - dc->maxPDUlength=daveGetU16from(pu2.param+6); - // if (daveDebug & daveDebugConnect) - LOG2("\n*** Partner offered PDU length: %d\n\n",dc->maxPDUlength); + dc->maxPDUlength = daveGetU16from(pu2.param + 6); + // if (daveDebug & daveDebugConnect) + LOG2("\n*** Partner offered PDU length: %d\n\n", dc->maxPDUlength); return res; - /* + /* memset((uc*)(&giveBack)+80,0,480); giveBack.payloadLength= 480; return _daveNegPDUlengthRequest(dc, &pu1); - */ + */ } //Look at the PLC Connection resources if this Function Closes a Connection!! @@ -7595,76 +8082,76 @@ int DECL2 _daveDisconnectPLCS7online(daveConnection * dc) { int a; int datalen; - + uc buffer[sizeof(S7OexchangeBlock)]; S7OexchangeBlock* fdr; - fdr=(S7OexchangeBlock*)dc->msgOut; - memset(dc->msgOut,0,80); - - fdr->user= 102; - fdr->subsystem=64; - fdr->opcode=8; - fdr->response=255; - fdr->application_block_opcode= dc->connectionNumber; + fdr = (S7OexchangeBlock*)dc->msgOut; + memset(dc->msgOut, 0, 80); + + fdr->user = 102; + fdr->subsystem = 64; + fdr->opcode = 8; + fdr->response = 255; + fdr->application_block_opcode = dc->connectionNumber; if (dc->DestinationIsIP) { - a= _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); + a = _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); SCP_receive((int)(dc->iface->fd.rfd), 0xFFFF, &datalen, sizeof(S7OexchangeBlock), buffer); } - - memset(dc->msgOut,0,80); - - fdr->subsystem=64; - fdr->opcode=0xC; - fdr->response=255; - fdr->application_block_service=0x8000; - fdr->application_block_opcode= dc->connectionNumber; + + memset(dc->msgOut, 0, 80); + + fdr->subsystem = 64; + fdr->opcode = 0xC; + fdr->response = 255; + fdr->application_block_service = 0x8000; + fdr->application_block_opcode = dc->connectionNumber; if (!dc->DestinationIsIP) - fdr->application_block_subsystem= 1; + fdr->application_block_subsystem = 1; - a= _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); + a = _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); SCP_receive((int)(dc->iface->fd.rfd), 0xFFFF, &datalen, sizeof(S7OexchangeBlock), buffer); - + return 0; } int DECL2 _daveSendMessageS7online(daveConnection *dc, PDU *p) { int a, b; int datalen; - int len=p->hlen+p->plen+p->dlen; + int len = p->hlen + p->plen + p->dlen; uc buffer[sizeof(S7OexchangeBlock)]; S7OexchangeBlock* fdr; - fdr=(S7OexchangeBlock*)dc->msgOut; - memset(dc->msgOut,0,80); - // fdr->user= 114; - fdr->subsystem=64; - fdr->opcode=6; + fdr = (S7OexchangeBlock*)dc->msgOut; + memset(dc->msgOut, 0, 80); + // fdr->user= 114; + fdr->subsystem = 64; + fdr->opcode = 6; //JK fdr->response=16642; - fdr->response=255; //JK + fdr->response = 255; //JK - fdr->fill_length_1=len; - fdr->seg_length_1=len; + fdr->fill_length_1 = len; + fdr->seg_length_1 = len; //JK fdr->application_block_subsystem= 1; //fdr->application_block_opcode= 10; //JK - fdr->application_block_opcode= dc->connectionNumber; //JK + fdr->application_block_opcode = dc->connectionNumber; //JK if (!dc->DestinationIsIP) - fdr->application_block_subsystem= 1; - fdr->application_block_subsystem=dc->application_block_subsystem; //Test - - // memcpy(&(fdr->payload),buffer,len); - a= _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); - if (daveDebug & daveDebugErrorReporting) - LOG2("RetVal SCP_send in SendMessageS7Online: ",a); - - b= SCP_receive((int)(dc->iface->fd.rfd), 0xFFFF, &datalen, sizeof(S7OexchangeBlock), buffer); - if (daveDebug & daveDebugErrorReporting) - LOG2("RetVal SCP_recieve in SendMessageS7Online: ",b); - // daveSCP_receive(dc->iface->fd.rfd, dc->msgIn); + fdr->application_block_subsystem = 1; + fdr->application_block_subsystem = dc->application_block_subsystem; //Test + + // memcpy(&(fdr->payload),buffer,len); + a = _daveSCP_send((int)(dc->iface->fd.wfd), dc->msgOut); + if (daveDebug & daveDebugErrorReporting) + LOG2("RetVal SCP_send in SendMessageS7Online: ", a); + + b = SCP_receive((int)(dc->iface->fd.rfd), 0xFFFF, &datalen, sizeof(S7OexchangeBlock), buffer); + if (daveDebug & daveDebugErrorReporting) + LOG2("RetVal SCP_recieve in SendMessageS7Online: ", b); + // daveSCP_receive(dc->iface->fd.rfd, dc->msgIn); return 0; -} +} int DECL2 _daveGetResponseS7online(daveConnection *dc) { int a, b; @@ -7672,73 +8159,73 @@ int DECL2 _daveGetResponseS7online(daveConnection *dc) { //if (dc->DestinationIsIP) // fdr->application_block_subsystem=1; - a= _daveSCP_send((int)(dc->iface->fd.rfd), dc->msgIn); - if (daveDebug & daveDebugErrorReporting) - LOG2("RetVal SCP_send in GetResponseS7online: ",a); + a = _daveSCP_send((int)(dc->iface->fd.rfd), dc->msgIn); + if (daveDebug & daveDebugErrorReporting) + LOG2("RetVal SCP_send in GetResponseS7online: ", a); - b= daveSCP_receive((int)(dc->iface->fd.rfd), dc->msgIn); - if (daveDebug & daveDebugErrorReporting) - LOG2("RetVal SCP_recieve in GetResponseS7online: ",b); + b = daveSCP_receive((int)(dc->iface->fd.rfd), dc->msgIn); + if (daveDebug & daveDebugErrorReporting) + LOG2("RetVal SCP_recieve in GetResponseS7online: ", b); return 0; } int DECL2 _daveExchangeS7online(daveConnection * dc, PDU * p) { int res; - res=_daveSendMessageS7online(dc, p); - dc->AnswLen=0; - res=_daveGetResponseS7online(dc); + res = _daveSendMessageS7online(dc, p); + dc->AnswLen = 0; + res = _daveGetResponseS7online(dc); return res; } -int DECL2 _daveListReachablePartnersS7online (daveInterface * di, char * buf) { +int DECL2 _daveListReachablePartnersS7online(daveInterface * di, char * buf) { int a; S7OexchangeBlock reqBlock; uc b1[sizeof(S7OexchangeBlock)]; S7OexchangeBlock* fdr; - fdr=&reqBlock; + fdr = &reqBlock; - memset(fdr,0,140); - fdr->user= 102; - fdr->priority= 1; - fdr->subsystem= 34; - fdr->response= 16642; - fdr->seg_length_1= 60; - fdr->application_block_service= 0x28; + memset(fdr, 0, 140); + fdr->user = 102; + fdr->priority = 1; + fdr->subsystem = 34; + fdr->response = 16642; + fdr->seg_length_1 = 60; + fdr->application_block_service = 0x28; - a= _daveSCP_send((int)(di->fd.wfd), (uc *) &reqBlock); + a = _daveSCP_send((int)(di->fd.wfd), (uc *)&reqBlock); daveSCP_receive((int)(di->fd.rfd), b1); - fdr->user= 103; - fdr->priority= 1; - fdr->subsystem= 34; - fdr->response= 16642; - fdr->application_block_service= 0x17; + fdr->user = 103; + fdr->priority = 1; + fdr->subsystem = 34; + fdr->response = 16642; + fdr->application_block_service = 0x17; - a= _daveSCP_send((int)(di->fd.wfd), (uc *) &reqBlock); + a = _daveSCP_send((int)(di->fd.wfd), (uc *)&reqBlock); daveSCP_receive((int)(di->fd.rfd), b1); - memset(fdr,0,140); - - fdr->user= 104; - fdr->priority= 1; - fdr->subsystem= 34; - fdr->response= 16642; - fdr->seg_length_1= 60; - fdr->application_block_service= 0x28; - a= _daveSCP_send((int)(di->fd.wfd), (uc *) &reqBlock); + memset(fdr, 0, 140); + + fdr->user = 104; + fdr->priority = 1; + fdr->subsystem = 34; + fdr->response = 16642; + fdr->seg_length_1 = 60; + fdr->application_block_service = 0x28; + a = _daveSCP_send((int)(di->fd.wfd), (uc *)&reqBlock); daveSCP_receive((int)(di->fd.rfd), b1); - memset(fdr,0,208); - fdr->user= 105; - fdr->priority= 1; - fdr->subsystem= 34; - fdr->response =16642; - fdr->seg_length_1= 128; - fdr->application_block_service= 0x1a; - a= _daveSCP_send((int)(di->fd.wfd), (uc *) &reqBlock); + memset(fdr, 0, 208); + fdr->user = 105; + fdr->priority = 1; + fdr->subsystem = 34; + fdr->response = 16642; + fdr->seg_length_1 = 128; + fdr->application_block_service = 0x1a; + a = _daveSCP_send((int)(di->fd.wfd), (uc *)&reqBlock); daveSCP_receive((int)(di->fd.rfd), b1); - memcpy(buf,b1+80,126); + memcpy(buf, b1 + 80, 126); return 126; } @@ -7747,20 +8234,20 @@ int DECL2 _daveListReachablePartnersS7online (daveInterface * di, char * buf) { /* This is not quite the same as in other protocols: Normally, we have a file descriptor or file handle in di->fd.rfd, di->fd.wfd. disconnectAdapter does something like making the -MPI adapter leaving the Profibus token ring. File descriptor remains valid until it is +MPI adapter leaving the Profibus token ring. File descriptor remains valid until it is closed with closePort(). -In principle, instead of closing it, we could redo the sequence +In principle, instead of closing it, we could redo the sequence daveNewInterface, initAdapter and then continue to use it. We cannot use closePort() on a "handle" retrieved from SCP_open(). It isn't a file handle. We cannot make closePort() treat it differently as there is no information in di->fd.rfd WHAT it is. - We could make di->fd.rfd a structure with extra information. - We could pass struct di->fd (daveOSserialtype) instead of di->fd.rfd / di->fd.wfd to all -all functions dealing with di->fd.rfd. Then we could add extra information to +all functions dealing with di->fd.rfd. Then we could add extra information to daveOSserialtype - We could better pas a pointer to an extended daveOSserialtype as it makes less problems when passing it to different programming languages. -These would be major changes. They would give up the (theroetical?) possibility to use file +These would be major changes. They would give up the (theroetical?) possibility to use file handles obtained in quite a different way and to put them into daveOSserialtype. I chose to change as little as possible for s7online and just SCP_close the handle here, expecting no one will try to reuse after this. @@ -7768,10 +8255,10 @@ expecting no one will try to reuse after this. Up to here is what version 0.8 does. The probleme is now that an application cannot do daveDisconnectAdapter(), closePort() as it does for other protocols. -Now comes a second kludge for 0.8.1: We replace the "file handles" value by -1. Now we can +Now comes a second kludge for 0.8.1: We replace the "file handles" value by -1. Now we can tell closePort() to do nothing for a value of -1. -Befor releasing that, I think it is better to use different close functions, closeS7oline +Befor releasing that, I think it is better to use different close functions, closeS7oline for s7online and closePort() for everything else. */ @@ -7785,90 +8272,90 @@ return res; } */ /*** - NetLink Pro -***/ + NetLink Pro + ***/ #define NET #ifdef NET -int DECL2 _daveReadMPINLPro(daveInterface * di,uc *b) { - int res,i,length; - i=_daveTimedRecv(di, b, 2); - res=i; +int DECL2 _daveReadMPINLPro(daveInterface * di, uc *b) { + int res, i, length; + i = _daveTimedRecv(di, b, 2); + res = i; if (res <= 0) return daveResTimeout; - if (res<2) { - if (daveDebug & daveDebugByte) { - LOG2("res %d ",res); - _daveDump("readISOpacket: short packet", b, res); - } - return daveResShortPacket; /* short packet */ - } - length=b[1]+0x100*b[0]; - i=_daveTimedRecv(di, b+2, length); - res+=i; + if (res < 2) { + if (daveDebug & daveDebugByte) { + LOG2("res %d ", res); + _daveDump("readISOpacket: short packet", b, res); + } + return daveResShortPacket; /* short packet */ + } + length = b[1] + 0x100 * b[0]; + i = _daveTimedRecv(di, b + 2, length); + res += i; if (daveDebug & daveDebugByte) { - LOG3("readMPINLpro: %d bytes read, %d needed\n",res, length); - _daveDump("readMPIpro: packet", b, res); + LOG3("readMPINLpro: %d bytes read, %d needed\n", res, length); + _daveDump("readMPIpro: packet", b, res); } return (res); - } +} #endif -/* +/* This initializes the MPI adapter. Andrew's version. */ int DECL2 _daveInitAdapterNLPro(daveInterface * di) /* serial interface */ { - uc b3[]={ - 0x01,0x03,0x02,0x27, 0x00,0x9F,0x01,0x14, - 0x00,0x90,0x01,0xc, 0x00, /* ^^^ MaxTsdr */ - 0x00,0x5, + uc b3[] = { + 0x01, 0x03, 0x02, 0x27, 0x00, 0x9F, 0x01, 0x14, + 0x00, 0x90, 0x01, 0xc, 0x00, /* ^^^ MaxTsdr */ + 0x00, 0x5, 0x02,/* Bus speed */ - 0x00,0x0F,0x05,0x01,0x01,0x03,0x81,/* from topserverdemo */ + 0x00, 0x0F, 0x05, 0x01, 0x01, 0x03, 0x81,/* from topserverdemo */ /*^^ - Local mpi */ - }; + }; int res; - b3[16]=di->localMPI; - if (di->speed==daveSpeed500k) - b3[7]=0x64; - if (di->speed==daveSpeed1500k) - b3[7]=0x96; - b3[15]=di->speed; - - // res=_daveInitStep(di, 1, b3, sizeof(b3),"initAdapter()"); - res=_daveInitStepNLPro(di, 1, b3, sizeof(b3),"initAdapter()", NULL); - - // res= _daveReadMPINLPro(di, b1); - if (daveDebug & daveDebugInitAdapter) + b3[16] = di->localMPI; + if (di->speed == daveSpeed500k) + b3[7] = 0x64; + if (di->speed == daveSpeed1500k) + b3[7] = 0x96; + b3[15] = di->speed; + + // res=_daveInitStep(di, 1, b3, sizeof(b3),"initAdapter()"); + res = _daveInitStepNLPro(di, 1, b3, sizeof(b3), "initAdapter()", NULL); + + // res= _daveReadMPINLPro(di, b1); + if (daveDebug & daveDebugInitAdapter) LOG2("%s initAdapter() success.\n", di->name); - // _daveSendSingle(di,DLE); - di->users=0; /* there cannot be any connections now */ + // _daveSendSingle(di,DLE); + di->users = 0; /* there cannot be any connections now */ return 0; } void DECL2 _daveSendSingleNLPro(daveInterface * di, /* serial interface */ uc c /* chracter to be send */ - ) + ) { unsigned long i; uc c3[3]; - c3[0]=0; - c3[1]=1; - c3[2]=c; - // di->ifwrite(di, c3, 3); + c3[0] = 0; + c3[1] = 1; + c3[2] = c; + // di->ifwrite(di, c3, 3); #ifdef HAVE_SELECT daveWriteFile(di->fd.wfd, c3, 3, i); -#endif +#endif #ifdef BCCWIN send((unsigned int)(di->fd.wfd), c3, 3, 0); #endif } -/* +/* This sends a string after doubling DLEs in the String and adding DLE,ETX and bcc. */ @@ -7876,46 +8363,46 @@ int DECL2 _daveSendWithCRCNLPro(daveInterface * di, /* serial interface */ uc *b, /* a buffer containing the message */ int size /* the size of the string */ ) -{ +{ uc target[daveMaxRawLen]; - int i,targetSize=2; - target[0]=size / 256; - target[1]=size % 256; + int i, targetSize = 2; + target[0] = size / 256; + target[1] = size % 256; - // int bcc=DLE^ETX; /* preload */ - for (i=0; iifwrite(di, target, targetSize); + // targetSize+=0; + // di->ifwrite(di, target, targetSize); #ifdef HAVE_SELECT daveWriteFile(di->fd.wfd, target, targetSize, i); -#endif +#endif #ifdef BCCWIN send((unsigned int)(di->fd.wfd), target, targetSize, 0); #endif if (daveDebug & daveDebugPacket) - _daveDump("_daveSendWithCRCNLPro",target, targetSize); + _daveDump("_daveSendWithCRCNLPro", target, targetSize); return 0; } -/* +/* Send a string of init data to the MPI adapter. */ -int DECL2 _daveInitStepNLPro(daveInterface * di, int nr, uc *fix, int len, char * caller, uc * buffer ) { +int DECL2 _daveInitStepNLPro(daveInterface * di, int nr, uc *fix, int len, char * caller, uc * buffer) { uc res[500]; int i; if (daveDebug & daveDebugInitAdapter) LOG4("%s %s step %d.\n", di->name, caller, nr); _daveSendWithCRCNLPro(di, fix, len); - i=_daveReadMPINLPro(di, (buffer != NULL) ? buffer : res ); + i = _daveReadMPINLPro(di, (buffer != NULL) ? buffer : res); return i; -} +} -/* +/* Open connection to a PLC. This assumes that dc is initialized by daveNewConnection and is not yet used. (or reused for the same PLC ?) @@ -7923,7 +8410,7 @@ daveNewConnection and is not yet used. int DECL2 _daveConnectPLCNLPro(daveConnection * dc) { int res, length; PDU p1; - uc b4[]={ + uc b4[] = { 0x04, //00 0x80, //01 (0x80 | MPI) 0x80, //02 @@ -7957,32 +8444,32 @@ int DECL2 _daveConnectPLCNLPro(daveConnection * dc) { 0x02, //29 Connection Type Routing 0x04 //30 Rack, Slot - }; + }; /*us t4[]={ 0x04,0x80,0x180,0x0C,0x114,0x103,0xD0,0x04, // 1/10/05 trying Andrew's patch 0x00,0x80, 0x00,0x02,0x00,0x02,0x01, 0x00,0x01,0x00, - };*/ - uc b5[]={ - 0x05,0x07, + };*/ + uc b5[] = { + 0x05, 0x07, }; - /*us t5[]={ + /*us t5[]={ 0x04, 0x80, 0x180,0x0C,0x114,0x103,0x05,0x01, - };*/ - b4[1]|=dc->MPIAdr; - b4[5]=dc->connectionNumber; // 1/10/05 trying Andrew's patch + };*/ + b4[1] |= dc->MPIAdr; + b4[5] = dc->connectionNumber; // 1/10/05 trying Andrew's patch //t4[1]|=dc->MPIAdr; //t5[1]|=dc->MPIAdr; - length = sizeof(b4)-13; + length = sizeof(b4) - 13; if (dc->routing) - { + { //b4[11] = dc->MPIAdr; b4[12] = 1; //b4[13] = dc->CPUConnectiontype; //not yet tested @@ -8002,39 +8489,39 @@ int DECL2 _daveConnectPLCNLPro(daveConnection * dc) { b4[27] = dc->_routingDestination3; b4[28] = dc->_routingDestination4; b4[29] = dc->routingConnectionType; //Connection type - b4[30] = dc->routingSlot + dc->routingRack*32; + b4[30] = dc->routingSlot + dc->routingRack * 32; } else { - length = sizeof(b4)-3; + length = sizeof(b4) - 3; b4[13] = 0x0c; b4[17] = 0x01; b4[26] = dc->routingConnectionType; //Connection type - b4[27] = dc->routingSlot + dc->routingRack*32; + b4[27] = dc->routingSlot + dc->routingRack * 32; } } - - res=_daveInitStepNLPro(dc->iface, 1, b4, length,"connectPLC(1)", dc->msgIn); - if (res-2 != length) + + res = _daveInitStepNLPro(dc->iface, 1, b4, length, "connectPLC(1)", dc->msgIn); + if (res - 2 != length) return daveResTimeout; // first 2 bytes of msgIn[] contain packet length - dc->connectionNumber2=dc->msgIn[2+5]; // 1/10/05 trying Andrew's patch - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC(1) step 4.\n", dc->iface->name); + dc->connectionNumber2 = dc->msgIn[2 + 5]; // 1/10/05 trying Andrew's patch + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC(1) step 4.\n", dc->iface->name); - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 5.\n", dc->iface->name); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 5.\n", dc->iface->name); - _daveSendWithPrefixNLPro(dc, b5, sizeof(b5)); + _daveSendWithPrefixNLPro(dc, b5, sizeof(b5)); - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); - res= _daveReadMPINLPro(dc->iface,dc->msgIn); - if (daveDebug & daveDebugConnect) - LOG2("%s daveConnectPLC() step 7.\n", dc->iface->name); - res= _daveNegPDUlengthRequest(dc, &p1); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 6.\n", dc->iface->name); + res = _daveReadMPINLPro(dc->iface, dc->msgIn); + if (daveDebug & daveDebugConnect) + LOG2("%s daveConnectPLC() step 7.\n", dc->iface->name); + res = _daveNegPDUlengthRequest(dc, &p1); return 0; } @@ -8044,10 +8531,10 @@ Executes part of the dialog necessary to send a message: */ int DECL2 _daveSendDialogNLPro(daveConnection * dc, int size) { - if (size>5){ - dc->needAckNumber=dc->messageNumber; - dc->msgOut[dc->iface->ackPos+1]=_daveIncMessageNumber(dc); - } + if (size > 5){ + dc->needAckNumber = dc->messageNumber; + dc->msgOut[dc->iface->ackPos + 1] = _daveIncMessageNumber(dc); + } _daveSendWithPrefix2NLPro(dc, size); return 0; } @@ -8059,39 +8546,39 @@ Sends a message and gets ackknowledge: int DECL2 _daveSendMessageNLPro(daveConnection * dc, PDU * p) { if (daveDebug & daveDebugExchange) { LOG2("%s enter _daveSendMessageNLPro\n", dc->iface->name); - } - if (_daveSendDialogNLPro(dc, /*2+*/p->hlen+p->plen+p->dlen)) { - LOG2("%s *** _daveSendMessageMPI error in _daveSendDialog.\n",dc->iface->name); + } + if (_daveSendDialogNLPro(dc, /*2+*/p->hlen + p->plen + p->dlen)) { + LOG2("%s *** _daveSendMessageMPI error in _daveSendDialog.\n", dc->iface->name); // return -1; - } + } if (daveDebug & daveDebugExchange) { - LOG3("%s _daveSendMessageMPI send done. needAck %x\n", dc->iface->name,dc->needAckNumber); - } + LOG3("%s _daveSendMessageMPI send done. needAck %x\n", dc->iface->name, dc->needAckNumber); + } return 0; } int DECL2 _daveExchangeNLPro(daveConnection * dc, PDU * p) { _daveSendMessageNLPro(dc, p); - dc->AnswLen=0; + dc->AnswLen = 0; return _daveGetResponseNLPro(dc); } int DECL2 _daveGetResponseNLPro(daveConnection *dc) { int res; if (daveDebug & daveDebugExchange) { - LOG2("%s _daveGetResponseNLPro receive message.\n", dc->iface->name); - } - res = _daveReadMPINLPro(dc->iface,dc->msgIn); - if (res<0) { + LOG2("%s _daveGetResponseNLPro receive message.\n", dc->iface->name); + } + res = _daveReadMPINLPro(dc->iface, dc->msgIn); + if (res < 0) { return res; - } - if (res==0) { + } + if (res == 0) { if (daveDebug & daveDebugPrintErrors) { LOG2("%s *** _daveGetResponseNLPro no answer data.\n", dc->iface->name); - } + } return -3; - } + } return 0; } @@ -8099,33 +8586,33 @@ int DECL2 _daveGetResponseNLPro(daveConnection *dc) { int DECL2 _daveSendWithPrefixNLPro(daveConnection * dc, uc *b, int size) { uc target[daveMaxRawLen]; - // uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14}; - uc fix[]= {0x4,0x80,0x80,0x0C,0x14,0x14}; - fix[4]=dc->connectionNumber2; // 1/10/05 trying Andrew's patch - fix[5]=dc->connectionNumber; // 1/10/05 trying Andrew's patch - memcpy(target,fix,sizeof(fix)); - memcpy(target+sizeof(fix),b,size); - target[1]|=dc->MPIAdr; + // uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14}; + uc fix[] = { 0x4, 0x80, 0x80, 0x0C, 0x14, 0x14 }; + fix[4] = dc->connectionNumber2; // 1/10/05 trying Andrew's patch + fix[5] = dc->connectionNumber; // 1/10/05 trying Andrew's patch + memcpy(target, fix, sizeof(fix)); + memcpy(target + sizeof(fix), b, size); + target[1] |= dc->MPIAdr; // target[2]|=dc->iface->localMPI; - memcpy(target+sizeof(fix),b,size); - return _daveSendWithCRCNLPro(dc->iface,target,size+sizeof(fix)); + memcpy(target + sizeof(fix), b, size); + return _daveSendWithCRCNLPro(dc->iface, target, size + sizeof(fix)); } int DECL2 _daveSendWithPrefix2NLPro(daveConnection * dc, int size) { - // uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14}; - uc fix[]= {0x14,0x80,0x80,0x0C,0x14,0x14}; - fix[4]=dc->connectionNumber2; // 1/10/05 trying Andrew's patch - fix[5]=dc->connectionNumber; // 1/10/05 trying Andrew's patch + // uc fix[]= {04,0x80,0x80,0x0C,0x03,0x14}; + uc fix[] = { 0x14, 0x80, 0x80, 0x0C, 0x14, 0x14 }; + fix[4] = dc->connectionNumber2; // 1/10/05 trying Andrew's patch + fix[5] = dc->connectionNumber; // 1/10/05 trying Andrew's patch memcpy(dc->msgOut, fix, sizeof(fix)); - dc->msgOut[1]|=dc->MPIAdr; + dc->msgOut[1] |= dc->MPIAdr; // dc->msgOut[2]|=dc->iface->localMPI; //??? /// dc->msgOut[sizeof(fix)]=0xF1; /* if (daveDebug & daveDebugPacket) _daveDump("_daveSendWithPrefix2",dc->msgOut,size+sizeof(fix)); */ - return _daveSendWithCRCNLPro(dc->iface, dc->msgOut, size+sizeof(fix)); - // } + return _daveSendWithCRCNLPro(dc->iface, dc->msgOut, size + sizeof(fix)); + // } return -1; /* shouldn't happen. */ } @@ -8133,30 +8620,30 @@ int DECL2 _daveDisconnectPLCNLPro(daveConnection * dc) { - + int res; - uc m[]={ + uc m[] = { 0x80 }; - uc t1[] = {0x04, 0x82, 0x80, 0x0c, 0x14, 0x14, 0x80}; + uc t1[] = { 0x04, 0x82, 0x80, 0x0c, 0x14, 0x14, 0x80 }; uc b1[daveMaxRawLen]; - + _daveSendWithPrefixNLPro(dc, t1, sizeof(t1)); //New Disconnection, old one did not work on my System. //JK _daveSendSingleNLPro(dc->iface, STX); //JK res=_daveReadMPINLPro(dc->iface,b1); //JK _daveSendWithPrefixNLPro(dc, m, 1); - res=_daveReadMPINLPro(dc->iface,b1); + res = _daveReadMPINLPro(dc->iface, b1); /* res=_daveReadMPI(dc->iface,b1); - if (daveDebug & daveDebugConnect) + if (daveDebug & daveDebugConnect) _daveDump("got",b1,10); _daveSendSingle(dc->iface, DLE); */ return 0; -} +} /* It seems to be better to complete this subroutine even if answers @@ -8164,52 +8651,53 @@ from adapter are not as expected. */ int DECL2 _daveDisconnectAdapterNLPro(daveInterface * di) { int res; - uc m2[]={ - 1,4,2 + uc m2[] = { + 1, 4, 2 }; uc b1[daveMaxRawLen]; - if (daveDebug & daveDebugInitAdapter) - LOG2("%s enter DisconnectAdapter()\n", di->name); - // _daveSendSingleNLPro(di, STX); - // res=_daveReadMPINLPro(di,b1); - /* if ((res!=1)||(b1[0]!=DLE)) return -1; */ - _daveSendWithCRCNLPro(di, m2, sizeof(m2)); - if (daveDebug & daveDebugInitAdapter) - LOG2("%s daveDisconnectAdapter() step 1.\n", di->name); - res=_daveReadMPINLPro(di, b1); - /* if ((res!=1)||(b1[0]!=DLE)) return -2; */ + if (daveDebug & daveDebugInitAdapter) + LOG2("%s enter DisconnectAdapter()\n", di->name); + // _daveSendSingleNLPro(di, STX); + // res=_daveReadMPINLPro(di,b1); + /* if ((res!=1)||(b1[0]!=DLE)) return -1; */ + _daveSendWithCRCNLPro(di, m2, sizeof(m2)); + if (daveDebug & daveDebugInitAdapter) + LOG2("%s daveDisconnectAdapter() step 1.\n", di->name); + res = _daveReadMPINLPro(di, b1); + /* if ((res!=1)||(b1[0]!=DLE)) return -2; */ /* res=_daveReadMPI(di, b1); - */ - /* if ((res!=1)||(b1[0]!=STX)) return -3; */ + */ + /* if ((res!=1)||(b1[0]!=STX)) return -3; */ /* - if (daveDebug & daveDebugInitAdapter) - LOG2("%s daveDisconnectAdapter() step 2.\n", di->name); + if (daveDebug & daveDebugInitAdapter) + LOG2("%s daveDisconnectAdapter() step 2.\n", di->name); _daveSendSingle(di, DLE); _daveReadChars2(di, b1, daveMaxRawLen); - // _daveReadChars(di, b1, tmo_normal, daveMaxRawLen); + // _daveReadChars(di, b1, tmo_normal, daveMaxRawLen); _daveSendSingle(di, DLE); - if (daveDebug & daveDebugInitAdapter) + if (daveDebug & daveDebugInitAdapter) _daveDump("got",b1,10); - */ - return 0; + */ + return 0; } /* */ -int DECL2 _daveListReachablePartnersNLPro(daveInterface * di,char * buf) { +int DECL2 _daveListReachablePartnersNLPro(daveInterface * di, char * buf) { uc b1[daveMaxRawLen]; - uc m1[]={1,7,2}; + uc m1[] = { 1, 7, 2 }; int res; _daveSendWithCRCNLPro(di, m1, sizeof(m1)); - res=_daveReadMPINLPro(di,b1); - // LOG2("res: %d\n", res); - if(135==res){ - memcpy(buf,b1+8,126); + res = _daveReadMPINLPro(di, b1); + // LOG2("res: %d\n", res); + if (135 == res){ + memcpy(buf, b1 + 8, 126); return 126; - } else - return 0; + } + else + return 0; } _openFunc SCP_open; @@ -8222,15 +8710,15 @@ _get_errnoFunc SCP_get_errno; /* -Changes: +Changes: 09/09/04 applied patch for variable Profibus speed from Andrew Rostovtsew. 12/09/04 removed debug printf from daveConnectPLC. -12/09/04 found and fixed a bug in daveFreeResults(): The result set is provided by the +12/09/04 found and fixed a bug in daveFreeResults(): The result set is provided by the application and not necessarily dynamic. So we shall not free() it. 12/10/04 added single bit read/write functions. 12/12/04 added Timer/Counter read functions. 12/13/04 changed dumpPDU to dump multiple results from daveFuncRead -12/15/04 changed comments to pure C style +12/15/04 changed comments to pure C style 12/15/04 replaced calls to write() with makro daveWriteFile. 12/15/04 removed daveSendDialog. Was only used in 1 place. 12/16/04 removed daveReadCharsPPI. It is replaced by daveReadChars. @@ -8258,23 +8746,23 @@ connection has been interrupted. 04/05/05 reworked error reporting. 04/06/05 renamed swap functions. When I began libnodave on little endian i386 and Linux, I used used Linux bswap functions. Then users (and later me) tried other systems without -a bswap. I also cannot use inline functions in Pascal. So I made my own bswaps. Then +a bswap. I also cannot use inline functions in Pascal. So I made my own bswaps. Then I, made the core of my own swaps dependent of DAVE_LITTLE_ENDIAN conditional to support also -bigendien systems. Now I want to rename them from bswap to something else to avoid +bigendien systems. Now I want to rename them from bswap to something else to avoid confusion for LINUX/UNIX users. The new names are daveSwapIed_16 and daveSwapIed_32. This -shall say swap "if endianness differs". While I could have used similar functions +shall say swap "if endianness differs". While I could have used similar functions from the network API (htons etc.) on Unix and Win32, they may not be present on e.g. microcontrollers. -I highly recommend to use these functions even when writing software for big endian +I highly recommend to use these functions even when writing software for big endian systems, where they effectively do nothing, as it will make your software portable. 04/09/05 removed template IBH_MPI header from daveConnection. Much of the information is -also available from other fields and the structure is simpler to define in other +also available from other fields and the structure is simpler to define in other languages. -04/09/05 removed CYGWIN defines. As there were no more differences against LINUX, it should -work with LINUX defines. +04/09/05 removed CYGWIN defines. As there were no more differences against LINUX, it should +work with LINUX defines. 04/21/05 renamed LITTLEENDIAN to DAVE_LITTLE_ENDIAN because it seems to conflict with -another #define in winsock2.h. -05/09/05 renamed more functions to daveXXX. +another #define in winsock2.h. +05/09/05 renamed more functions to daveXXX. 05/11/05 added some functions for the convenience of usage with .net or mono. The goal is that the application doesn't have to use members of data structures defined herein directly. This avoids "unsafe" pointer expressions in .net/MONO. It should also ease @@ -8291,18 +8779,18 @@ byte equivalents of these structures. - More common code. Only the very fundamental read/functions differ between Linux and Win32. 09/24/05 added MPI protocol version 3. This is what Step7 talks to MPI adapters and seems to be the only thing the Siemens USB-MPI adapter understands. This adapter is -currently only useable under Linux via libusb. +currently only useable under Linux via libusb. 09/27/05 added bug fix from Renato Gartmann: freeResults didn't free() the memory used for the result pointer array. -09/29/05 hopefully fixed superfluos STX in daveConnectPLCMPI2. +09/29/05 hopefully fixed superfluos STX in daveConnectPLCMPI2. 10/04/05 No there are adapters which want it... 10/05/05 Added first helper functions to use s7onlinx.dll for transport. 10/06/05 Added standard protocol specific functions to use s7onlinx.dll for transport. 10/06/05 renamed LITTLE_ENDIAN to DAVE_LITTLE_ENDIAN because it conflicts with another #define in some headers on some ARM systems. 10/10/05 change some pointer increments for gcc-4.0.2 compatibility. -10/18/05 Indroduced a (temporary?) work around to allow applications to use normal sequence -disconnectAdapter/closePort also with s7online. +10/18/05 Indroduced a (temporary?) work around to allow applications to use normal sequence +disconnectAdapter/closePort also with s7online. 02/20/06 Added code to support NetLink Pro. 05/15/06 Applied changes from Ken Wenzel for NetLink Pro. 07/28/06 Added CRC calculation code from Peter Etheridge. @@ -8311,7 +8799,7 @@ disconnectAdapter/closePort also with s7online. 01/04/07 Set last byte of resp09 to don't care as reported by Axel Kinting. 02/07/08 Removed patch from Keith Harris for RTS line. -Version 0.8.4.5 +Version 0.8.4.5 07/10/09 Changed readISOpacket for Win32 to select() before recv(). 07/10/09 Added daveCopyRAMtoROM 07/11/09 Changed calculation of netLen in doUpload() diff --git a/externalDlls/libnodave/nodave.h b/externalDlls/libnodave/nodave.h index 09626774..df703d9f 100644 --- a/externalDlls/libnodave/nodave.h +++ b/externalDlls/libnodave/nodave.h @@ -7,7 +7,7 @@ IBH/MHJ-NetLink or CPs 243, 343 and 443 or VIPA Speed7 with builtin ethernet support. - + (C) Thomas Hergenhahn (thomas.hergenhahn@web.de) 2002..2005 Libnodave is free software; you can redistribute it and/or modify @@ -22,8 +22,8 @@ You should have received a copy of the GNU Library General Public License along with Libnodave; see the file COPYING. If not, write to - the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ + the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + */ #ifdef __cplusplus @@ -40,11 +40,11 @@ extern "C" { #ifdef LINUX #define DECL2 #define EXPORTSPEC -typedef struct dost { - int rfd; - int wfd; -// int connectionType; -} _daveOSserialType; + typedef struct dost { + int rfd; + int wfd; + // int connectionType; + } _daveOSserialType; #include #define tmotype int #define OS_KNOWN // get rid of nested ifdefs. @@ -65,11 +65,11 @@ typedef struct dost { #define EXPORTSPEC __declspec (dllimport) #endif -typedef struct dost { - HANDLE rfd; - HANDLE wfd; -// int connectionType; -} _daveOSserialType; + typedef struct dost { + HANDLE rfd; + HANDLE wfd; + // int connectionType; + } _daveOSserialType; #define OS_KNOWN #endif @@ -77,16 +77,16 @@ typedef struct dost { #ifdef DOS #include #include -//#define DECL2 WINAPI -// #define DECL2 + //#define DECL2 WINAPI + // #define DECL2 #define DECL2 __cedcl #define EXPORTSPEC -typedef struct dost { - int rfd; - int wfd; - int connectionType; -} _daveOSserialType; + typedef struct dost { + int rfd; + int wfd; + int connectionType; + } _daveOSserialType; #define OS_KNOWN #endif @@ -95,11 +95,11 @@ typedef struct dost { #include #define DECL2 #define EXPORTSPEC -typedef struct dost { - int rfd; - int wfd; - int connectionType; -} _daveOSserialType; + typedef struct dost { + int rfd; + int wfd; + int connectionType; + } _daveOSserialType; #define tmotype long #define OS_KNOWN #endif @@ -108,9 +108,9 @@ typedef struct dost { #error Fill in what you need for your OS or API. #endif -/* - some frequently used ASCII control codes: -*/ + /* + some frequently used ASCII control codes: + */ #define DLE 0x10 #define ETX 0x03 #define STX 0x02 @@ -118,9 +118,9 @@ typedef struct dost { #define NAK 0x15 #define EOT 0x04 // for S5 #define ACK 0x06 // for S5 -/* - Protocol types to be used with newInterface: -*/ + /* + Protocol types to be used with newInterface: + */ #define daveProtoMPI 0 /* MPI for S7 300/400 */ #define daveProtoMPI2 1 /* MPI for S7 300/400, "Andrew's version" without STX */ #define daveProtoMPI3 2 /* MPI for S7 300/400, Step 7 Version, not yet implemented */ @@ -144,11 +144,11 @@ typedef struct dost { #define daveProtoNLProFamily 231 /* MPI with NetLink 50 MPI to ethernet gateway */ #define daveProtoUserTransport 255 /* Libnodave will pass the PDUs of S7 Communication to user */ - /* defined call back functions. */ + /* defined call back functions. */ -/* - * ProfiBus speed constants: -*/ + /* + * ProfiBus speed constants: + */ #define daveSpeed9k 0 #define daveSpeed19k 1 #define daveSpeed187k 2 @@ -157,9 +157,9 @@ typedef struct dost { #define daveSpeed45k 5 #define daveSpeed93k 6 -/* - Some MPI function codes (yet unused ones may be incorrect). -*/ + /* + Some MPI function codes (yet unused ones may be incorrect). + */ #define daveFuncOpenS7Connection 0xF0 #define daveFuncRead 0x04 #define daveFuncWrite 0x05 @@ -170,9 +170,9 @@ typedef struct dost { #define daveFuncUpload 0x1E #define daveFuncEndUpload 0x1F #define daveFuncInsertBlock 0x28 -/* - S7 specific constants: -*/ + /* + S7 specific constants: + */ #define daveBlockType_OB '8' #define daveBlockType_DB 'A' #define daveBlockType_SDB 'B' @@ -189,9 +189,9 @@ typedef struct dost { #define daveS5BlockType_DX 0x0C #define daveS5BlockType_OB 0x10 -/* - Use these constants for parameter "area" in daveReadBytes and daveWriteBytes -*/ + /* + Use these constants for parameter "area" in daveReadBytes and daveWriteBytes + */ #define daveSysInfo 0x3 /* System info of 200 family */ #define daveSysFlags 0x5 /* System flags of 200 family */ #define daveAnaIn 0x6 /* analog inputs of 200 family */ @@ -212,24 +212,24 @@ typedef struct dost { #define daveSysDataS5 0x86 /* system data area ? */ #define daveRawMemoryS5 0 /* just the raw memory */ -/** - Library specific: -**/ -/* - Result codes. Genarally, 0 means ok, - >0 are results (also errors) reported by the PLC - <0 means error reported by library code. -*/ + /** + Library specific: + **/ + /* + Result codes. Genarally, 0 means ok, + >0 are results (also errors) reported by the PLC + <0 means error reported by library code. + */ #define daveResOK 0 /* means all ok */ #define daveResNoPeripheralAtAddress 1 /* CPU tells there is no peripheral at address */ #define daveResMultipleBitsNotSupported 6 /* CPU tells it does not support to read a bit block with a */ - /* length other than 1 bit. */ + /* length other than 1 bit. */ #define daveResItemNotAvailable200 3 /* means a a piece of data is not available in the CPU, e.g. */ - /* when trying to read a non existing DB or bit bloc of length<>1 */ - /* This code seems to be specific to 200 family. */ - + /* when trying to read a non existing DB or bit bloc of length<>1 */ + /* This code seems to be specific to 200 family. */ + #define daveResItemNotAvailable 10 /* means a a piece of data is not available in the CPU, e.g. */ - /* when trying to read a non existing DB */ + /* when trying to read a non existing DB */ #define daveAddressOutOfRange 5 /* means the data address is beyond the CPUs address range */ #define daveWriteDataSizeMismatch 7 /* means the write data size doesn't fit item size */ @@ -249,43 +249,43 @@ typedef struct dost { #define daveResShortPacket -1024 #define daveResTimeout -1025 -/* - Error code to message string conversion: - Call this function to get an explanation for error codes returned by other functions. -*/ -EXPORTSPEC char * DECL2 daveStrerror(int code); // result is char because this is usual for strings - -/* - Copy an internal String into an external string buffer. This is needed to interface - with Visual Basic. Maybe it is helpful elsewhere, too. -*/ -EXPORTSPEC void DECL2 daveStringCopy(char * intString, char * extString); // args are char because this is usual for strings - - -/* - Max number of bytes in a single message. - An upper limit for MPI over serial is: - 8 transport header - +2*240 max PDU len *2 if every character were a DLE - +3 DLE,ETX and BCC - = 491 - - Later I saw some programs offering up to 960 bytes in PDU size negotiation - - Max number of bytes in a single message. - An upper limit for MPI over serial is: - 8 transport header - +2*960 max PDU len *2 if every character were a DLE - +3 DLE,ETX and BCC - = 1931 - - For now, we take the rounded max of all this to determine our buffer size. This is ok - for PC systems, where one k less or more doesn't matter. -*/ + /* + Error code to message string conversion: + Call this function to get an explanation for error codes returned by other functions. + */ + EXPORTSPEC char * DECL2 daveStrerror(int code); // result is char because this is usual for strings + + /* + Copy an internal String into an external string buffer. This is needed to interface + with Visual Basic. Maybe it is helpful elsewhere, too. + */ + EXPORTSPEC void DECL2 daveStringCopy(char * intString, char * extString); // args are char because this is usual for strings + + + /* + Max number of bytes in a single message. + An upper limit for MPI over serial is: + 8 transport header + +2*240 max PDU len *2 if every character were a DLE + +3 DLE,ETX and BCC + = 491 + + Later I saw some programs offering up to 960 bytes in PDU size negotiation + + Max number of bytes in a single message. + An upper limit for MPI over serial is: + 8 transport header + +2*960 max PDU len *2 if every character were a DLE + +3 DLE,ETX and BCC + = 1931 + + For now, we take the rounded max of all this to determine our buffer size. This is ok + for PC systems, where one k less or more doesn't matter. + */ #define daveMaxRawLen 2048 -/* - Some definitions for debugging: -*/ + /* + Some definitions for debugging: + */ #define daveDebugRawRead 0x01 /* Show the single bytes received */ #define daveDebugSpecialChars 0x02 /* Show when special chars are read */ #define daveDebugRawWrite 0x04 /* Show the single bytes written */ @@ -306,9 +306,9 @@ EXPORTSPEC void DECL2 daveStringCopy(char * intString, char * extString); // arg #define daveDebugOpen 0x10000 /* print messages in openSocket and setPort */ #define daveDebugAll 0x1ffff -/* - IBH-NetLink packet types: -*/ + /* + IBH-NetLink packet types: + */ #define _davePtEmpty -2 #define _davePtMPIAck -3 #define _davePtUnknownMPIFunc -4 @@ -316,652 +316,652 @@ EXPORTSPEC void DECL2 daveStringCopy(char * intString, char * extString); // arg #define _davePtReadResponse 1 #define _davePtWriteResponse 2 -/* - set and read debug level: -*/ -EXPORTSPEC void DECL2 daveSetDebug(int nDebug); -EXPORTSPEC int DECL2 daveGetDebug(void); -/* - Some data types: -*/ + /* + set and read debug level: + */ + EXPORTSPEC void DECL2 daveSetDebug(int nDebug); + EXPORTSPEC int DECL2 daveGetDebug(void); + /* + Some data types: + */ #define uc unsigned char #define us unsigned short #define u32 unsigned int -/* - This is a wrapper for the serial or ethernet interface. This is here to make porting easier. -*/ - -typedef struct _daveConnection daveConnection; -typedef struct _daveInterface daveInterface; - -/* - Helper struct to manage PDUs. This is NOT the part of the packet I would call PDU, but - a set of pointers that ease access to the "private parts" of a PDU. -*/ -typedef struct { - uc * header; /* pointer to start of PDU (PDU header) */ - uc * param; /* pointer to start of parameters inside PDU */ - uc * data; /* pointer to start of data inside PDU */ - uc * udata; /* pointer to start of data inside PDU */ - int hlen; /* header length */ - int plen; /* parameter length */ - int dlen; /* data length */ - int udlen; /* user or result data length */ -} PDU; - -/* - Definitions of prototypes for the protocol specific functions. The library "switches" - protocol by setting pointers to the protol specific implementations. -*/ -typedef int (DECL2 * _initAdapterFunc) (daveInterface * ); -typedef int (DECL2 * _connectPLCFunc) (daveConnection *); -typedef int (DECL2 * _disconnectPLCFunc) (daveConnection *); -typedef int (DECL2 * _disconnectAdapterFunc) (daveInterface * ); -typedef int (DECL2 * _exchangeFunc) (daveConnection *, PDU *); -typedef int (DECL2 * _sendMessageFunc) (daveConnection *, PDU *); -typedef int (DECL2 * _getResponseFunc) (daveConnection *); -typedef int (DECL2 * _listReachablePartnersFunc) (daveInterface * di, char * buf); // changed to unsigned char because it is a copy of an uc buffer - -/* - Definitions of prototypes for i/O functions. -*/ -typedef int (DECL2 * _writeFunc) (daveInterface *, char *, int); // changed to char because char is what system read/write expects -typedef int (DECL2 * _readFunc) (daveInterface *, char *, int); - -/* - This groups an interface together with some information about it's properties - in the library's context. -*/ -struct _daveInterface { - tmotype timeout; /* Timeout in microseconds used in transort. */ - _daveOSserialType fd; /* some handle for the serial interface */ - int localMPI; /* the adapter's MPI address */ - - int users; /* a counter used when multiple PLCs are accessed via */ - /* the same serial interface and adapter. */ - char * name; /* just a name that can be used in programs dealing with multiple */ - /* daveInterfaces */ - int protocol; /* The kind of transport protocol used on this interface. */ - int speed; /* The MPI or Profibus speed */ - int ackPos; /* position of some packet number that has to be repeated in ackknowledges */ - int nextConnection; - _initAdapterFunc initAdapter; /* pointers to the protocol */ - _connectPLCFunc connectPLC; /* specific implementations */ - _disconnectPLCFunc disconnectPLC; /* of these functions */ - _disconnectAdapterFunc disconnectAdapter; - _exchangeFunc exchange; - _sendMessageFunc sendMessage; - _getResponseFunc getResponse; - _listReachablePartnersFunc listReachablePartners; - char realName[20]; - _readFunc ifread; - _writeFunc ifwrite; - int seqNumber; -}; - -EXPORTSPEC daveInterface * DECL2 daveNewInterface(_daveOSserialType nfd, char * nname, int localMPI, int protocol, int speed); -EXPORTSPEC daveInterface * DECL2 davePascalNewInterface(_daveOSserialType* nfd, char * nname, int localMPI, int protocol, int speed); -/* - This is the packet header used by IBH ethernet NetLink. -*/ -typedef struct { - uc ch1; // logical connection or channel ? - uc ch2; // logical connection or channel ? - uc len; // number of bytes counted from the ninth one. - uc packetNumber; // a counter, response packets refer to request packets - us sFlags; // my guess - us rFlags; // my interpretation -} IBHpacket; - -/* - Header for MPI packets on IBH-NetLink: -*/ - -typedef struct { - uc src_conn; - uc dst_conn; - uc MPI; - uc localMPI; - uc len; - uc func; - uc packetNumber; -} MPIheader; - -typedef struct { - uc src_conn; - uc dst_conn; - uc MPI; - uc xxx1; - uc xxx2; - uc xx22; - uc len; - uc func; - uc packetNumber; -} MPIheader2; - -typedef struct _daveS5AreaInfo { - int area; - int DBnumber; - int address; - int len; - struct _daveS5AreaInfo * next; -} daveS5AreaInfo; - -typedef struct _daveS5cache { - int PAE; // start of inputs - int PAA; // start of outputs - int flags; // start of flag (marker) memory - int timers; // start of timer memory - int counters;// start of counter memory - int systemData;// start of system data - daveS5AreaInfo * first; -} daveS5cache; - -/* - This holds data for a PLC connection; -*/ - -struct _daveConnection { - int AnswLen; /* length of last message */ - uc * resultPointer; /* used to retrieve single values from the result byte array */ - int maxPDUlength; - int MPIAdr; /* The PLC's address */ - daveInterface * iface; /* pointer to used interface */ - int needAckNumber; /* message number we need ackknowledge for */ - int PDUnumber; /* current PDU number */ - int ibhSrcConn; - int ibhDstConn; - uc msgIn[daveMaxRawLen]; - uc msgOut[daveMaxRawLen]; - uc * _resultPointer; - int PDUstartO; /* position of PDU in outgoing messages. This is different for different transport methodes. */ - int PDUstartI; /* position of PDU in incoming messages. This is different for different transport methodes. */ - int rack; /* rack number for ISO over TCP */ - int slot; /* slot number for ISO over TCP */ - int connectionNumber; - int connectionNumber2; - uc messageNumber; /* current MPI message number */ - uc packetNumber; /* packetNumber in transport layer */ - void * hook; /* used in CPU/CP simulation: pointer to the rest we have to send if message doesn't fit in a single packet */ - daveS5cache * cache; /* used in AS511: We cache addresses of memory areas and datablocks here */ - - int TPDUsize; // size of TPDU for ISO over TCP - int partPos; // remember position for ISO over TCP fragmentation - - uc application_block_subsystem; /* used in S7Online */ - - int DestinationIsIP; - - int _Destination1; - int _Destination2; - int _Destination3; - int _Destination4; - int _DestinationSize; - - int ConnectionType; // 1=PG Conn, 2=OP Conn - - int routing; - unsigned int routingSubnetFirst; - unsigned int routingSubnetSecond; - int routingRack; - int routingSlot; - int routingDestinationIsIP; - int routingConnectionType; // 1=PG Conn, 2=OP Conn - - int _routingDestination1; - int _routingDestination2; - int _routingDestination3; - int _routingDestination4; - int _routingDestinationSize; -}; - - -/* - Setup a new connection structure using an initialized - daveInterface and PLC's MPI address. -*/ -EXPORTSPEC -daveConnection * DECL2 daveNewConnection(daveInterface * di, int MPI,int rack, int slot); - -/* - Setup a new Extended Connection (or a TCPIP Connection to -*/ -EXPORTSPEC -daveConnection * DECL2 daveNewExtendedConnection(daveInterface * di, void * Destination, int DestinationIsIP, int rack, int slot, int routing, int routingSubnetFirst, int routingSubnetSecond, int routingRack, int routingSlot, void * routingDestination, int routingDestinationIsIP, int ConnectionType, int routingConnectionType); - -/* - Setup a new connection structure using an daveConnection Structure -*/ -daveConnection * DECL2 _daveNewConnection(daveConnection * dc); - -typedef struct { - uc type[2]; - unsigned short count; -} daveBlockTypeEntry; - -typedef struct { - unsigned short number; - uc type[2]; -} daveBlockEntry; - -typedef struct { - uc type[2]; - uc x1[2]; /* 00 4A */ - uc w1[2]; /* some word var? */ - char pp[2]; /* allways 'pp' */ - uc x2[4]; /* 00 4A */ - unsigned short number; /* the block's number */ - uc x3[26]; /* ? */ - unsigned short length; /* the block's length */ - uc x4[16]; - uc name[8]; - uc x5[12]; -} daveBlockInfo; -/** - PDU handling: - PDU is the central structure present in S7 communication. - It is composed of a 10 or 12 byte header,a parameter block and a data block. - When reading or writing values, the data field is itself composed of a data - header followed by payload data -**/ -typedef struct { - uc P; /* allways 0x32 */ - uc type; /* Header type, one of 1,2,3 or 7. type 2 and 3 headers are two bytes longer. */ - uc a,b; /* currently unknown. Maybe it can be used for long numbers? */ - us number; /* A number. This can be used to make sure a received answer */ + /* + This is a wrapper for the serial or ethernet interface. This is here to make porting easier. + */ + + typedef struct _daveConnection daveConnection; + typedef struct _daveInterface daveInterface; + + /* + Helper struct to manage PDUs. This is NOT the part of the packet I would call PDU, but + a set of pointers that ease access to the "private parts" of a PDU. + */ + typedef struct { + uc * header; /* pointer to start of PDU (PDU header) */ + uc * param; /* pointer to start of parameters inside PDU */ + uc * data; /* pointer to start of data inside PDU */ + uc * udata; /* pointer to start of data inside PDU */ + int hlen; /* header length */ + int plen; /* parameter length */ + int dlen; /* data length */ + int udlen; /* user or result data length */ + } PDU; + + /* + Definitions of prototypes for the protocol specific functions. The library "switches" + protocol by setting pointers to the protol specific implementations. + */ + typedef int (DECL2 * _initAdapterFunc) (daveInterface *); + typedef int (DECL2 * _connectPLCFunc) (daveConnection *); + typedef int (DECL2 * _disconnectPLCFunc) (daveConnection *); + typedef int (DECL2 * _disconnectAdapterFunc) (daveInterface *); + typedef int (DECL2 * _exchangeFunc) (daveConnection *, PDU *); + typedef int (DECL2 * _sendMessageFunc) (daveConnection *, PDU *); + typedef int (DECL2 * _getResponseFunc) (daveConnection *); + typedef int (DECL2 * _listReachablePartnersFunc) (daveInterface * di, char * buf); // changed to unsigned char because it is a copy of an uc buffer + + /* + Definitions of prototypes for i/O functions. + */ + typedef int (DECL2 * _writeFunc) (daveInterface *, char *, int); // changed to char because char is what system read/write expects + typedef int (DECL2 * _readFunc) (daveInterface *, char *, int); + + /* + This groups an interface together with some information about it's properties + in the library's context. + */ + struct _daveInterface { + tmotype timeout; /* Timeout in microseconds used in transort. */ + _daveOSserialType fd; /* some handle for the serial interface */ + int localMPI; /* the adapter's MPI address */ + + int users; /* a counter used when multiple PLCs are accessed via */ + /* the same serial interface and adapter. */ + char * name; /* just a name that can be used in programs dealing with multiple */ + /* daveInterfaces */ + int protocol; /* The kind of transport protocol used on this interface. */ + int speed; /* The MPI or Profibus speed */ + int ackPos; /* position of some packet number that has to be repeated in ackknowledges */ + int nextConnection; + _initAdapterFunc initAdapter; /* pointers to the protocol */ + _connectPLCFunc connectPLC; /* specific implementations */ + _disconnectPLCFunc disconnectPLC; /* of these functions */ + _disconnectAdapterFunc disconnectAdapter; + _exchangeFunc exchange; + _sendMessageFunc sendMessage; + _getResponseFunc getResponse; + _listReachablePartnersFunc listReachablePartners; + char realName[20]; + _readFunc ifread; + _writeFunc ifwrite; + int seqNumber; + }; + + EXPORTSPEC daveInterface * DECL2 daveNewInterface(_daveOSserialType nfd, char * nname, int localMPI, int protocol, int speed); + EXPORTSPEC daveInterface * DECL2 davePascalNewInterface(_daveOSserialType* nfd, char * nname, int localMPI, int protocol, int speed); + /* + This is the packet header used by IBH ethernet NetLink. + */ + typedef struct { + uc ch1; // logical connection or channel ? + uc ch2; // logical connection or channel ? + uc len; // number of bytes counted from the ninth one. + uc packetNumber; // a counter, response packets refer to request packets + us sFlags; // my guess + us rFlags; // my interpretation + } IBHpacket; + + /* + Header for MPI packets on IBH-NetLink: + */ + + typedef struct { + uc src_conn; + uc dst_conn; + uc MPI; + uc localMPI; + uc len; + uc func; + uc packetNumber; + } MPIheader; + + typedef struct { + uc src_conn; + uc dst_conn; + uc MPI; + uc xxx1; + uc xxx2; + uc xx22; + uc len; + uc func; + uc packetNumber; + } MPIheader2; + + typedef struct _daveS5AreaInfo { + int area; + int DBnumber; + int address; + int len; + struct _daveS5AreaInfo * next; + } daveS5AreaInfo; + + typedef struct _daveS5cache { + int PAE; // start of inputs + int PAA; // start of outputs + int flags; // start of flag (marker) memory + int timers; // start of timer memory + int counters;// start of counter memory + int systemData;// start of system data + daveS5AreaInfo * first; + } daveS5cache; + + /* + This holds data for a PLC connection; + */ + + struct _daveConnection { + int AnswLen; /* length of last message */ + uc * resultPointer; /* used to retrieve single values from the result byte array */ + int maxPDUlength; + int MPIAdr; /* The PLC's address */ + daveInterface * iface; /* pointer to used interface */ + int needAckNumber; /* message number we need ackknowledge for */ + int PDUnumber; /* current PDU number */ + int ibhSrcConn; + int ibhDstConn; + uc msgIn[daveMaxRawLen]; + uc msgOut[daveMaxRawLen]; + uc * _resultPointer; + int PDUstartO; /* position of PDU in outgoing messages. This is different for different transport methodes. */ + int PDUstartI; /* position of PDU in incoming messages. This is different for different transport methodes. */ + int rack; /* rack number for ISO over TCP */ + int slot; /* slot number for ISO over TCP */ + int connectionNumber; + int connectionNumber2; + uc messageNumber; /* current MPI message number */ + uc packetNumber; /* packetNumber in transport layer */ + void * hook; /* used in CPU/CP simulation: pointer to the rest we have to send if message doesn't fit in a single packet */ + daveS5cache * cache; /* used in AS511: We cache addresses of memory areas and datablocks here */ + + int TPDUsize; // size of TPDU for ISO over TCP + int partPos; // remember position for ISO over TCP fragmentation + + uc application_block_subsystem; /* used in S7Online */ + + int DestinationIsIP; + + int _Destination1; + int _Destination2; + int _Destination3; + int _Destination4; + int _DestinationSize; + + int ConnectionType; // 1=PG Conn, 2=OP Conn + + int routing; + unsigned int routingSubnetFirst; + unsigned int routingSubnetSecond; + int routingRack; + int routingSlot; + int routingDestinationIsIP; + int routingConnectionType; // 1=PG Conn, 2=OP Conn + + int _routingDestination1; + int _routingDestination2; + int _routingDestination3; + int _routingDestination4; + int _routingDestinationSize; + }; + + + /* + Setup a new connection structure using an initialized + daveInterface and PLC's MPI address. + */ + EXPORTSPEC + daveConnection * DECL2 daveNewConnection(daveInterface * di, int MPI, int rack, int slot); + + /* + Setup a new Extended Connection (or a TCPIP Connection to + */ + EXPORTSPEC + daveConnection * DECL2 daveNewExtendedConnection(daveInterface * di, void * Destination, int DestinationIsIP, int rack, int slot, int routing, int routingSubnetFirst, int routingSubnetSecond, int routingRack, int routingSlot, void * routingDestination, int routingDestinationIsIP, int ConnectionType, int routingConnectionType); + + /* + Setup a new connection structure using an daveConnection Structure + */ + daveConnection * DECL2 _daveNewConnection(daveConnection * dc); + + typedef struct { + uc type[2]; + unsigned short count; + } daveBlockTypeEntry; + + typedef struct { + unsigned short number; + uc type[2]; + } daveBlockEntry; + + typedef struct { + uc type[2]; + uc x1[2]; /* 00 4A */ + uc w1[2]; /* some word var? */ + char pp[2]; /* allways 'pp' */ + uc x2[4]; /* 00 4A */ + unsigned short number; /* the block's number */ + uc x3[26]; /* ? */ + unsigned short length; /* the block's length */ + uc x4[16]; + uc name[8]; + uc x5[12]; + } daveBlockInfo; + /** + PDU handling: + PDU is the central structure present in S7 communication. + It is composed of a 10 or 12 byte header,a parameter block and a data block. + When reading or writing values, the data field is itself composed of a data + header followed by payload data + **/ + typedef struct { + uc P; /* allways 0x32 */ + uc type; /* Header type, one of 1,2,3 or 7. type 2 and 3 headers are two bytes longer. */ + uc a, b; /* currently unknown. Maybe it can be used for long numbers? */ + us number; /* A number. This can be used to make sure a received answer */ /* corresponds to the request with the same number. */ - us plen; /* length of parameters which follow this header */ - us dlen; /* length of data which follow the parameters */ - uc result[2]; /* only present in type 2 and 3 headers. This contains error information. */ -} PDUHeader; - -/* - same as above, but made up of single bytes only, so that every single byte can be adressed separately -*/ -typedef struct { - uc P; /* allways 0x32 */ - uc type; /* Header type, one of 1,2,3 or 7. type 2 and 3 headers are two bytes longer. */ - uc a,b; /* currently unknown. Maybe it can be used for long numbers? */ - uc numberHi,numberLo; /* A number. This can be used to make sure a received answer */ + us plen; /* length of parameters which follow this header */ + us dlen; /* length of data which follow the parameters */ + uc result[2]; /* only present in type 2 and 3 headers. This contains error information. */ + } PDUHeader; + + /* + same as above, but made up of single bytes only, so that every single byte can be adressed separately + */ + typedef struct { + uc P; /* allways 0x32 */ + uc type; /* Header type, one of 1,2,3 or 7. type 2 and 3 headers are two bytes longer. */ + uc a, b; /* currently unknown. Maybe it can be used for long numbers? */ + uc numberHi, numberLo; /* A number. This can be used to make sure a received answer */ /* corresponds to the request with the same number. */ - uc plenHi,plenLo; /* length of parameters which follow this header */ - uc dlenHi,dlenLo; /* length of data which follow the parameters */ - uc result[2]; /* only present in type 2 and 3 headers. This contains error information. */ -} PDUHeader2; - -/* - set up the header. Needs valid header pointer in the struct p points to. -*/ -EXPORTSPEC void DECL2 _daveInitPDUheader(PDU * p, int type); -/* - add parameters after header, adjust pointer to data. - needs valid header -*/ -EXPORTSPEC void DECL2 _daveAddParam(PDU * p,uc * param,us len); -/* - add data after parameters, set dlen - needs valid header,and valid parameters. -*/ -EXPORTSPEC void DECL2 _daveAddData(PDU * p,void * data,int len); -/* - add values after value header in data, adjust dlen and data count. - needs valid header,parameters,data,dlen -*/ -EXPORTSPEC void DECL2 _daveAddValue(PDU * p,void * data,int len); -/* - add data in user data. Add a user data header, if not yet present. -*/ -EXPORTSPEC void DECL2 _daveAddUserData(PDU * p, uc * da, int len); -/* - set up pointers to the fields of a received message -*/ -EXPORTSPEC int DECL2 _daveSetupReceivedPDU(daveConnection * dc,PDU * p); -/* - Get the eror code from a PDU, if one. -*/ -EXPORTSPEC int DECL2 daveGetPDUerror(PDU * p); -/* - send PDU to PLC and retrieve the answer -*/ -EXPORTSPEC int DECL2 _daveExchange(daveConnection * dc,PDU *p); -/* - retrieve the answer -*/ -EXPORTSPEC int DECL2 daveGetResponse(daveConnection * dc); -/* - send PDU to PLC -*/ -EXPORTSPEC int DECL2 daveSendMessage(daveConnection * dc, PDU * p); - -EXPORTSPEC int DECL2 daveGetPDUData(daveConnection * dc, PDU*p2, uc* data, int* ldata, uc* param, int* lparam); - -/****** - - Utilities: - -****/ -/* - Hex dump PDU: -*/ -EXPORTSPEC void DECL2 _daveDumpPDU(PDU * p); - -/* - This is an extended memory compare routine. It can handle don't care and stop flags - in the sample data. A stop flag lets it return success, if there were no mismatches - up to this point. -*/ -EXPORTSPEC int DECL2 _daveMemcmp(us * a, uc *b, size_t len); - -/* - Hex dump. Write the name followed by len bytes written in hex and a newline: -*/ -//EXPORTSPEC void DECL2 _daveDump(char * name, uc *b, int len); -EXPORTSPEC void DECL2 _daveDump(char * name, void *b, int len); - -/* - names for PLC objects: -*/ -EXPORTSPEC char * DECL2 daveBlockName(uc bn); // char or uc,to decide -EXPORTSPEC char * DECL2 daveAreaName(uc n); // to decide - -/* - swap functions. These swap function do a swao on little endian machines only: -*/ -EXPORTSPEC short DECL2 daveSwapIed_16(short ff); -EXPORTSPEC int DECL2 daveSwapIed_32(int ff); - -/** - Data conversion convenience functions. The older set has been removed. - Newer conversion routines. As the terms WORD, INT, INTEGER etc have different meanings - for users of different programming languages and compilers, I choose to provide a new - set of conversion routines named according to the bit length of the value used. The 'U' - or 'S' stands for unsigned or signed. -**/ -/* - Get a value from the position b points to. B is typically a pointer to a buffer that has - been filled with daveReadBytes: -*/ -EXPORTSPEC float DECL2 daveGetFloatAt(daveConnection * dc, int pos); -EXPORTSPEC float DECL2 daveGetKGAt(daveConnection * dc, int pos); - - -EXPORTSPEC float DECL2 toPLCfloat(float ff); -EXPORTSPEC int DECL2 daveToPLCfloat(float ff); -EXPORTSPEC int DECL2 daveToKG(float ff); - - -EXPORTSPEC int DECL2 daveGetS8from(uc *b); -EXPORTSPEC int DECL2 daveGetU8from(uc *b); -EXPORTSPEC int DECL2 daveGetS16from(uc *b); -EXPORTSPEC int DECL2 daveGetU16from(uc *b); -EXPORTSPEC int DECL2 daveGetS32from(uc *b); -EXPORTSPEC unsigned int DECL2 daveGetU32from(uc *b); -EXPORTSPEC float DECL2 daveGetFloatfrom(uc *b); -EXPORTSPEC float DECL2 daveGetKGfrom(uc *b); -/* - Get a value from the current position in the last result read on the connection dc. - This will increment an internal pointer, so the next value is read from the position - following this value. -*/ - -EXPORTSPEC int DECL2 daveGetS8(daveConnection * dc); -EXPORTSPEC int DECL2 daveGetU8(daveConnection * dc); -EXPORTSPEC int DECL2 daveGetS16(daveConnection * dc); -EXPORTSPEC int DECL2 daveGetU16(daveConnection * dc); -EXPORTSPEC int DECL2 daveGetS32(daveConnection * dc); -EXPORTSPEC unsigned int DECL2 daveGetU32(daveConnection * dc); -EXPORTSPEC float DECL2 daveGetFloat(daveConnection * dc); -EXPORTSPEC float DECL2 daveGetKG(daveConnection * dc); -/* - Get a value from a given position in the last result read on the connection dc. -*/ -EXPORTSPEC int DECL2 daveGetS8At(daveConnection * dc, int pos); -EXPORTSPEC int DECL2 daveGetU8At(daveConnection * dc, int pos); -EXPORTSPEC int DECL2 daveGetS16At(daveConnection * dc, int pos); -EXPORTSPEC int DECL2 daveGetU16At(daveConnection * dc, int pos); -EXPORTSPEC int DECL2 daveGetS32At(daveConnection * dc, int pos); -EXPORTSPEC unsigned int DECL2 daveGetU32At(daveConnection * dc, int pos); -/* - put one byte into buffer b: -*/ -EXPORTSPEC uc * DECL2 davePut8(uc *b,int v); -EXPORTSPEC uc * DECL2 davePut16(uc *b,int v); -EXPORTSPEC uc * DECL2 davePut32(uc *b,int v); -EXPORTSPEC uc * DECL2 davePutFloat(uc *b,float v); -EXPORTSPEC uc * DECL2 davePutKG(uc *b,float v); -EXPORTSPEC void DECL2 davePut8At(uc *b, int pos, int v); -EXPORTSPEC void DECL2 davePut16At(uc *b, int pos, int v); -EXPORTSPEC void DECL2 davePut32At(uc *b, int pos, int v); -EXPORTSPEC void DECL2 davePutFloatAt(uc *b,int pos, float v); -EXPORTSPEC void DECL2 davePutKGAt(uc *b,int pos, float v); -/** - Timer and Counter conversion functions: -**/ -/* - get time in seconds from current read position: -*/ -EXPORTSPEC float DECL2 daveGetSeconds(daveConnection * dc); -/* - get time in seconds from random position: -*/ -EXPORTSPEC float DECL2 daveGetSecondsAt(daveConnection * dc, int pos); -/* - get counter value from current read position: -*/ -EXPORTSPEC int DECL2 daveGetCounterValue(daveConnection * dc); -/* - get counter value from random read position: -*/ -EXPORTSPEC int DECL2 daveGetCounterValueAt(daveConnection * dc,int pos); - -/* - Functions to load blocks from PLC: -*/ -EXPORTSPEC void DECL2 _daveConstructUpload(PDU *p, char blockType, int blockNr); // char or uc,to decide -EXPORTSPEC void DECL2 _daveConstructDoUpload(PDU * p, int uploadID); -EXPORTSPEC void DECL2 _daveConstructEndUpload(PDU * p, int uploadID); - -/* - Functions to load files from NC: -*/ -EXPORTSPEC void DECL2 _daveConstructUploadNC(PDU *p, const char *filename); -EXPORTSPEC void DECL2 _daveConstructDoUploadNC(PDU * p, uc *uploadID); -EXPORTSPEC void DECL2 _daveConstructEndUploadNC(PDU * p, uc *uploadID); -/* - Get the PLC's order code as ASCIIZ. Buf must provide space for - 21 characters at least. -*/ + uc plenHi, plenLo; /* length of parameters which follow this header */ + uc dlenHi, dlenLo; /* length of data which follow the parameters */ + uc result[2]; /* only present in type 2 and 3 headers. This contains error information. */ + } PDUHeader2; + + /* + set up the header. Needs valid header pointer in the struct p points to. + */ + EXPORTSPEC void DECL2 _daveInitPDUheader(PDU * p, int type); + /* + add parameters after header, adjust pointer to data. + needs valid header + */ + EXPORTSPEC void DECL2 _daveAddParam(PDU * p, uc * param, us len); + /* + add data after parameters, set dlen + needs valid header,and valid parameters. + */ + EXPORTSPEC void DECL2 _daveAddData(PDU * p, void * data, int len); + /* + add values after value header in data, adjust dlen and data count. + needs valid header,parameters,data,dlen + */ + EXPORTSPEC void DECL2 _daveAddValue(PDU * p, void * data, int len); + /* + add data in user data. Add a user data header, if not yet present. + */ + EXPORTSPEC void DECL2 _daveAddUserData(PDU * p, uc * da, int len); + /* + set up pointers to the fields of a received message + */ + EXPORTSPEC int DECL2 _daveSetupReceivedPDU(daveConnection * dc, PDU * p); + /* + Get the eror code from a PDU, if one. + */ + EXPORTSPEC int DECL2 daveGetPDUerror(PDU * p); + /* + send PDU to PLC and retrieve the answer + */ + EXPORTSPEC int DECL2 _daveExchange(daveConnection * dc, PDU *p); + /* + retrieve the answer + */ + EXPORTSPEC int DECL2 daveGetResponse(daveConnection * dc); + /* + send PDU to PLC + */ + EXPORTSPEC int DECL2 daveSendMessage(daveConnection * dc, PDU * p); + + EXPORTSPEC int DECL2 daveGetPDUData(daveConnection * dc, PDU*p2, uc* data, int* ldata, uc* param, int* lparam); + + /****** + + Utilities: + + ****/ + /* + Hex dump PDU: + */ + EXPORTSPEC void DECL2 _daveDumpPDU(PDU * p); + + /* + This is an extended memory compare routine. It can handle don't care and stop flags + in the sample data. A stop flag lets it return success, if there were no mismatches + up to this point. + */ + EXPORTSPEC int DECL2 _daveMemcmp(us * a, uc *b, size_t len); + + /* + Hex dump. Write the name followed by len bytes written in hex and a newline: + */ + //EXPORTSPEC void DECL2 _daveDump(char * name, uc *b, int len); + EXPORTSPEC void DECL2 _daveDump(char * name, void *b, int len); + + /* + names for PLC objects: + */ + EXPORTSPEC char * DECL2 daveBlockName(uc bn); // char or uc,to decide + EXPORTSPEC char * DECL2 daveAreaName(uc n); // to decide + + /* + swap functions. These swap function do a swao on little endian machines only: + */ + EXPORTSPEC short DECL2 daveSwapIed_16(short ff); + EXPORTSPEC int DECL2 daveSwapIed_32(int ff); + + /** + Data conversion convenience functions. The older set has been removed. + Newer conversion routines. As the terms WORD, INT, INTEGER etc have different meanings + for users of different programming languages and compilers, I choose to provide a new + set of conversion routines named according to the bit length of the value used. The 'U' + or 'S' stands for unsigned or signed. + **/ + /* + Get a value from the position b points to. B is typically a pointer to a buffer that has + been filled with daveReadBytes: + */ + EXPORTSPEC float DECL2 daveGetFloatAt(daveConnection * dc, int pos); + EXPORTSPEC float DECL2 daveGetKGAt(daveConnection * dc, int pos); + + + EXPORTSPEC float DECL2 toPLCfloat(float ff); + EXPORTSPEC int DECL2 daveToPLCfloat(float ff); + EXPORTSPEC int DECL2 daveToKG(float ff); + + + EXPORTSPEC int DECL2 daveGetS8from(uc *b); + EXPORTSPEC int DECL2 daveGetU8from(uc *b); + EXPORTSPEC int DECL2 daveGetS16from(uc *b); + EXPORTSPEC int DECL2 daveGetU16from(uc *b); + EXPORTSPEC int DECL2 daveGetS32from(uc *b); + EXPORTSPEC unsigned int DECL2 daveGetU32from(uc *b); + EXPORTSPEC float DECL2 daveGetFloatfrom(uc *b); + EXPORTSPEC float DECL2 daveGetKGfrom(uc *b); + /* + Get a value from the current position in the last result read on the connection dc. + This will increment an internal pointer, so the next value is read from the position + following this value. + */ + + EXPORTSPEC int DECL2 daveGetS8(daveConnection * dc); + EXPORTSPEC int DECL2 daveGetU8(daveConnection * dc); + EXPORTSPEC int DECL2 daveGetS16(daveConnection * dc); + EXPORTSPEC int DECL2 daveGetU16(daveConnection * dc); + EXPORTSPEC int DECL2 daveGetS32(daveConnection * dc); + EXPORTSPEC unsigned int DECL2 daveGetU32(daveConnection * dc); + EXPORTSPEC float DECL2 daveGetFloat(daveConnection * dc); + EXPORTSPEC float DECL2 daveGetKG(daveConnection * dc); + /* + Get a value from a given position in the last result read on the connection dc. + */ + EXPORTSPEC int DECL2 daveGetS8At(daveConnection * dc, int pos); + EXPORTSPEC int DECL2 daveGetU8At(daveConnection * dc, int pos); + EXPORTSPEC int DECL2 daveGetS16At(daveConnection * dc, int pos); + EXPORTSPEC int DECL2 daveGetU16At(daveConnection * dc, int pos); + EXPORTSPEC int DECL2 daveGetS32At(daveConnection * dc, int pos); + EXPORTSPEC unsigned int DECL2 daveGetU32At(daveConnection * dc, int pos); + /* + put one byte into buffer b: + */ + EXPORTSPEC uc * DECL2 davePut8(uc *b, int v); + EXPORTSPEC uc * DECL2 davePut16(uc *b, int v); + EXPORTSPEC uc * DECL2 davePut32(uc *b, int v); + EXPORTSPEC uc * DECL2 davePutFloat(uc *b, float v); + EXPORTSPEC uc * DECL2 davePutKG(uc *b, float v); + EXPORTSPEC void DECL2 davePut8At(uc *b, int pos, int v); + EXPORTSPEC void DECL2 davePut16At(uc *b, int pos, int v); + EXPORTSPEC void DECL2 davePut32At(uc *b, int pos, int v); + EXPORTSPEC void DECL2 davePutFloatAt(uc *b, int pos, float v); + EXPORTSPEC void DECL2 davePutKGAt(uc *b, int pos, float v); + /** + Timer and Counter conversion functions: + **/ + /* + get time in seconds from current read position: + */ + EXPORTSPEC float DECL2 daveGetSeconds(daveConnection * dc); + /* + get time in seconds from random position: + */ + EXPORTSPEC float DECL2 daveGetSecondsAt(daveConnection * dc, int pos); + /* + get counter value from current read position: + */ + EXPORTSPEC int DECL2 daveGetCounterValue(daveConnection * dc); + /* + get counter value from random read position: + */ + EXPORTSPEC int DECL2 daveGetCounterValueAt(daveConnection * dc, int pos); + + /* + Functions to load blocks from PLC: + */ + EXPORTSPEC void DECL2 _daveConstructUpload(PDU *p, char blockType, int blockNr); // char or uc,to decide + EXPORTSPEC void DECL2 _daveConstructDoUpload(PDU * p, int uploadID); + EXPORTSPEC void DECL2 _daveConstructEndUpload(PDU * p, int uploadID); + + /* + Functions to load files from NC: + */ + EXPORTSPEC void DECL2 _daveConstructUploadNC(PDU *p, const char *filename); + EXPORTSPEC void DECL2 _daveConstructDoUploadNC(PDU * p, uc *uploadID); + EXPORTSPEC void DECL2 _daveConstructEndUploadNC(PDU * p, uc *uploadID); + /* + Get the PLC's order code as ASCIIZ. Buf must provide space for + 21 characters at least. + */ #define daveOrderCodeSize 21 -EXPORTSPEC int DECL2 daveGetOrderCode(daveConnection * dc, char * buf); // char, users buffer, or to decide - -/* - connect to a PLC. returns 0 on success. -*/ -EXPORTSPEC int DECL2 daveConnectPLC(daveConnection * dc); - -/* - Read len bytes from the PLC. Start determines the first byte. - Area denotes whether the data comes from FLAGS, DATA BLOCKS, - INPUTS or OUTPUTS, etc. - DB is the number of the data block to be used. Set it to zero - for other area types. - Buffer is a pointer to a memory block provided by the calling - program. If the pointer is not NULL, the result data will be copied thereto. - Hence it must be big enough to take up the result. - In any case, you can also retrieve the result data using the get macros - on the connection pointer. - - RESTRICTION:There is no check for max. message len or automatic splitting into + EXPORTSPEC int DECL2 daveGetOrderCode(daveConnection * dc, char * buf); // char, users buffer, or to decide + + /* + connect to a PLC. returns 0 on success. + */ + EXPORTSPEC int DECL2 daveConnectPLC(daveConnection * dc); + + /* + Read len bytes from the PLC. Start determines the first byte. + Area denotes whether the data comes from FLAGS, DATA BLOCKS, + INPUTS or OUTPUTS, etc. + DB is the number of the data block to be used. Set it to zero + for other area types. + Buffer is a pointer to a memory block provided by the calling + program. If the pointer is not NULL, the result data will be copied thereto. + Hence it must be big enough to take up the result. + In any case, you can also retrieve the result data using the get macros + on the connection pointer. + + RESTRICTION:There is no check for max. message len or automatic splitting into multiple messages. Use daveReadManyBytes() in case the data you want to read doesn't fit into a single PDU. - -*/ -EXPORTSPEC int DECL2 daveReadBytes(daveConnection * dc, int area, int DB, int start, int len, void * buffer); - -/* - Read len bytes from the PLC. Start determines the first byte. - In contrast to daveReadBytes(), this function can read blocks - that are too long for a single transaction. To achieve this, - the data is fetched with multiple subsequent read requests to - the CPU. - Area denotes whether the data comes from FLAGS, DATA BLOCKS, - INPUTS or OUTPUTS, etc. - DB is the number of the data block to be used. Set it to zero - for other area types. - Buffer is a pointer to a memory block provided by the calling - program. It may not be NULL, the result data will be copied thereto. - Hence it must be big enough to take up the result. - You CANNOT read result bytes from the internal buffer of the - daveConnection. This buffer is overwritten in each read request. -*/ -EXPORTSPEC int DECL2 daveReadManyBytes(daveConnection * dc,int area, int DBnum, int start,int len, void * buffer); - -/* - Write len bytes from buffer to the PLC. - Start determines the first byte. - Area denotes whether the data goes to FLAGS, DATA BLOCKS, - INPUTS or OUTPUTS, etc. - DB is the number of the data block to be used. Set it to zero - for other area types. - RESTRICTION: There is no check for max. message len or automatic splitting into - multiple messages. Use daveReadManyBytes() in case the data you want - to read doesn't fit into a single PDU. - -*/ -EXPORTSPEC int DECL2 daveWriteBytes(daveConnection * dc,int area, int DB, int start, int len, void * buffer); - -/* - Write len bytes to the PLC. Start determines the first byte. - In contrast to daveWriteBytes(), this function can write blocks - that are too long for a single transaction. To achieve this, the - the data is transported with multiple subsequent write requests to - the CPU. - Area denotes whether the data comes from FLAGS, DATA BLOCKS, - INPUTS or OUTPUTS, etc. - DB is the number of the data block to be used. Set it to zero - for other area types. - Buffer is a pointer to a memory block provided by the calling - program. It may not be NULL. -*/ -EXPORTSPEC int DECL2 daveWriteManyBytes(daveConnection * dc,int area, int DB, int start, int len, void * buffer); -/* - Bit manipulation: -*/ -EXPORTSPEC int DECL2 daveReadBits(daveConnection * dc, int area, int DB, int start, int len, void * buffer); -EXPORTSPEC int DECL2 daveWriteBits(daveConnection * dc,int area, int DB, int start, int len, void * buffer); -EXPORTSPEC int DECL2 daveSetBit(daveConnection * dc,int area, int DB, int byteAdr, int bitAdr); -EXPORTSPEC int DECL2 daveClrBit(daveConnection * dc,int area, int DB, int byteAdr, int bitAdr); - -/* - PLC diagnostic and inventory functions: -*/ -EXPORTSPEC int DECL2 daveBuildAndSendPDU(daveConnection * dc, PDU*p2,uc *pa,int psize, uc *ud,int usize); -EXPORTSPEC int DECL2 daveReadSZL(daveConnection * dc, int ID, int index, void * buf, int buflen); -EXPORTSPEC int DECL2 daveListBlocksOfType(daveConnection * dc,uc type,daveBlockEntry * buf); -EXPORTSPEC int DECL2 daveListBlocks(daveConnection * dc,daveBlockTypeEntry * buf); -EXPORTSPEC int DECL2 daveGetBlockInfo(daveConnection * dc, daveBlockInfo *dbi, uc type, int number); -/* - PLC program read functions: -*/ -EXPORTSPEC int DECL2 initUpload(daveConnection * dc, char blockType, int blockNr, int * uploadID); // char or uc,to decide -EXPORTSPEC int DECL2 doUpload(daveConnection*dc, int * more, uc**buffer, int*len, int uploadID); -EXPORTSPEC int DECL2 endUpload(daveConnection*dc, int uploadID); - -/* - NC file read functions: -*/ -EXPORTSPEC int DECL2 initUploadNC(daveConnection *dc, const char *filename, uc *uploadID); -EXPORTSPEC int DECL2 doUploadNC(daveConnection *dc, int *more, uc**buffer, int *len, uc *uploadID); -EXPORTSPEC int DECL2 doSingleUploadNC(daveConnection *dc, int *more, uc *buffer, int *len, uc *uploadID); -EXPORTSPEC int DECL2 endUploadNC(daveConnection *dc, uc *uploadID); - -/* - PLC run/stop control functions: -*/ -EXPORTSPEC int DECL2 daveStop(daveConnection*dc); -EXPORTSPEC int DECL2 daveStart(daveConnection*dc); -/* - PLC special commands -*/ -EXPORTSPEC int DECL2 daveCopyRAMtoROM(daveConnection*dc); + */ + EXPORTSPEC int DECL2 daveReadBytes(daveConnection * dc, int area, int DB, int start, int len, void * buffer); + + /* + Read len bytes from the PLC. Start determines the first byte. + In contrast to daveReadBytes(), this function can read blocks + that are too long for a single transaction. To achieve this, + the data is fetched with multiple subsequent read requests to + the CPU. + Area denotes whether the data comes from FLAGS, DATA BLOCKS, + INPUTS or OUTPUTS, etc. + DB is the number of the data block to be used. Set it to zero + for other area types. + Buffer is a pointer to a memory block provided by the calling + program. It may not be NULL, the result data will be copied thereto. + Hence it must be big enough to take up the result. + You CANNOT read result bytes from the internal buffer of the + daveConnection. This buffer is overwritten in each read request. + */ + EXPORTSPEC int DECL2 daveReadManyBytes(daveConnection * dc, int area, int DBnum, int start, int len, void * buffer); + + /* + Write len bytes from buffer to the PLC. + Start determines the first byte. + Area denotes whether the data goes to FLAGS, DATA BLOCKS, + INPUTS or OUTPUTS, etc. + DB is the number of the data block to be used. Set it to zero + for other area types. + RESTRICTION: There is no check for max. message len or automatic splitting into + multiple messages. Use daveReadManyBytes() in case the data you want + to read doesn't fit into a single PDU. -EXPORTSPEC int DECL2 daveForce200(daveConnection * dc, int area, int start, int val); -/* - Multiple variable support: -*/ -typedef struct { - int error; - int length; - uc * bytes; -} daveResult; - -typedef struct { - int numResults; - daveResult * results; -} daveResultSet; - - -/* use this to initialize a multivariable read: */ -EXPORTSPEC void DECL2 davePrepareReadRequest(daveConnection * dc, PDU *p); -/* Adds a new variable to a prepared request: */ -EXPORTSPEC void DECL2 daveAddVarToReadRequest(PDU *p, int area, int DBnum, int start, int bytes); -/* Adds a new symbol variable to a prepared request: */ -EXPORTSPEC void DECL2 daveAddSymbolVarToReadRequest(PDU *p, void * completeSymbol, int completeSymbolLength); -/* Adds a fill byte to the last request */ -EXPORTSPEC void DECL2 daveAddFillByteToReadRequest(PDU *p); -/* Adds a new symbol variable to a prepared request: */ -EXPORTSPEC void DECL2 daveAddDbRead400ToReadRequest(PDU *p, int DBnum, int offset, int byteCount); -/* Executes the complete request. */ -EXPORTSPEC int DECL2 daveExecReadRequest(daveConnection * dc, PDU *p, daveResultSet * rl); -/* Lets the functions daveGet work on the n-th result: */ -EXPORTSPEC int DECL2 daveUseResult(daveConnection * dc, daveResultSet * rl, int n, void * buffer); -/* Lets the Result get into the buffer */ -EXPORTSPEC int DECL2 daveUseResultBuffer(daveResultSet * rl, int n, void * buffer); -/* Frees the memory occupied by the result structure */ -EXPORTSPEC void DECL2 daveFreeResults(daveResultSet * rl); -/* Adds a new bit variable to a prepared request: */ -EXPORTSPEC void DECL2 daveAddBitVarToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount); -/* Adds a new NCK read Request... (see: http://www.sps-forum.de/hochsprachen-opc/80971-dotnetsiemensplctoolboxlibrary-libnodave-zugriff-auf-dual-port-ram-fb15-post611026.html#post611026) */ -EXPORTSPEC void DECL2 daveAddNCKToReadRequest(PDU *p, int area, int unit, int column, int line, int module, int linecount); -/* Adds a new NCK write Request... */ -EXPORTSPEC void DECL2 daveAddNCKToWriteRequest(PDU *p, int area, int unit, int column, int line, int module, int linecount, int byteCount, void * buffer); - -/* use this to initialize a NC PI-Service: (see: http://www.sps-forum.de/hochsprachen-opc/80971-dotnetsiemensplctoolboxlibrary-libnodave-zugriff-auf-dual-port-ram-fb15-12.html#post619571)*/ -EXPORTSPEC int DECL2 davePIstart_nc(daveConnection *dc, const char *piservice, const char *param[], int paramCount); - -/* use this to initialize a multivariable write: */ -EXPORTSPEC void DECL2 davePrepareWriteRequest(daveConnection * dc, PDU *p); -/* Add a preformed variable aderess to a read request: */ -EXPORTSPEC void DECL2 daveAddToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount, int isBit); -/* Adds a new variable to a prepared request: */ -EXPORTSPEC void DECL2 daveAddVarToWriteRequest(PDU *p, int area, int DBnum, int start, int bytes, void * buffer); -/* Adds a new bit variable to a prepared write request: */ -EXPORTSPEC void DECL2 daveAddBitVarToWriteRequest(PDU *p, int area, int DBnum, int start, int byteCount, void * buffer); -EXPORTSPEC int DECL2 _daveTestResultData(PDU * p); -EXPORTSPEC int DECL2 _daveTestReadResult(PDU * p); -EXPORTSPEC int DECL2 _daveTestPGReadResult(PDU * p); - -/* Executes the complete request. */ -EXPORTSPEC int DECL2 daveExecWriteRequest(daveConnection * dc, PDU *p, daveResultSet * rl); -EXPORTSPEC int DECL2 _daveTestWriteResult(PDU * p); - -EXPORTSPEC int DECL2 daveInitAdapter(daveInterface * di); -EXPORTSPEC int DECL2 daveConnectPLC(daveConnection * dc); -EXPORTSPEC int DECL2 daveDisconnectPLC(daveConnection * dc); - -EXPORTSPEC int DECL2 daveDisconnectAdapter(daveInterface * di); -EXPORTSPEC int DECL2 daveListReachablePartners(daveInterface * di, char * buf); - -EXPORTSPEC int DECL2 _daveReturnOkDummy(daveInterface * di); -EXPORTSPEC int DECL2 _daveReturnOkDummy2(daveConnection * dc); -EXPORTSPEC int DECL2 _daveListReachablePartnersDummy(daveInterface * di, char * buf); - -EXPORTSPEC int DECL2 _daveNegPDUlengthRequest(daveConnection * dc, PDU *p); - -/* MPI specific functions */ + */ + EXPORTSPEC int DECL2 daveWriteBytes(daveConnection * dc, int area, int DB, int start, int len, void * buffer); + + /* + Write len bytes to the PLC. Start determines the first byte. + In contrast to daveWriteBytes(), this function can write blocks + that are too long for a single transaction. To achieve this, the + the data is transported with multiple subsequent write requests to + the CPU. + Area denotes whether the data comes from FLAGS, DATA BLOCKS, + INPUTS or OUTPUTS, etc. + DB is the number of the data block to be used. Set it to zero + for other area types. + Buffer is a pointer to a memory block provided by the calling + program. It may not be NULL. + */ + EXPORTSPEC int DECL2 daveWriteManyBytes(daveConnection * dc, int area, int DB, int start, int len, void * buffer); + /* + Bit manipulation: + */ + EXPORTSPEC int DECL2 daveReadBits(daveConnection * dc, int area, int DB, int start, int len, void * buffer); + EXPORTSPEC int DECL2 daveWriteBits(daveConnection * dc, int area, int DB, int start, int len, void * buffer); + EXPORTSPEC int DECL2 daveSetBit(daveConnection * dc, int area, int DB, int byteAdr, int bitAdr); + EXPORTSPEC int DECL2 daveClrBit(daveConnection * dc, int area, int DB, int byteAdr, int bitAdr); + + /* + PLC diagnostic and inventory functions: + */ + EXPORTSPEC int DECL2 daveBuildAndSendPDU(daveConnection * dc, PDU*p2, uc *pa, int psize, uc *ud, int usize); + EXPORTSPEC int DECL2 daveReadSZL(daveConnection * dc, int ID, int index, void * buf, int buflen); + EXPORTSPEC int DECL2 daveListBlocksOfType(daveConnection * dc, uc type, daveBlockEntry * buf); + EXPORTSPEC int DECL2 daveListBlocks(daveConnection * dc, daveBlockTypeEntry * buf); + EXPORTSPEC int DECL2 daveGetBlockInfo(daveConnection * dc, daveBlockInfo *dbi, uc type, int number); + /* + PLC program read functions: + */ + EXPORTSPEC int DECL2 initUpload(daveConnection * dc, char blockType, int blockNr, int * uploadID); // char or uc,to decide + EXPORTSPEC int DECL2 doUpload(daveConnection*dc, int * more, uc**buffer, int*len, int uploadID); + EXPORTSPEC int DECL2 endUpload(daveConnection*dc, int uploadID); + + /* + NC file read functions: + */ + EXPORTSPEC int DECL2 initUploadNC(daveConnection *dc, const char *filename, uc *uploadID); + EXPORTSPEC int DECL2 doUploadNC(daveConnection *dc, int *more, uc**buffer, int *len, uc *uploadID); + EXPORTSPEC int DECL2 doSingleUploadNC(daveConnection *dc, int *more, uc *buffer, int *len, uc *uploadID); + EXPORTSPEC int DECL2 endUploadNC(daveConnection *dc, uc *uploadID); + + + /* + PLC run/stop control functions: + */ + EXPORTSPEC int DECL2 daveStop(daveConnection*dc); + EXPORTSPEC int DECL2 daveStart(daveConnection*dc); + /* + PLC special commands + */ + EXPORTSPEC int DECL2 daveCopyRAMtoROM(daveConnection*dc); + + EXPORTSPEC int DECL2 daveForce200(daveConnection * dc, int area, int start, int val); + /* + Multiple variable support: + */ + typedef struct { + int error; + int length; + uc * bytes; + } daveResult; + + typedef struct { + int numResults; + daveResult * results; + } daveResultSet; + + + /* use this to initialize a multivariable read: */ + EXPORTSPEC void DECL2 davePrepareReadRequest(daveConnection * dc, PDU *p); + /* Adds a new variable to a prepared request: */ + EXPORTSPEC void DECL2 daveAddVarToReadRequest(PDU *p, int area, int DBnum, int start, int bytes); + /* Adds a new symbol variable to a prepared request: */ + EXPORTSPEC void DECL2 daveAddSymbolVarToReadRequest(PDU *p, void * completeSymbol, int completeSymbolLength); + /* Adds a fill byte to the last request */ + EXPORTSPEC void DECL2 daveAddFillByteToReadRequest(PDU *p); + /* Adds a new symbol variable to a prepared request: */ + EXPORTSPEC void DECL2 daveAddDbRead400ToReadRequest(PDU *p, int DBnum, int offset, int byteCount); + /* Executes the complete request. */ + EXPORTSPEC int DECL2 daveExecReadRequest(daveConnection * dc, PDU *p, daveResultSet * rl); + /* Lets the functions daveGet work on the n-th result: */ + EXPORTSPEC int DECL2 daveUseResult(daveConnection * dc, daveResultSet * rl, int n, void * buffer); + /* Lets the Result get into the buffer */ + EXPORTSPEC int DECL2 daveUseResultBuffer(daveResultSet * rl, int n, void * buffer); + /* Frees the memory occupied by the result structure */ + EXPORTSPEC void DECL2 daveFreeResults(daveResultSet * rl); + /* Adds a new bit variable to a prepared request: */ + EXPORTSPEC void DECL2 daveAddBitVarToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount); + /* Adds a new NCK read Request... (see: http://www.sps-forum.de/hochsprachen-opc/80971-dotnetsiemensplctoolboxlibrary-libnodave-zugriff-auf-dual-port-ram-fb15-post611026.html#post611026) */ + EXPORTSPEC void DECL2 daveAddNCKToReadRequest(PDU *p, int area, int unit, int column, int line, int module, int linecount); + /* Adds a new NCK write Request... */ + EXPORTSPEC void DECL2 daveAddNCKToWriteRequest(PDU *p, int area, int unit, int column, int line, int module, int linecount, int byteCount, void * buffer); + + /* use this to initialize a NC PI-Service: (see: http://www.sps-forum.de/hochsprachen-opc/80971-dotnetsiemensplctoolboxlibrary-libnodave-zugriff-auf-dual-port-ram-fb15-12.html#post619571)*/ + EXPORTSPEC int DECL2 davePIstart_nc(daveConnection *dc, const char *piservice, const char *param[], int paramCount); + + /* use this to initialize a multivariable write: */ + EXPORTSPEC void DECL2 davePrepareWriteRequest(daveConnection * dc, PDU *p); + /* Add a preformed variable aderess to a read request: */ + EXPORTSPEC void DECL2 daveAddToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount, int isBit); + /* Adds a new variable to a prepared request: */ + EXPORTSPEC void DECL2 daveAddVarToWriteRequest(PDU *p, int area, int DBnum, int start, int bytes, void * buffer); + /* Adds a new bit variable to a prepared write request: */ + EXPORTSPEC void DECL2 daveAddBitVarToWriteRequest(PDU *p, int area, int DBnum, int start, int byteCount, void * buffer); + EXPORTSPEC int DECL2 _daveTestResultData(PDU * p); + EXPORTSPEC int DECL2 _daveTestReadResult(PDU * p); + EXPORTSPEC int DECL2 _daveTestPGReadResult(PDU * p); + + /* Executes the complete request. */ + EXPORTSPEC int DECL2 daveExecWriteRequest(daveConnection * dc, PDU *p, daveResultSet * rl); + EXPORTSPEC int DECL2 _daveTestWriteResult(PDU * p); + + EXPORTSPEC int DECL2 daveInitAdapter(daveInterface * di); + EXPORTSPEC int DECL2 daveConnectPLC(daveConnection * dc); + EXPORTSPEC int DECL2 daveDisconnectPLC(daveConnection * dc); + + EXPORTSPEC int DECL2 daveDisconnectAdapter(daveInterface * di); + EXPORTSPEC int DECL2 daveListReachablePartners(daveInterface * di, char * buf); + + EXPORTSPEC int DECL2 _daveReturnOkDummy(daveInterface * di); + EXPORTSPEC int DECL2 _daveReturnOkDummy2(daveConnection * dc); + EXPORTSPEC int DECL2 _daveListReachablePartnersDummy(daveInterface * di, char * buf); + + EXPORTSPEC int DECL2 _daveNegPDUlengthRequest(daveConnection * dc, PDU *p); + + /* MPI specific functions */ #define daveMPIReachable 0x30 #define daveMPIActive 0x30 @@ -969,196 +969,204 @@ EXPORTSPEC int DECL2 _daveNegPDUlengthRequest(daveConnection * dc, PDU *p); #define daveMPIunused 0x10 #define davePartnerListSize 126 -EXPORTSPEC int DECL2 _daveListReachablePartnersMPI(daveInterface * di, char * buf); -EXPORTSPEC int DECL2 _daveInitAdapterMPI1(daveInterface * di); -EXPORTSPEC int DECL2 _daveInitAdapterMPI2(daveInterface * di); -EXPORTSPEC int DECL2 _daveConnectPLCMPI1(daveConnection * dc); -EXPORTSPEC int DECL2 _daveConnectPLCMPI2(daveConnection * dc); -EXPORTSPEC int DECL2 _daveDisconnectPLCMPI(daveConnection * dc); -EXPORTSPEC int DECL2 _daveDisconnectAdapterMPI(daveInterface * di); -EXPORTSPEC int DECL2 _daveExchangeMPI(daveConnection * dc,PDU * p1); - -EXPORTSPEC int DECL2 _daveListReachablePartnersMPI3(daveInterface * di,char * buf); -EXPORTSPEC int DECL2 _daveInitAdapterMPI3(daveInterface * di); -EXPORTSPEC int DECL2 _daveConnectPLCMPI3(daveConnection * dc); -EXPORTSPEC int DECL2 _daveDisconnectPLCMPI3(daveConnection * dc); -EXPORTSPEC int DECL2 _daveDisconnectAdapterMPI3(daveInterface * di); -EXPORTSPEC int DECL2 _daveExchangeMPI3(daveConnection * dc,PDU * p1); -EXPORTSPEC int DECL2 _daveIncMessageNumber(daveConnection * dc); - -/* ISO over TCP specific functions */ -EXPORTSPEC int DECL2 _daveExchangeTCP(daveConnection * dc,PDU * p1); -EXPORTSPEC int DECL2 _daveConnectPLCTCP(daveConnection * dc); -/* - make internal PPI functions available for experimental use: -*/ -EXPORTSPEC int DECL2 _daveExchangePPI(daveConnection * dc,PDU * p1); -EXPORTSPEC void DECL2 _daveSendLength(daveInterface * di, int len); -EXPORTSPEC void DECL2 _daveSendRequestData(daveConnection * dc, int alt); -EXPORTSPEC void DECL2 _daveSendIt(daveInterface * di, uc * b, int size); -EXPORTSPEC int DECL2 _daveGetResponsePPI(daveConnection *dc); -EXPORTSPEC int DECL2 _daveReadChars(daveInterface * di, uc *b, tmotype tmo, int max); -EXPORTSPEC int DECL2 _daveReadChars2(daveInterface * di,uc *b, int max); -EXPORTSPEC int DECL2 _daveConnectPLCPPI(daveConnection * dc); - -/* - make internal MPI functions available for experimental use: -*/ -EXPORTSPEC int DECL2 _daveReadMPI(daveInterface * di, uc *b); -EXPORTSPEC void DECL2 _daveSendSingle(daveInterface * di, uc c); -EXPORTSPEC int DECL2 _daveSendAck(daveConnection * dc, int nr); -EXPORTSPEC int DECL2 _daveGetAck(daveConnection * dc); -EXPORTSPEC int DECL2 _daveSendDialog2(daveConnection * dc, int size); -EXPORTSPEC int DECL2 _daveSendWithPrefix(daveConnection * dc, uc * b, int size); -EXPORTSPEC int DECL2 _daveSendWithPrefix2(daveConnection * dc, int size); -EXPORTSPEC int DECL2 _daveSendWithCRC(daveInterface * di, uc *b, int size); -EXPORTSPEC int DECL2 _daveReadSingle(daveInterface * di); -EXPORTSPEC int DECL2 _daveReadOne(daveInterface * di, uc *b); -EXPORTSPEC int DECL2 _daveReadMPI2(daveInterface * di, uc *b); -EXPORTSPEC int DECL2 _daveGetResponseMPI(daveConnection *dc); -EXPORTSPEC int DECL2 _daveSendMessageMPI(daveConnection * dc, PDU * p); - -/* - make internal ISO_TCP functions available for experimental use: -*/ -/* - Read one complete packet. The bytes 3 and 4 contain length information. -*/ -EXPORTSPEC int DECL2 _daveReadISOPacket(daveInterface * di,uc *b); -EXPORTSPEC int DECL2 _daveGetResponseISO_TCP(daveConnection *dc); - -/* Sendet eine PDU, ohne auf Antwort zu warten */ -EXPORTSPEC int DECL2 _daveSendTCP(daveConnection *dc, PDU *p); - - -typedef uc * (*userReadFunc) (int , int, int, int, int *); -typedef void (*userWriteFunc) (int , int, int, int, int *,uc *); -extern userReadFunc readCallBack; -extern userWriteFunc writeCallBack; - -EXPORTSPEC void DECL2 _daveConstructReadResponse(PDU * p); -EXPORTSPEC void DECL2 _daveConstructWriteResponse(PDU * p); -EXPORTSPEC void DECL2 _daveConstructBadReadResponse(PDU * p); -EXPORTSPEC void DECL2 _daveHandleRead(PDU * p1,PDU * p2); -EXPORTSPEC void DECL2 _daveHandleWrite(PDU * p1,PDU * p2); -/* - make internal IBH functions available for experimental use: -*/ -EXPORTSPEC int DECL2 _daveReadIBHPacket(daveInterface * di,uc *b); -EXPORTSPEC int DECL2 _daveWriteIBH(daveInterface * di, uc * buffer, int len); -EXPORTSPEC int DECL2 _davePackPDU(daveConnection * dc,PDU *p); -EXPORTSPEC void DECL2 _daveSendMPIAck_IBH(daveConnection*dc); -EXPORTSPEC void DECL2 _daveSendIBHNetAck(daveConnection * dc); -EXPORTSPEC int DECL2 __daveAnalyze(daveConnection * dc); -EXPORTSPEC int DECL2 _daveExchangeIBH(daveConnection * dc, PDU * p); -EXPORTSPEC int DECL2 _daveSendMessageMPI_IBH(daveConnection * dc, PDU * p); -EXPORTSPEC int DECL2 _daveGetResponseMPI_IBH(daveConnection *dc); -EXPORTSPEC int DECL2 _daveInitStepIBH(daveInterface * iface, uc * chal, int cl, us* resp,int rl, uc*b); - -EXPORTSPEC int DECL2 _daveConnectPLC_IBH(daveConnection*dc); -EXPORTSPEC int DECL2 _daveDisconnectPLC_IBH(daveConnection*dc); -EXPORTSPEC void DECL2 _daveSendMPIAck2(daveConnection *dc); -EXPORTSPEC int DECL2 _davePackPDU_PPI(daveConnection * dc,PDU *p); -EXPORTSPEC void DECL2 _daveSendIBHNetAckPPI(daveConnection * dc); -EXPORTSPEC int DECL2 _daveListReachablePartnersMPI_IBH(daveInterface *di, char * buf); -EXPORTSPEC int DECL2 __daveAnalyzePPI(daveConnection * dc, uc sa); -EXPORTSPEC int DECL2 _daveExchangePPI_IBH(daveConnection * dc, PDU * p); -EXPORTSPEC int DECL2 _daveGetResponsePPI_IBH(daveConnection *dc); - -/** - C# interoperability: -**/ -EXPORTSPEC void DECL2 daveSetTimeout(daveInterface * di, int tmo); -EXPORTSPEC int DECL2 daveGetTimeout(daveInterface * di); -EXPORTSPEC char * DECL2 daveGetName(daveInterface * di); - -EXPORTSPEC int DECL2 daveGetMPIAdr(daveConnection * dc); -EXPORTSPEC int DECL2 daveGetAnswLen(daveConnection * dc); -EXPORTSPEC int DECL2 daveGetMaxPDULen(daveConnection * dc); -EXPORTSPEC daveResultSet * DECL2 daveNewResultSet(); -EXPORTSPEC void DECL2 daveFree(void * dc); -EXPORTSPEC PDU * DECL2 daveNewPDU(); -EXPORTSPEC int DECL2 daveGetErrorOfResult(daveResultSet *,int number); - -/* - Special function do disconnect arbitrary connections on IBH-Link: -*/ -EXPORTSPEC int DECL2 daveForceDisconnectIBH(daveInterface * di, int src, int dest, int mpi); -/* - Special function do reset an IBH-Link: -*/ -EXPORTSPEC int DECL2 daveResetIBH(daveInterface * di); -/* - Program Block from PLC: -*/ -EXPORTSPEC int DECL2 daveGetProgramBlock(daveConnection * dc, int blockType, int number, char* buffer, int * length); -/* - Program Block to PLC: -*/ -EXPORTSPEC int DECL2 davePutProgramBlock(daveConnection * dc, int blockType, int number, char* buffer, int length); -/* - Delete Block from PLC: -*/ -EXPORTSPEC int DECL2 daveDeleteProgramBlock(daveConnection*dc, int blockType, int number); - -/* - Send Receive NC Program: -*/ -EXPORTSPEC int DECL2 daveGetNCProgram(daveConnection *dc, const char *filename, uc *buffer, int *length); - -//DateTime ts Format: yyMMddHHmmss -EXPORTSPEC int DECL2 davePutNCProgram(daveConnection *dc, char *filename, char *pathname, char *ts, char *buffer, int length); - - -/* - PLC realtime clock handling: -*/ -/* - read out clock: -*/ -EXPORTSPEC int DECL2 daveReadPLCTime(daveConnection * dc); -/* - set clock to a value given by user: -*/ -EXPORTSPEC int DECL2 daveSetPLCTime(daveConnection * dc,uc * ts); -/* - set clock to PC system clock: -*/ -EXPORTSPEC int DECL2 daveSetPLCTimeToSystime(daveConnection * dc); -/* - BCD conversions: -*/ -EXPORTSPEC uc DECL2 daveToBCD(uc i); -EXPORTSPEC uc DECL2 daveFromBCD(uc i); -/* - S5: -*/ -EXPORTSPEC int DECL2 _daveFakeExchangeAS511(daveConnection * dc, PDU * p); -EXPORTSPEC int DECL2 _daveExchangeAS511(daveConnection * dc, uc * b,int len,int maxLen, int trN); -EXPORTSPEC int DECL2 _daveSendMessageAS511(daveConnection * dc, PDU * p); -EXPORTSPEC int DECL2 _daveConnectPLCAS511(daveConnection * dc); -EXPORTSPEC int DECL2 _daveDisconnectPLCAS511(daveConnection * dc); -EXPORTSPEC int DECL2 _daveIsS5BlockArea(uc area); -EXPORTSPEC int DECL2 _daveReadS5BlockAddress(daveConnection * dc, uc area, uc BlockN, daveS5AreaInfo * ai); -EXPORTSPEC int DECL2 _daveReadS5ImageAddress(daveConnection * dc, uc area, daveS5AreaInfo * ai); -EXPORTSPEC int DECL2 _daveIsS5ImageArea(uc area); -EXPORTSPEC int DECL2 _daveIsS5DBBlockArea(uc area); -EXPORTSPEC int DECL2 daveReadS5Bytes(daveConnection * dc, uc area, uc BlockN, int offset, int count); -EXPORTSPEC int DECL2 daveWriteS5Bytes(daveConnection * dc, uc area, uc BlockN, int offset, int count, void * buf); -EXPORTSPEC int DECL2 daveStopS5(daveConnection * dc); -EXPORTSPEC int DECL2 daveStartS5(daveConnection * dc); -EXPORTSPEC int DECL2 daveGetS5ProgramBlock(daveConnection * dc, int blockType, int number, char* buffer, int * length); - -/* - MPI version 3: -*/ -EXPORTSPEC int DECL2 _daveSendWithPrefix31(daveConnection * dc, uc *b, int size); -EXPORTSPEC int DECL2 _daveGetResponseMPI3(daveConnection *dc); -EXPORTSPEC int DECL2 _daveSendMessageMPI3(daveConnection * dc, PDU * p); - -/* - using S7 dlls for transporrt: -*/ + EXPORTSPEC int DECL2 _daveListReachablePartnersMPI(daveInterface * di, char * buf); + EXPORTSPEC int DECL2 _daveInitAdapterMPI1(daveInterface * di); + EXPORTSPEC int DECL2 _daveInitAdapterMPI2(daveInterface * di); + EXPORTSPEC int DECL2 _daveConnectPLCMPI1(daveConnection * dc); + EXPORTSPEC int DECL2 _daveConnectPLCMPI2(daveConnection * dc); + EXPORTSPEC int DECL2 _daveDisconnectPLCMPI(daveConnection * dc); + EXPORTSPEC int DECL2 _daveDisconnectAdapterMPI(daveInterface * di); + EXPORTSPEC int DECL2 _daveExchangeMPI(daveConnection * dc, PDU * p1); + + EXPORTSPEC int DECL2 _daveListReachablePartnersMPI3(daveInterface * di, char * buf); + EXPORTSPEC int DECL2 _daveInitAdapterMPI3(daveInterface * di); + EXPORTSPEC int DECL2 _daveConnectPLCMPI3(daveConnection * dc); + EXPORTSPEC int DECL2 _daveDisconnectPLCMPI3(daveConnection * dc); + EXPORTSPEC int DECL2 _daveDisconnectAdapterMPI3(daveInterface * di); + EXPORTSPEC int DECL2 _daveExchangeMPI3(daveConnection * dc, PDU * p1); + EXPORTSPEC int DECL2 _daveIncMessageNumber(daveConnection * dc); + + /* ISO over TCP specific functions */ + EXPORTSPEC int DECL2 _daveExchangeTCP(daveConnection * dc, PDU * p1); + EXPORTSPEC int DECL2 _daveConnectPLCTCP(daveConnection * dc); + /* + make internal PPI functions available for experimental use: + */ + EXPORTSPEC int DECL2 _daveExchangePPI(daveConnection * dc, PDU * p1); + EXPORTSPEC void DECL2 _daveSendLength(daveInterface * di, int len); + EXPORTSPEC void DECL2 _daveSendRequestData(daveConnection * dc, int alt); + EXPORTSPEC void DECL2 _daveSendIt(daveInterface * di, uc * b, int size); + EXPORTSPEC int DECL2 _daveGetResponsePPI(daveConnection *dc); + EXPORTSPEC int DECL2 _daveReadChars(daveInterface * di, uc *b, tmotype tmo, int max); + EXPORTSPEC int DECL2 _daveReadChars2(daveInterface * di, uc *b, int max); + EXPORTSPEC int DECL2 _daveConnectPLCPPI(daveConnection * dc); + + /* + make internal MPI functions available for experimental use: + */ + EXPORTSPEC int DECL2 _daveReadMPI(daveInterface * di, uc *b); + EXPORTSPEC void DECL2 _daveSendSingle(daveInterface * di, uc c); + EXPORTSPEC int DECL2 _daveSendAck(daveConnection * dc, int nr); + EXPORTSPEC int DECL2 _daveGetAck(daveConnection * dc); + EXPORTSPEC int DECL2 _daveSendDialog2(daveConnection * dc, int size); + EXPORTSPEC int DECL2 _daveSendWithPrefix(daveConnection * dc, uc * b, int size); + EXPORTSPEC int DECL2 _daveSendWithPrefix2(daveConnection * dc, int size); + EXPORTSPEC int DECL2 _daveSendWithCRC(daveInterface * di, uc *b, int size); + EXPORTSPEC int DECL2 _daveReadSingle(daveInterface * di); + EXPORTSPEC int DECL2 _daveReadOne(daveInterface * di, uc *b); + EXPORTSPEC int DECL2 _daveReadMPI2(daveInterface * di, uc *b); + EXPORTSPEC int DECL2 _daveGetResponseMPI(daveConnection *dc); + EXPORTSPEC int DECL2 _daveSendMessageMPI(daveConnection * dc, PDU * p); + + /* + make internal ISO_TCP functions available for experimental use: + */ + /* + Read one complete packet. The bytes 3 and 4 contain length information. + */ + EXPORTSPEC int DECL2 _daveReadISOPacket(daveInterface * di, uc *b); + EXPORTSPEC int DECL2 _daveGetResponseISO_TCP(daveConnection *dc); + + /* Sendet eine PDU, ohne auf Antwort zu warten */ + EXPORTSPEC int DECL2 _daveSendTCP(daveConnection *dc, PDU *p); + + + typedef uc * (*userReadFunc) (int, int, int, int, int *); + typedef void(*userWriteFunc) (int, int, int, int, int *, uc *); + extern userReadFunc readCallBack; + extern userWriteFunc writeCallBack; + + EXPORTSPEC void DECL2 _daveConstructReadResponse(PDU * p); + EXPORTSPEC void DECL2 _daveConstructWriteResponse(PDU * p); + EXPORTSPEC void DECL2 _daveConstructBadReadResponse(PDU * p); + EXPORTSPEC void DECL2 _daveHandleRead(PDU * p1, PDU * p2); + EXPORTSPEC void DECL2 _daveHandleWrite(PDU * p1, PDU * p2); + /* + make internal IBH functions available for experimental use: + */ + EXPORTSPEC int DECL2 _daveReadIBHPacket(daveInterface * di, uc *b); + EXPORTSPEC int DECL2 _daveWriteIBH(daveInterface * di, uc * buffer, int len); + EXPORTSPEC int DECL2 _davePackPDU(daveConnection * dc, PDU *p); + EXPORTSPEC void DECL2 _daveSendMPIAck_IBH(daveConnection*dc); + EXPORTSPEC void DECL2 _daveSendIBHNetAck(daveConnection * dc); + EXPORTSPEC int DECL2 __daveAnalyze(daveConnection * dc); + EXPORTSPEC int DECL2 _daveExchangeIBH(daveConnection * dc, PDU * p); + EXPORTSPEC int DECL2 _daveSendMessageMPI_IBH(daveConnection * dc, PDU * p); + EXPORTSPEC int DECL2 _daveGetResponseMPI_IBH(daveConnection *dc); + EXPORTSPEC int DECL2 _daveInitStepIBH(daveInterface * iface, uc * chal, int cl, us* resp, int rl, uc*b); + + EXPORTSPEC int DECL2 _daveConnectPLC_IBH(daveConnection*dc); + EXPORTSPEC int DECL2 _daveDisconnectPLC_IBH(daveConnection*dc); + EXPORTSPEC void DECL2 _daveSendMPIAck2(daveConnection *dc); + EXPORTSPEC int DECL2 _davePackPDU_PPI(daveConnection * dc, PDU *p); + EXPORTSPEC void DECL2 _daveSendIBHNetAckPPI(daveConnection * dc); + EXPORTSPEC int DECL2 _daveListReachablePartnersMPI_IBH(daveInterface *di, char * buf); + EXPORTSPEC int DECL2 __daveAnalyzePPI(daveConnection * dc, uc sa); + EXPORTSPEC int DECL2 _daveExchangePPI_IBH(daveConnection * dc, PDU * p); + EXPORTSPEC int DECL2 _daveGetResponsePPI_IBH(daveConnection *dc); + + /** + C# interoperability: + **/ + EXPORTSPEC void DECL2 daveSetTimeout(daveInterface * di, int tmo); + EXPORTSPEC int DECL2 daveGetTimeout(daveInterface * di); + EXPORTSPEC char * DECL2 daveGetName(daveInterface * di); + + EXPORTSPEC int DECL2 daveGetMPIAdr(daveConnection * dc); + EXPORTSPEC int DECL2 daveGetAnswLen(daveConnection * dc); + EXPORTSPEC int DECL2 daveGetMaxPDULen(daveConnection * dc); + EXPORTSPEC daveResultSet * DECL2 daveNewResultSet(); + EXPORTSPEC void DECL2 daveFree(void * dc); + EXPORTSPEC PDU * DECL2 daveNewPDU(); + EXPORTSPEC int DECL2 daveGetErrorOfResult(daveResultSet *, int number); + + /* + Special function do disconnect arbitrary connections on IBH-Link: + */ + EXPORTSPEC int DECL2 daveForceDisconnectIBH(daveInterface * di, int src, int dest, int mpi); + /* + Special function do reset an IBH-Link: + */ + EXPORTSPEC int DECL2 daveResetIBH(daveInterface * di); + /* + Program Block from PLC: + */ + EXPORTSPEC int DECL2 daveGetProgramBlock(daveConnection * dc, int blockType, int number, char* buffer, int * length); + /* + Program Block to PLC: + */ + EXPORTSPEC int DECL2 davePutProgramBlock(daveConnection * dc, int blockType, int number, char* buffer, int length); + /* + Delete Block from PLC: + */ + EXPORTSPEC int DECL2 daveDeleteProgramBlock(daveConnection*dc, int blockType, int number); + + /* + Send Receive NC Program: + */ + EXPORTSPEC int DECL2 daveGetNCProgram(daveConnection *dc, const char *filename, uc *buffer, int *length); + + EXPORTSPEC int DECL2 daveGetNcFile(daveConnection *dc, const char *filename, char *buffer, int *length); + + EXPORTSPEC int DECL2 daveGetNcFileSize(daveConnection *dc, const char *filename, int *length); + + //DateTime ts Format: yyMMddHHmmss + EXPORTSPEC int DECL2 davePutNCProgram(daveConnection *dc, char *filename, char *pathname, char *ts, char *buffer, int length); + + /* + Receive Alarm query: + */ + EXPORTSPEC int DECL2 alarmQueryAlarm_S(daveConnection *dc, void *buffer, int buflen, int *number_of_alarms); + + /* + PLC realtime clock handling: + */ + /* + read out clock: + */ + EXPORTSPEC int DECL2 daveReadPLCTime(daveConnection * dc); + /* + set clock to a value given by user: + */ + EXPORTSPEC int DECL2 daveSetPLCTime(daveConnection * dc, uc * ts); + /* + set clock to PC system clock: + */ + EXPORTSPEC int DECL2 daveSetPLCTimeToSystime(daveConnection * dc); + /* + BCD conversions: + */ + EXPORTSPEC uc DECL2 daveToBCD(uc i); + EXPORTSPEC uc DECL2 daveFromBCD(uc i); + /* + S5: + */ + EXPORTSPEC int DECL2 _daveFakeExchangeAS511(daveConnection * dc, PDU * p); + EXPORTSPEC int DECL2 _daveExchangeAS511(daveConnection * dc, uc * b, int len, int maxLen, int trN); + EXPORTSPEC int DECL2 _daveSendMessageAS511(daveConnection * dc, PDU * p); + EXPORTSPEC int DECL2 _daveConnectPLCAS511(daveConnection * dc); + EXPORTSPEC int DECL2 _daveDisconnectPLCAS511(daveConnection * dc); + EXPORTSPEC int DECL2 _daveIsS5BlockArea(uc area); + EXPORTSPEC int DECL2 _daveReadS5BlockAddress(daveConnection * dc, uc area, uc BlockN, daveS5AreaInfo * ai); + EXPORTSPEC int DECL2 _daveReadS5ImageAddress(daveConnection * dc, uc area, daveS5AreaInfo * ai); + EXPORTSPEC int DECL2 _daveIsS5ImageArea(uc area); + EXPORTSPEC int DECL2 _daveIsS5DBBlockArea(uc area); + EXPORTSPEC int DECL2 daveReadS5Bytes(daveConnection * dc, uc area, uc BlockN, int offset, int count); + EXPORTSPEC int DECL2 daveWriteS5Bytes(daveConnection * dc, uc area, uc BlockN, int offset, int count, void * buf); + EXPORTSPEC int DECL2 daveStopS5(daveConnection * dc); + EXPORTSPEC int DECL2 daveStartS5(daveConnection * dc); + EXPORTSPEC int DECL2 daveGetS5ProgramBlock(daveConnection * dc, int blockType, int number, char* buffer, int * length); + + /* + MPI version 3: + */ + EXPORTSPEC int DECL2 _daveSendWithPrefix31(daveConnection * dc, uc *b, int size); + EXPORTSPEC int DECL2 _daveGetResponseMPI3(daveConnection *dc); + EXPORTSPEC int DECL2 _daveSendMessageMPI3(daveConnection * dc, PDU * p); + + /* + using S7 dlls for transporrt: + */ #pragma pack(1) @@ -1166,189 +1174,189 @@ EXPORTSPEC int DECL2 _daveSendMessageMPI3(daveConnection * dc, PDU * p); #define HANDLE int #endif -/* - Prototypes for the functions in S7onlinx.dll: - They are guessed. -*/ -typedef int (DECL2 * _openFunc) (const uc *); -typedef int (DECL2 * _closeFunc) (int); -typedef int (DECL2 * _sendFunc) (int, us, uc *); -typedef int (DECL2 * _receiveFunc) (int, us, int *, us, uc *); -//typedef int (DECL2 * _SetHWndMsgFunc) (int, int, ULONG); -//typedef int (DECL2 * _SetHWndFunc) (int, HANDLE); -typedef int (DECL2 * _get_errnoFunc) (void); - -/* - And pointers to the functions. We load them using GetProcAddress() on their names because: - 1. We have no .lib file for s7onlinx. - 2. We don't want to link with a particular version. - 3. Libnodave shall remain useable without Siemens .dlls. So it shall not try to access them - unless the user chooses the daveProtoS7online transport. -*/ -extern _openFunc SCP_open; -extern _closeFunc SCP_close; -extern _sendFunc SCP_send; -extern _receiveFunc SCP_receive; -//_SetHWndMsgFunc SetSinecHWndMsg; -//_SetHWndFunc SetSinecHWnd; -extern _get_errnoFunc SCP_get_errno; -/* - A block of data exchanged between S7onlinx.dll and a program using it. Most fields seem to - be allways 0. Meaningful names are guessed. -*/ - -typedef struct { - //Header - us unknown [2]; - uc headerlength; //Length of the Request Block without Userdata_1 and 2 (80 Bytes!) - us user; //Application Specific - uc rb_type; //Request Block type (always 2) - uc priority; //Priority of the Task, identical like serv_class in the application block - uc reserved_1; - us reserved_2; - uc subsystem; //For FDL Communication this is 22h = 34 - uc opcode; //request, confirm, indication => same as opcode in application block - us response; //return-parameter => same as l_status in application block - us fill_length_1; - uc reserved_3; - us seg_length_1; //Lengthz of Userdata_1 - us offset_1; - us reserved_4; - us fill_length_2; - uc reserved_5; - us seg_length_2; - us offset_2; - us reserved_6; - //End of Header - - //Application Block - uc application_block_opcode; // class of communication (00 = request, 01=confirm, 02=indication) - uc application_block_subsystem; // number of source-task (only necessary for MTK-user !!!!!) - us application_block_id; // identification of FDL-USER - us application_block_service; // identification of service (00 -> SDA, send data with acknowlege) - uc application_block_local_address_station; // only for network-connection !!! - uc application_block_local_address_segment; // only for network-connection !!! - uc application_block_ssap; // source-service-access-point - uc application_block_dsap; // destination-service-access-point - uc application_block_remote_address_station; // address of the remote-station - uc application_block_remote_address_segment; // only for network-connection !!! - us application_block_service_class; // priority of service - void* application_block_receive_l_sdu_buffer_ptr; // address and length of received netto-data, exception: - uc application_block_receive_l_sdu_length; // address and length of received netto-data, exception: - uc application_block_reserved_1; // (reserved for FDL !!!!!!!!!!) - uc application_block_reserved; // (reserved for FDL !!!!!!!!!!) - void* application_block_send_l_sdu_buffer_ptr; // address and length of send-netto-data, exception: - uc application_block_send_l_sdu_length; // address and length of send-netto-data, exception: - // 1. csrd : length means number of POLL-elements - // 2. await_indication : concatenation of application-blocks and - // withdraw_indication : number of application-blocks - us application_block_l_status; // link-status of service or update_state for srd-indication - us application_block_reserved_2[2]; // for concatenated lists (reserved for FDL !!!!!!!!!!) - //End Application block - - uc reserveed [12]; - uc reference[2]; - - uc user_data_1[260]; - uc user_data_2[260]; - -} S7OexchangeBlock; - -EXPORTSPEC int DECL2 _daveCP_send(int fd, int len, uc * reqBlock); -EXPORTSPEC int DECL2 _daveSCP_send(int fd, uc * reqBlock); -EXPORTSPEC int DECL2 _daveConnectPLCS7online (daveConnection * dc); -EXPORTSPEC int DECL2 _daveSendMessageS7online(daveConnection * dc, PDU *p); -EXPORTSPEC int DECL2 _daveGetResponseS7online(daveConnection * dc); -EXPORTSPEC int DECL2 _daveExchangeS7online(daveConnection * dc, PDU * p); -EXPORTSPEC int DECL2 _daveListReachablePartnersS7online (daveInterface * di, char * buf); -EXPORTSPEC int DECL2 _daveDisconnectAdapterS7online(daveInterface * di); -EXPORTSPEC int DECL2 _daveDisconnectPLCS7online(daveConnection*dc); - -EXPORTSPEC int DECL2 stdwrite(daveInterface * di, char * buffer, int length); -EXPORTSPEC int DECL2 stdread(daveInterface * di, char * buffer, int length); - -EXPORTSPEC int DECL2 _daveInitAdapterNLPro(daveInterface * di); -EXPORTSPEC int DECL2 _daveInitStepNLPro(daveInterface * iface, int nr, uc* fix, int len, char*caller, uc * buffer); -EXPORTSPEC int DECL2 _daveConnectPLCNLPro (daveConnection * dc); -EXPORTSPEC int DECL2 _daveSendMessageNLPro(daveConnection *dc, PDU *p); -EXPORTSPEC int DECL2 _daveGetResponseNLPro(daveConnection *dc); -EXPORTSPEC int DECL2 _daveExchangeNLPro(daveConnection * dc, PDU * p); -EXPORTSPEC int DECL2 _daveSendDialogNLPro(daveConnection * dc, int size); -EXPORTSPEC int DECL2 _daveSendWithPrefixNLPro(daveConnection * dc, uc * b,int size); -EXPORTSPEC int DECL2 _daveSendWithPrefix2NLPro(daveConnection * dc, int size); -EXPORTSPEC int DECL2 _daveDisconnectPLCNLPro(daveConnection * dc); -EXPORTSPEC int DECL2 _daveDisconnectAdapterNLPro(daveInterface * di); -EXPORTSPEC int DECL2 _daveListReachablePartnersNLPro(daveInterface * di, char * buf); - -/* -EXPORTSPEC int DECL2 _daveConnectPLCNLProFamily(daveConnection * dc); -EXPORTSPEC int DECL2 _daveExchangeNLProFamily(daveConnection * dc, PDU * p); -EXPORTSPEC int DECL2 _daveGetResponseNLProFamily_TCP(daveConnection * dc); -EXPORTSPEC int DECL2 _daveReadNLProFamilyPacket(daveInterface * di,uc *b); -EXPORTSPEC int DECL2 _daveSendNLProFamilyPacket(daveConnection * dc, int size); -EXPORTSPEC int DECL2 _daveInitAdapterNLProFamily(daveInterface * di); -EXPORTSPEC int DECL2 _daveDisconnectPLCNLProFamily(daveConnection * dc); -EXPORTSPEC int DECL2 _daveDisconnectAdapterNLProFamily(daveInterface * di); -EXPORTSPEC int DECL2 _daveListReachablePartnersNLProFamily(daveInterface * di, char * buf); -*/ + /* + Prototypes for the functions in S7onlinx.dll: + They are guessed. + */ + typedef int (DECL2 * _openFunc) (const uc *); + typedef int (DECL2 * _closeFunc) (int); + typedef int (DECL2 * _sendFunc) (int, us, uc *); + typedef int (DECL2 * _receiveFunc) (int, us, int *, us, uc *); + //typedef int (DECL2 * _SetHWndMsgFunc) (int, int, ULONG); + //typedef int (DECL2 * _SetHWndFunc) (int, HANDLE); + typedef int (DECL2 * _get_errnoFunc) (void); + + /* + And pointers to the functions. We load them using GetProcAddress() on their names because: + 1. We have no .lib file for s7onlinx. + 2. We don't want to link with a particular version. + 3. Libnodave shall remain useable without Siemens .dlls. So it shall not try to access them + unless the user chooses the daveProtoS7online transport. + */ + extern _openFunc SCP_open; + extern _closeFunc SCP_close; + extern _sendFunc SCP_send; + extern _receiveFunc SCP_receive; + //_SetHWndMsgFunc SetSinecHWndMsg; + //_SetHWndFunc SetSinecHWnd; + extern _get_errnoFunc SCP_get_errno; + /* + A block of data exchanged between S7onlinx.dll and a program using it. Most fields seem to + be allways 0. Meaningful names are guessed. + */ + + typedef struct { + //Header + us unknown[2]; + uc headerlength; //Length of the Request Block without Userdata_1 and 2 (80 Bytes!) + us user; //Application Specific + uc rb_type; //Request Block type (always 2) + uc priority; //Priority of the Task, identical like serv_class in the application block + uc reserved_1; + us reserved_2; + uc subsystem; //For FDL Communication this is 22h = 34 + uc opcode; //request, confirm, indication => same as opcode in application block + us response; //return-parameter => same as l_status in application block + us fill_length_1; + uc reserved_3; + us seg_length_1; //Lengthz of Userdata_1 + us offset_1; + us reserved_4; + us fill_length_2; + uc reserved_5; + us seg_length_2; + us offset_2; + us reserved_6; + //End of Header + + //Application Block + uc application_block_opcode; // class of communication (00 = request, 01=confirm, 02=indication) + uc application_block_subsystem; // number of source-task (only necessary for MTK-user !!!!!) + us application_block_id; // identification of FDL-USER + us application_block_service; // identification of service (00 -> SDA, send data with acknowlege) + uc application_block_local_address_station; // only for network-connection !!! + uc application_block_local_address_segment; // only for network-connection !!! + uc application_block_ssap; // source-service-access-point + uc application_block_dsap; // destination-service-access-point + uc application_block_remote_address_station; // address of the remote-station + uc application_block_remote_address_segment; // only for network-connection !!! + us application_block_service_class; // priority of service + void* application_block_receive_l_sdu_buffer_ptr; // address and length of received netto-data, exception: + uc application_block_receive_l_sdu_length; // address and length of received netto-data, exception: + uc application_block_reserved_1; // (reserved for FDL !!!!!!!!!!) + uc application_block_reserved; // (reserved for FDL !!!!!!!!!!) + void* application_block_send_l_sdu_buffer_ptr; // address and length of send-netto-data, exception: + uc application_block_send_l_sdu_length; // address and length of send-netto-data, exception: + // 1. csrd : length means number of POLL-elements + // 2. await_indication : concatenation of application-blocks and + // withdraw_indication : number of application-blocks + us application_block_l_status; // link-status of service or update_state for srd-indication + us application_block_reserved_2[2]; // for concatenated lists (reserved for FDL !!!!!!!!!!) + //End Application block + + uc reserveed[12]; + uc reference[2]; + + uc user_data_1[260]; + uc user_data_2[260]; + + } S7OexchangeBlock; + + EXPORTSPEC int DECL2 _daveCP_send(int fd, int len, uc * reqBlock); + EXPORTSPEC int DECL2 _daveSCP_send(int fd, uc * reqBlock); + EXPORTSPEC int DECL2 _daveConnectPLCS7online(daveConnection * dc); + EXPORTSPEC int DECL2 _daveSendMessageS7online(daveConnection * dc, PDU *p); + EXPORTSPEC int DECL2 _daveGetResponseS7online(daveConnection * dc); + EXPORTSPEC int DECL2 _daveExchangeS7online(daveConnection * dc, PDU * p); + EXPORTSPEC int DECL2 _daveListReachablePartnersS7online(daveInterface * di, char * buf); + EXPORTSPEC int DECL2 _daveDisconnectAdapterS7online(daveInterface * di); + EXPORTSPEC int DECL2 _daveDisconnectPLCS7online(daveConnection*dc); + + EXPORTSPEC int DECL2 stdwrite(daveInterface * di, char * buffer, int length); + EXPORTSPEC int DECL2 stdread(daveInterface * di, char * buffer, int length); + + EXPORTSPEC int DECL2 _daveInitAdapterNLPro(daveInterface * di); + EXPORTSPEC int DECL2 _daveInitStepNLPro(daveInterface * iface, int nr, uc* fix, int len, char*caller, uc * buffer); + EXPORTSPEC int DECL2 _daveConnectPLCNLPro(daveConnection * dc); + EXPORTSPEC int DECL2 _daveSendMessageNLPro(daveConnection *dc, PDU *p); + EXPORTSPEC int DECL2 _daveGetResponseNLPro(daveConnection *dc); + EXPORTSPEC int DECL2 _daveExchangeNLPro(daveConnection * dc, PDU * p); + EXPORTSPEC int DECL2 _daveSendDialogNLPro(daveConnection * dc, int size); + EXPORTSPEC int DECL2 _daveSendWithPrefixNLPro(daveConnection * dc, uc * b, int size); + EXPORTSPEC int DECL2 _daveSendWithPrefix2NLPro(daveConnection * dc, int size); + EXPORTSPEC int DECL2 _daveDisconnectPLCNLPro(daveConnection * dc); + EXPORTSPEC int DECL2 _daveDisconnectAdapterNLPro(daveInterface * di); + EXPORTSPEC int DECL2 _daveListReachablePartnersNLPro(daveInterface * di, char * buf); + + /* + EXPORTSPEC int DECL2 _daveConnectPLCNLProFamily(daveConnection * dc); + EXPORTSPEC int DECL2 _daveExchangeNLProFamily(daveConnection * dc, PDU * p); + EXPORTSPEC int DECL2 _daveGetResponseNLProFamily_TCP(daveConnection * dc); + EXPORTSPEC int DECL2 _daveReadNLProFamilyPacket(daveInterface * di,uc *b); + EXPORTSPEC int DECL2 _daveSendNLProFamilyPacket(daveConnection * dc, int size); + EXPORTSPEC int DECL2 _daveInitAdapterNLProFamily(daveInterface * di); + EXPORTSPEC int DECL2 _daveDisconnectPLCNLProFamily(daveConnection * dc); + EXPORTSPEC int DECL2 _daveDisconnectAdapterNLProFamily(daveInterface * di); + EXPORTSPEC int DECL2 _daveListReachablePartnersNLProFamily(daveInterface * di, char * buf); + */ #endif /* _nodave */ #ifdef __cplusplus -//#ifdef CPLUSPLUS - } + //#ifdef CPLUSPLUS +} #endif /* - Changes: - 07/19/04 added the definition of daveExchange(). - 09/09/04 applied patch for variable Profibus speed from Andrew Rostovtsew. - 09/09/04 applied patch from Bryan D. Payne to make this compile under Cygwin and/or newer gcc. - 12/09/04 added daveReadBits(), daveWriteBits() - 12/09/04 added some more comments. - 12/09/04 changed declaration of _daveMemcmp to use typed pointers. - 01/15/04 generic getResponse, more internal functions, use a single dummy to replace - initAdapterDummy, - 01/26/05 replaced _daveConstructReadRequest by the sequence prepareReadRequest, addVarToReadRequest - 01/26/05 added multiple write - 02/02/05 added readIBHpacket - 02/06/05 replaced _daveConstructBitWriteRequest by the sequence prepareWriteRequest, addBitVarToWriteRequest - 02/08/05 removed inline functions. - 03/23/05 added _daveGetResponsePPI_IBH(). - 03/24/05 added function codes for download. - 03/28/05 added some comments. - 04/05/05 reworked error reporting. - 04/06/05 renamed swap functions. When I began libnodave on little endian i386 and Linux, I used - used Linux bswap functions. Then users (and later me) tried other systems without - a bswap. I also cannot use inline functions in Pascal. So I made my own bswaps. Then - I, made the core of my own swaps dependent of LITTLEENDIAN conditional to support also - bigendien systems. Now I want to rename them from bswap to something else to avoid - confusion for LINUX/UNIX users. The new names are swapIed_16 and swapIed_32. This - shall say swap "if endianness differs". While I could have used similar functions - from the network API (htons etc.) on Unix and Win32, they may not be present on - e.g. microcontrollers. - I highly recommend to use these functions even when writing software for big endian - systems, where they effectively do nothing, as it will make your software portable. - 04/08/05 removed deprecated conversion functions. - 04/09/05 removed daveDebug. Use setDebug and getDebug instead. Some programming environments - do not allow access to global variables in a shared library. - 04/09/05 removed CYGWIN defines. As there were no more differences against LINUX, it should - work with LINUX defines. - 04/09/05 reordered fields in daveInterface to put fields used in normal test programs at the - beginning. This allows to make a simplified version in nodavesimple as short as - possible. - 05/09/05 renamed more functions to daveXXX. - 05/11/05 added some functions for the convenience of usage with .net or mono. The goal is - that the application doesn't have to use members of data structures defined herein - directly. This avoids "unsafe" pointer expressions in .net/MONO. It should also ease - porting to VB or other languages for which it could be difficult to define byte by - byte equivalents of these structures. - 07/31/05 added message string copying for Visual Basic. - 08/28/05 added some functions to read and set PLC realtime clock. - 09/02/05 added the pointer "hook" to daveConnection. This can be used by applications to pass - data between function calls using the dc. - 09/11/05 added read/write functions for long blocks of data. - 09/17/05 incorporation of S5 functions - 09/18/05 implemented conversions from and to S5 KG format (old, propeiatary floating point format). -*/ + Changes: + 07/19/04 added the definition of daveExchange(). + 09/09/04 applied patch for variable Profibus speed from Andrew Rostovtsew. + 09/09/04 applied patch from Bryan D. Payne to make this compile under Cygwin and/or newer gcc. + 12/09/04 added daveReadBits(), daveWriteBits() + 12/09/04 added some more comments. + 12/09/04 changed declaration of _daveMemcmp to use typed pointers. + 01/15/04 generic getResponse, more internal functions, use a single dummy to replace + initAdapterDummy, + 01/26/05 replaced _daveConstructReadRequest by the sequence prepareReadRequest, addVarToReadRequest + 01/26/05 added multiple write + 02/02/05 added readIBHpacket + 02/06/05 replaced _daveConstructBitWriteRequest by the sequence prepareWriteRequest, addBitVarToWriteRequest + 02/08/05 removed inline functions. + 03/23/05 added _daveGetResponsePPI_IBH(). + 03/24/05 added function codes for download. + 03/28/05 added some comments. + 04/05/05 reworked error reporting. + 04/06/05 renamed swap functions. When I began libnodave on little endian i386 and Linux, I used + used Linux bswap functions. Then users (and later me) tried other systems without + a bswap. I also cannot use inline functions in Pascal. So I made my own bswaps. Then + I, made the core of my own swaps dependent of LITTLEENDIAN conditional to support also + bigendien systems. Now I want to rename them from bswap to something else to avoid + confusion for LINUX/UNIX users. The new names are swapIed_16 and swapIed_32. This + shall say swap "if endianness differs". While I could have used similar functions + from the network API (htons etc.) on Unix and Win32, they may not be present on + e.g. microcontrollers. + I highly recommend to use these functions even when writing software for big endian + systems, where they effectively do nothing, as it will make your software portable. + 04/08/05 removed deprecated conversion functions. + 04/09/05 removed daveDebug. Use setDebug and getDebug instead. Some programming environments + do not allow access to global variables in a shared library. + 04/09/05 removed CYGWIN defines. As there were no more differences against LINUX, it should + work with LINUX defines. + 04/09/05 reordered fields in daveInterface to put fields used in normal test programs at the + beginning. This allows to make a simplified version in nodavesimple as short as + possible. + 05/09/05 renamed more functions to daveXXX. + 05/11/05 added some functions for the convenience of usage with .net or mono. The goal is + that the application doesn't have to use members of data structures defined herein + directly. This avoids "unsafe" pointer expressions in .net/MONO. It should also ease + porting to VB or other languages for which it could be difficult to define byte by + byte equivalents of these structures. + 07/31/05 added message string copying for Visual Basic. + 08/28/05 added some functions to read and set PLC realtime clock. + 09/02/05 added the pointer "hook" to daveConnection. This can be used by applications to pass + data between function calls using the dc. + 09/11/05 added read/write functions for long blocks of data. + 09/17/05 incorporation of S5 functions + 09/18/05 implemented conversions from and to S5 KG format (old, propeiatary floating point format). + */ diff --git a/externalDlls/libnodave/testISO_TCP b/externalDlls/libnodave/testISO_TCP new file mode 100644 index 00000000..8252cb0c Binary files /dev/null and b/externalDlls/libnodave/testISO_TCP differ diff --git a/externalDlls/libnodave/testISO_TCP.c b/externalDlls/libnodave/testISO_TCP.c index 4935adbb..8d4cf5de 100644 --- a/externalDlls/libnodave/testISO_TCP.c +++ b/externalDlls/libnodave/testISO_TCP.c @@ -1,136 +1,21 @@ -/* - Test and demo program for Libnodave, a free communication libray for Siemens S7. - - ********************************************************************** - * WARNING: This and other test programs overwrite data in your PLC. * - * DO NOT use it on PLC's when anything is connected to their outputs.* - * This is alpha software. Use entirely on your own risk. * - ********************************************************************** - - (C) Thomas Hergenhahn (thomas.hergenhahn@web.de) 2002, 2003, 2004. - This is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. - - This is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Libnodave; see the file COPYING. If not, write to - the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -//#define PLAY_WITH_KEEPALIVE #include #include #include -#include "nodavesimple.h" +#include "nodave.h" #include "openSocket.h" -#ifdef LINUX -#include -#include -#include -#define UNIX_STYLE -#endif +#include // TWI -#ifdef BCCWIN -#include - void usage(void); - void wait(void); -#define WIN_STYLE -#endif +//#include +//#include -#ifdef PLAY_WITH_KEEPALIVE -#include -#endif -#include - - -void readSZL(daveConnection *dc,int id, int index) { - int res, SZLid, indx, SZcount, SZlen,i,j,len; - uc * d,*dd; - uc ddd[3000]; - printf("Trying to read SZL-ID %04X index %02X.\n",id,index); - res=daveReadSZL(dc,id,index, ddd, 3000); - printf("Function result: %d %s len:%d\n",res,daveStrerror(res),dc->AnswLen); -// _daveDump("Data",dc->resultPointer,dc->AnswLen); - _daveDump("Data",ddd,dc->AnswLen); - - if ((dc->AnswLen)>=4) { - d=ddd; - dd=ddd; - dd+=8; - len=dc->AnswLen-8; - SZLid=0x100*d[0]+d[1]; - indx=0x100*d[2]+d[3]; - printf("result SZL ID %04X %02X \n",SZLid,indx); - - if ((dc->AnswLen)>=8) { - SZlen=0x100*d[4]+d[5]; - SZcount=0x100*d[6]+d[7]; - printf("%d elements of %d bytes\n",SZcount,SZlen); - if(len>0){ - for (i=0;i0){ - for (j=0; j0){ - printf("%02X,",*dd); - dd++; - } - len--; - } - printf("\n"); - } - } - } - } - } - printf("\n"); -} - -void readSZLAll(daveConnection *dc) { - uc SzlList[1000]; - int res, SZLid, indx, SZcount, SZlen,i,j, rid, rind; - uc * d,*dd; - - res=daveReadSZL(dc,0,0,SzlList, 1000); - printf("%d %d\n",res,dc->AnswLen); - if ((dc->AnswLen)>=4) { - d=dc->resultPointer; - dd=SzlList; - dd+=8; - SZLid=0x100*d[0]+d[1]; - indx=0x100*d[2]+d[3]; - printf("result SZL ID %04X %02X \n",SZLid,indx); - - if ((dc->AnswLen)>=8) { - SZlen=0x100*d[4]+d[5]; - SZcount=0x100*d[6]+d[7]; - printf("%d elements of %d bytes\n",SZcount,SZlen); - for (i=0;i sets slot for PLC (default is 2).\n"); printf("--ram2rom tries to Copy RAM to ROM.\n"); @@ -162,59 +53,58 @@ void usage() printf("Example: testISO_TCP -w 192.168.19.1\n"); } -void loadBlocksOfType(daveConnection * dc, int blockType) { - int j, i, uploadID, len, more; -#ifdef UNIX_STYLE - int fd; -#endif -#ifdef WIN_STYLE - HANDLE fd; - unsigned long res; -#endif - char blockName [20]; - uc blockBuffer[20000],*bb; - daveBlockEntry dbe[256]; - j=daveListBlocksOfType(dc, blockType, dbe); - if (j<0) { - printf("error %d = %s\n",-j,daveStrerror(-j)); - return; - } - printf("%d blocks of type %s\n",j,daveBlockName(blockType)); - for (i=0; i ergänzen */ + + int i,a,b,c,adrPos,doWrite,doBenchmark, doSZLread, doMultiple, doClear, res, useProtocol,doSZLreadAll, doRun, doStop, doCopyRAMtoROM, doReadout, doSFBandSFC, - doNewfunctions, saveDebug, + doSync, doReadTime, + doTestMany, + doNewfunctions, + aLongDB, + saveDebug, doRouting, doList, doListall, useSlot; #ifdef PLAY_WITH_KEEPALIVE int opt; @@ -226,7 +116,17 @@ int main(int argc, char **argv) { PDU p; daveResultSet rs; - daveSetDebug(0x1ffff); + char routeargs[100]; + char * first,*second; + int subnet1; + int subnet3; + int PLCadrsize; + uc PLCaddress[4]; + uc buffer[1000000]; + int length; + + + daveSetDebug(daveDebugPrintErrors); adrPos=1; doWrite=0; doBenchmark=0; @@ -239,439 +139,199 @@ int main(int argc, char **argv) { doCopyRAMtoROM=0; doReadout=0; doSFBandSFC=0; + doSync=0; + doReadTime=0; doNewfunctions=0; - + doRouting=0; + doList=0; + doListall=0; useProtocol=daveProtoISOTCP; useSlot=2; - + if (argc<2) { usage(); exit(-1); } while (argv[adrPos][0]=='-') { - if (strcmp(argv[adrPos],"-2")==0) { - useProtocol=daveProtoISOTCP243; - } else if (strncmp(argv[adrPos],"--debug=",8)==0) { - daveSetDebug(atol(argv[adrPos]+8)); - printf("setting debug to: 0x%lx\n",atol(argv[adrPos]+8)); - } else if (strcmp(argv[adrPos],"-s")==0) { - doStop=1; - } else if (strcmp(argv[adrPos],"-r")==0) { - doRun=1; - } else if (strncmp(argv[adrPos],"--ram2rom",9)==0) { - doCopyRAMtoROM=1; - } else if (strncmp(argv[adrPos],"--readoutall",12)==0) { - doReadout=1; - doSFBandSFC=1; - } else if (strncmp(argv[adrPos],"--readout",9)==0) { - doReadout=1; - } else if (strncmp(argv[adrPos],"--slot=",7)==0) { - useSlot=atol(argv[adrPos]+7); - } else if (strcmp(argv[adrPos],"-d")==0) { - daveSetDebug(daveDebugAll); - } else if (strcmp(argv[adrPos],"-n")==0) { - doNewfunctions=1; - } else - if (strcmp(argv[adrPos],"-w")==0) { - doWrite=1; - } else - if (strcmp(argv[adrPos],"-b")==0) { - doBenchmark=1; - } else - if (strcmp(argv[adrPos],"-z")==0) { - doSZLread=1; - } else - if (strcmp(argv[adrPos],"-a")==0) { - doSZLreadAll=1; - } else - if (strcmp(argv[adrPos],"-m")==0) { - doMultiple=1; - } - adrPos++; - if (argc<=adrPos) { - usage(); - exit(-1); - } + if (strcmp(argv[adrPos],"-2")==0) { + useProtocol=daveProtoISOTCP243; + } else if (strncmp(argv[adrPos],"--debug=",8)==0) { + daveSetDebug(atol(argv[adrPos]+8)); + printf("setting debug to: 0x%lx\n",atol(argv[adrPos]+8)); + } else if (strcmp(argv[adrPos],"-s")==0) { + doStop=1; + } else if (strcmp(argv[adrPos],"-t")==0) { + doReadTime=1; + } else if (strcmp(argv[adrPos],"-r")==0) { + doRun=1; + } else if (strncmp(argv[adrPos],"--many=",7)==0) { + aLongDB=atol(argv[adrPos]+7); + doTestMany=1; + } else if (strncmp(argv[adrPos],"--listall",9)==0) { + doListall=1; + doList=1; + } else if (strncmp(argv[adrPos],"--list",6)==0) { + doList=1; + } else if (strncmp(argv[adrPos],"--ram2rom",9)==0) { + doCopyRAMtoROM=1; + } else if (strncmp(argv[adrPos],"--readoutall",12)==0) { + doReadout=1; + doSFBandSFC=1; + } else if (strncmp(argv[adrPos],"--readout",9)==0) { + doReadout=1; + } else if (strncmp(argv[adrPos],"--route=",8)==0) { + doRouting=1; + strncpy(routeargs,argv[adrPos]+8, 100); + printf("routing arguments: %s\n",routeargs); + subnet1=strtol(routeargs,&first,16); + printf("1st part subnet ID: %d\n",subnet1); + first++; + subnet3=strtol(first,&first,16); + printf("2nd part subnet ID: %d\n",subnet3); + first++; + printf("rest: %s\n",first); + PLCaddress[0]=strtol(first,&first,10); + if (strlen(first)!=0) { + printf("PLC address is IP\n"); + PLCadrsize=4; + first++; + PLCaddress[1]=strtol(first,&first,10); + first++; + PLCaddress[2]=strtol(first,&first,10); + first++; + PLCaddress[3]=strtol(first,&first,10); + + } else { + printf("PLC address: %d\n", PLCaddress[0]); + PLCadrsize=1; + } + } else if (strncmp(argv[adrPos],"--slot=",7)==0) { + useSlot=atol(argv[adrPos]+7); + } else if (strcmp(argv[adrPos],"-d")==0) { + daveSetDebug(daveDebugAll); + } else if (strcmp(argv[adrPos],"-n")==0) { + doNewfunctions=1; + } else + if (strcmp(argv[adrPos],"-w")==0) { + doWrite=1; + } else + if (strcmp(argv[adrPos],"-b")==0) { + doBenchmark=1; + } else + if (strcmp(argv[adrPos],"-z")==0) { + doSZLread=1; + } else + if (strcmp(argv[adrPos],"-a")==0) { + doSZLreadAll=1; + } else + if (strcmp(argv[adrPos],"-m")==0) { + doMultiple=1; + } else + if (strncmp(argv[adrPos],"--sync",6)==0) { + doSync=1; + } + adrPos++; + if (argc<=adrPos) { + usage(); + exit(-1); + } } fds.rfd=openSocket(102, argv[adrPos]); -#ifdef PLAY_WITH_KEEPALIVE - errno=0; - opt=1; - res=setsockopt(fds.rfd, SOL_SOCKET, SO_KEEPALIVE, &opt, 4); - LOG3("setsockopt %s %d\n", strerror(errno),res); -#endif fds.wfd=fds.rfd; if (fds.rfd>0) { - printf("aa.\n"); - di =daveNewInterface(fds,"IF1",0, useProtocol, daveSpeed187k); - printf("bb.\n"); - daveSetTimeout(di,15000000); - printf("cc.\n"); - dc =daveNewConnection(di,2,0,useSlot); // insert your rack and slot here - printf("dd.\n"); - if (0==daveConnectPLC(dc)) { - printf("Connected.\n"); -// di->timeout=1000; -/* - printf("Trying to read 1 byte from system data,address 0x1e3 like MicroWin does.\n"); - daveReadBytes(dc,3,0,0x1e3,1,NULL); - - printf("Trying to read 20 bytes from system data,address 0 like MicroWin does.\n"); - daveReadBytes(dc,3,0,0,20,NULL); -*/ - printf("Trying to read 64 bytes (16 dwords) from data block 1.\n"); - wait(); - res=daveReadBytes(dc,daveDB,1,0,64,NULL); - if(0==res) { - a=daveGetU16(dc); - printf("DB1:DW0: %d\n",a); - a=daveGetU16(dc); - printf("DB1:DW1: %d\n...\n",a); - a=daveGetU16At(dc,62); - printf("DB1:DW32: %d\n",a); - } else - printf("failed! (%d)\n",res); - printf("Trying to read 16 bytes from FW0.\n"); - wait(); -/* - * Some comments about daveReadBytes(): - * - * Here we read flags and not data blocks, because we cannot know which data blocks will - * exist in a user's PLC, but flags are always present. To read from data block 5 use: - * daveReadBytes(dc, daveDB, 5, 0, 16, NULL); - * to read DBD68 and DBW72 use: - * daveReadBytes(dc, daveDB, 5, 68, 6, NULL); - * to read DBD68 and DBW72 into your applications buffer appBuffer use: - * daveReadBytes(dc, daveDB, 5, 68, 6, appBuffer); - * to read DBD68 and DBD78 into your applications buffer appBuffer use: - * daveReadBytes(dc, daveDB, 5, 68, 14, appBuffer); - * this reads DBD68 and DBD78 and everything in between and fills the range - * appBuffer+4 to appBuffer+9 with unwanted bytes, but is much faster than: - * daveReadBytes(dc, daveDB, 5, 68, 4, appBuffer); - * daveReadBytes(dc, daveDB, 5, 78, 4, appBuffer+4); - * - * Users of the 200 family please note: - * The 200 family PLCs have the V area. This is accessed like a datablock with number 1. - * This is not a quirk or convention introduced by libnodave, but the command transmitted - * to the PLC is exactly the same that would read from DB1 off a 300 or 400. - * - */ - res=daveReadBytes(dc,daveFlags,0,0,16,NULL); - if (0==res) { -/* - * daveGetU32(dc); reads a word (2 bytes) from the current buffer position and increments - * an internal pointer by 2, so next daveGetXXX() wil read from the new position behind that - * word. If you want to read from a position you specify, use daveGetU16at(dc, position). - */ - a=daveGetU32(dc); - b=daveGetU32(dc); - c=daveGetU32(dc); - d=daveGetFloat(dc); - printf("FD0: %d\n",a); - printf("FD4: %d\n",b); - printf("FD8: %d\n",c); - printf("FD12: %f\n",d); - } else - printf("failed! (%d)\n",res); + di =daveNewInterface(fds,"IF1",0, useProtocol, daveSpeed187k); + daveSetTimeout(di,5000000); + dc =daveNewConnection(di,2,0,useSlot); // insert your rack and slot here + + /*if (doRouting) { + daveSetRoutingDestination(dc, subnet1, subnet3, PLCadrsize, PLCaddress); + }*/ - if(doSZLread) { - readSZL(dc,0xA0,0x0); - readSZL(dc,0x92,0x0); - readSZL(dc,0xB4,0x1024); - readSZL(dc,0x111,0x1); - readSZL(dc,0x111,0x2); - readSZL(dc,0xD91,0x0); - readSZL(dc,0x232,0x4); - readSZL(dc,0x1A0,0x0); - } - if(doSZLreadAll) { - readSZLAll(dc); - } - if(doMultiple) { - printf("Now testing read multiple variables.\n" - "This will read 1 Byte from inputs,\n" - "4 bytes from flags, 2 bytes from DB6\n" - " and other 2 bytes from flags\n"); - wait(); - davePrepareReadRequest(dc, &p); - daveAddVarToReadRequest(&p,daveInputs,0,0,1); - daveAddVarToReadRequest(&p,daveFlags,0,0,4); - daveAddVarToReadRequest(&p,daveDB,6,20,2); - daveAddVarToReadRequest(&p,daveFlags,0,12,2); - res=daveExecReadRequest(dc, &p, &rs); - - printf("Input Byte 0 "); - res=daveUseResult(dc, &rs, 0); // first result - if (res==0) { - a=daveGetU8(dc); - printf("%d\n",a); - } else - printf("*** Error: %s\n",daveStrerror(res)); - - printf("Flag DWord 0 "); - res=daveUseResult(dc, &rs, 1); // 2nd result - if (res==0) { - a=daveGetS16(dc); - printf("%d\n",a); - } else - printf("*** Error: %s\n",daveStrerror(res)); - - printf("DB 6 Word 20: "); - res=daveUseResult(dc, &rs, 2); // 3rd result - if (res==0) { - a=daveGetS16(dc); - printf("%d\n",a); - } else - printf("*** Error: %s\n",daveStrerror(res)); - - printf("Flag Word 12: "); - res=daveUseResult(dc, &rs, 3); // 4th result - if (res==0) { - a=daveGetU16(dc); - printf("%d\n",a); - } else - printf("*** Error: %s\n",daveStrerror(res)); - - printf("non existing result: "); - res=daveUseResult(dc, &rs, 4); // 5th result - if (res==0) { - a=daveGetU16(dc); - printf("%d\n",a); - } else - printf("*** Error: %s\n",daveStrerror(res)); - daveFreeResults(&rs); - -/* - for (i=0; ierror), r2->length); - res=daveUseResult(dc, &rs, i); - if (r2->length>0) _daveDump("bytes",r2->bytes,r2->length); - if (r2->bytes!=NULL) { - _daveDump("bytes",r2->bytes,r2->length); - d=daveGetFloat(dc); - printf("FD12: %f\n",d); - } - } -*/ - } + if (0==daveConnectPLC(dc)) { + printf("Connected.\n"); + + printf("Starte GetNC Program...\n"); + + /* Das NC-Programm */ + /* + int iPos = 0; + for (i=1; i<90001; i++) { + sprintf(buffer+iPos, "G0 F%05d\r\n",i); + iPos+=strlen("G0 F00000\r\n"); + //std::stringstream ss; + //ss << "G0 F" << i << "\r\n"; + //std::string s = ss.str(); + //strcpy(buffer, "G0 Fxxxx\r\n"); + }; + sprintf(buffer+iPos, "M30\r\n\r\n"); + */ + //strcpy(buffer, "M30\r\n\r\n"); + //strcpy(buffer, "G4F5\r\nUNTERPROG1\r\nG4F5\r\nUNTERPROG1\r\nG4F5\r\nM30\r\n\r\n"); + //res = davePutNCProgram2(dc, "_N_TEST_DOWNLOAD1_MPF", "/_N_WKS_DIR/_N_TEST_JE_WPD", "160513072915", buffer, strlen(buffer)); + + uc* param[10]; + int length; + printf("daveSetDebug: 0x1ffff\n"); + daveSetDebug(0x1ffff); + //daveSetDebug(daveGetDebug()|daveDebugPDU|daveDebugUpload); //daveDebug & daveDebugUpload - - if(doWrite) { - printf("Now we write back these data after incrementing the first 3 by 1,2,3 and the float by 1.1.\n"); - wait(); -/* - Attention! you need to daveSwapIed little endian variables before using them as a buffer for - daveWriteBytes() or before copying them into a buffer for daveWriteBytes()! -*/ - a=daveSwapIed_32(a+1); - daveWriteBytes(dc,daveFlags,0,00,4,&a); - b=daveSwapIed_32(b+2); - daveWriteBytes(dc,daveFlags,0,4,4,&b); - c=daveSwapIed_32(c+3); - daveWriteBytes(dc,daveFlags,0,8,4,&c); - d=toPLCfloat(d+1.1); - daveWriteBytes(dc,daveFlags,0,12,4,&d); - if(0==daveReadBytes(dc,daveFlags,0,0,16,NULL)) { - a=daveGetU32(dc); - b=daveGetU32(dc); - c=daveGetU32(dc); - d=daveGetFloat(dc); - printf("FD0: %d\n",a); - printf("FD4: %d\n",b); - printf("FD8: %d\n",c); - printf("FD12: %f\n",d); - } else { - - } - wait(); - } // doWrite - if(doClear) { - printf("Now writing 0 to the bytes FB0...FB15.\n"); - wait(); - a=0; - daveWriteBytes(dc,daveFlags,0,0,4,&a); - daveWriteBytes(dc,daveFlags,0,4,4,&a); - daveWriteBytes(dc,daveFlags,0,8,4,&a); - daveWriteBytes(dc,daveFlags,0,12,4,&a); - daveReadBytes(dc,daveFlags,0,0,16,NULL); - a=daveGetU32(dc); - b=daveGetU32(dc); - c=daveGetU32(dc); - d=daveGetFloat(dc); - printf("FD0: %d\n",a); - printf("FD4: %d\n",b); - printf("FD8: %d\n",c); - printf("FD12: %f\n",d); - } // doClear + //############# Upload DIR ######### + printf("Init PI-Service...\n"); + param[0]= "P01"; + param[1]= "/_N_wks_dir"; + printf("Start PI-Service \"DIR\"...\n"); + res = davePIstart_nc(dc, "_N_F_XFER", ¶m, 2); + printf("davePIstart_nc res=%d\n", res); - if(doNewfunctions) { - saveDebug=daveGetDebug(); - - printf("\nTrying to read two consecutive bits from DB11.DBX0.1 (supposed to fail)\n");; - res=daveReadBits(dc, daveDB, 11, 1, 2,NULL); - printf("function result:%d=%s\n", res, daveStrerror(res)); - - printf("\nTrying to read no bit (length 0) from DB17.DBX0.1 (supposed to fail)\n"); - res=daveReadBits(dc, daveDB, 17, 1, 0,NULL); - printf("function result:%d=%s\n", res, daveStrerror(res)); + //Upload DIR von NC + printf("Starte daveGetNcFile...\n"); + res = daveGetNcFile(dc, "_N_wks_dir", buffer, &length); + //###################### - daveSetDebug(daveGetDebug()|daveDebugPDU); - printf("\nTrying to read a single bit from DB17.DBX0.3 (supposed to work if you have a DB17)\n"); - res=daveReadBits(dc, daveDB, 17, 3, 1,NULL); - printf("function result:%d=%s\n", res, daveStrerror(res)); - - printf("\nTrying to read a single bit from E0.2 (supposed to work)\n"); - res=daveReadBits(dc, daveInputs, 0, 2, 1,NULL); - printf("function result:%d=%s\n", res, daveStrerror(res)); - - a=0; - printf("\nWriting 0 to EB0 (supposed to work)\n"); - res=daveWriteBytes(dc, daveOutputs, 0, 0, 1, &a); - a=1; - printf("\nTrying to set single bit E0.5 (supposed to work)\n"); - res=daveWriteBits(dc, daveOutputs, 0, 5, 1, &a); - printf("function result:%d=%s\n", res, daveStrerror(res)); - - printf("\nTrying to read 1 byte from AAW0 (supposed to work on S7-2xx)\n"); - res=daveReadBytes(dc, daveAnaIn, 0, 0, 2,NULL); - printf("function result:%d=%s\n", res, daveStrerror(res)); - - a=2341; - printf("\nTrying to write 1 word (2 bytes) to AAW0 (supposed to work on S7-2xx)\n"); - res=daveWriteBytes(dc, daveAnaOut, 0, 0, 2,&a); - printf("function result:%d=%s\n", res, daveStrerror(res)); - - printf("\nTrying to read 4 items from Timers (supposed to work S7-3xx/4xx)\n"); - res=daveReadBytes(dc, daveTimer, 0, 0, 4,NULL); - printf("function result:%d=%s\n", res, daveStrerror(res)); + //############ Upload Folder ########## - if (res==0) { - printf("\nShowing the results using daveGetSeconds(dc)\n"); - d=daveGetSeconds(dc); - printf("Time: %0.3f, ",d); - d=daveGetSeconds(dc); - printf("%0.3f, ",d); - d=daveGetSeconds(dc); - printf("%0.3f, ",d); - d=daveGetSeconds(dc); - printf(" %0.3f\n",d); - printf("\nShowing the same results using daveGetSecondsAt(dc,position)\n"); - d=daveGetSecondsAt(dc,0); - printf("Time: %0.3f, ",d); - d=daveGetSecondsAt(dc,2); - printf("%0.3f, ",d); - d=daveGetSecondsAt(dc,4); - printf("%0.3f, ",d); - d=daveGetSecondsAt(dc,6); - printf(" %0.3f\n",d); - } - printf("\nTrying to read 4 items from Counters (supposed to work S7-3xx/4xx)\n"); - res=daveReadBytes(dc, daveCounter, 0, 0, 4,NULL); - printf("function result:%d=%s\n", res, daveStrerror(res)); - if (res==0) { - printf("\nShowing the results using daveGetCounterValue(dc)\n"); - c=daveGetCounterValue(dc); - printf("Count: %d, ",c); - c=daveGetCounterValue(dc); - printf("%d, ",c); - c=daveGetCounterValue(dc); - printf("%d, ",c); - c=daveGetCounterValue(dc); - printf(" %d\n",c); - - printf("\nShowing the same results using daveGetCounterValueAt(dc, position)\n"); - c=daveGetCounterValueAt(dc,0); - printf("Count: %d, ",c); - c=daveGetCounterValueAt(dc,2); - printf("%d, ",c); - c=daveGetCounterValueAt(dc,4); - printf("%d, ",c); - c=daveGetCounterValueAt(dc,6); - printf(" %d\n",c); - } - - printf("\nTrying to read multiple items with multiple variable read. (1st 2 supposed to work ok, rests may debend on CPU type and contents)\n"); - davePrepareReadRequest(dc, &p); - daveAddVarToReadRequest(&p,daveInputs,0,0,1); - daveAddVarToReadRequest(&p,daveFlags,0,0,4); - daveAddVarToReadRequest(&p,daveDB,6,20,2); - daveAddVarToReadRequest(&p,daveTimer,0,0,4); - daveAddVarToReadRequest(&p,daveTimer,0,1,4); - daveAddVarToReadRequest(&p,daveTimer,0,2,4); - daveAddVarToReadRequest(&p,daveCounter,0,0,4); - daveAddVarToReadRequest(&p,daveCounter,0,1,4); - daveAddVarToReadRequest(&p,daveCounter,0,2,4); - res=daveExecReadRequest(dc, &p, &rs); - - daveSetDebug(saveDebug); - } + //PI-Service + //PI_Service("_N_F_XFER", new string[] { "P01", fullFileName }); + //davePIstart_nc(daveConnection *dc, const char *piservice, const char *param[], int paramCount) + printf("Init PI-Service...\n"); + + param[0]= "P01"; + //param[1]= "/_N_WKS_DIR/_N_TEST_JE_WPD/_N_TEST_UPLOAD2_MPF"; + param[1]= "/_N_wks_dir/_N_G_GQ_TEST_SWITCH_VCS_WPD"; + printf("Start PI-Service...\n"); - if(doStop) { - daveStop(dc); - } - if(doRun) { - daveStart(dc); - } - if(doCopyRAMtoROM) { - res = daveCopyRAMtoROM(dc); - printf("RetVal=(%04X)\n",res); - } - if(doReadout) { - loadBlocksOfType(dc, daveBlockType_OB); - loadBlocksOfType(dc, daveBlockType_FB); - loadBlocksOfType(dc, daveBlockType_FC); - loadBlocksOfType(dc, daveBlockType_DB); - loadBlocksOfType(dc, daveBlockType_SDB); - if (doSFBandSFC){ - loadBlocksOfType(dc, daveBlockType_SFB); - loadBlocksOfType(dc, daveBlockType_SFC); - } - } - - if(doBenchmark) { - if(useProtocol==daveProtoISOTCP243) { // we have a 200 CPU, use V memory - rBenchmark(dc, daveDB); - if(doWrite) { - wBenchmark(dc, daveDB); - } // doWrite - } else { // we have a 300/400 CPU, use Flags(Merkers) - rBenchmark(dc, daveFlags); - if(doWrite) { - wBenchmark(dc, daveFlags); - } // doWrite - } - } // doBenchmark - closeSocket(fds.rfd); - printf("Finished.\n"); - - return 0; - } else { - printf("Couldn't connect to PLC.\n Please make sure you use the -2 option with a CP243 but not with CPs 343 or 443.\n"); - closeSocket(fds.rfd); - return -2; - } + //int DECL2 davePIstart_nc(daveConnection *dc, const char *piservice, const char *param[], int paramCount) + //PI_Service("_N_F_XFER", new string[] { "P01", "/_N_wks_dir/_N_G_GQ_TEST_SWITCH_VCS_WPD" }); + res = davePIstart_nc(dc, "_N_F_XFER", ¶m, 2); + printf("davePIstart_nc res=%d\n", res); + + + //Upload von NC + printf("Starte daveGetNcFile...\n"); + res = daveGetNcFile(dc, "_N_G_GQ_TEST_SWITCH_VCS_WPD", buffer, &length); + //daveGetNCfile(dc, "N_TEST_UPLOAD2_MPF", buffer, &length); + + printf("daveGetNcFile res=%d\n", res); + //###################### + + wait(); + + closeSocket(fds.rfd); + printf("Finished.\n"); + + return 0; + } else { + printf("Couldn't connect to PLC.\n Please make sure you use the -2 option with a CP243 but not with CPs 343 or 443.\n"); + closeSocket(fds.rfd); + return -2; + } } else { - printf("Couldn't open TCP port. \nPlease make sure a CP is connected and the IP address is ok. \n"); + printf("Couldn't open TCP port. \nPlease make sure a CP is connected and the IP address is ok. \n"); return -1; } } - -/* - Changes: - 07/19/04 removed unused vars. - 09/09/04 applied patch for variable Profibus speed from Andrew Rostovtsew. - 09/09/04 removed unused includes byteswap.h. Made some code to test TCP keepalive conditional. - 03/03/05 added slot select option. - 03/28/05 changed code to use flag (M) area for write. Was DB17. - 04/09/05 removed CYGWIN defines. As there were no more differences against LINUX, it should - work with LINUX defines. -Version 0.8.4.5 - 07/10/09 Added closeSocket() - 07/10/09 Added daveCopyRAMtoROM -*/ diff --git a/externalDlls/libnodave/testISO_TCP.exe b/externalDlls/libnodave/testISO_TCP.exe new file mode 100644 index 00000000..52f740bc Binary files /dev/null and b/externalDlls/libnodave/testISO_TCP.exe differ diff --git a/externalDlls/libnodave/testISO_TCP.exp b/externalDlls/libnodave/testISO_TCP.exp index 7fef0f4f..6d931dce 100644 Binary files a/externalDlls/libnodave/testISO_TCP.exp and b/externalDlls/libnodave/testISO_TCP.exp differ diff --git a/externalDlls/libnodave/testISO_TCP.lib b/externalDlls/libnodave/testISO_TCP.lib new file mode 100644 index 00000000..7945dd08 Binary files /dev/null and b/externalDlls/libnodave/testISO_TCP.lib differ diff --git a/externalDlls/libnodave_jfkmod.dll b/externalDlls/libnodave_jfkmod.dll index a06f7295..44f366be 100644 Binary files a/externalDlls/libnodave_jfkmod.dll and b/externalDlls/libnodave_jfkmod.dll differ diff --git a/externalDlls/libnodave_jfkmod64.dll b/externalDlls/libnodave_jfkmod64.dll index 08849210..3f21da4b 100644 Binary files a/externalDlls/libnodave_jfkmod64.dll and b/externalDlls/libnodave_jfkmod64.dll differ