Ejemplo n.º 1
0
        private void ReadWrite()
        {
            Random rnd = new Random((int)Stopwatch.GetTimestamp());

            for (int i = 0; i < 10; i++)
            {
                short short_number = (short)rnd.Next(short.MinValue, short.MaxValue);
                int   int_number   = rnd.Next(int.MinValue, int.MaxValue);
                float float_number = int_number / 100;
                var   bool_value   = short_number % 2 == 1;
                client.Write("X100", bool_value);
                Assert.True(client.ReadBoolean("X100").Value == bool_value);
                client.Write("Y100", !bool_value);
                Assert.True(client.ReadBoolean("Y100").Value == !bool_value);
                client.Write("M100", !bool_value);
                Assert.True(client.ReadBoolean("M100").Value == !bool_value);
                client.Write("M101", bool_value);
                Assert.True(client.ReadBoolean("M101").Value == bool_value);
                client.Write("M102", bool_value);
                Assert.True(client.ReadBoolean("M102").Value == bool_value);
                client.Write("M103", !bool_value);
                Assert.True(client.ReadBoolean("M103").Value == !bool_value);
                client.Write("M104", bool_value);
                Assert.True(client.ReadBoolean("M104").Value == bool_value);
                //client.Write("L100", !bool_value);
                //Assert.True(client.ReadBoolean("L100").Value == !bool_value);
                //client.Write("F100", bool_value);
                //Assert.True(client.ReadBoolean("F100").Value == bool_value);
                //client.Write("V100", !bool_value);
                //Assert.True(client.ReadBoolean("V100").Value == !bool_value);
                //client.Write("B100", bool_value);
                //Assert.True(client.ReadBoolean("B100").Value == bool_value);
                //client.Write("S100", bool_value);
                //Assert.True(client.ReadBoolean("S100").Value == bool_value);

                client.Write("D200", short_number);
                Assert.True(client.ReadInt16("D200").Value == short_number);

                client.Write("D200", int_number);
                Assert.True(client.ReadInt32("D200").Value == int_number);

                client.Write("D200", Convert.ToInt64(int_number));
                Assert.True(client.ReadInt64("D200").Value == Convert.ToInt64(int_number));

                client.Write("D200", float_number);
                Assert.True(client.ReadFloat("D200").Value == float_number);

                client.Write("D200", Convert.ToDouble(float_number));
                Assert.True(client.ReadDouble("D200").Value == Convert.ToDouble(float_number));

                //client.Write("W400", short_number);
                //Assert.True(client.ReadInt16("W400").Value == short_number);

                //client.Write("R200", r_number);
                //Assert.True(client.ReadInt16("R200").Value == r_number);
                Debug.WriteLine(short_number);
            }
        }
Ejemplo n.º 2
0
        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}");
                }
                else
                {
                    AppendText($"[读取 {txt_address.Text?.Trim()} 失败]:{result.Err}");
                }
                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);
            }
        }
Ejemplo n.º 3
0
        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();
                switch (version)
                {
                case MitsubishiVersion.A_1E:
                    config.MitsubishiA1E_IP          = txt_ip.Text;
                    config.MitsubishiA1E_Port        = txt_port.Text;
                    config.MitsubishiA1E_Address     = txt_address.Text;
                    config.MitsubishiA1E_Value       = txt_value.Text;
                    config.MitsubishiA1E_ShowPackage = chb_show_package.Checked;
                    config.MitsubishiA1E_Datatype    = string.Empty;
                    if (rd_bit.Checked)
                    {
                        config.MitsubishiA1E_Datatype = "rd_bit";
                    }
                    else if (rd_short.Checked)
                    {
                        config.MitsubishiA1E_Datatype = "rd_short";
                    }
                    else if (rd_ushort.Checked)
                    {
                        config.MitsubishiA1E_Datatype = "rd_ushort";
                    }
                    else if (rd_int.Checked)
                    {
                        config.MitsubishiA1E_Datatype = "rd_int";
                    }
                    else if (rd_uint.Checked)
                    {
                        config.MitsubishiA1E_Datatype = "rd_uint";
                    }
                    else if (rd_long.Checked)
                    {
                        config.MitsubishiA1E_Datatype = "rd_long";
                    }
                    else if (rd_ulong.Checked)
                    {
                        config.MitsubishiA1E_Datatype = "rd_ulong";
                    }
                    else if (rd_float.Checked)
                    {
                        config.MitsubishiA1E_Datatype = "rd_float";
                    }
                    else if (rd_double.Checked)
                    {
                        config.MitsubishiA1E_Datatype = "rd_double";
                    }
                    break;

                case MitsubishiVersion.Qna_3E:
                    config.MitsubishiQna3E_IP          = txt_ip.Text;
                    config.MitsubishiQna3E_Port        = txt_port.Text;
                    config.MitsubishiQna3E_Address     = txt_address.Text;
                    config.MitsubishiQna3E_Value       = txt_value.Text;
                    config.MitsubishiQna3E_ShowPackage = chb_show_package.Checked;
                    config.MitsubishiQna3E_Datatype    = string.Empty;
                    if (rd_bit.Checked)
                    {
                        config.MitsubishiQna3E_Datatype = "rd_bit";
                    }
                    else if (rd_short.Checked)
                    {
                        config.MitsubishiQna3E_Datatype = "rd_short";
                    }
                    else if (rd_ushort.Checked)
                    {
                        config.MitsubishiQna3E_Datatype = "rd_ushort";
                    }
                    else if (rd_int.Checked)
                    {
                        config.MitsubishiQna3E_Datatype = "rd_int";
                    }
                    else if (rd_uint.Checked)
                    {
                        config.MitsubishiQna3E_Datatype = "rd_uint";
                    }
                    else if (rd_long.Checked)
                    {
                        config.MitsubishiQna3E_Datatype = "rd_long";
                    }
                    else if (rd_ulong.Checked)
                    {
                        config.MitsubishiQna3E_Datatype = "rd_ulong";
                    }
                    else if (rd_float.Checked)
                    {
                        config.MitsubishiQna3E_Datatype = "rd_float";
                    }
                    else if (rd_double.Checked)
                    {
                        config.MitsubishiQna3E_Datatype = "rd_double";
                    }
                    break;
                }
                config.SaveConfig();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }