所以我正在开发一个使用串行端口进行通信的程序。这是我第一次使用WPF,但是我曾经使用Win Forms进行项目,但是我认为学习WPF会很好。现在,我正在使用主窗口中的组合框设置串行端口。我将在本文末尾提供我当前的代码和屏幕截图。问题是我想创建一个自定义对话框,其中包含这些设置,并且将在单击按钮时打开,因此我不会在主窗口中拥有这些设置。关闭此对话框后,这些设置将在主窗口的.cs文件中用于设置端口。我并不需要确切的代码或解决方案,我只是需要一些入门帮助。
public partial class MainWindow : Window
{
SerialPort port = new SerialPort();
public MainWindow()
{
InitializeComponent();
COM_Port_Name.ItemsSource = SerialPort.GetPortNames();
Parity_Select.ItemsSource = Enum.GetValues(typeof(Parity));
Stop_Bits_Select.ItemsSource = Enum.GetValues(typeof(StopBits));
Handshake_Select.ItemsSource = Enum.GetValues(typeof(Handshake));
}
private void Start_Read_Click(object sender, RoutedEventArgs e)
{
Serial_Port_Set();
Serial_Port_Read();
}
private void Refresh_Click(object sender, RoutedEventArgs e)
{
COM_Port_Name.ItemsSource = SerialPort.GetPortNames();
}
private void Stop_Read_Click(object sender, RoutedEventArgs e)
{
Serial_Port_Stop();
}
private void Close_App_Click(object sender, RoutedEventArgs e)
{
Serial_Port_Stop();
port.Dispose();
System.Windows.Application.Current.Shutdown();
}
public void Serial_Port_Set()
{
port.PortName = COM_Port_Name.Text;
port.BaudRate = Convert.ToInt32(Baud_Rate_Select.Text);
port.Parity = (Parity)Enum.Parse(typeof(Parity), Parity_Select.Text);
port.DataBits = Convert.ToInt32(Data_Bits_Select.Text);
port.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Stop_Bits_Select.Text);
port.Handshake = (Handshake)Enum.Parse(typeof(Handshake), Handshake_Select.Text);
port.ReadTimeout = Convert.ToInt32(Read_Timeout_Set.Text);
port.WriteTimeout = Convert.ToInt32(Write_Timeout_Set.Text);
}