private void but_open_Click(object sender, EventArgs e) { try { client?.Close(); client = new MitsubishiClient(txt_ip.Text?.Trim(), int.Parse(txt_port.Text.Trim())); var result = client.Open(); if (!result.IsSucceed) { MessageBox.Show($"连接失败:{result.Err}"); } else { but_read.Enabled = true; but_write.Enabled = true; but_open.Enabled = false; but_close.Enabled = true; but_sendData.Enabled = true; AppendText($"连接成功"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void but_open_Click(object sender, EventArgs e) { Task.Run(() => { try { but_open.Text = "连接中..."; client?.Close(); client = new MitsubishiClient(version, txt_ip.Text?.Trim(), int.Parse(txt_port.Text.Trim())); var result = client.Open(); if (!result.IsSucceed) { MessageBox.Show($"连接失败:{result.Err}"); } else { but_read.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"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { but_open.Text = "连接"; } }); }
private void but_sendData_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(txt_dataPackage.Text)) { MessageBox.Show("请输入要发送的报文"); return; } var dataPackageString = txt_dataPackage.Text.Replace(" ", ""); if (dataPackageString.Length % 2 != 0) { MessageBox.Show("请输入正确的的报文"); return; } var dataPackage = DataConvert.StringToByteArray(txt_dataPackage.Text?.Trim(), false); var msg = client.SendPackage(dataPackage); AppendText($"[请求报文]{string.Join(" ", dataPackage.Select(t => t.ToString("X2")))}"); AppendText($"[响应报文]{string.Join(" ", msg.Select(t => t.ToString("X2")))}\r\n"); } catch (Exception ex) { MessageBox.Show(ex.Message); client.Close(); client.Open(); } }
public void 长连接主动开关(MitsubishiVersion version, int port) { client = new MitsubishiClient(version, ip, port); client.Open(); ReadWrite(); client?.Close(); }
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(); }
public void 批量读写() { client.Open(); client?.Close(); }