Example #1
0
        private static void AssertToByteArrayAndBackEquals(string value, int reservedLength, params byte[] expected)
        {
            var convertedData = S7WString.ToByteArray(value, reservedLength);

            CollectionAssert.AreEqual(expected, convertedData);
            var convertedBack = S7WString.FromByteArray(convertedData);

            Assert.AreEqual(value, convertedBack);
        }
Example #2
0
        public static byte[] SerializeDataItem(DataItem dataItem)
        {
            if (dataItem.Value == null)
            {
                throw new Exception($"DataItem.Value is null, cannot serialize. StartAddr={dataItem.StartByteAdr} VarType={dataItem.VarType}");
            }

            if (dataItem.Value is string s)
            {
                return dataItem.VarType switch
                       {
                           VarType.S7String => S7String.ToByteArray(s, dataItem.Count),
                           VarType.S7WString => S7WString.ToByteArray(s, dataItem.Count),
                           _ => Types.String.ToByteArray(s, dataItem.Count)
                       }
            }
            ;

            return(SerializeValue(dataItem.Value));
        }
Example #3
0
        /// <summary>
        /// Given a S7 variable type (Bool, Word, DWord, etc.), it converts the bytes in the appropriate C# format.
        /// </summary>
        /// <param name="varType"></param>
        /// <param name="bytes"></param>
        /// <param name="varCount"></param>
        /// <param name="bitAdr"></param>
        /// <returns></returns>
        private object?ParseBytes(VarType varType, byte[] bytes, int varCount, byte bitAdr = 0)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return(null);
            }

            switch (varType)
            {
            case VarType.Byte:
                if (varCount == 1)
                {
                    return(bytes[0]);
                }
                else
                {
                    return(bytes);
                }

            case VarType.Word:
                if (varCount == 1)
                {
                    return(Word.FromByteArray(bytes));
                }
                else
                {
                    return(Word.ToArray(bytes));
                }

            case VarType.Int:
                if (varCount == 1)
                {
                    return(Int.FromByteArray(bytes));
                }
                else
                {
                    return(Int.ToArray(bytes));
                }

            case VarType.DWord:
                if (varCount == 1)
                {
                    return(DWord.FromByteArray(bytes));
                }
                else
                {
                    return(DWord.ToArray(bytes));
                }

            case VarType.DInt:
                if (varCount == 1)
                {
                    return(DInt.FromByteArray(bytes));
                }
                else
                {
                    return(DInt.ToArray(bytes));
                }

            case VarType.Real:
                if (varCount == 1)
                {
                    return(Types.Real.FromByteArray(bytes));
                }
                else
                {
                    return(Types.Real.ToArray(bytes));
                }

            case VarType.LReal:
                if (varCount == 1)
                {
                    return(Types.LReal.FromByteArray(bytes));
                }
                else
                {
                    return(Types.LReal.ToArray(bytes));
                }

            case VarType.String:
                return(Types.String.FromByteArray(bytes));

            case VarType.S7String:
                return(S7String.FromByteArray(bytes));

            case VarType.S7WString:
                return(S7WString.FromByteArray(bytes));

            case VarType.Timer:
                if (varCount == 1)
                {
                    return(Timer.FromByteArray(bytes));
                }
                else
                {
                    return(Timer.ToArray(bytes));
                }

            case VarType.Counter:
                if (varCount == 1)
                {
                    return(Counter.FromByteArray(bytes));
                }
                else
                {
                    return(Counter.ToArray(bytes));
                }

            case VarType.Bit:
                if (varCount == 1)
                {
                    if (bitAdr > 7)
                    {
                        return(null);
                    }
                    else
                    {
                        return(Bit.FromByte(bytes[0], bitAdr));
                    }
                }
                else
                {
                    return(Bit.ToBitArray(bytes, varCount));
                }

            case VarType.DateTime:
                if (varCount == 1)
                {
                    return(DateTime.FromByteArray(bytes));
                }
                else
                {
                    return(DateTime.ToArray(bytes));
                }

            case VarType.DateTimeLong:
                if (varCount == 1)
                {
                    return(DateTimeLong.FromByteArray(bytes));
                }
                else
                {
                    return(DateTimeLong.ToArray(bytes));
                }

            default:
                return(null);
            }
        }
Example #4
0
 public void ReadMalformedStringSizeLargerThanCapacity()
 {
     Assert.ThrowsException <PlcException>(() => S7WString.FromByteArray(new byte[] { 0, 3, 0, 5, 0, 0x00, 0x41, 0x00, 0x41, 0x00, 0x41 }));
 }
Example #5
0
        private static void AssertFromByteArrayEquals(string expected, params byte[] bytes)
        {
            var convertedString = S7WString.FromByteArray(bytes);

            Assert.AreEqual(expected, convertedString);
        }
Example #6
0
 public void WriteAbcWithStringLargerThanReservedLength()
 {
     Assert.ThrowsException <ArgumentException>(() => S7WString.ToByteArray("Abc", 2));
 }