Ejemplo n.º 1
0
        /// <summary>
        /// Create a read item
        /// </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>
        /// <param name="unicode">IF the given type is a string or char you can also specifiy if its the unicode variant of them (this means 2byte per sign)</param>
        /// <returns></returns>
        public static ReadItem Create <T>(string area, int offset, ushort length, PlcEncoding encoding = PlcEncoding.Windows1252)
        {
            if (!TagParser.TryDetectArea(area.AsSpan(), out var selector, out var db))
            {
                ThrowHelper.ThrowInvalidAreaException(area);
            }

            return(SetupTypes <T>(new ReadItem
            {
                Area = selector,
                DbNumber = db,
                Offset = offset,
                NumberOfItems = length,
                Encoding = encoding
            }));
        }
Ejemplo n.º 2
0
        public static ushort GetElementSize(PlcArea area, Type t, PlcEncoding encoding)
        {
            if (area == PlcArea.CT || area == PlcArea.TM)
            {
                return(2);
            }

            if (t.IsArray)
            {
                t = t.GetElementType();
            }

            if (t == typeof(byte) || t == typeof(Memory <byte>))
            {
                return(1);
            }
            if (t == typeof(bool))
            {
                return(1);
            }
            if (t == typeof(char) || t == typeof(string))
            {
                return((ushort)(encoding == PlcEncoding.Unicode ? 2 : 1));
            }
            if (t == typeof(short) || t == typeof(ushort))
            {
                return(2);
            }
            if (t == typeof(int) || t == typeof(uint))
            {
                return(4);
            }
            if (t == typeof(ulong) || t == typeof(long))
            {
                return(8);
            }
            if (t == typeof(float))
            {
                return(4);
            }
            if (t == typeof(sbyte))
            {
                return(1);
            }

            return(1);
        }
Ejemplo n.º 3
0
        private static bool TryDetectTypes(ReadOnlySpan <char> type, ref ushort length, ref int offset, out Type vtype, out Type rType, out PlcEncoding encoding)
        {
            vtype    = typeof(object);
            rType    = typeof(object);
            encoding = PlcEncoding.Ascii;

            switch (type[0])
            {
            case 'b':
                vtype = typeof(byte);
                rType = length > 1 ? typeof(byte[]) : vtype;
                return(true);

            case 'c':
                vtype = typeof(char);
                rType = length > 1 ? typeof(char[]) : vtype;
                return(true);

            case 'w' when type.Length > 1:
            {
                switch (type[1])
                {
                case 's':
                    vtype    = rType = typeof(string);
                    encoding = PlcEncoding.Unicode;
                    break;

                case 'c':
                    vtype    = typeof(char);
                    rType    = typeof(string);
                    encoding = PlcEncoding.Unicode;
                    break;
                }
                break;
            }

            case 'w':
                vtype = typeof(ushort);
                rType = length > 1 ? typeof(ushort[]) : vtype;
                return(true);

            case 'l' when type.Length > 1 && type[1] == 'i':
                vtype = typeof(sbyte);
                rType = length > 1 ? typeof(sbyte[]) : vtype;
                break;

            case 'i':
                vtype = typeof(short);
                rType = length > 1 ? typeof(short[]) : vtype;
                return(true);

            case 'd' when type.Length > 1 && type[1] == 'w':
                vtype = typeof(uint);
                rType = length > 1 ? typeof(uint[]) : vtype;
                return(true);

            case 'd' when type.Length > 1 && type[1] == 'i':
                vtype = typeof(int);
                rType = length > 1 ? typeof(int[]) : vtype;
                return(true);

            case 'r':
                vtype = typeof(float);
                rType = length > 1 ? typeof(float[]) : vtype;
                return(true);

            case 's' when type.Length > 1 && type[1] == 'i':
                vtype = typeof(sbyte);
                rType = length > 1 ? typeof(sbyte[]) : vtype;
                break;

            case 's':
                vtype = rType = typeof(string);
                return(true);

            case 'x' when type.Length > 1:
                vtype = rType = typeof(bool);
                rType = length > 1 ? typeof(bool[]) : vtype;
#if SPANSUPPORT
                offset = ((offset * 8) + int.Parse(type.Slice(1)));
#else
                offset = ((offset * 8) + SpanToInt(type.Slice(1)));
#endif


                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Create a <see cref="WriteItem"/> from the given parameter.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="area">Area in the plc (e.g.  db1, i or e, m, q or a, t, c or z </param>
 /// <param name="offset">offset in byte</param>
 /// <param name="data">data to write</param>
 /// <param name="encoding">write unicode data (only TIA  WString and WChar</param>
 /// <returns><see cref="WriteItem"/></returns>
 public static WriteItem Create <T>(string area, int offset, T data, PlcEncoding encoding = PlcEncoding.Ascii)
 => Create(area, offset, GetDataItemCount(data), data, encoding);
Ejemplo n.º 5
0
        /// <summary>
        /// Create a <see cref="WriteItem"/> from the given parameter.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="area">Area in the plc (e.g.  db1, i or e, m, q or a, t, c or z </param>
        /// <param name="offset">offset in byte</param>
        /// <param name="length">number of items</param>
        /// <param name="data">data to write</param>
        /// <param name="encoding">write unicode data (only TIA  WString and WChar</param>
        /// <returns><see cref="WriteItem"/></returns>
        public static WriteItem Create <T>(string area, int offset, ushort length, T data, PlcEncoding encoding = PlcEncoding.Ascii)
        {
            var result = Create <T>(area, offset, length, encoding).Clone();

            result.Data = result.ConvertDataToMemory(data);
            return(NormalizeAndValidate(result));
        }
Ejemplo n.º 6
0
 public static ReadItem Create <T>(string area, int offset, PlcEncoding encoding = PlcEncoding.Windows1252)
 => Create <T>(area, offset, 1, encoding);
Ejemplo n.º 7
0
 public static ReadItem Create <T>(string area, int offset, PlcEncoding encoding = PlcEncoding.Ascii)
 => Create <T>(area, offset, 1, encoding);