ConnectionDialog.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.IO.Ports;
  3. using System.Windows.Forms;
  4. using APS7100TestTool.Models;
  5. namespace APS7100TestTool.Forms
  6. {
  7. public partial class ConnectionDialog : Form
  8. {
  9. private AppSettings _settings;
  10. private DeviceType _deviceType;
  11. public AppSettings Settings => _settings;
  12. public ConnectionDialog(AppSettings settings, DeviceType deviceType)
  13. {
  14. InitializeComponent();
  15. _settings = settings;
  16. _deviceType = deviceType;
  17. InitializeUI();
  18. }
  19. private void InitializeUI()
  20. {
  21. // Set title
  22. string deviceName = _deviceType == DeviceType.APS7100 ? "APS-7100" : "PSW-250";
  23. this.Text = $"连接 {deviceName}";
  24. lblTitle.Text = $"连接到 {deviceName}";
  25. // Load COM ports
  26. RefreshComPorts();
  27. // Load settings
  28. if (_deviceType == DeviceType.APS7100)
  29. {
  30. rbSerial.Checked = _settings.APS7100ConnectionType == "Serial";
  31. rbEthernet.Checked = _settings.APS7100ConnectionType == "Ethernet";
  32. SelectComPort(_settings.APS7100ComPort);
  33. cmbBaudRate.Text = _settings.APS7100BaudRate.ToString();
  34. txtIpAddress.Text = _settings.APS7100IpAddress;
  35. numPort.Value = _settings.APS7100Port;
  36. }
  37. else
  38. {
  39. rbSerial.Checked = _settings.PSW250ConnectionType == "Serial";
  40. rbEthernet.Checked = _settings.PSW250ConnectionType == "Ethernet";
  41. SelectComPort(_settings.PSW250ComPort);
  42. cmbBaudRate.Text = _settings.PSW250BaudRate.ToString();
  43. txtIpAddress.Text = _settings.PSW250IpAddress;
  44. numPort.Value = _settings.PSW250Port;
  45. }
  46. UpdateUI();
  47. }
  48. private void RefreshComPorts()
  49. {
  50. cmbComPort.Items.Clear();
  51. string[] ports = SerialPort.GetPortNames();
  52. cmbComPort.Items.AddRange(ports);
  53. }
  54. private void SelectComPort(string portName)
  55. {
  56. if (!string.IsNullOrEmpty(portName))
  57. {
  58. int index = cmbComPort.Items.IndexOf(portName);
  59. if (index >= 0) cmbComPort.SelectedIndex = index;
  60. else if (cmbComPort.Items.Count > 0) cmbComPort.SelectedIndex = 0;
  61. }
  62. else if (cmbComPort.Items.Count > 0)
  63. {
  64. cmbComPort.SelectedIndex = 0;
  65. }
  66. }
  67. private void UpdateUI()
  68. {
  69. bool isSerial = rbSerial.Checked;
  70. lblComPort.Visible = isSerial;
  71. cmbComPort.Visible = isSerial;
  72. lblBaudRate.Visible = isSerial;
  73. cmbBaudRate.Visible = isSerial;
  74. btnRefresh.Visible = isSerial;
  75. lblIpAddress.Visible = !isSerial;
  76. txtIpAddress.Visible = !isSerial;
  77. lblPort.Visible = !isSerial;
  78. numPort.Visible = !isSerial;
  79. }
  80. private void rbSerial_CheckedChanged(object sender, EventArgs e) => UpdateUI();
  81. private void rbEthernet_CheckedChanged(object sender, EventArgs e) => UpdateUI();
  82. private void btnRefresh_Click(object sender, EventArgs e) => RefreshComPorts();
  83. private void btnConnect_Click(object sender, EventArgs e)
  84. {
  85. // Save settings
  86. if (_deviceType == DeviceType.APS7100)
  87. {
  88. _settings.APS7100ConnectionType = rbSerial.Checked ? "Serial" : "Ethernet";
  89. _settings.APS7100ComPort = cmbComPort.SelectedItem?.ToString() ?? "";
  90. _settings.APS7100BaudRate = int.TryParse(cmbBaudRate.Text, out int br) ? br : 9600;
  91. _settings.APS7100IpAddress = txtIpAddress.Text.Trim();
  92. _settings.APS7100Port = (int)numPort.Value;
  93. }
  94. else
  95. {
  96. _settings.PSW250ConnectionType = rbSerial.Checked ? "Serial" : "Ethernet";
  97. _settings.PSW250ComPort = cmbComPort.SelectedItem?.ToString() ?? "";
  98. _settings.PSW250BaudRate = int.TryParse(cmbBaudRate.Text, out int br) ? br : 9600;
  99. _settings.PSW250IpAddress = txtIpAddress.Text.Trim();
  100. _settings.PSW250Port = (int)numPort.Value;
  101. }
  102. this.DialogResult = DialogResult.OK;
  103. this.Close();
  104. }
  105. private void btnCancel_Click(object sender, EventArgs e)
  106. {
  107. this.DialogResult = DialogResult.Cancel;
  108. this.Close();
  109. }
  110. }
  111. }