Skip to content

Commit

Permalink
Merge pull request #494 from S7NetPlus/warnings
Browse files Browse the repository at this point in the history
Cleanup of warnings
  • Loading branch information
mycroes authored Jul 29, 2023
2 parents b3077b2 + e26860b commit 361db8b
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 46 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
fail-fast: false

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Install Snap7 Linux
if: ${{ matrix.os == 'ubuntu-20.04' }}
Expand All @@ -90,14 +90,14 @@ jobs:
brew install snap7
- name: Setup Dotnet
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6.x
7.x
- name: Nuget Cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.nuget/packages
# Look to see if there is a cache hit for the corresponding requirements file
Expand Down
2 changes: 2 additions & 0 deletions S7.Net/COTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public TPDU(TPKT tPKT)
/// See: https://tools.ietf.org/html/rfc905
/// </summary>
/// <param name="stream">The socket to read from</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
/// <returns>COTP DPDU instance</returns>
public static async Task<TPDU> ReadAsync(Stream stream, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -89,6 +90,7 @@ public class TSDU
/// See: https://tools.ietf.org/html/rfc905
/// </summary>
/// <param name="stream">The stream to read from</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
/// <returns>Data in TSDU</returns>
public static async Task<byte[]> ReadAsync(Stream stream, CancellationToken cancellationToken)
{
Expand Down
5 changes: 3 additions & 2 deletions S7.Net/PLCHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public partial class Plc
/// <summary>
/// Creates the header to read bytes from the PLC
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
/// <param name="stream">The stream to write to.</param>
/// <param name="amount">The number of items to read.</param>
private static void BuildHeaderPackage(System.IO.MemoryStream stream, int amount = 1)
{
//header size = 19 bytes
Expand All @@ -32,6 +32,7 @@ private static void BuildHeaderPackage(System.IO.MemoryStream stream, int amount
/// Create the bytes-package to request data from the PLC. You have to specify the memory type (dataType),
/// the address of the memory, the address of the byte and the bytes count.
/// </summary>
/// <param name="stream">The stream to write the read data request to.</param>
/// <param name="dataType">MemoryType (DB, Timer, Counter, etc.)</param>
/// <param name="db">Address of the memory to be read</param>
/// <param name="startByteAdr">Start address of the byte</param>
Expand Down
2 changes: 1 addition & 1 deletion S7.Net/PlcAsynchronous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ public async Task WriteAsync(DataType dataType, int db, int startByteAdr, object

/// <summary>
/// Writes a single variable from the PLC, takes in input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.
/// If the write was not successful, check <see cref="LastErrorCode"/> or <see cref="LastErrorString"/>.
/// </summary>
/// <param name="variable">Input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.</param>
/// <param name="value">Value to be written to the PLC</param>
Expand Down Expand Up @@ -507,6 +506,7 @@ public async Task WriteAsync(params DataItem[] dataItems)
/// <param name="db">Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.</param>
/// <param name="startByteAdr">Start byte address. If you want to read DB1.DBW200, this is 200.</param>
/// <param name="value">Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
private async Task WriteBytesWithASingleRequestAsync(DataType dataType, int db, int startByteAdr, ReadOnlyMemory<byte> value, CancellationToken cancellationToken)
{
Expand Down
1 change: 0 additions & 1 deletion S7.Net/PlcSynchronous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ public void Write(DataType dataType, int db, int startByteAdr, object value, int

/// <summary>
/// Writes a single variable from the PLC, takes in input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.
/// If the write was not successful, check <see cref="LastErrorCode"/> or <see cref="LastErrorString"/>.
/// </summary>
/// <param name="variable">Input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.</param>
/// <param name="value">Value to be written to the PLC</param>
Expand Down
3 changes: 2 additions & 1 deletion S7.Net/S7.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
<RepositoryType>git</RepositoryType>
<PackageTags>PLC Siemens Communication S7</PackageTags>
<Copyright>Derek Heiser 2015</Copyright>
<LangVersion>8.0</LangVersion>
<LangVersion>latest</LangVersion>
<Nullable>Enable</Nullable>
<DebugType>portable</DebugType>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591;NETSDK1138</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net452' Or '$(TargetFramework)' == 'net462' Or '$(TargetFramework)' == 'netstandard2.0' ">
Expand Down
1 change: 1 addition & 0 deletions S7.Net/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static int ReadExact(this Stream stream, byte[] buffer, int offset, int c
/// <param name="buffer">the buffer to read into</param>
/// <param name="offset">the offset in the buffer to read into</param>
/// <param name="count">the amount of bytes to read into the buffer</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
/// <returns>returns the amount of read bytes</returns>
public static async Task<int> ReadExactAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
Expand Down
1 change: 1 addition & 0 deletions S7.Net/TPKT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private TPKT(byte version, byte reserved1, int length, byte[] data)
/// Reads a TPKT from the socket Async
/// </summary>
/// <param name="stream">The stream to read from</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
/// <returns>Task TPKT Instace</returns>
public static async Task<TPKT> ReadAsync(Stream stream, CancellationToken cancellationToken)
{
Expand Down
37 changes: 26 additions & 11 deletions S7.Net/Types/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ private static double GetIncreasedNumberOfBytes(double numBytes, Type type, Prop
numBytes += attribute.ReservedLengthInBytes;
break;
default:
var propertyClass = Activator.CreateInstance(type);
var propertyClass = Activator.CreateInstance(type) ??
throw new ArgumentException($"Failed to create instance of type {type}.", nameof(type));
numBytes = GetClassSize(propertyClass, numBytes, true);
break;
}
Expand All @@ -76,6 +77,8 @@ private static double GetIncreasedNumberOfBytes(double numBytes, Type type, Prop
/// Gets the size of the class in bytes.
/// </summary>
/// <param name="instance">An instance of the class</param>
/// <param name="numBytes">The offset of the current field.</param>
/// <param name="isInnerProperty"><see langword="true" /> if this property belongs to a class being serialized as member of the class requested for serialization; otherwise, <see langword="false" />.</param>
/// <returns>the number of bytes</returns>
public static double GetClassSize(object instance, double numBytes = 0.0, bool isInnerProperty = false)
{
Expand All @@ -84,8 +87,10 @@ public static double GetClassSize(object instance, double numBytes = 0.0, bool i
{
if (property.PropertyType.IsArray)
{
Type elementType = property.PropertyType.GetElementType();
Array array = (Array)property.GetValue(instance, null);
Type elementType = property.PropertyType.GetElementType()!;
Array array = (Array?) property.GetValue(instance, null) ??
throw new ArgumentException($"Property {property.Name} on {instance} must have a non-null value to get it's size.", nameof(instance));

if (array.Length <= 0)
{
throw new Exception("Cannot determine size of class, because an array is defined which has no fixed size greater than zero.");
Expand Down Expand Up @@ -199,7 +204,9 @@ public static double GetClassSize(object instance, double numBytes = 0.0, bool i
numBytes += sData.Length;
break;
default:
var propClass = Activator.CreateInstance(propertyType);
var propClass = Activator.CreateInstance(propertyType) ??
throw new ArgumentException($"Failed to create instance of type {propertyType}.", nameof(propertyType));

numBytes = FromBytes(propClass, bytes, numBytes);
value = propClass;
break;
Expand All @@ -213,6 +220,8 @@ public static double GetClassSize(object instance, double numBytes = 0.0, bool i
/// </summary>
/// <param name="sourceClass">The object to fill in the given array of bytes</param>
/// <param name="bytes">The array of bytes</param>
/// <param name="numBytes">The offset for the current field.</param>
/// <param name="isInnerClass"><see langword="true" /> if this class is the type of a member of the class to be serialized; otherwise, <see langword="false" />.</param>
public static double FromBytes(object sourceClass, byte[] bytes, double numBytes = 0, bool isInnerClass = false)
{
if (bytes == null)
Expand All @@ -223,9 +232,11 @@ public static double FromBytes(object sourceClass, byte[] bytes, double numBytes
{
if (property.PropertyType.IsArray)
{
Array array = (Array)property.GetValue(sourceClass, null);
Array array = (Array?) property.GetValue(sourceClass, null) ??
throw new ArgumentException($"Property {property.Name} on sourceClass must be an array instance.", nameof(sourceClass));

IncrementToEven(ref numBytes);
Type elementType = property.PropertyType.GetElementType();
Type elementType = property.PropertyType.GetElementType()!;
for (int i = 0; i < array.Length && numBytes < bytes.Length; i++)
{
array.SetValue(
Expand Down Expand Up @@ -320,26 +331,30 @@ private static double SetBytesFromProperty(object propertyValue, PropertyInfo? p
/// <summary>
/// Creates a byte array depending on the struct type.
/// </summary>
/// <param name="sourceClass">The struct object</param>
/// <param name="sourceClass">The struct object.</param>
/// <param name="bytes">The target byte array.</param>
/// <param name="numBytes">The offset for the current field.</param>
/// <returns>A byte array or null if fails.</returns>
public static double ToBytes(object sourceClass, byte[] bytes, double numBytes = 0.0)
{
var properties = GetAccessableProperties(sourceClass.GetType());
foreach (var property in properties)
{
var value = property.GetValue(sourceClass, null) ??
throw new ArgumentException($"Property {property.Name} on sourceClass can't be null.", nameof(sourceClass));

if (property.PropertyType.IsArray)
{
Array array = (Array)property.GetValue(sourceClass, null);
Array array = (Array) value;
IncrementToEven(ref numBytes);
Type elementType = property.PropertyType.GetElementType();
for (int i = 0; i < array.Length && numBytes < bytes.Length; i++)
{
numBytes = SetBytesFromProperty(array.GetValue(i), property, bytes, numBytes);
numBytes = SetBytesFromProperty(array.GetValue(i)!, property, bytes, numBytes);
}
}
else
{
numBytes = SetBytesFromProperty(property.GetValue(sourceClass, null), property, bytes, numBytes);
numBytes = SetBytesFromProperty(value, property, bytes, numBytes);
}
}
return numBytes;
Expand Down
2 changes: 1 addition & 1 deletion S7.Net/Types/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ byte EncodeBcd(int value)
/// Converts an array of <see cref="T:System.DateTime"/> values to a byte array.
/// </summary>
/// <param name="dateTimes">The DateTime values to convert.</param>
/// <returns>A byte array containing the S7 date time representations of <paramref name="dateTime"/>.</returns>
/// <returns>A byte array containing the S7 date time representations of <paramref name="dateTimes"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when any value of
/// <paramref name="dateTimes"/> is before <see cref="P:SpecMinimumDateTime"/>
/// or after <see cref="P:SpecMaximumDateTime"/>.</exception>
Expand Down
18 changes: 9 additions & 9 deletions S7.Net/Types/S7String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ namespace S7.Net.Types
/// An S7 String has a preceeding 2 byte header containing its capacity and length
/// </summary>
public static class S7String
{
private static Encoding stringEncoding = Encoding.ASCII;

{
private static Encoding stringEncoding = Encoding.ASCII;

/// <summary>
/// The Encoding used when serializing and deserializing S7String (Encoding.ASCII by default)
/// </summary>
/// <exception cref="ArgumentNullException">StringEncoding must not be null</exception>
public static Encoding StringEncoding
{
get => stringEncoding;
set => stringEncoding = value ?? throw new ArgumentNullException(nameof(StringEncoding));
/// <exception cref="ArgumentNullException">StringEncoding must not be null</exception>
public static Encoding StringEncoding
{
get => stringEncoding;
set => stringEncoding = value ?? throw new ArgumentNullException(nameof(StringEncoding));
}

/// <summary>
Expand Down Expand Up @@ -58,7 +58,7 @@ public static string FromByteArray(byte[] bytes)
/// <param name="value">The string to convert to byte array.</param>
/// <param name="reservedLength">The length (in characters) allocated in PLC for the string.</param>
/// <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)
public static byte[] ToByteArray(string? value, int reservedLength)
{
if (value is null)
{
Expand Down
2 changes: 1 addition & 1 deletion S7.Net/Types/S7WString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static string FromByteArray(byte[] bytes)
/// <param name="value">The string to convert to byte array.</param>
/// <param name="reservedLength">The length (in characters) allocated in PLC for the string.</param>
/// <returns>A <see cref="T:byte[]" /> containing the string header and string value with a maximum length of <paramref name="reservedLength"/> + 4.</returns>
public static byte[] ToByteArray(string value, int reservedLength)
public static byte[] ToByteArray(string? value, int reservedLength)
{
if (value is null)
{
Expand Down
10 changes: 6 additions & 4 deletions S7.Net/Types/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ public class String
/// <param name="reservedLength">The amount of bytes reserved for the <paramref name="value"/> in the PLC.</param>
public static byte[] ToByteArray(string value, int reservedLength)
{
var length = value?.Length;
if (length > reservedLength) length = reservedLength;
var bytes = new byte[reservedLength];
if (value == null) return bytes;

var length = value.Length;
if (length == 0) return bytes;

if (length == null || length == 0) return bytes;
if (length > reservedLength) length = reservedLength;

System.Text.Encoding.ASCII.GetBytes(value, 0, length.Value, bytes, 0);
System.Text.Encoding.ASCII.GetBytes(value, 0, length, bytes, 0);

return bytes;
}
Expand Down
Loading

0 comments on commit 361db8b

Please sign in to comment.