Exemplo n.º 1
0
 /// <summary>
 /// 打开连接
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void but_open_Click(object sender, EventArgs e)
 {
     try
     {
         var PortName = cb_portNameSend.Text.ToString();
         var BaudRate = int.Parse(txt_baudRate.Text.ToString());
         var DataBits = int.Parse(txt_dataBit.Text.ToString());
         var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString());
         client?.Close();
         client = new ModBusRtuClient(PortName, BaudRate, DataBits, StopBits);
         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;
             AppendText("连接成功");
         }
         else
         {
             AppendText($"连接失败:{result.Err}");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemplo 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);
            }
        }
Exemplo 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")))}\r");
                AppendText($"[响应报文]{string.Join(" ", msg.Select(t => t.ToString("X2")))}\r\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                client.Close();
                client.Open();
            }
        }
        public async Task 长连接主动开关()
        {
            client.Open();

            short Number = 33;

            client.Write("4", Number, stationNumber);
            Assert.True(client.ReadInt16("4", stationNumber).Value == Number);

            Number = 34;
            client.Write("4", Number, stationNumber);
            Assert.True(client.ReadInt16("4", stationNumber).Value == Number);

            Number = 1;
            client.Write("12", Number, stationNumber);
            Assert.True(client.ReadInt16("12", stationNumber).Value == 1);

            Number = 0;
            client.Write("12", Number, stationNumber);
            Assert.True(client.ReadInt16("12", stationNumber).Value == 0);

            int numberInt32 = -12;

            client.Write("4", numberInt32, stationNumber);
            Assert.True(client.ReadInt32("4", stationNumber).Value == numberInt32);

            float numberFloat = 112;

            client.Write("4", numberFloat, stationNumber);
            Assert.True(client.ReadFloat("4", stationNumber).Value == numberFloat);

            double numberDouble = 32;

            client.Write("4", numberDouble, stationNumber);
            Assert.True(client.ReadDouble("4", stationNumber).Value == numberDouble);

            client.Close();
        }