-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from ChrisPulman/AddPLCtypes
Update and Add PLC Types
- Loading branch information
Showing
23 changed files
with
1,708 additions
and
450 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Chris Pulman. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace S7PlcRx.Enums; | ||
|
||
/// <summary> | ||
/// String type. | ||
/// </summary> | ||
public enum S7StringType | ||
{ | ||
/// <summary> | ||
/// ASCII string. | ||
/// </summary> | ||
S7String = VarType.S7String, | ||
|
||
/// <summary> | ||
/// Unicode string. | ||
/// </summary> | ||
S7WString = VarType.S7WString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
// Copyright (c) Chris Pulman. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace S7PlcRx | ||
namespace S7PlcRx; | ||
|
||
/// <summary> | ||
/// ITag. | ||
/// </summary> | ||
public interface ITag | ||
{ | ||
/// <summary> | ||
/// ITag. | ||
/// Sets the do not poll. | ||
/// </summary> | ||
public interface ITag | ||
{ | ||
/// <summary> | ||
/// Sets the do not poll. | ||
/// </summary> | ||
/// <param name="value">if set to <c>true</c> [value].</param> | ||
void SetDoNotPoll(bool value); | ||
} | ||
/// <param name="value">if set to <c>true</c> [value].</param> | ||
void SetDoNotPoll(bool value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Chris Pulman. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using S7PlcRx.Enums; | ||
|
||
namespace S7PlcRx; | ||
|
||
/// <summary> | ||
/// PlcException. | ||
/// </summary> | ||
/// <seealso cref="System.Exception" /> | ||
[Serializable] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Roslynator", "RCS1194:Implement exception constructors", Justification = "Not desired in this instance.")] | ||
public class PlcException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlcException"/> class. | ||
/// </summary> | ||
/// <param name="errorCode">The error code.</param> | ||
public PlcException(ErrorCode errorCode) | ||
: this(errorCode, $"PLC communication failed with error '{errorCode}'.") | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlcException"/> class. | ||
/// </summary> | ||
/// <param name="errorCode">The error code.</param> | ||
/// <param name="innerException">The inner exception.</param> | ||
public PlcException(ErrorCode errorCode, Exception? innerException) | ||
: this(errorCode, innerException?.Message, innerException) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlcException"/> class. | ||
/// </summary> | ||
/// <param name="errorCode">The error code.</param> | ||
/// <param name="message">The message.</param> | ||
public PlcException(ErrorCode errorCode, string? message) | ||
: base(message) => ErrorCode = errorCode; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlcException"/> class. | ||
/// </summary> | ||
/// <param name="errorCode">The error code.</param> | ||
/// <param name="message">The message.</param> | ||
/// <param name="inner">The inner.</param> | ||
public PlcException(ErrorCode errorCode, string? message, Exception? inner) | ||
: base(message, inner) => ErrorCode = errorCode; | ||
|
||
/// <summary> | ||
/// Gets the error code. | ||
/// </summary> | ||
/// <value> | ||
/// The error code. | ||
/// </value> | ||
public ErrorCode ErrorCode { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) Chris Pulman. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections; | ||
|
||
namespace S7PlcRx.PlcTypes; | ||
|
||
/// <summary> | ||
/// Contains the conversion methods to convert Bit from S7 plc to C#. | ||
/// </summary> | ||
public static class Bit | ||
{ | ||
/// <summary> | ||
/// Converts a Bit to bool. | ||
/// </summary> | ||
/// <param name="v">The v.</param> | ||
/// <param name="bitAdr">The bit adr.</param> | ||
/// <returns> | ||
/// A bool. | ||
/// </returns> | ||
public static bool FromByte(byte v, byte bitAdr) => (v & (1 << bitAdr)) != 0; | ||
|
||
/// <summary> | ||
/// Converts an array of bytes to a BitArray. | ||
/// </summary> | ||
/// <param name="bytes">The bytes to convert.</param> | ||
/// <returns> | ||
/// A BitArray with the same number of bits and equal values as <paramref name="bytes" />. | ||
/// </returns> | ||
public static BitArray ToBitArray(byte[] bytes) => ToBitArray(bytes, bytes?.Length * 8); | ||
|
||
/// <summary> | ||
/// Converts an array of bytes to a BitArray. | ||
/// </summary> | ||
/// <param name="bytes">The bytes to convert.</param> | ||
/// <param name="length">The number of bits to return.</param> | ||
/// <returns>A BitArray with <paramref name="length"/> bits.</returns> | ||
public static BitArray ToBitArray(byte[] bytes, int? length) | ||
{ | ||
if (length == null) | ||
{ | ||
throw new ArgumentNullException(nameof(length)); | ||
} | ||
|
||
if (bytes == null) | ||
{ | ||
throw new ArgumentNullException(nameof(bytes)); | ||
} | ||
|
||
if (length > bytes?.Length * 8) | ||
{ | ||
throw new ArgumentException($"Not enough data in bytes to return {length} bits.", nameof(bytes)); | ||
} | ||
|
||
var bitArr = new BitArray(bytes!); | ||
var bools = new bool[length.Value]; | ||
for (var i = 0; i < length; i++) | ||
{ | ||
bools[i] = bitArr[i]; | ||
} | ||
|
||
return new BitArray(bools); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Chris Pulman. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace S7PlcRx.PlcTypes; | ||
|
||
/// <summary> | ||
/// Contains the methods to read, set and reset bits inside bytes. | ||
/// </summary> | ||
public static class Boolean | ||
{ | ||
/// <summary> | ||
/// Returns the value of a bit in a bit, given the address of the bit. | ||
/// </summary> | ||
/// <param name="value">The value.</param> | ||
/// <param name="bit">The bit.</param> | ||
/// <returns>A bool.</returns> | ||
public static bool GetValue(byte value, int bit) => (value & (1 << bit)) != 0; | ||
|
||
/// <summary> | ||
/// Sets the value of a bit to 1 (true), given the address of the bit. Returns | ||
/// a copy of the value with the bit set. | ||
/// </summary> | ||
/// <param name="value">The input value to modify.</param> | ||
/// <param name="bit">The index (zero based) of the bit to set.</param> | ||
/// <returns>The modified value with the bit at index set.</returns> | ||
public static byte SetBit(byte value, int bit) | ||
{ | ||
SetBit(ref value, bit); | ||
|
||
return value; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the value of a bit to 1 (true), given the address of the bit. | ||
/// </summary> | ||
/// <param name="value">The value to modify.</param> | ||
/// <param name="bit">The index (zero based) of the bit to set.</param> | ||
public static void SetBit(ref byte value, int bit) => value = (byte)((value | (1 << bit)) & 0xFF); | ||
|
||
/// <summary> | ||
/// Resets the value of a bit to 0 (false), given the address of the bit. Returns | ||
/// a copy of the value with the bit cleared. | ||
/// </summary> | ||
/// <param name="value">The input value to modify.</param> | ||
/// <param name="bit">The index (zero based) of the bit to clear.</param> | ||
/// <returns>The modified value with the bit at index cleared.</returns> | ||
public static byte ClearBit(byte value, int bit) | ||
{ | ||
ClearBit(ref value, bit); | ||
|
||
return value; | ||
} | ||
|
||
/// <summary> | ||
/// Resets the value of a bit to 0 (false), given the address of the bit. | ||
/// </summary> | ||
/// <param name="value">The input value to modify.</param> | ||
/// <param name="bit">The index (zero based) of the bit to clear.</param> | ||
public static void ClearBit(ref byte value, int bit) => value = (byte)(value & ~(1 << bit) & 0xFF); | ||
} |
Oops, something went wrong.