コード例 #1
0
        public void 批量读写(MitsubishiVersion version, int port)
        {
            client = new MitsubishiClient(version, ip, port);

            client.Open();

            Random rnd           = new Random((int)Stopwatch.GetTimestamp());
            short  short_number1 = (short)rnd.Next(short.MinValue, short.MaxValue);
            short  short_number2 = (short)rnd.Next(short.MinValue, short.MaxValue);
            short  short_number3 = (short)rnd.Next(short.MinValue, short.MaxValue);
            short  short_number4 = (short)rnd.Next(short.MinValue, short.MaxValue);
            short  short_number5 = (short)rnd.Next(short.MinValue, short.MaxValue);
            var    bool_value    = short_number1 % 2 == 1;

            client.Write("M100", !bool_value);
            client.Write("M101", !bool_value);
            client.Write("M102", bool_value);
            client.Write("M103", !bool_value);
            client.Write("M104", bool_value);

            var result = client.ReadBoolean("M100", 5);

            foreach (var item in result.Value)
            {
                if (item.Key == "M100" || item.Key == "M101" || item.Key == "M103")
                {
                    Assert.True(item.Value == !bool_value);
                }
                else
                {
                    Assert.True(item.Value == bool_value);
                }
            }

            client.Write("D100", short_number1);
            client.Write("D101", short_number2);
            client.Write("D102", short_number3);
            client.Write("D103", short_number4);
            client.Write("D104", short_number5);

            Assert.True(client.ReadInt16("D100").Value == short_number1);
            Assert.True(client.ReadInt16("D101").Value == short_number2);
            Assert.True(client.ReadInt16("D102").Value == short_number3);
            Assert.True(client.ReadInt16("D103").Value == short_number4);
            Assert.True(client.ReadInt16("D104").Value == short_number5);

            client?.Close();
        }
コード例 #2
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("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);
            }
        }
コード例 #3
0
        private void but_write_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txt_address.Text))
            {
                MessageBox.Show("请输入地址");
                return;
            }
            if (string.IsNullOrWhiteSpace(txt_value.Text))
            {
                MessageBox.Show("请输入值");
                return;
            }

            try
            {
                dynamic result = null;
                if (rd_bit.Checked)
                {
                    if (!bool.TryParse(txt_value.Text?.Trim(), out bool bit))
                    {
                        if (txt_value.Text?.Trim() == "0")
                        {
                            bit = false;
                        }
                        else if (txt_value.Text?.Trim() == "1")
                        {
                            bit = true;
                        }
                        else
                        {
                            MessageBox.Show("请输入 True 或 False");
                            return;
                        }
                    }
                    result = client.Write(txt_address.Text, bit);
                }
                else if (rd_short.Checked)
                {
                    result = client.Write(txt_address.Text, short.Parse(txt_value.Text?.Trim()));
                }
                else if (rd_ushort.Checked)
                {
                    result = client.Write(txt_address.Text, ushort.Parse(txt_value.Text?.Trim()));
                }
                else if (rd_int.Checked)
                {
                    result = client.Write(txt_address.Text, int.Parse(txt_value.Text?.Trim()));
                }
                else if (rd_uint.Checked)
                {
                    result = client.Write(txt_address.Text, uint.Parse(txt_value.Text?.Trim()));
                }
                else if (rd_long.Checked)
                {
                    result = client.Write(txt_address.Text, long.Parse(txt_value.Text?.Trim()));
                }
                else if (rd_ulong.Checked)
                {
                    result = client.Write(txt_address.Text, ulong.Parse(txt_value.Text?.Trim()));
                }
                else if (rd_float.Checked)
                {
                    result = client.Write(txt_address.Text, float.Parse(txt_value.Text?.Trim()));
                }
                else if (rd_double.Checked)
                {
                    result = client.Write(txt_address.Text, double.Parse(txt_value.Text?.Trim()));
                }


                if (result.IsSucceed)
                {
                    AppendText($"[写入 {txt_address.Text?.Trim()} 成功]:{txt_value.Text?.Trim()} OK");
                }
                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);
            }
        }