利用wpf实现串口助手
串口助手
UI效果图
UI代码
<Window x:Class="Modbus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="串口助手" Height="400" Width="600" Closing="CloseWindow" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="192*"></RowDefinition>
<RowDefinition Height="82*"></RowDefinition>
<RowDefinition Height="97*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="266*"></ColumnDefinition>
<ColumnDefinition Width="327*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--设置一个控件块-->
<GroupBox Header="串口参数设置"
BorderBrush="Blue"></GroupBox>
<StackPanel>
<!-- 第一行要摆放的控件-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="串口号" Width="auto" Margin="5,10" Padding="0,6,0,0"></TextBlock>
<ComboBox Name="cb_SerialPort" Margin="5,15" Width="65" >
</ComboBox>
<TextBlock Text="波特率" Width="auto" Margin="5,10" Padding="0,6,0,0"></TextBlock>
<ComboBox Name="cb_Baudate" Margin="5,15" Width="65" SelectedIndex="4">
<ComboBoxItem Content="600"></ComboBoxItem>
<ComboBoxItem Content="1200"></ComboBoxItem>
<ComboBoxItem Content="2400"></ComboBoxItem>
<ComboBoxItem Content="4800"></ComboBoxItem>
<ComboBoxItem Content="9600"></ComboBoxItem>
<ComboBoxItem Content="14400"></ComboBoxItem>
<ComboBoxItem Content="19200"></ComboBoxItem>
</ComboBox>
</StackPanel>
<!--第二行要摆放的控件-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="校验位" Margin="5" Width="auto" Padding="0,6,0,0"></TextBlock>
<ComboBox Name="cb_Parity" Width="65" Margin="5,10,5,10" SelectedIndex="2" >
<ComboBoxItem Content="奇校验"></ComboBoxItem>
<ComboBoxItem Content="偶校验"></ComboBoxItem>
<ComboBoxItem Content="无校验"></ComboBoxItem>
</ComboBox>
<TextBlock Text="数据位" Margin="5,10,5,10" ></TextBlock>
<ComboBox Name="cb_DataBit" Width="68" Margin="5,10" SelectedIndex="1">
<ComboBoxItem Content="0"></ComboBoxItem>
<ComboBoxItem Content="8"></ComboBoxItem>
<ComboBoxItem Content="2"></ComboBoxItem>
</ComboBox>
</StackPanel>
<!--第三行摆放的控件-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="停止位" Margin="5" Padding="0,6,0,0"></TextBlock>
<ComboBox Name="cb_StopBit" Margin="5,10" Width="65" SelectedIndex="1">
<ComboBoxItem Content="0"></ComboBoxItem>
<ComboBoxItem Content="1"></ComboBoxItem>
<ComboBoxItem Content="2"></ComboBoxItem>
</ComboBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Name="tb_SwitchSerial" Text="串口为关闭状态" Margin="5" Padding="0,6,0,0" Width="auto" ></TextBlock>
<Ellipse Name="e_status" Fill="#000000" Stroke="Black" Height="10" Width="10" Margin="5,10,5,10" ></Ellipse>
<Button Name="bt_serialSwitch" Content="打开串口" Margin="5" Click="Button_CLickSwithSerial"></Button>
</StackPanel>
</StackPanel>
<!-- 接受数据设置-->
<GroupBox Grid.Row="1" Grid.Column="0" BorderBrush="Black" Header="接受数据设置" ></GroupBox>
<StackPanel Grid.Row="1" Grid.Column="0" >
<StackPanel Orientation="Horizontal" >
<Button Content="清空接收数据" Margin="10,20" Click="Button_ClickClrReceive" ></Button>
<Button Name="bt_StopReceive" Content="停止接收数据" Margin="40,20" Click="Button_ClickStopReceive"></Button>
</StackPanel>
</StackPanel>
<!--发送数据设置-->
<GroupBox Grid.Row="2" Grid.Column="0" BorderBrush="Yellow"></GroupBox>
<StackPanel Grid.Row="2" Grid.Column="0">
<StackPanel Orientation="Horizontal">
<Button Content="清空发送数据" Margin="10,20" Click="Button_ClickClrSend"></Button>
<Button Name="bt_HandSend" Content="手动发送" Margin="40,20" Click="Button_ClickHandSend"></Button>
</StackPanel>
</StackPanel>
<!--接收数据文本框设置-->
<GroupBox Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" BorderBrush="Red" Header="接收数据区"></GroupBox>
<StackPanel Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" >
<TextBox Margin="5,20" TextWrapping="Wrap" Name="tbox_receive" Height="240"></TextBox>
</StackPanel>
<!--发送文本框-->
<GroupBox Grid.Row="2" Grid.Column="1" BorderBrush="Black" Header="数据发送"></GroupBox>
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Vertical">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBox Name="tbox_send" Margin="5,20,5,5" Height="70" ></TextBox>
</ScrollViewer>
</StackPanel>
</Grid>
</Window>
参考资料:
[1]
https://blog.****.net/weixin_42462552/article/details/85937289
[2]
https://docs.microsoft.com/zh-cn/dotnet/api/system.io.ports?view=netframework-4.7.2
[3]