/// <summary> /// Converts a S7 DWord to double /// </summary> public static double FromDWord(UInt32 value) { byte[] b = DWord.ToByteArray(value); double d = FromByteArray(b); return(d); }
/// <summary> /// Converts a S7 DWord to float /// </summary> public static float FromDWord(UInt32 value) { byte[] b = DWord.ToByteArray(value); float d = FromByteArray(b); return(d); }
private static double SetBytesFromProperty(object propertyValue, byte[] bytes, double numBytes) { int bytePos = 0; int bitPos = 0; byte[] bytes2 = null; switch (propertyValue.GetType().Name) { case "Boolean": // get the value bytePos = (int)Math.Floor(numBytes); bitPos = (int)((numBytes - (double)bytePos) / 0.125); if ((bool)propertyValue) { 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)propertyValue; numBytes++; break; case "Int16": bytes2 = Int.ToByteArray((Int16)propertyValue); break; case "UInt16": bytes2 = Word.ToByteArray((UInt16)propertyValue); break; case "Int32": bytes2 = DInt.ToByteArray((Int32)propertyValue); break; case "UInt32": bytes2 = DWord.ToByteArray((UInt32)propertyValue); break; case "Double": bytes2 = Double.ToByteArray((double)propertyValue); break; case "Single": bytes2 = Single.ToByteArray((float)propertyValue); break; default: numBytes = ToBytes(propertyValue, bytes, numBytes); 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(numBytes); }
private static object GetPropertyValue(Type propertyType, byte[] bytes, ref double numBytes) { object value = null; switch (propertyType.Name) { case "Boolean": // get the value int bytePos = (int)Math.Floor(numBytes); int bitPos = (int)((numBytes - (double)bytePos) / 0.125); if ((bytes[bytePos] & (int)Math.Pow(2, bitPos)) != 0) { value = true; } else { value = false; } numBytes += 0.125; break; case "Byte": numBytes = Math.Ceiling(numBytes); value = (byte)(bytes[(int)numBytes]); numBytes++; break; case "Int16": numBytes = Math.Ceiling(numBytes); if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) { numBytes++; } // hier auswerten ushort source = Word.FromBytes(bytes[(int)numBytes + 1], bytes[(int)numBytes]); value = source.ConvertToShort(); numBytes += 2; break; case "UInt16": numBytes = Math.Ceiling(numBytes); if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) { numBytes++; } // hier auswerten value = Word.FromBytes(bytes[(int)numBytes + 1], bytes[(int)numBytes]); numBytes += 2; break; case "Int32": numBytes = Math.Ceiling(numBytes); if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) { numBytes++; } // hier auswerten uint sourceUInt = DWord.FromBytes(bytes[(int)numBytes + 3], bytes[(int)numBytes + 2], bytes[(int)numBytes + 1], bytes[(int)numBytes + 0]); value = sourceUInt.ConvertToInt(); numBytes += 4; break; case "UInt32": numBytes = Math.Ceiling(numBytes); if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) { numBytes++; } // hier auswerten value = DWord.FromBytes( bytes[(int)numBytes], bytes[(int)numBytes + 1], bytes[(int)numBytes + 2], bytes[(int)numBytes + 3]); numBytes += 4; break; case "Double": numBytes = Math.Ceiling(numBytes); if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) { numBytes++; } // hier auswerten value = Double.FromByteArray( new byte[] { bytes[(int)numBytes], bytes[(int)numBytes + 1], bytes[(int)numBytes + 2], bytes[(int)numBytes + 3] }); numBytes += 4; break; case "Single": numBytes = Math.Ceiling(numBytes); if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) { numBytes++; } // hier auswerten value = Single.FromByteArray( new byte[] { bytes[(int)numBytes], bytes[(int)numBytes + 1], bytes[(int)numBytes + 2], bytes[(int)numBytes + 3] }); numBytes += 4; break; default: var propClass = Activator.CreateInstance(propertyType); numBytes = FromBytes(propClass, bytes, numBytes); value = propClass; break; } return(value); }