Пример #1
0
        /// <summary>
        /// Converts a S7 DInt to double
        /// </summary>
        public static double FromDWord(Int32 value)
        {
            byte[] b = DInt.ToByteArray(value);
            double d = FromByteArray(b);

            return(d);
        }
Пример #2
0
        /// <summary>
        /// Creates a byte array depending on the struct type.
        /// </summary>
        /// <param name="structValue">The struct object</param>
        /// <returns>A byte array or null if fails.</returns>
        public static byte[] ToBytes(object structValue)
        {
            Type type = structValue.GetType();

            int size = Struct.GetStructSize(type);

            byte[] bytes  = new byte[size];
            byte[] bytes2 = null;

            int    bytePos  = 0;
            int    bitPos   = 0;
            double numBytes = 0.0;

            System.Reflection.FieldInfo[] infos = type.GetFields();
            foreach (System.Reflection.FieldInfo info in infos)
            {
                bytes2 = null;
                switch (info.FieldType.Name)
                {
                case "Boolean":
                    // get the value
                    bytePos = (int)Math.Floor(numBytes);
                    bitPos  = (int)((numBytes - (double)bytePos) / 0.125);
                    if ((bool)info.GetValue(structValue))
                    {
                        bytes[bytePos] |= (byte)Math.Pow(2, bitPos);                // is true
                    }
                    else
                    {
                        bytes[bytePos] &= (byte)(~(byte)Math.Pow(2, bitPos));       // is false
                    }
                    numBytes += 0.125;
                    break;

                case "Byte":
                    numBytes       = (int)Math.Ceiling(numBytes);
                    bytePos        = (int)numBytes;
                    bytes[bytePos] = (byte)info.GetValue(structValue);
                    numBytes++;
                    break;

                case "Int16":
                    bytes2 = Int.ToByteArray((Int16)info.GetValue(structValue));
                    break;

                case "UInt16":
                    bytes2 = Word.ToByteArray((UInt16)info.GetValue(structValue));
                    break;

                case "Int32":
                    bytes2 = DInt.ToByteArray((Int32)info.GetValue(structValue));
                    break;

                case "UInt32":
                    bytes2 = DWord.ToByteArray((UInt32)info.GetValue(structValue));
                    break;

                case "Double":
                    bytes2 = Double.ToByteArray((double)info.GetValue(structValue));
                    break;
                }
                if (bytes2 != null)
                {
                    // add them
                    numBytes = Math.Ceiling(numBytes);
                    if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0)
                    {
                        numBytes++;
                    }
                    bytePos = (int)numBytes;
                    for (int bCnt = 0; bCnt < bytes2.Length; bCnt++)
                    {
                        bytes[bytePos + bCnt] = bytes2[bCnt];
                    }
                    numBytes += bytes2.Length;
                }
            }
            return(bytes);
        }
Пример #3
0
        /// <summary>
        /// Creates a byte array depending on the struct type.
        /// </summary>
        /// <param name="sourceClass">The struct object</param>
        /// <returns>A byte array or null if fails.</returns>
        public static byte[] ToBytes(object sourceClass)
        {
            Type type = sourceClass.GetType();

            int size = GetClassSize(type);

            byte[] bytes  = new byte[size];
            byte[] bytes2 = null;

            int    bytePos  = 0;
            int    bitPos   = 0;
            double numBytes = 0.0;

            var properties = sourceClass.GetType().GetProperties();

            foreach (var property in properties)
            {
                bytes2 = null;
                switch (property.PropertyType.Name)
                {
                case "Boolean":
                    // get the value
                    bytePos = (int)Math.Floor(numBytes);
                    bitPos  = (int)((numBytes - (double)bytePos) / 0.125);
                    if ((bool)property.GetValue(sourceClass, null))
                    {
                        bytes[bytePos] |= (byte)Math.Pow(2, bitPos);                // is true
                    }
                    else
                    {
                        bytes[bytePos] &= (byte)(~(byte)Math.Pow(2, bitPos));       // is false
                    }
                    numBytes += 0.125;
                    break;

                case "Byte":
                    numBytes       = (int)Math.Ceiling(numBytes);
                    bytePos        = (int)numBytes;
                    bytes[bytePos] = (byte)property.GetValue(sourceClass, null);
                    numBytes++;
                    break;

                case "Int16":
                    bytes2 = Int.ToByteArray((Int16)property.GetValue(sourceClass, null));
                    break;

                case "UInt16":
                    bytes2 = Word.ToByteArray((UInt16)property.GetValue(sourceClass, null));
                    break;

                case "Int32":
                    bytes2 = DInt.ToByteArray((Int32)property.GetValue(sourceClass, null));
                    break;

                case "UInt32":
                    bytes2 = DWord.ToByteArray((UInt32)property.GetValue(sourceClass, null));
                    break;

                case "Double":
                    bytes2 = Double.ToByteArray((double)property.GetValue(sourceClass, null));
                    break;
                }
                if (bytes2 != null)
                {
                    // add them
                    numBytes = Math.Ceiling(numBytes);
                    if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0)
                    {
                        numBytes++;
                    }
                    bytePos = (int)numBytes;
                    for (int bCnt = 0; bCnt < bytes2.Length; bCnt++)
                    {
                        bytes[bytePos + bCnt] = bytes2[bCnt];
                    }
                    numBytes += bytes2.Length;
                }
            }
            return(bytes);
        }