示例#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);
        }
示例#2
0
        public static WriteItem From(this ReadItem ri, Memory <byte> data)
        {
            var result = ri.Clone();

            result.Data = data;
            return(result);
        }
示例#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);
        }
示例#4
0
 private ReadItem RegisteredOrGiven(string tag)
 {
     if (_registeredTags.TryGetValue(tag, out var nodeId))
     {
         return(nodeId);
     }
     return(ReadItem.CreateFromTag(tag));
 }
示例#5
0
文件: ReadItem.cs 项目: lanicon/dacs7
 /// <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
     });
 }
示例#6
0
文件: ReadItem.cs 项目: lanicon/dacs7
 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();
 }
示例#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
     });
 }
示例#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);
        }
示例#9
0
 internal DataValue(ReadItem meta, S7DataItemSpecification data)
 {
     _meta       = meta;
     _returnCode = data.ReturnCode;
     _data       = data.Data;
 }
示例#10
0
 internal DataValue(ReadItem meta, S7DataItemSpecification data)
 {
     _meta      = meta;
     ReturnCode = (ItemResponseRetValue)data.ReturnCode;
     Data       = data.Data;
 }