Skip to content

Commit

Permalink
Merge pull request #47 from ChrisPulman/FixWrite
Browse files Browse the repository at this point in the history
Fix for Write (UInt16, Byte[], ushort[])
  • Loading branch information
ChrisPulman authored Jan 18, 2024
2 parents 4c9317b + dfc916e commit 370bd8c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.3.1",
"version": "1.3.2",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
53 changes: 17 additions & 36 deletions src/S7PlcRx/RxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,14 +1290,10 @@ private IObservable<Unit> WatchDogObservable() =>
/// <param name="startByteAdr">
/// Start byte address. If you want to read DB1.DBW200, this is 200.
/// </param>
/// <param name="value">
/// Bytes to write. The length of this parameter can't be higher than 200. If you need more,
/// use recursion.
/// </param>
/// <returns>NoError if it was successful, or the error is specified.</returns>
private bool Write(Tag tag, DataType dataType, int db, int startByteAdr, object? value = null)
private bool Write(Tag tag, DataType dataType, int db, int startByteAdr)
{
if (tag.NewValue == null && value == null)
if (tag.NewValue == null)
{
return false;
}
Expand All @@ -1307,32 +1303,25 @@ private bool Write(Tag tag, DataType dataType, int db, int startByteAdr, object?
{
case "Boolean":
case "Byte":
package = [(byte)(value ?? Convert.ChangeType(tag.NewValue, typeof(byte)))!];
package = [(byte)Convert.ChangeType(tag.NewValue, typeof(byte))!];
break;

case "Int16":
case "short":
package = Int.ToByteArray((short)tag.NewValue!);
break;

case "UInt16":
if (value == null)
{
return false;
}

var parsed = ushort.Parse(value.ToString()!);
var vOut = BitConverter.GetBytes(parsed);
package = [vOut[1], vOut[0]];
break;

case "ushort":
package = Word.ToByteArray((ushort)Convert.ChangeType(tag.NewValue, typeof(ushort))!);
break;

case "Int32":
case "int":
package = DInt.ToByteArray((int)tag.NewValue!);
break;

case "UInt32":
case "uint":
package = DWord.ToByteArray((uint)Convert.ChangeType(tag.NewValue, typeof(uint))!);
break;
Expand All @@ -1346,31 +1335,25 @@ private bool Write(Tag tag, DataType dataType, int db, int startByteAdr, object?
break;

case "Byte[]":
if (value == null)
{
return false;
}

package = (byte[])value;
package = (byte[])tag.NewValue;
break;

case "Int16[]":
case "short[]":
package = Int.ToByteArray((short[])tag.NewValue!);
break;

case "UInt16[]":
case "ushort[]":
if (value == null)
{
return false;
}

package = Word.ToByteArray((ushort[])value);
package = Word.ToByteArray((ushort[])tag.NewValue);
break;

case "Int32[]":
case "int[]":
package = DInt.ToByteArray((int[])tag.NewValue!);
break;

case "UInt32[]":
case "uint[]":
package = DWord.ToByteArray((uint[])Convert.ChangeType(tag.NewValue, typeof(uint[]))!);
break;
Expand All @@ -1384,11 +1367,6 @@ private bool Write(Tag tag, DataType dataType, int db, int startByteAdr, object?
break;

case "String":
if (value == null)
{
return false;
}

package = PlcTypes.String.ToByteArray(tag.NewValue! as string);
break;

Expand Down Expand Up @@ -1487,7 +1465,9 @@ private bool WriteString(Tag? tag)
b = (byte)(b & (b ^ (byte)Math.Pow(2, mBit))); // reset bit
}

return Write(tag, DataType.DataBlock, mDB, mByte, b);
tag.NewValue = b;

return Write(tag, DataType.DataBlock, mDB, mByte);

default:
throw new Exception(string.Format("Addressing Error: Unable to parse address {0}. Supported formats include DBB (BYTE), DBW (WORD), DBD (DWORD), DBX (BITWISE), DBS (STRING).", dbType));
Expand Down Expand Up @@ -1590,7 +1570,8 @@ private bool WriteString(Tag? tag)
}
}

return Write(tag, mDataType, 0, mByte, @byte);
tag.NewValue = @byte;
return Write(tag, mDataType, 0, mByte);
}
}
catch (Exception exc)
Expand Down

0 comments on commit 370bd8c

Please sign in to comment.