Merge pull request #45 from watashimeandeu/master

Read structs recursively
This commit is contained in:
Michele Cattafesta
2016-03-09 23:39:52 +01:00
3 changed files with 35 additions and 4 deletions

View File

@@ -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;

View File

@@ -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
/// <param name="db">The DB number</param>
/// <returns>ErrorCode when writing (NoError if everything was ok)</returns>
private ErrorCode WriteMultipleBytes(List<byte> bytes, int db)
{
return WriteMultipleBytes(bytes, db, 0);
}
private ErrorCode WriteMultipleBytes(List<byte> bytes, int db, int startByteAdr)
{
ErrorCode errCode = ErrorCode.NoError;
int index = 0;
int index = startByteAdr;
try
{
while (bytes.Count > 0)

View File

@@ -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
/// <param name="db">The DB number</param>
/// <returns>ErrorCode when writing (NoError if everything was ok)</returns>
private ErrorCode WriteMultipleBytes(List<byte> bytes, int db)
{
return WriteMultipleBytes(bytes, db, 0);
}
private ErrorCode WriteMultipleBytes(List<byte> bytes, int db, int startByteAdr)
{
ErrorCode errCode = ErrorCode.NoError;
int index = 0;
int index = startByteAdr;
try
{
while (bytes.Count > 0)