Merge branch 'develop' into fb-fixWarnings

This commit is contained in:
Michael Croes
2020-09-09 21:01:54 +02:00
committed by GitHub
6 changed files with 67 additions and 39 deletions

View File

@@ -324,9 +324,9 @@ namespace S7.Net
if (bitAdr != -1)
{
//Must be writing a bit value as bitAdr is specified
if (value is bool)
if (value is bool boolean)
{
await WriteBitAsync(dataType, db, startByteAdr, bitAdr, (bool) value);
await WriteBitAsync(dataType, db, startByteAdr, bitAdr, boolean);
}
else if (value is int intValue)
{

View File

@@ -289,9 +289,9 @@ namespace S7.Net
if (bitAdr != -1)
{
//Must be writing a bit value as bitAdr is specified
if (value is bool)
if (value is bool boolean)
{
WriteBit(dataType, db, startByteAdr, bitAdr, (bool) value);
WriteBit(dataType, db, startByteAdr, bitAdr, boolean);
}
else if (value is int intValue)
{
@@ -442,7 +442,6 @@ namespace S7.Net
private byte[] BuildWriteBitPackage(DataType dataType, int db, int startByteAdr, bool bitValue, int bitAdr)
{
var stream = GetStreamIfAvailable();
var value = new[] { bitValue ? (byte)1 : (byte)0 };
int varCount = 1;
// first create the header

View File

@@ -17,7 +17,6 @@ namespace S7.Net.Protocol
(ushort) (2 + paramSize));
var paramOffset = Header.Template.Length;
var dataOffset = paramOffset + paramSize;
var data = new ByteArray();
var itemCount = 0;

View File

@@ -79,9 +79,9 @@ namespace S7.Net.Protocol
{
var start = startByte * 8 + bitNumber;
buffer[index + 2] = (byte)start;
start = start >> 8;
start >>= 8;
buffer[index + 1] = (byte)start;
start = start >> 8;
start >>= 8;
buffer[index] = (byte)start;
}

View File

@@ -17,10 +17,17 @@ namespace S7.Net.Types
/// <returns></returns>
public static string FromByteArray(byte[] bytes)
{
if (bytes.Length < 2) return "";
if (bytes.Length < 2)
{
throw new PlcException(ErrorCode.ReadData, "Malformed S7 String / too short");
}
int size = bytes[0];
int length = bytes[1];
if (length > size)
{
throw new PlcException(ErrorCode.ReadData, "Malformed S7 String / length larger than capacity");
}
try
{
@@ -43,18 +50,21 @@ namespace S7.Net.Types
/// <returns>A <see cref="T:byte[]" /> containing the string header and string value with a maximum length of <paramref name="reservedLength"/> + 2.</returns>
public static byte[] ToByteArray(string value, int reservedLength)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (reservedLength > byte.MaxValue) throw new ArgumentException($"The maximum string length supported is {byte.MaxValue}.");
var length = value?.Length;
if (length > reservedLength) length = reservedLength;
var bytes = Encoding.ASCII.GetBytes(value);
if (bytes.Length > reservedLength) throw new ArgumentException($"The provided string length ({bytes.Length} is larger than the specified reserved length ({reservedLength}).");
var bytes = new byte[(length ?? 0) + 2];
bytes[0] = (byte) reservedLength;
if (value == null) return bytes;
bytes[1] = (byte) Encoding.ASCII.GetBytes(value, 0, length.Value, bytes, 2);
return bytes;
var buffer = new byte[2 + reservedLength];
Array.Copy(bytes, 0, buffer, 2, bytes.Length);
buffer[0] = (byte)reservedLength;
buffer[1] = (byte)bytes.Length;
return buffer;
}
}
}