예제 #1
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();
        }
        private void but_read_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txt_address.Text))
                {
                    MessageBox.Show("请输入地址");
                    return;
                }
                dynamic result = null;
                if (rd_bit.Checked)
                {
                    result = client.ReadBoolean(txt_address.Text);
                }
                else if (rd_short.Checked)
                {
                    result = client.ReadInt16(txt_address.Text);
                }
                else if (rd_ushort.Checked)
                {
                    result = client.ReadUInt16(txt_address.Text);
                }
                else if (rd_int.Checked)
                {
                    result = client.ReadInt32(txt_address.Text);
                }
                else if (rd_uint.Checked)
                {
                    result = client.ReadUInt32(txt_address.Text);
                }
                else if (rd_long.Checked)
                {
                    result = client.ReadInt64(txt_address.Text);
                }
                else if (rd_ulong.Checked)
                {
                    result = client.ReadUInt64(txt_address.Text);
                }
                else if (rd_float.Checked)
                {
                    result = client.ReadFloat(txt_address.Text);
                }
                else if (rd_double.Checked)
                {
                    result = client.ReadDouble(txt_address.Text);
                }

                if (result.IsSucceed)
                {
                    AppendText($"[读取 {txt_address.Text?.Trim()} 成功]:{result.Value}\t\t耗时:{result.TimeConsuming}ms");
                }
                else
                {
                    AppendText($"[读取 {txt_address.Text?.Trim()} 失败]:{result.Err}\t\t耗时:{result.TimeConsuming}ms");
                }
                if (chb_show_package.Checked || (ModifierKeys & Keys.Control) == Keys.Control)
                {
                    AppendText($"[请求报文]{result.Requst}");
                    AppendText($"[响应报文]{result.Response}\r\n");
                }

                var config = ConnectionConfig.GetConfig();
                config.AllenBradley_IP          = txt_ip.Text;
                config.AllenBradley_Port        = txt_port.Text;
                config.AllenBradley_Address     = txt_address.Text;
                config.AllenBradley_Value       = txt_value.Text;
                config.AllenBradley_Slot        = txt_slot.Text;
                config.AllenBradley_ShowPackage = chb_show_package.Checked;
                config.AllenBradley_Datatype    = string.Empty;
                if (rd_bit.Checked)
                {
                    config.AllenBradley_Datatype = "rd_bit";
                }
                else if (rd_short.Checked)
                {
                    config.AllenBradley_Datatype = "rd_short";
                }
                else if (rd_ushort.Checked)
                {
                    config.AllenBradley_Datatype = "rd_ushort";
                }
                else if (rd_int.Checked)
                {
                    config.AllenBradley_Datatype = "rd_int";
                }
                else if (rd_uint.Checked)
                {
                    config.AllenBradley_Datatype = "rd_uint";
                }
                else if (rd_long.Checked)
                {
                    config.AllenBradley_Datatype = "rd_long";
                }
                else if (rd_ulong.Checked)
                {
                    config.AllenBradley_Datatype = "rd_ulong";
                }
                else if (rd_float.Checked)
                {
                    config.AllenBradley_Datatype = "rd_float";
                }
                else if (rd_double.Checked)
                {
                    config.AllenBradley_Datatype = "rd_double";
                }
                config.SaveConfig();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }