private void but_read_Click(object sender, EventArgs e) { byte.TryParse(txt_stationNumber.Text?.Trim(), out byte stationNumber); if (string.IsNullOrWhiteSpace(txt_address.Text)) { MessageBox.Show("请输入地址"); return; } try { if (txt_address.Text.Contains("-")) { AppendText($"ModbusAsciiClient 暂不支持批量读取"); return; } dynamic result = null; if (rd_bit.Checked) { result = client.ReadCoil(txt_address.Text, stationNumber); } else if (rd_short.Checked) { result = client.ReadInt16(txt_address.Text, stationNumber); } else if (rd_ushort.Checked) { result = client.ReadUInt16(txt_address.Text, stationNumber); } else if (rd_int.Checked) { result = client.ReadInt32(txt_address.Text, stationNumber); } else if (rd_uint.Checked) { result = client.ReadUInt32(txt_address.Text, stationNumber); } else if (rd_long.Checked) { result = client.ReadInt64(txt_address.Text, stationNumber); } else if (rd_ulong.Checked) { result = client.ReadUInt64(txt_address.Text, stationNumber); } else if (rd_float.Checked) { result = client.ReadFloat(txt_address.Text, stationNumber); } else if (rd_double.Checked) { result = client.ReadDouble(txt_address.Text, stationNumber); } else if (rd_discrete.Checked) { result = client.ReadDiscrete(txt_address.Text, stationNumber); } 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); } }
private void but_read_Click(object sender, EventArgs e) { byte.TryParse(txt_stationNumber.Text?.Trim(), out byte stationNumber); if (string.IsNullOrWhiteSpace(txt_address.Text)) { MessageBox.Show("请输入地址"); return; } try { if (txt_address.Text.Contains("-")) { AppendText($"ModbusAsciiClient 暂不支持批量读取"); return; } dynamic result = null; if (rd_coil.Checked) { result = client.ReadCoil(txt_address.Text, stationNumber); } else if (rd_short.Checked) { result = client.ReadInt16(txt_address.Text, stationNumber); } else if (rd_ushort.Checked) { result = client.ReadUInt16(txt_address.Text, stationNumber); } else if (rd_int.Checked) { result = client.ReadInt32(txt_address.Text, stationNumber); } else if (rd_uint.Checked) { result = client.ReadUInt32(txt_address.Text, stationNumber); } else if (rd_long.Checked) { result = client.ReadInt64(txt_address.Text, stationNumber); } else if (rd_ulong.Checked) { result = client.ReadUInt64(txt_address.Text, stationNumber); } else if (rd_float.Checked) { result = client.ReadFloat(txt_address.Text, stationNumber); } else if (rd_double.Checked) { result = client.ReadDouble(txt_address.Text, stationNumber); } else if (rd_discrete.Checked) { result = client.ReadDiscrete(txt_address.Text, stationNumber); } 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.ModBusAscii_Value = txt_value.Text; config.ModBusAscii_Address = txt_address.Text; config.ModBusAscii_StationNumber = txt_stationNumber.Text; config.ModBusAscii_ShowPackage = chb_show_package.Checked; config.ModBusAscii_Datatype = string.Empty; if (rd_coil.Checked) { config.ModBusAscii_Datatype = "rd_coil"; } else if (rd_discrete.Checked) { config.ModBusAscii_Datatype = "rd_discrete"; } else if (rd_short.Checked) { config.ModBusAscii_Datatype = "rd_short"; } else if (rd_ushort.Checked) { config.ModBusAscii_Datatype = "rd_ushort"; } else if (rd_int.Checked) { config.ModBusAscii_Datatype = "rd_int"; } else if (rd_uint.Checked) { config.ModBusAscii_Datatype = "rd_uint"; } else if (rd_long.Checked) { config.ModBusAscii_Datatype = "rd_long"; } else if (rd_ulong.Checked) { config.ModBusAscii_Datatype = "rd_ulong"; } else if (rd_float.Checked) { config.ModBusAscii_Datatype = "rd_float"; } else if (rd_double.Checked) { config.ModBusAscii_Datatype = "rd_double"; } config.SaveConfig(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }