Ejemplo n.º 1
0
        private void button3_Click(object sender, EventArgs e)
        {
            byte.TryParse(txt_stationNumber.Text?.Trim(), out byte stationNumber);
            if (string.IsNullOrWhiteSpace(txt_address.Text))
            {
                MessageBox.Show("请输入地址");
                return;
            }
            dynamic result = null;

            try
            {
                var addressAndReadLength = txt_address.Text.Split('-');
                var addressAndReadNumber = txt_address.Text.Split(',', '、', ',');
                //批量读取
                if (addressAndReadLength.Length == 2)
                {
                    var    address    = int.Parse(addressAndReadLength[0]);
                    var    readNumber = ushort.Parse(addressAndReadLength[1]);
                    ushort bLength    = 1;
                    if (rd_bit.Checked || rd_discrete.Checked || rd_short.Checked || rd_ushort.Checked)
                    {
                        bLength = 1;
                    }
                    else if (rd_int.Checked || rd_uint.Checked || rd_float.Checked)
                    {
                        bLength = 2;
                    }
                    else if (rd_long.Checked || rd_ulong.Checked || rd_double.Checked)
                    {
                        bLength = 4;
                    }

                    var  readLength = Convert.ToUInt16(bLength * readNumber);
                    byte functionCode;
                    if (rd_bit.Checked)
                    {
                        functionCode = 1;
                    }
                    else if (rd_discrete.Checked)
                    {
                        functionCode = 2;
                    }
                    else
                    {
                        functionCode = 3;
                    }

                    result = client.Read(address.ToString(), stationNumber, functionCode, readLength: readLength);

                    if (result.IsSucceed)
                    {
                        AppendEmptyText();
                        byte[] rValue = result.Value;
                        rValue = rValue.Reverse().ToArray();
                        for (int i = 0; i < readNumber; i++)
                        {
                            var cAddress = (address + i * bLength).ToString();
                            if (rd_bit.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadCoil(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_discrete.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadDiscrete(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_short.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadInt16(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_ushort.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadUInt16(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_int.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadInt32(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_uint.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadUInt32(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_long.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadInt64(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_ulong.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadUInt64(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_float.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadFloat(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_double.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadDouble(address.ToString(), cAddress, rValue).Value}");
                            }
                        }
                    }
                    else
                    {
                        AppendText($"[读取 {txt_address.Text?.Trim()} 失败]:{result.Err}");
                    }
                }
                //批量读取
                else if (addressAndReadNumber.Length >= 2)
                {
                    DataTypeEnum datatype     = DataTypeEnum.None;
                    byte         functionCode = 3;
                    //线圈
                    if (rd_bit.Checked)
                    {
                        datatype     = DataTypeEnum.Bool;
                        functionCode = 1;
                    }
                    //离散
                    else if (rd_discrete.Checked)
                    {
                        datatype     = DataTypeEnum.Bool;
                        functionCode = 2;
                    }
                    else if (rd_short.Checked)
                    {
                        datatype = DataTypeEnum.Int16;
                    }
                    else if (rd_ushort.Checked)
                    {
                        datatype = DataTypeEnum.UInt16;
                    }
                    else if (rd_int.Checked)
                    {
                        datatype = DataTypeEnum.Int32;
                    }
                    else if (rd_uint.Checked)
                    {
                        datatype = DataTypeEnum.UInt32;
                    }
                    else if (rd_long.Checked)
                    {
                        datatype = DataTypeEnum.Int64;
                    }
                    else if (rd_ulong.Checked)
                    {
                        datatype = DataTypeEnum.UInt64;
                    }
                    else if (rd_float.Checked)
                    {
                        datatype = DataTypeEnum.Float;
                    }
                    else if (rd_double.Checked)
                    {
                        datatype = DataTypeEnum.Double;
                    }

                    List <ModbusInput> addresses = new List <ModbusInput>();
                    foreach (var item in addressAndReadNumber)
                    {
                        addresses.Add(new ModbusInput()
                        {
                            Address       = item,
                            DataType      = datatype,
                            FunctionCode  = functionCode,
                            StationNumber = stationNumber,
                        });
                    }

                    result = client.BatchRead(addresses);

                    if (result.IsSucceed)
                    {
                        AppendEmptyText();
                        foreach (var item in result.Value)
                        {
                            AppendText($"[读取 {item.Address} 成功]:{item.Value}\t\t耗时:{result.TimeConsuming}ms");
                        }
                    }
                    else
                    {
                        AppendText($"[读取 {txt_address.Text?.Trim()} 失败]:{result.Err}\t\t耗时:{result.TimeConsuming}ms");
                    }
                }
                //单个读取
                else
                {
                    if (rd_bit.Checked)
                    {
                        result = client.ReadCoil(txt_address.Text, stationNumber);
                    }
                    else if (rd_short.Checked)
                    {
                        result = client.ReadInt16(txt_address.Text, stationNumber);
                    }
                    else if (rd_ushort.Checked)
                    {
                        result = client.ReadUInt16(txt_address.Text, stationNumber);
                    }
                    else if (rd_int.Checked)
                    {
                        result = client.ReadInt32(txt_address.Text, stationNumber);
                    }
                    else if (rd_uint.Checked)
                    {
                        result = client.ReadUInt32(txt_address.Text, stationNumber);
                    }
                    else if (rd_long.Checked)
                    {
                        result = client.ReadInt64(txt_address.Text, stationNumber);
                    }
                    else if (rd_ulong.Checked)
                    {
                        result = client.ReadUInt64(txt_address.Text, stationNumber);
                    }
                    else if (rd_float.Checked)
                    {
                        result = client.ReadFloat(txt_address.Text, stationNumber);
                    }
                    else if (rd_double.Checked)
                    {
                        result = client.ReadDouble(txt_address.Text, stationNumber);
                    }
                    else if (rd_discrete.Checked)
                    {
                        result = client.ReadDiscrete(txt_address.Text, stationNumber);
                    }

                    if (result.IsSucceed)
                    {
                        AppendText($"[读取 {txt_address.Text?.Trim()} 成功]:{result.Value}");
                    }
                    else
                    {
                        AppendText($"[读取 {txt_address.Text?.Trim()} 失败]:{result.Err}");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (chb_show_package.Checked || (ModifierKeys & Keys.Control) == Keys.Control)
                {
                    AppendText($"[请求报文]{result?.Requst}");
                    AppendText($"[响应报文]{result?.Response}\r\n");
                }
            }
        }
Ejemplo n.º 2
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]}");
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void button3_Click(object sender, EventArgs e)
        {
            byte.TryParse(txt_stationNumber.Text?.Trim(), out byte stationNumber);
            if (string.IsNullOrWhiteSpace(txt_address.Text))
            {
                MessageBox.Show("请输入地址");
                return;
            }
            dynamic result = null;

            try
            {
                var addressAndReadLength = txt_address.Text.Split('-');
                //批量读取
                if (addressAndReadLength.Length == 2)
                {
                    var    address    = int.Parse(addressAndReadLength[0]);
                    var    readNumber = ushort.Parse(addressAndReadLength[1]);
                    ushort bLength    = 1;
                    if (rd_bit.Checked || rd_discrete.Checked || rd_short.Checked || rd_ushort.Checked)
                    {
                        bLength = 1;
                    }
                    else if (rd_int.Checked || rd_uint.Checked || rd_float.Checked)
                    {
                        bLength = 2;
                    }
                    else if (rd_long.Checked || rd_ulong.Checked || rd_double.Checked)
                    {
                        bLength = 4;
                    }

                    var  readLength = Convert.ToUInt16(bLength * readNumber);
                    byte functionCode;
                    if (rd_bit.Checked)
                    {
                        functionCode = 1;
                    }
                    else if (rd_discrete.Checked)
                    {
                        functionCode = 2;
                    }
                    else
                    {
                        functionCode = 3;
                    }

                    result = client.Read(address.ToString(), stationNumber, functionCode, readLength: readLength);

                    if (result.IsSucceed)
                    {
                        AppendEmptyText();
                        byte[] rValue = result.Value;
                        rValue = rValue.Reverse().ToArray();
                        for (int i = 0; i < readNumber; i++)
                        {
                            var cAddress = (address + i * bLength).ToString();
                            if (rd_bit.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadCoil(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_discrete.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadDiscrete(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_short.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadInt16(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_ushort.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadUInt16(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_int.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadInt32(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_uint.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadUInt32(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_long.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadInt64(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_ulong.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadUInt64(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_float.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadFloat(address.ToString(), cAddress, rValue).Value}");
                            }
                            else if (rd_double.Checked)
                            {
                                AppendText($"[读取 {address + i * bLength} 成功]:{ client.ReadDouble(address.ToString(), cAddress, rValue).Value}");
                            }
                        }
                    }
                    else
                    {
                        AppendText($"[读取 {txt_address.Text?.Trim()} 失败]:{result.Err}");
                    }
                }
                //单个读取
                else
                {
                    if (rd_bit.Checked)
                    {
                        result = client.ReadCoil(txt_address.Text, stationNumber);
                    }
                    else if (rd_short.Checked)
                    {
                        result = client.ReadInt16(txt_address.Text, stationNumber);
                    }
                    else if (rd_ushort.Checked)
                    {
                        result = client.ReadUInt16(txt_address.Text, stationNumber);
                    }
                    else if (rd_int.Checked)
                    {
                        result = client.ReadInt32(txt_address.Text, stationNumber);
                    }
                    else if (rd_uint.Checked)
                    {
                        result = client.ReadUInt32(txt_address.Text, stationNumber);
                    }
                    else if (rd_long.Checked)
                    {
                        result = client.ReadInt64(txt_address.Text, stationNumber);
                    }
                    else if (rd_ulong.Checked)
                    {
                        result = client.ReadUInt64(txt_address.Text, stationNumber);
                    }
                    else if (rd_float.Checked)
                    {
                        result = client.ReadFloat(txt_address.Text, stationNumber);
                    }
                    else if (rd_double.Checked)
                    {
                        result = client.ReadDouble(txt_address.Text, stationNumber);
                    }
                    else if (rd_discrete.Checked)
                    {
                        result = client.ReadDiscrete(txt_address.Text, stationNumber);
                    }

                    if (result.IsSucceed)
                    {
                        AppendText($"[读取 {txt_address.Text?.Trim()} 成功]:{result.Value}");
                    }
                    else
                    {
                        AppendText($"[读取 {txt_address.Text?.Trim()} 失败]:{result.Err}");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (chb_show_package.Checked || (ModifierKeys & Keys.Control) == Keys.Control)
                {
                    AppendText($"[请求报文]{result?.Requst}");
                    AppendText($"[响应报文]{result?.Response}\r\n");
                }
            }
        }