Esempio n. 1
0
        /// <summary>
        ///  Helper function reading holding 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 ReadingHoldingRegisters(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.ReadHexString(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.ReadString(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.ReadBits(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.ReadBytes(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.ReadBytes(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.ReadShort(slave, offset);
                        console.Out.WriteLine($"Value of single short = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} shorts from offset = {offset}");
                        }
                        short[] values = client.ReadShortArray(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.ReadUShort(slave, offset);
                        console.Out.WriteLine($"Value of single ushort = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} ushorts from offset = {offset}");
                        }
                        ushort[] values = client.ReadUShortArray(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 integer from offset = {offset}");
                        }
                        int value = client.ReadInt32(slave, offset);
                        console.Out.WriteLine($"Value of single integer = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number}  integers from offset = {offset}");
                        }
                        int[] values = client.ReadInt32Array(slave, offset, number);

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

                    break;
                }

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

                        for (int index = 0; index < values.Length; ++index)
                        {
                            console.Out.WriteLine($"Value of unsigned integer 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.ReadFloat(slave, offset);
                        console.Out.WriteLine($"Value of single float = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} floats from offset = {offset}");
                        }
                        float[] values = client.ReadFloatArray(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.ReadDouble(slave, offset);
                        console.Out.WriteLine($"Value of single double = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} doubles from offset = {offset}");
                        }
                        double[] values = client.ReadDoubleArray(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.ReadLong(slave, offset);
                        console.Out.WriteLine($"Value of single long = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} longs from offset = {offset}");
                        }
                        long[] values = client.ReadLongArray(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 ulong from offset = {offset}");
                        }
                        ulong value = client.ReadULong(slave, offset);
                        console.Out.WriteLine($"Value of single ulong = {value}");
                    }
                    else
                    {
                        if (header)
                        {
                            console.Out.WriteLine($"Reading {number} ulongs from offset = {offset}");
                        }
                        ulong[] values = client.ReadULongArray(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 holding register[{offset}]");
                    }
                    ushort[] values = client.ReadHoldingRegisters(slave, offset, number);
                    if (hex)
                    {
                        console.Out.WriteLine($"Value of holding register[{offset}] = {values[0]:X04}");
                    }
                    else
                    {
                        console.Out.WriteLine($"Value of holding register[{offset}] = {values[0]}");
                    }
                }
                else
                {
                    if (header)
                    {
                        console.Out.WriteLine($"Reading {number} holding registers starting at {offset}");
                    }
                    ushort[] values = client.ReadHoldingRegisters(slave, offset, number);

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