using System; using System.IO.Ports; using System.Windows.Forms; using APS7100TestTool.Models; namespace APS7100TestTool.Forms { public partial class ConnectionDialog : Form { private AppSettings _settings; private DeviceType _deviceType; public AppSettings Settings => _settings; public ConnectionDialog(AppSettings settings, DeviceType deviceType) { InitializeComponent(); _settings = settings; _deviceType = deviceType; InitializeUI(); } private void InitializeUI() { // Set title string deviceName = _deviceType == DeviceType.APS7100 ? "APS-7100" : "PSW-250"; this.Text = $"连接 {deviceName}"; lblTitle.Text = $"连接到 {deviceName}"; // Load COM ports RefreshComPorts(); // Load settings if (_deviceType == DeviceType.APS7100) { rbSerial.Checked = _settings.APS7100ConnectionType == "Serial"; rbEthernet.Checked = _settings.APS7100ConnectionType == "Ethernet"; SelectComPort(_settings.APS7100ComPort); cmbBaudRate.Text = _settings.APS7100BaudRate.ToString(); txtIpAddress.Text = _settings.APS7100IpAddress; numPort.Value = _settings.APS7100Port; } else { rbSerial.Checked = _settings.PSW250ConnectionType == "Serial"; rbEthernet.Checked = _settings.PSW250ConnectionType == "Ethernet"; SelectComPort(_settings.PSW250ComPort); cmbBaudRate.Text = _settings.PSW250BaudRate.ToString(); txtIpAddress.Text = _settings.PSW250IpAddress; numPort.Value = _settings.PSW250Port; } UpdateUI(); } private void RefreshComPorts() { cmbComPort.Items.Clear(); string[] ports = SerialPort.GetPortNames(); cmbComPort.Items.AddRange(ports); } private void SelectComPort(string portName) { if (!string.IsNullOrEmpty(portName)) { int index = cmbComPort.Items.IndexOf(portName); if (index >= 0) cmbComPort.SelectedIndex = index; else if (cmbComPort.Items.Count > 0) cmbComPort.SelectedIndex = 0; } else if (cmbComPort.Items.Count > 0) { cmbComPort.SelectedIndex = 0; } } private void UpdateUI() { bool isSerial = rbSerial.Checked; lblComPort.Visible = isSerial; cmbComPort.Visible = isSerial; lblBaudRate.Visible = isSerial; cmbBaudRate.Visible = isSerial; btnRefresh.Visible = isSerial; lblIpAddress.Visible = !isSerial; txtIpAddress.Visible = !isSerial; lblPort.Visible = !isSerial; numPort.Visible = !isSerial; } private void rbSerial_CheckedChanged(object sender, EventArgs e) => UpdateUI(); private void rbEthernet_CheckedChanged(object sender, EventArgs e) => UpdateUI(); private void btnRefresh_Click(object sender, EventArgs e) => RefreshComPorts(); private void btnConnect_Click(object sender, EventArgs e) { // Save settings if (_deviceType == DeviceType.APS7100) { _settings.APS7100ConnectionType = rbSerial.Checked ? "Serial" : "Ethernet"; _settings.APS7100ComPort = cmbComPort.SelectedItem?.ToString() ?? ""; _settings.APS7100BaudRate = int.TryParse(cmbBaudRate.Text, out int br) ? br : 9600; _settings.APS7100IpAddress = txtIpAddress.Text.Trim(); _settings.APS7100Port = (int)numPort.Value; } else { _settings.PSW250ConnectionType = rbSerial.Checked ? "Serial" : "Ethernet"; _settings.PSW250ComPort = cmbComPort.SelectedItem?.ToString() ?? ""; _settings.PSW250BaudRate = int.TryParse(cmbBaudRate.Text, out int br) ? br : 9600; _settings.PSW250IpAddress = txtIpAddress.Text.Trim(); _settings.PSW250Port = (int)numPort.Value; } this.DialogResult = DialogResult.OK; this.Close(); } private void btnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } } }