Ejemplo n.º 1
0
        private void but_open_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_content.Text.Contains("小技巧"))
                {
                    txt_content.Text = string.Empty;
                }
                client?.Close();

                EndianFormat format = EndianFormat.ABCD;
                switch (comboBox1.SelectedIndex)
                {
                case 0:
                    format = EndianFormat.ABCD;
                    break;

                case 1:
                    format = EndianFormat.BADC;
                    break;

                case 2:
                    format = EndianFormat.CDAB;
                    break;

                case 3:
                    format = EndianFormat.DCBA;
                    break;
                }

                if (chb_rtudata.Checked)
                {
                    client = new ModbusTcpRtuClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text?.Trim()), format: format);
                }
                else
                {
                    client = new ModbusTcpClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text?.Trim()), format: format);
                }
                var result = client.Open();
                if (result.IsSucceed)
                {
                    but_read.Enabled     = true;
                    but_write.Enabled    = true;
                    but_open.Enabled     = false;
                    but_close.Enabled    = true;
                    but_sendData.Enabled = true;
                    AppendText($"连接成功");
                    ControlEnabledFalse();
                }
                else
                {
                    MessageBox.Show($"连接失败:{result.Err}");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 打开连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_open_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_content.Text.Contains("小技巧"))
                {
                    txt_content.Text = string.Empty;
                }
                var PortName = cb_portNameSend.Text.ToString();
                var BaudRate = int.Parse(cb_baudRate.Text.ToString());
                var DataBits = int.Parse(txt_dataBit.Text.ToString());
                var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString());
                var parity   = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even);
                client?.Close();
                client = new ModbusRtuClient(PortName, BaudRate, DataBits, StopBits, parity);
                var result = client.Open();
                if (result.IsSucceed)
                {
                    but_open.Enabled        = false;
                    cb_portNameSend.Enabled = false;
                    but_read.Enabled        = true;
                    but_write.Enabled       = true;
                    but_open.Enabled        = false;
                    but_close.Enabled       = true;
                    but_sendData.Enabled    = true;

                    //按了Ctrl后的鼠标点击
                    if ((ModifierKeys & Keys.Control) == Keys.Control)
                    {
                        Task.Run(() =>
                        {
                            AutoOpenRead();
                        });
                    }
                    else
                    {
                        AppendText("连接成功");
                    }
                }
                else
                {
                    AppendText($"连接失败:{result.Err}");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 3
0
        private void but_sendData_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txt_dataPackage.Text))
                {
                    MessageBox.Show("请输入要发送的报文");
                    return;
                }
                var dataPackageString = txt_dataPackage.Text.Replace(" ", "");
                if (dataPackageString.Length % 2 != 0)
                {
                    MessageBox.Show("请输入正确的的报文");
                    return;
                }

                var dataPackage = DataConvert.StringToByteArray(txt_dataPackage.Text?.Trim(), false);
                var msg         = client.SendPackage(dataPackage);
                AppendText($"[请求报文]{string.Join(" ", dataPackage.Select(t => t.ToString("X2")))}");
                AppendText($"[响应报文]{string.Join(" ", msg.Select(t => t.ToString("X2")))}\r\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                client.Close();
                client.Open();
            }
        }
Ejemplo n.º 4
0
 private void but_open_Click(object sender, EventArgs e)
 {
     try
     {
         if (txt_content.Text.Contains("小技巧"))
         {
             txt_content.Text = string.Empty;
         }
         client?.Close();
         if (chb_rtudata.Checked)
         {
             client = new ModbusTcpRtuClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text?.Trim()));
         }
         else
         {
             client = new ModbusTcpClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text?.Trim()));
         }
         var result = client.Open();
         if (result.IsSucceed)
         {
             but_read.Enabled     = true;
             but_write.Enabled    = true;
             but_open.Enabled     = false;
             but_close.Enabled    = true;
             but_sendData.Enabled = true;
             AppendText($"连接成功");
         }
         else
         {
             MessageBox.Show($"连接失败:{result.Err}");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 打开连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_open_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_content.Text.Contains("小技巧"))
                {
                    txt_content.Text = string.Empty;
                }
                var PortName = cb_portNameSend.Text.ToString();
                var BaudRate = int.Parse(cb_baudRate.Text.ToString());
                var DataBits = int.Parse(txt_dataBit.Text.ToString());
                var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString());
                var parity   = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even);
                client?.Close();
                EndianFormat format = EndianFormat.ABCD;
                switch (cmb_EndianFormat.SelectedIndex)
                {
                case 0:
                    format = EndianFormat.ABCD;
                    break;

                case 1:
                    format = EndianFormat.BADC;
                    break;

                case 2:
                    format = EndianFormat.CDAB;
                    break;

                case 3:
                    format = EndianFormat.DCBA;
                    break;
                }
                client = new ModbusRtuClient(PortName, BaudRate, DataBits, StopBits, parity, format: format);
                var result = client.Open();
                if (result.IsSucceed)
                {
                    but_open.Enabled        = false;
                    cb_portNameSend.Enabled = false;
                    but_read.Enabled        = true;
                    but_write.Enabled       = true;
                    but_open.Enabled        = false;
                    but_close.Enabled       = true;
                    but_sendData.Enabled    = true;

                    //按了Ctrl后的鼠标点击
                    if ((ModifierKeys & Keys.Control) == Keys.Control)
                    {
                        Task.Run(() =>
                        {
                            AutoOpenRead();
                        });
                    }
                    else
                    {
                        AppendText($"连接成功\t\t\t\t耗时:{result.TimeConsuming}ms");
                        ControlEnabledFalse();
                    }
                }
                else
                {
                    AppendText($"连接失败:{result.Err}");
                }

                var config = ConnectionConfig.GetConfig();
                config.ModBusRtu_PortName     = PortName;
                config.ModBusRtu_BaudRate     = BaudRate.ToString();
                config.ModBusRtu_DataBits     = DataBits.ToString();
                config.ModBusRtu_StopBits     = StopBits;
                config.ModBusRtu_Parity       = parity;
                config.ModBusRtu_Value        = txt_value.Text;
                config.ModBusRtu_Address      = txt_address.Text;
                config.ModBusRtu_ShowPackage  = chb_show_package.Checked;
                config.ModBusRtu_EndianFormat = format;
                config.SaveConfig();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void but_open_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                try
                {
                    but_open.Text = "连接中...";
                    if (txt_content.Text.Contains("小技巧"))
                    {
                        txt_content.Text = string.Empty;
                    }
                    client?.Close();

                    switch (cmb_EndianFormat.SelectedIndex)
                    {
                    case 0:
                        format = EndianFormat.ABCD;
                        break;

                    case 1:
                        format = EndianFormat.BADC;
                        break;

                    case 2:
                        format = EndianFormat.CDAB;
                        break;

                    case 3:
                        format = EndianFormat.DCBA;
                        break;
                    }

                    if (chb_rtudata.Checked)
                    {
                        client = new ModbusRtuOverTcpClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text?.Trim()), format: format);
                    }
                    else
                    {
                        client = new ModbusTcpClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text?.Trim()), format: format);
                    }
                    var result = client.Open();
                    if (result.IsSucceed)
                    {
                        but_read.Enabled       = true;
                        but_brokenline.Enabled = true;
                        but_write.Enabled      = true;
                        but_open.Enabled       = false;
                        but_close.Enabled      = true;
                        but_sendData.Enabled   = true;
                        AppendText($"连接成功\t\t\t\t耗时:{result.TimeConsuming}ms");
                        ControlEnabledFalse();
                    }
                    else
                    {
                        MessageBox.Show($"连接失败:{result.Err}");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    but_open.Text = "连接";
                }
            });
        }