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");
                }
            }
        }
Beispiel #2
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");
                }
            }
        }