public static string MergeComplexValue(byte[] values, ModbusValueType valueType, bool endianSwap = false, bool midEndianSwap = false)
        {
            //In case of adding new value type, modify endian handling logic
            if (midEndianSwap && (valueType == ModbusValueType.Float || valueType == ModbusValueType.Int32))
            {
                //Mid endian swap logic
                Array.Reverse(values, 0, 2);
                Array.Reverse(values, 2, 2);
            }
            if (endianSwap)
            {
                Array.Reverse(values);
            }

            switch (valueType)
            {
            case ModbusValueType.Float:
                return(String.Format("{0:F3}", BitConverter.ToSingle(values)));

            case ModbusValueType.Int32:
                return(BitConverter.ToInt32(values).ToString());

            default:
                return("Uknown value type");
            }
        }
Пример #2
0
        Control GetControlByType(ModbusValueType valueType)
        {
            switch (valueType)
            {
            case ModbusValueType.Bool:
                return(new CheckBox {
                });

            case ModbusValueType.Float:
                return(new NumericUpDown
                {
                    Maximum = decimal.MaxValue,
                    Minimum = decimal.MinValue,
                    DecimalPlaces = 6,
                });

            case ModbusValueType.Int16:
                return(new NumericUpDown {
                });

            case ModbusValueType.Int32:
                return(new NumericUpDown {
                });

            default:
                return(new TextBox {
                });
            }
        }
Пример #3
0
        public static int GetByteLength(ModbusValueType modbusValueType)
        {
            switch (modbusValueType)
            {
            case ModbusValueType.Bool:
                return(2);

            case ModbusValueType.Float:
                return(4);

            case ModbusValueType.Int16:
                return(2);

            case ModbusValueType.Int32:
                return(4);

            default:
                return(2);
            }
        }
        private static Int16[] SplitComplexValue(string value, ModbusValueType valueType)
        {
            byte[] bf;
            //float f = float.Parse(value);
            switch (valueType)
            {
            case ModbusValueType.Float:
                bf = BitConverter.GetBytes(float.Parse(value));
                break;

            case ModbusValueType.Int32:
                bf = BitConverter.GetBytes(Convert.ToInt32(value));
                break;

            default:
                return(null);
            }

            //At this point all complex values used need only 2 registries (2 Int16)
            var p1 = BitConverter.ToInt16(bf.SubArray(0, 2), 0);
            var p2 = BitConverter.ToInt16(bf.SubArray(2, 2), 0);

            return(new Int16[] { p1, p2 });
        }
Пример #5
0
        private static bool TryParseType(string src, out ModbusValueType type)
        {
            type = ModbusValueType.Int16;

            var a = src.Trim().ToLower();

            if (a.StartsWith("array"))
            {
                if (a.Contains(" "))
                {
                    a = a.Substring(a.LastIndexOf(' '));
                }
                else
                {
                    return(false);
                }
            }

            if (Enum.TryParse(a, true, out ModbusValueType parsedType))
            {
                type = parsedType;
                return(true);
            }


            // экзотика
            switch (a)
            {
            case "bit":
                type = ModbusValueType.Bool;
                break;

            case "float":
            case "real":
                type = ModbusValueType.Float;
                break;

            case "sint":
            case "byte":
            case "word":
            case "int":
                type = ModbusValueType.Int16;
                break;

            case "usint":
            case "uint":
                type = ModbusValueType.Int16;
                break;

            case "dword":
            case "dint":
                type = ModbusValueType.Int32;
                break;

            case "lword":
            case "lint":
            case "ulint":
            case "lreal":
            default:
                return(false);
            }

            return(true);
        }
Пример #6
0
        private static bool TryParseVal(string src, ModbusValueType type, out object val)
        {
            val = null;

            var defValStr = src
                            .Trim()
                            .ToLower()
                            .Replace(',', '.')
                            .Replace("_", "");

            if (defValStr == "")
            {
                switch (type)
                {
                case ModbusValueType.Bool:
                    val = false;
                    break;

                case ModbusValueType.Float:
                    val = 0.0f;
                    break;

                case ModbusValueType.Int16:
                    val = (Int16)0;
                    break;

                case ModbusValueType.Int32:
                    val = (Int32)0;
                    break;
                }
                return(false);
            }

            bool success;

            switch (type)
            {
            case ModbusValueType.Bool:
                val     = defValStr != "0" && defValStr != "false";
                success = true;
                break;

            case ModbusValueType.Float:
                success = float.TryParse(defValStr, out float valf);
                if (success)
                {
                    val = valf;
                }
                break;

            case ModbusValueType.Int16:
                success = Int16.TryParse(defValStr, out Int16 vals);
                if (success)
                {
                    val = vals;
                }
                break;

            case ModbusValueType.Int32:
                success = Int32.TryParse(defValStr, out Int32 vali);
                if (success)
                {
                    val = vali;
                }
                break;

            default:
                return(false);
            }

            return(success);
        }
 internal static ushort GetCountForValueType(ModbusValueType valueType)
 {
     return(RegistryCountsForValue.GetValueOrDefault(valueType));
 }
 public static Int16[] SplitComplexValue(float value, ModbusValueType valueType)
 {
     throw new NotImplementedException();
 }