Merge branch 'feature/S7Net' into develop

This commit is contained in:
Samuele Locatelli
2020-07-21 17:44:58 +02:00
2 changed files with 33 additions and 2 deletions
+9 -2
View File
@@ -822,8 +822,15 @@ namespace CMS_CORE_Library.S7Net
if (libraryError.IsError())
return libraryError;
// need byte swap!
List<int> fixValues = new List<int>();
foreach(var item in readValues)
{
fixValues.Add(SwapBytes(item));
}
// Convert ints into an array of bools
bool[] bits = IntToBits(readValues.ToArray());
bool[] bits = IntToBits(fixValues.ToArray());
// Convert array into structured data
for (ushort i = 0; i < bits.Count() / 2; i++)
@@ -845,7 +852,7 @@ namespace CMS_CORE_Library.S7Net
if (id > USER_SOFTKEYS_NUMBER)
return INCORRECT_PARAMETERS_ERROR;
// Write strobe into memory --> check if bitArray
// Write strobe into memory --> false because is bitArray
CmsError libraryError = PLC_WStrobe(false, USER_SOFT_KEYS_ACK, USER_SOFT_KEYS_CLICKED, id);
if (libraryError.IsError())
return libraryError;
+24
View File
@@ -171,6 +171,30 @@ namespace CMS_CORE_Library.Utils
{
return IntToBits(new int[] { val });
}
/// <summary>
/// Swap byte in 32bit DWord (uint/uint)
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
internal static uint SwapBytes(uint x)
{
// swap adjacent 16-bit blocks
x = (x >> 16) | (x << 16);
// swap adjacent 8-bit blocks
return ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
}
/// <summary>
/// Swap byte in 32bit DWord (int/int)
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
internal static int SwapBytes(int x)
{
byte[] bytes = BitConverter.GetBytes(x);
Array.Reverse(bytes);
int result = BitConverter.ToInt32(bytes, 0);
return result;
}
// Convert array of integers in an array of bools
internal static bool[] IntToBits(int[] val)