From d3f5a8d09909e9cd81dcbd0d76a31501b529ca26 Mon Sep 17 00:00:00 2001 From: Marcos Pereira Date: Wed, 9 Mar 2016 14:26:54 +0000 Subject: [PATCH 1/3] Write struct and multiple bytes from a specific start bytes address. --- S7.Net.Core/PLC.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/S7.Net.Core/PLC.cs b/S7.Net.Core/PLC.cs index f7d32a9..b1fa1d4 100644 --- a/S7.Net.Core/PLC.cs +++ b/S7.Net.Core/PLC.cs @@ -803,9 +803,14 @@ namespace S7.Net } public ErrorCode WriteStruct(object structValue, int db) + { + return WriteStruct(structValue, db, 0); + } + + public ErrorCode WriteStruct(object structValue, int db, int startByteAdr) { var bytes = Types.Struct.ToBytes(structValue).ToList(); - var errCode = WriteMultipleBytes(bytes, db); + var errCode = WriteMultipleBytes(bytes, db, startByteAdr); return errCode; } @@ -823,9 +828,14 @@ namespace S7.Net /// The DB number /// ErrorCode when writing (NoError if everything was ok) private ErrorCode WriteMultipleBytes(List bytes, int db) + { + return WriteMultipleBytes(bytes, db, 0); + } + + private ErrorCode WriteMultipleBytes(List bytes, int db, int startByteAdr) { ErrorCode errCode = ErrorCode.NoError; - int index = 0; + int index = startByteAdr; try { while (bytes.Count > 0) From 3e877cad7c3f7a3c3ec141b84173cf2c7e2bc085 Mon Sep 17 00:00:00 2001 From: Marcos Pereira Date: Wed, 9 Mar 2016 14:30:20 +0000 Subject: [PATCH 2/3] Read structs recursively on get struct size and build struct from bytes. --- S7.Net.Common/Types/Struct.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/S7.Net.Common/Types/Struct.cs b/S7.Net.Common/Types/Struct.cs index 9ecf2bf..15214b2 100644 --- a/S7.Net.Common/Types/Struct.cs +++ b/S7.Net.Common/Types/Struct.cs @@ -49,6 +49,9 @@ namespace S7.Net.Types numBytes++; numBytes += 4; break; + default: + numBytes += GetStructSize(info.FieldType); + break; } } return (int)numBytes; @@ -146,6 +149,14 @@ namespace S7.Net.Types bytes[(int)numBytes + 3] })); numBytes += 4; break; + default: + var buffer = new byte[GetStructSize(info.FieldType)]; + if (buffer.Length == 0) + continue; + Buffer.BlockCopy(bytes, (int)Math.Ceiling(numBytes), buffer, 0, buffer.Length); + info.SetValue(structValue, FromBytes(info.FieldType, buffer)); + numBytes += buffer.Length; + break; } } return structValue; From 5dcd52e9d2ccaf6f20745d8d486c442e0f815355 Mon Sep 17 00:00:00 2001 From: Marcos Pereira Date: Wed, 9 Mar 2016 14:32:19 +0000 Subject: [PATCH 3/3] Write struct and multiple bytes from a specific start bytes address. --- S7.Net/PLC.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index a0b4abf..554b89a 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -803,9 +803,14 @@ namespace S7.Net } public ErrorCode WriteStruct(object structValue, int db) + { + return WriteStruct(structValue, db, 0); + } + + public ErrorCode WriteStruct(object structValue, int db, int startByteAdr) { var bytes = Types.Struct.ToBytes(structValue).ToList(); - var errCode = WriteMultipleBytes(bytes, db); + var errCode = WriteMultipleBytes(bytes, db, startByteAdr); return errCode; } @@ -823,9 +828,14 @@ namespace S7.Net /// The DB number /// ErrorCode when writing (NoError if everything was ok) private ErrorCode WriteMultipleBytes(List bytes, int db) + { + return WriteMultipleBytes(bytes, db, 0); + } + + private ErrorCode WriteMultipleBytes(List bytes, int db, int startByteAdr) { ErrorCode errCode = ErrorCode.NoError; - int index = 0; + int index = startByteAdr; try { while (bytes.Count > 0)