コード例 #1
0
        public static byte[] GetBytesFromAngleString(this WatchVariable watchVar, ProcessStream stream, uint offset, string value, WatchVariableControl.AngleViewModeType viewMode)
        {
            if (watchVar.Type != typeof(UInt32) && watchVar.Type != typeof(UInt16) &&
                watchVar.Type != typeof(Int32) && watchVar.Type != typeof(Int16))
            {
                return(null);
            }

            UInt32 writeValue = 0;

            // Print hex
            if (ParsingUtilities.IsHex(value))
            {
                ParsingUtilities.TryParseHex(value, out writeValue);
            }
            else
            {
                switch (viewMode)
                {
                case WatchVariableControl.AngleViewModeType.Signed:
                case WatchVariableControl.AngleViewModeType.Unsigned:
                case WatchVariableControl.AngleViewModeType.Recommended:
                    int tempValue;
                    if (int.TryParse(value, out tempValue))
                    {
                        writeValue = (uint)tempValue;
                    }
                    else if (!uint.TryParse(value, out writeValue))
                    {
                        return(null);
                    }
                    break;


                case WatchVariableControl.AngleViewModeType.Degrees:
                    double degValue;
                    if (!double.TryParse(value, out degValue))
                    {
                        return(null);
                    }
                    writeValue = (UInt16)(degValue / (360d / 65536));
                    break;

                case WatchVariableControl.AngleViewModeType.Radians:
                    double radValue;
                    if (!double.TryParse(value, out radValue))
                    {
                        return(null);
                    }
                    writeValue = (UInt16)(radValue / (2 * Math.PI / 65536));
                    break;
                }
            }

            var byteCount = WatchVariable.TypeSize[watchVar.Type];

            return(BitConverter.GetBytes(writeValue).Take(byteCount).ToArray());
        }
コード例 #2
0
        public static byte[] GetBytesFromString(this WatchVariable watchVar, ProcessStream stream, uint offset, string value)
        {
            // Get dataBytes
            var byteCount = WatchVariable.TypeSize[watchVar.Type];
            var address   = watchVar.OtherOffset ? offset + watchVar.Address : watchVar.Address;
            var dataBytes = new byte[8];

            stream.ReadRam(address, byteCount, watchVar.AbsoluteAddressing).CopyTo(dataBytes, 0);
            UInt64 oldValue = BitConverter.ToUInt64(dataBytes, 0);
            UInt64 newValue;

            // Handle hex variable
            if (ParsingUtilities.IsHex(value))
            {
                if (!ParsingUtilities.TryParseExtHex(value, out newValue))
                {
                    return(null);
                }
            }
            // Handle floats
            else if (watchVar.Type == typeof(float))
            {
                float newFloatValue;
                if (!float.TryParse(value, out newFloatValue))
                {
                    return(null);
                }

                // Get bytes
                newValue = BitConverter.ToUInt32(BitConverter.GetBytes(newFloatValue), 0);
            }
            else if (watchVar.Type == typeof(double))
            {
                double newFloatValue;
                if (double.TryParse(value, out newFloatValue))
                {
                    return(null);
                }

                // Get bytes
                newValue = BitConverter.ToUInt64(BitConverter.GetBytes(newFloatValue), 0);
            }
            else if (watchVar.Type == typeof(UInt64))
            {
                if (!UInt64.TryParse(value, out newValue))
                {
                    Int64 newValueInt;
                    if (!Int64.TryParse(value, out newValueInt))
                    {
                        return(null);
                    }

                    newValue = (UInt64)newValueInt;
                }
            }
            else
            {
                Int64 tempInt;
                if (!Int64.TryParse(value, out tempInt))
                {
                    return(null);
                }
                newValue = (UInt64)tempInt;
            }

            // Apply mask
            if (watchVar.Mask.HasValue)
            {
                newValue = (newValue & watchVar.Mask.Value) | ((~watchVar.Mask.Value) & oldValue);
            }

            var writeBytes = new byte[byteCount];
            var valueBytes = BitConverter.GetBytes(newValue);

            Array.Copy(valueBytes, 0, writeBytes, 0, byteCount);

            return(writeBytes);
        }
コード例 #3
0
        public static byte[] GetBytesFromString(this WatchVariable watchVar, uint offset, string value)
        {
            // Get dataBytes
            var address   = watchVar.HasAdditiveOffset ? offset + watchVar.Address : watchVar.Address;
            var dataBytes = new byte[8];

            Config.Stream.ReadRamLittleEndian(new UIntPtr(address), watchVar.ByteCount, watchVar.UseAbsoluteAddressing).CopyTo(dataBytes, 0);
            UInt64 oldValue = BitConverter.ToUInt64(dataBytes, 0);
            UInt64 newValue;


            // Handle object values
            uint?objectAddress;

            if (watchVar.IsObject && (objectAddress = ManagerContext.Current.ObjectSlotManager.GetSlotAddressFromName(value)).HasValue)
            {
                newValue = objectAddress.Value;
            }
            else
            // Handle hex variable
            if (ParsingUtilities.IsHex(value))
            {
                if (!ParsingUtilities.TryParseExtHex(value, out newValue))
                {
                    return(null);
                }
            }
            // Handle floats
            else if (watchVar.Type == typeof(float))
            {
                float newFloatValue;
                if (!float.TryParse(value, out newFloatValue))
                {
                    return(null);
                }

                // Get bytes
                newValue = BitConverter.ToUInt32(BitConverter.GetBytes(newFloatValue), 0);
            }
            else if (watchVar.Type == typeof(double))
            {
                double newFloatValue;
                if (double.TryParse(value, out newFloatValue))
                {
                    return(null);
                }

                // Get bytes
                newValue = BitConverter.ToUInt64(BitConverter.GetBytes(newFloatValue), 0);
            }
            else if (watchVar.Type == typeof(UInt64))
            {
                if (!UInt64.TryParse(value, out newValue))
                {
                    Int64 newValueInt;
                    if (!Int64.TryParse(value, out newValueInt))
                    {
                        return(null);
                    }

                    newValue = (UInt64)newValueInt;
                }
            }
            else
            {
                Int64 tempInt;
                if (!Int64.TryParse(value, out tempInt))
                {
                    return(null);
                }
                newValue = (UInt64)tempInt;
            }

            // Apply mask
            if (watchVar.Mask.HasValue)
            {
                newValue = (newValue & watchVar.Mask.Value) | ((~watchVar.Mask.Value) & oldValue);
            }

            var writeBytes = new byte[watchVar.ByteCount];
            var valueBytes = BitConverter.GetBytes(newValue);

            Array.Copy(valueBytes, 0, writeBytes, 0, watchVar.ByteCount);

            return(writeBytes);
        }