Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="area">Where to read:  e.g.  DB1  or M or...</param>
        /// <param name="offset">offset in bytes, if you address booleans, you have to pass the address in bits (byteoffset * 8 + bitoffset)</param>
        /// <param name="length">The number of items to read</param>
        /// <returns></returns>
        public static WriteItem CreateChild(WriteItem item, ushort offset, ushort length)
        {
            var result = ReadItem.CreateChild(item, offset, length).Clone();

            result.Data = item.Data.Slice(offset - item.Offset, length);
            return(result);
        }
Exemplo n.º 2
0
        public static WriteItem From(this ReadItem ri, Memory <byte> data)
        {
            var result = ri.Clone();

            result.Data = data;
            return(result);
        }
Exemplo n.º 3
0
        private static ReadItem SetupTypes <T>(ReadItem result)
        {
            var t = typeof(T);

            if (t.IsArray)
            {
                result.VarType    = t.GetElementType();
                result.ResultType = t;
            }
            else if (t.GetInterface(typeof(IList <>).FullName) != null)
            {
                result.VarType    = t.GetGenericArguments().Single();
                result.ResultType = t;
            }
            else if (t == typeof(string))
            {
                result.VarType        = result.ResultType = t;
                result.NumberOfItems += result.Encoding == PlcEncoding.Unicode ? UnicodeStringHeaderSize : StringHeaderSize;
            }
            else if (t == typeof(Memory <byte>))
            {
                result.VarType = result.ResultType = t;
            }
            else
            {
                result.VarType    = t;
                result.ResultType = result.NumberOfItems > 1 ? typeof(T[]) : result.VarType;
            }


            EnsureSupportedType(result);
            result.ElementSize = GetElementSize(result.Area, result.VarType, result.Encoding);
            return(result);
        }
Exemplo n.º 4
0
 private ReadItem RegisteredOrGiven(string tag)
 {
     if (_registeredTags.TryGetValue(tag, out var nodeId))
     {
         return(nodeId);
     }
     return(ReadItem.CreateFromTag(tag));
 }
Exemplo n.º 5
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="area">Where to read:  e.g.  DB1  or M or...</param>
 /// <param name="offset">offset in bytes, if you address booleans, you have to pass the address in bits (byteoffset * 8 + bitoffset)</param>
 /// <param name="length">The number of items to read</param>
 /// <returns></returns>
 public static ReadItem CreateChild(ReadItem item, ushort offset, ushort length)
 {
     return(new ReadItem
     {
         Area = item.Area,
         DbNumber = item.DbNumber,
         Offset = offset,
         Length = length,
         VarType = item.VarType,
         ResultType = item.ResultType,
         Parent = item
     });
 }
Exemplo n.º 6
0
 internal static object ConvertMemoryToData(ReadItem item, Memory <byte> data)
 {
     if (item.ResultType == typeof(byte))
     {
         return(data.Span[0]);
     }
     else if (item.ResultType == typeof(byte[]) || item.ResultType == typeof(Memory <byte>))
     {
         return(data);
     }
     else if (item.ResultType == typeof(char))
     {
         return(Convert.ToChar(data.Span[0]));
     }
     else if (item.ResultType == typeof(char[]) || item.ResultType == typeof(Memory <char>))
     {
         return(null); // TODO
     }
     else if (item.ResultType == typeof(string))
     {
         var length = data.Span[1];
         return(Encoding.ASCII.GetString(data.Span.Slice(2, length).ToArray()));
     }
     else if (item.ResultType == typeof(Int16))
     {
         return(BinaryPrimitives.ReadInt16BigEndian(data.Span));
     }
     else if (item.ResultType == typeof(UInt16))
     {
         return(BinaryPrimitives.ReadUInt16BigEndian(data.Span));
     }
     else if (item.ResultType == typeof(Int32))
     {
         return(BinaryPrimitives.ReadInt32BigEndian(data.Span));
     }
     else if (item.ResultType == typeof(UInt32))
     {
         return(BinaryPrimitives.ReadUInt32BigEndian(data.Span));
     }
     else if (item.ResultType == typeof(Int64))
     {
         return(BinaryPrimitives.ReadInt64BigEndian(data.Span));
     }
     else if (item.ResultType == typeof(UInt64))
     {
         return(BinaryPrimitives.ReadUInt64BigEndian(data.Span));
     }
     throw new InvalidCastException();
 }
Exemplo n.º 7
0
 /// <summary>
 /// Create a child read item. (More than one items are  sharing their memory)
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="area">Where to read:  e.g.  DB1  or M or...</param>
 /// <param name="offset">offset in bytes, if you address booleans, you have to pass the address in bits (byteoffset * 8 + bitoffset)</param>
 /// <param name="length">The number of items to read</param>
 /// <returns></returns>
 internal static ReadItem CreateChild(ReadItem item, int offset, ushort length)
 {
     return(new ReadItem
     {
         Area = item.Area,
         DbNumber = item.DbNumber,
         Offset = offset,
         NumberOfItems = length,
         VarType = item.VarType,
         ResultType = item.ResultType,
         Parent = item,
         ElementSize = item.ElementSize,
         Encoding = item.Encoding
     });
 }
Exemplo n.º 8
0
        public static void EnsureSupportedType(ReadItem item)
        {
            if (item.ResultType == typeof(byte) || item.ResultType == typeof(byte[]) || item.ResultType == typeof(List <byte>) || item.ResultType == typeof(Memory <byte>) ||
                item.ResultType == typeof(bool) ||
                item.ResultType == typeof(string) ||
                item.ResultType == typeof(char) || item.ResultType == typeof(char[]) || item.ResultType == typeof(List <char>) ||
                item.ResultType == typeof(short) || item.ResultType == typeof(short[]) || item.ResultType == typeof(List <short>) ||
                item.ResultType == typeof(ushort) || item.ResultType == typeof(ushort[]) || item.ResultType == typeof(List <ushort>) ||
                item.ResultType == typeof(int) || item.ResultType == typeof(int[]) || item.ResultType == typeof(List <int>) ||
                item.ResultType == typeof(uint) || item.ResultType == typeof(uint[]) || item.ResultType == typeof(List <uint>) ||
                item.ResultType == typeof(ulong) || item.ResultType == typeof(ulong[]) || item.ResultType == typeof(List <ulong>) ||
                item.ResultType == typeof(long) || item.ResultType == typeof(long[]) || item.ResultType == typeof(List <long>) ||
                item.ResultType == typeof(float) || item.ResultType == typeof(float[]) || item.ResultType == typeof(List <float>) ||
                item.ResultType == typeof(sbyte))
            {
                return;
            }

            ThrowHelper.ThrowTypeNotSupportedException(item.ResultType);
        }
Exemplo n.º 9
0
 internal DataValue(ReadItem meta, S7DataItemSpecification data)
 {
     _meta       = meta;
     _returnCode = data.ReturnCode;
     _data       = data.Data;
 }
Exemplo n.º 10
0
 internal DataValue(ReadItem meta, S7DataItemSpecification data)
 {
     _meta      = meta;
     ReturnCode = (ItemResponseRetValue)data.ReturnCode;
     Data       = data.Data;
 }