public void 长连接主动开关(int port) { client = new OmronFinsClient(ip, port); client.Open(); 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 = int_number / 100; var bool_value = short_number % 2 == 1; client.Write("D100", bool_value); Assert.True(client.ReadBoolean("D100").Value == bool_value); client.Write("D101", bool_value); Assert.True(client.ReadBoolean("D101").Value == bool_value); client.Write("D102", !bool_value); Assert.True(client.ReadBoolean("D102").Value == !bool_value); client.Write("D100", short_number); Assert.True(client.ReadInt16("D100").Value == short_number); client.Write("D100", short_number_1); Assert.True(client.ReadUInt16("D100").Value == short_number_1); client.Write("D100", int_number); Assert.True(client.ReadInt32("D100").Value == int_number); client.Write("D100", int_number_1); Assert.True(client.ReadUInt32("D100").Value == int_number_1); client.Write("D100", Convert.ToInt64(int_number)); Assert.True(client.ReadInt64("D100").Value == Convert.ToInt64(int_number)); client.Write("D100", Convert.ToUInt64(int_number_1)); Assert.True(client.ReadUInt64("D100").Value == Convert.ToUInt64(int_number_1)); client.Write("D200", float_number); Assert.True(client.ReadFloat("D200").Value == float_number); client.Write("D300", Convert.ToDouble(float_number)); Assert.True(client.ReadDouble("D300").Value == Convert.ToDouble(float_number)); } client.Close(); }
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\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"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }