Skip to content

Commit 73e7fe7

Browse files
authored
Merge pull request #14 from dotnetprojects/complSign
fix for complex signals
2 parents 50f5fc8 + 2216ab6 commit 73e7fe7

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

TiaCodeGen.Tests/SampleTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using System.Text;
56
using TiaCodegen.Blocks;
67
using TiaCodegen.Commands;
78
using TiaCodegen.Commands.Coils;
@@ -337,5 +338,25 @@ public void PeripheryFixedSignal()
337338
block.Interface = TestInterface;
338339
var xml = block.GetCode();
339340
}
341+
342+
[Test]
343+
public void ComplexVariableAccessTest1()
344+
{
345+
var sb = new StringBuilder();
346+
var s = new Signal("#A.B.C[#D.E.F, 1]");
347+
s.AddXmlToStringBuilder(1, sb);
348+
var xml = sb.ToString();
349+
Assert.AreEqual("<Access Scope=\"LocalVariable\" UId=\"1\">\r\n<Symbol>\r\n<Component Name=\"A\">\r\n</Component>\r\n<Component Name=\"B\">\r\n</Component>\r\n<Component Name=\"C\">\r\n<Access Scope=\"LocalVariable\">\r\n<Symbol>\r\n<Component Name=\"D\" />\r\n<Component Name=\"E\" />\r\n<Component Name=\"F\" />\r\n<Access Scope=\"LiteralConstant\">\r\n<Constant>\r\n<ConstantType>DInt</ConstantType>\r\n<ConstantValue>1</ConstantValue>\r\n</Constant>\r\n</Access>\r\n</Symbol>\r\n</Access>\r\n</Component>\r\n</Symbol>\r\n</Access>\r\n".Replace("\n","").Replace("\r", ""), xml.Replace("\n", "").Replace("\r", ""));
350+
}
351+
352+
[Test]
353+
public void ComplexVariableAccessTest2()
354+
{
355+
var sb = new StringBuilder();
356+
var s = new Signal("#A.B.C[#D.E.F, #G.H.I]");
357+
s.AddXmlToStringBuilder(1, sb);
358+
var xml = sb.ToString();
359+
Assert.AreEqual("<Access Scope=\"LocalVariable\" UId=\"1\">\r\n<Symbol>\r\n<Component Name=\"A\">\r\n</Component>\r\n<Component Name=\"B\">\r\n</Component>\r\n<Component Name=\"C\">\r\n<Access Scope=\"LocalVariable\">\r\n<Symbol>\r\n<Component Name=\"D\" />\r\n<Component Name=\"E\" />\r\n<Component Name=\"F\" />\r\n</Symbol>\r\n</Access>\r\n<Access Scope=\"LocalVariable\">\r\n<Symbol>\r\n<Component Name=\"G\" />\r\n<Component Name=\"H\" />\r\n<Component Name=\"I\" />\r\n</Symbol>\r\n</Access>\r\n</Component>\r\n</Symbol>\r\n</Access>\r\n".Replace("\n", "").Replace("\r", ""), xml.Replace("\n", "").Replace("\r", ""));
360+
}
340361
}
341362
}

TiaCodegen/Commands/Signals/Signal.cs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text;
5+
using TiaCodegen.Commands.Comparisons;
46
using TiaCodegen.Enums;
57
using TiaCodegen.Interfaces;
68

@@ -352,13 +354,59 @@ public void AddXmlToStringBuilder(ulong id, StringBuilder sb)
352354
{
353355
i++;
354356
p1 = Unescape(Escape(LocalName).Split('.')[i]);
355-
if (p1.Contains("]"))
357+
if (p1.Contains(","))
356358
{
357-
sb.AppendLine("<Component Name=\"" + p1.Substring(0, p1.Length - 1) + "\" />");
358-
p1 = null;
359+
var innerParts = p1.Split(',');
360+
sb.AppendLine("<Component Name=\"" + innerParts[0] + "\" />");
361+
foreach (var p in innerParts.Skip(1))
362+
{
363+
var pNe = p.Trim();
364+
bool close = false;
365+
if (pNe.Contains("]"))
366+
{
367+
pNe = pNe.Substring(0, pNe.Length - 1);
368+
close = true;
369+
}
370+
if (int.TryParse(pNe, out _))
371+
{
372+
sb.AppendLine("<Access Scope=\"LiteralConstant\">");
373+
sb.AppendLine("<Constant>");
374+
sb.AppendLine("<ConstantType>DInt</ConstantType>");
375+
sb.AppendLine("<ConstantValue>" + pNe + "</ConstantValue>");
376+
sb.AppendLine("</Constant>");
377+
sb.AppendLine("</Access>");
378+
}
379+
else
380+
{
381+
sb.AppendLine("</Symbol>");
382+
sb.AppendLine("</Access>");
383+
384+
var accessTypePne = "GlobalVariable";
385+
if (pNe[0] == '#')
386+
{
387+
pNe = pNe.Substring(1);
388+
accessTypePne = "LocalVariable";
389+
}
390+
sb.AppendLine("<Access Scope=\"" + accessTypePne + "\">");
391+
sb.AppendLine("<Symbol>");
392+
sb.AppendLine("<Component Name=\"" + pNe + "\" />");
393+
}
394+
}
395+
if (p1.Contains("]"))
396+
{
397+
p1 = null;
398+
}
359399
}
360400
else
361-
sb.AppendLine("<Component Name=\"" + p1 + "\" />");
401+
{
402+
if (p1.Contains("]"))
403+
{
404+
sb.AppendLine("<Component Name=\"" + p1.Substring(0, p1.Length - 1) + "\" />");
405+
p1 = null;
406+
}
407+
else
408+
sb.AppendLine("<Component Name=\"" + p1 + "\" />");
409+
}
362410
}
363411
sb.AppendLine("</Symbol>");
364412
sb.AppendLine("</Access>");
@@ -387,10 +435,10 @@ public void AddXmlToStringBuilder(ulong id, StringBuilder sb)
387435
}
388436
sb.AppendLine("</Component>");
389437
}
390-
if (this is FixedPeripherySignal)
391-
sb.AppendLine("<Address Area=\"PeripheryInput\" Type=\"Bool\" BitOffset=\"0\" Informative=\"true\" />");
392-
else
393-
sb.AppendLine("<Address Area=\"None\" Type=\"Bool\" BlockNumber=\"0\" BitOffset=\"0\" Informative=\"true\" />");
438+
//if (this is FixedPeripherySignal)
439+
// sb.AppendLine("<Address Area=\"PeripheryInput\" Type=\"Bool\" BitOffset=\"0\" Informative=\"true\" />");
440+
//else
441+
// sb.AppendLine("<Address Area=\"None\" Type=\"Bool\" BlockNumber=\"0\" BitOffset=\"0\" Informative=\"true\" />");
394442
sb.AppendLine("</Symbol>");
395443
sb.AppendLine("</Access>");
396444
}

0 commit comments

Comments
 (0)