Esempio n. 1
0
 public static bool TryConvert(object source, out LTIME time)
 {
     time = null;
     if (source.GetType() == typeof(long))
     {
         time = CreateLTime((ulong)source);
     }
     else if (source.GetType() == typeof(ulong))
     {
         time = CreateLTime((ulong)source);
     }
     else if (source.GetType() == typeof(uint))
     {
         time = CreateLTime((ulong)source);
     }
     else if (source.GetType() == typeof(int))
     {
         time = CreateLTime((ulong)source);
     }
     else if (source.GetType() == typeof(TimeSpan))
     {
         time = new LTIME((TimeSpan)source);
     }
     else if (source.GetType() == typeof(string))
     {
         TryParse((string)source, out time);
     }
     return(time != null);
 }
Esempio n. 2
0
        public static bool TryParse(string s, out LTIME time)
        {
            TimeSpan span;
            ulong    nanoseconds = 0UL;

            if (TryParseToNanoseconds(s, out nanoseconds))
            {
                time = new LTIME(nanoseconds);
                return(true);
            }
            if (PlcOpenDateConverterBase.TryParseTimeSpan(s, out span))
            {
                time = new LTIME(span);
                return(true);
            }
            time = null;
            return(false);
        }
Esempio n. 3
0
        public AdsErrorCode ReadAny(uint indexGroup, uint indexOffset, Type type, bool throwAdsException, out object value)
        {
            AdsErrorCode code;

            if (!type.IsPrimitive)
            {
                if (type == typeof(string))
                {
                    throw new ArgumentException("Use overload ReadAnyString(uint indexGroup, uint indexOffset,  Type type, int characters) for strings.", "type");
                }
                if (type.IsArray)
                {
                    throw new ArgumentException("Use overload ReadAny(uint indexGroup, uint indexOffset,  Type type, int[] args) for arrays.", "type");
                }
                if (type == typeof(TimeSpan))
                {
                    uint milliseconds = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = PlcOpenTimeConverter.MillisecondsToTimeSpan(milliseconds);
                }
                else if (type == typeof(DateTime))
                {
                    uint dateValue = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = PlcOpenDateConverterBase.ToDateTime(dateValue);
                }
                else if (type == typeof(TIME))
                {
                    uint timeValue = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = new TIME(timeValue);
                }
                else if (type == typeof(LTIME))
                {
                    ulong timeValue = base.ReadUInt64(indexGroup, indexOffset, throwAdsException, out code);
                    value = new LTIME(timeValue);
                }
                else if (type == typeof(TOD))
                {
                    uint time = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = new TOD(time);
                }
                else if (type == typeof(DATE))
                {
                    uint dateValue = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = new DATE(dateValue);
                }
                else if (type != typeof(DT))
                {
                    value = base.ReadStruct(indexGroup, indexOffset, type, throwAdsException, out code);
                }
                else
                {
                    uint dateValue = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = new DT(dateValue);
                }
            }
            else if (type == typeof(bool))
            {
                value = base.ReadBoolean(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(int))
            {
                value = base.ReadInt32(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(short))
            {
                value = base.ReadInt16(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(byte))
            {
                value = base.ReadUInt8(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(float))
            {
                value = base.ReadReal32(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(double))
            {
                value = base.ReadReal64(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(long))
            {
                value = base.ReadInt64(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(uint))
            {
                value = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(ushort))
            {
                value = base.ReadUInt16(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(ulong))
            {
                value = base.ReadUInt64(indexGroup, indexOffset, throwAdsException, out code);
            }
            else
            {
                if (type != typeof(sbyte))
                {
                    throw new ArgumentException("Unable to marshal type.", "type");
                }
                value = base.ReadInt8(indexGroup, indexOffset, throwAdsException, out code);
            }
            return(code);
        }
Esempio n. 4
0
 public static byte[] GetBytes(LTIME time) =>
 BitConverter.GetBytes(time.Ticks);