示例#1
0
        /// <summary>
        ///  Helper function reading input registers.
        /// </summary>
        /// <param name="console"></param>
        /// <param name="client"></param>
        /// <param name="slave"></param>
        /// <param name="number"></param>
        /// <param name="offset"></param>
        /// <param name="type"></param>
        /// <param name="hex"></param>
        /// <param name="header"></param>
        public static void ReadingInputRegisters(IConsole console,
                                                 IModbusClient client,
                                                 byte slave,
                                                 ushort number,
                                                 ushort offset,
                                                 string type,
                                                 bool hex,
                                                 bool header = true)
        {
            if (!string.IsNullOrEmpty(type))
            {
                switch (type.ToLowerInvariant())
                {
                case "string":
                {
                    if (hex)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a HEX string from offset = {offset}");
                        }
                        string value = client.ReadOnlyHexString(slave, offset, number);
                        console.Out.WriteLine($"Value of HEX string = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading an ASCII string from offset = {offset}");
                        }
                        string value = client.ReadOnlyString(slave, offset, number);
                        console.Out.WriteLine($"Value of ASCII string = {value}");
                    }

                    break;
                }

                case "bits":
                {
                    if (header)
                    {
                        console.Out.WriteLine($"Reading a 16 bit array from offset = {offset}");
                    }
                    BitArray value = client.ReadOnlyBits(slave, offset);
                    console.Out.WriteLine($"Value of 16 bit array = {value.ToDigitString()}");

                    break;
                }

                case "byte":
                {
                    if (number == 1)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a single byte from offset = {offset}");
                        }
                        byte[] values = client.ReadOnlyBytes(slave, offset, number);
                        console.Out.WriteLine($"Value of single byte = {values[0]}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} bytes from offset = {offset}");
                        }
                        byte[] values = client.ReadOnlyBytes(slave, offset, number);

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of byte array[{index}] = {values[index]}");
                        }
                    }

                    break;
                }

                case "short":
                {
                    if (number == 1)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a single short from offset = {offset}");
                        }
                        short value = client.ReadOnlyShort(slave, offset);
                        console.Out.WriteLine($"Value of single short = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} short values from offset = {offset}");
                        }
                        short[] values = client.ReadOnlyShortArray(slave, offset, number);

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of short array[{index}] = {values[index]}");
                        }
                    }

                    break;
                }

                case "ushort":
                {
                    if (number == 1)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a single ushort from offset = {offset}");
                        }
                        ushort value = client.ReadOnlyUShort(slave, offset);
                        console.Out.WriteLine($"Value of single ushort = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} ushort values from offset = {offset}");
                        }
                        ushort[] values = client.ReadOnlyUShortArray(slave, offset, number);

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of ushort array[{index}] = {values[index]}");
                        }
                    }

                    break;
                }

                case "int":
                {
                    if (number == 1)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a single int from offset = {offset}");
                        }
                        int value = client.ReadOnlyInt32(slave, offset);
                        console.Out.WriteLine($"Value of single integer = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} int values from offset = {offset}");
                        }
                        int[] values = client.ReadOnlyInt32Array(slave, offset, number);

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of int array[{index}] = {values[index]}");
                        }
                    }

                    break;
                }

                case "uint":
                {
                    if (number == 1)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a single unsigned int from offset = {offset}");
                        }
                        uint value = client.ReadOnlyUInt32(slave, offset);
                        console.Out.WriteLine($"Value of single unsigned int = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} unsigned int values from offset = {offset}");
                        }
                        uint[] values = client.ReadOnlyUInt32Array(slave, offset, number);

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of unsigned int array[{index}] = {values[index]}");
                        }
                    }

                    break;
                }

                case "float":
                {
                    if (number == 1)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a single float from offset = {offset}");
                        }
                        float value = client.ReadOnlyFloat(slave, offset);
                        console.Out.WriteLine($"Value of single float = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} float values from offset = {offset}");
                        }
                        float[] values = client.ReadOnlyFloatArray(slave, offset, number);

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of float array[{index}] = {values[index]}");
                        }
                    }

                    break;
                }

                case "double":
                {
                    if (number == 1)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a single double from offset = {offset}");
                        }
                        double value = client.ReadOnlyDouble(slave, offset);
                        console.Out.WriteLine($"Value of single double = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} double values from offset = {offset}");
                        }
                        double[] values = client.ReadOnlyDoubleArray(slave, offset, number);

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of double array[{index}] = {values[index]}");
                        }
                    }

                    break;
                }

                case "long":
                {
                    if (number == 1)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a single long from offset = {offset}");
                        }
                        long value = client.ReadOnlyLong(slave, offset);
                        console.Out.WriteLine($"Value of single long = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} long values from offset = {offset}");
                        }
                        long[] values = client.ReadOnlyLongArray(slave, offset, number);

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of long array[{index}] = {values[index]}");
                        }
                    }

                    break;
                }

                case "ulong":
                {
                    if (number == 1)
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading a single unsigned long from offset = {offset}");
                        }
                        ulong value = client.ReadOnlyULong(slave, offset);
                        console.Out.WriteLine($"Value of single ulong = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} unsigned long values from offset = {offset}");
                        }
                        ulong[] values = client.ReadOnlyULongArray(slave, offset, number);

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of ulong array[{index}] = {values[index]}");
                        }
                    }

                    break;
                }
                }
            }
            else
            {
                if (number == 1)
                {
                    if (header)
                    {
                        console.Out.WriteLine($"Reading a input register[{offset}]");
                    }
                    ushort[] values = client.ReadInputRegisters(slave, offset, number);
                    if (hex)
                    {
                        console.Out.WriteLine($"Value of input register[{offset}] = {values[0]:X2}");
                    }
                    else
                    {
                        console.Out.WriteLine($"Value of input register[{offset}] = {values[0]}");
                    }
                }
                else
                {
                    if (header)
                    {
                        console.Out.WriteLine($"Reading {number} input registers starting at {offset}");
                    }
                    ushort[] values = client.ReadInputRegisters(slave, offset, number);

                    for (int index = 0; index < values.Length; ++index)
                    {
                        if (hex)
                        {
                            console.Out.WriteLine($"Value of input register[{offset + index}] = {values[index]:X2}");
                        }
                        else
                        {
                            console.Out.WriteLine($"Value of input register[{offset + index}] = {values[index]}");
                        }
                    }
                }
            }
        }