Beispiel #1
0
 private void but_open_Click(object sender, EventArgs e)
 {
     Task.Run(() =>
     {
         try
         {
             but_open.Text = "连接中...";
             client?.Close();
             client     = new AllenBradleyClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text.Trim()), byte.Parse(txt_slot.Text.Trim()));
             var result = client.Open();
             if (!result.IsSucceed)
             {
                 MessageBox.Show($"连接失败:{result.Err}");
                 if (chb_show_package.Checked || (ModifierKeys & Keys.Control) == Keys.Control)
                 {
                     AppendText($"[请求报文]{result.Requst}");
                     if (result.Response.IsAny())
                     {
                         AppendText($"[响应报文]{result.Response}\r\n");
                     }
                 }
             }
             else
             {
                 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");
                 if (chb_show_package.Checked || (ModifierKeys & Keys.Control) == Keys.Control)
                 {
                     AppendText($"[请求报文]{result.Requst}");
                     AppendText($"[响应报文]{result.Response}\r\n");
                 }
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
         finally
         {
             but_open.Text = "连接";
         }
     });
 }
Beispiel #2
0
        public void 长连接主动开关(int port)
        {
            client = new AllenBradleyClient(ip, port);

            client.Open();

            #region MyRegion

            Random rnd = new Random((int)Stopwatch.GetTimestamp());
            for (int i = 0; i < 100; i++)
            {
                short  short_number   = (short)rnd.Next(short.MinValue, short.MaxValue);
                ushort short_number_1 = (ushort)rnd.Next(ushort.MinValue, ushort.MaxValue);

                int  int_number   = rnd.Next(int.MinValue, int.MaxValue);
                uint int_number_1 = (uint)rnd.Next(0, int.MaxValue);

                float float_number = rnd.Next(-100000, 100000) / 100;

                var bool_value = short_number % 2 == 1;

                client.Write("A1", bool_value);
                Assert.True(client.ReadBoolean("A1").Value == bool_value);
                client.Write("A1", !bool_value);
                Assert.True(client.ReadBoolean("A1").Value == !bool_value);

                client.Write("A1", short_number);
                Assert.True(client.ReadInt16("A1").Value == short_number);
                client.Write("A1", short_number_1);
                Assert.True(client.ReadUInt16("A1").Value == short_number_1);

                client.Write("A1", int_number);
                Assert.True(client.ReadInt32("A1").Value == int_number);
                client.Write("A1", int_number_1);
                Assert.True(client.ReadUInt32("A1").Value == int_number_1);

                client.Write("A1", Convert.ToSingle(float_number));
                Assert.True(client.ReadFloat("A1").Value == Convert.ToSingle(float_number));
            }
            #endregion

            client.Close();
        }