Skip to content

Commit 9bb7021

Browse files
committed
Allow negative indexes for symbolic array access
1 parent 0209140 commit 9bb7021

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/S7CommPlusDriver/S7CommPlusConnection.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ public PObject getTypeInfoByRelId(uint ti_relid)
10971097
/// <exception cref="Exception">Symbol syntax error</exception>
10981098
private void calcAccessSeqFor1DimArray(ref string symbol, PVartypeListElement varType, VarInfo varInfo)
10991099
{
1100-
Regex re = new Regex(@"^\[(\d+)\]");
1100+
Regex re = new Regex(@"^\[(-?\d+)\]");
11011101
Match m = re.Match(symbol);
11021102
if (!m.Success) throw new Exception("Symbol syntax error");
11031103
parseSymbolLevel(ref symbol); // remove index from symbol string
@@ -1122,16 +1122,14 @@ private void calcAccessSeqFor1DimArray(ref string symbol, PVartypeListElement va
11221122
/// <exception cref="Exception">Symbol syntax error</exception>
11231123
private void calcAccessSeqForMDimArray(ref string symbol, PVartypeListElement varType, VarInfo varInfo)
11241124
{
1125-
Regex re = new Regex(@"^\[([0-9, ]+)\]");
1125+
Regex re = new Regex(@"^\[( ?-?\d+ ?(, ?-?\d+ ?)+)\]");
11261126
Match m = re.Match(symbol);
11271127
if (!m.Success) throw new Exception("Symbol syntax error");
11281128
parseSymbolLevel(ref symbol); // remove index from symbol string
11291129
string idxs = m.Groups[1].Value.Replace(" ", "");
11301130

1131-
uint[] indexes = Array.ConvertAll(idxs.Split(','), e => uint.Parse(e));
1131+
int[] indexes = Array.ConvertAll(idxs.Split(','), e => int.Parse(e));
11321132
var ioit = (IOffsetInfoType_MDim)varType.OffsetInfoType;
1133-
uint ArrayElementCount = ioit.GetArrayElementCount();
1134-
int ArrayLowerBounds = ioit.GetArrayLowerBounds();
11351133
uint[] MdimArrayElementCount = (uint[])ioit.GetMdimArrayElementCount().Clone();
11361134
int[] MdimArrayLowerBounds = ioit.GetMdimArrayLowerBounds();
11371135

@@ -1141,7 +1139,7 @@ private void calcAccessSeqForMDimArray(ref string symbol, PVartypeListElement va
11411139
// check bounds
11421140
for (int i = 0; i < dimCount; ++i)
11431141
{
1144-
indexes[i] = (uint)(indexes[i] - MdimArrayLowerBounds[dimCount - i - 1]);
1142+
indexes[i] = (indexes[i] - MdimArrayLowerBounds[dimCount - i - 1]);
11451143
if (indexes[i] > MdimArrayElementCount[dimCount - i - 1]) throw new Exception("Out of bounds");
11461144
if (indexes[i] < 0) throw new Exception("Out of bounds");
11471145
}
@@ -1161,10 +1159,10 @@ private void calcAccessSeqForMDimArray(ref string symbol, PVartypeListElement va
11611159
dimSize[dimCount - 1] = g;
11621160

11631161
// calc id
1164-
uint arrayIndex = 0;
1162+
int arrayIndex = 0;
11651163
for (int i = 0; i < dimCount; ++i)
11661164
{
1167-
arrayIndex += indexes[i] * dimSize[dimCount - i - 1];
1165+
arrayIndex += indexes[i] * (int)dimSize[dimCount - i - 1];
11681166
}
11691167

11701168
varInfo.AccessSequence += "." + String.Format("{0:X}", arrayIndex);

0 commit comments

Comments
 (0)