借助网络配置,控制Modbus工具的日志显示,避免卡死界面
智能大石头 authored at 2021-12-11 22:09:19
9.32 KiB
XCoder
<Window x:Class="CrazyCoder.Views.SerialPortWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="串口调试工具" Height="700" Width="1000" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <Style x:Key="GroupBorder" TargetType="Border">
            <Setter Property="BorderBrush" Value="#D0D0D0"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Margin" Value="0,0,0,6"/>
        </Style>

        <Style x:Key="SectionTitle" TargetType="TextBlock">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Margin" Value="4,3"/>
            <Setter Property="FontSize" Value="13"/>
        </Style>

        <Style x:Key="ConfigLabel" TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="4,2"/>
        </Style>

        <Style x:Key="ConfigTextBox" TargetType="TextBox">
            <Setter Property="Margin" Value="2"/>
            <Setter Property="Height" Value="26"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="BorderBrush" Value="#D0D0D0"/>
        </Style>

        <Style x:Key="ConfigComboBox" TargetType="ComboBox">
            <Setter Property="Margin" Value="2"/>
            <Setter Property="Height" Value="26"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
        </Style>

        <Style x:Key="ActionButton" TargetType="Button">
            <Setter Property="Height" Value="30"/>
            <Setter Property="Margin" Value="2"/>
            <Setter Property="Padding" Value="8,0"/>
        </Style>

        <Style x:Key="SendButton" TargetType="Button">
            <Setter Property="Height" Value="50"/>
            <Setter Property="Margin" Value="2"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
    </Window.Resources>

    <Grid Margin="6">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="280"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <!-- ============ 左侧:配置面板 ============ -->
        <ScrollViewer Grid.Column="0" VerticalScrollBarVisibility="Auto" Margin="0,0,4,0">
            <StackPanel>
                <!-- 串口配置 -->
                <Border Style="{StaticResource GroupBorder}">
                    <StackPanel>
                        <TextBlock Text="串口配置" Style="{StaticResource SectionTitle}"/>

                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Row="0" Grid.Column="0" Text="端口" Style="{StaticResource ConfigLabel}"/>
                            <ComboBox Grid.Row="0" Grid.Column="1"
                                      ItemsSource="{Binding PortNames}"
                                      SelectedItem="{Binding PortName, UpdateSourceTrigger=PropertyChanged}"
                                      Style="{StaticResource ConfigComboBox}"/>
                            <Button Grid.Row="0" Grid.Column="2" Content="刷新" Command="{Binding RefreshPortsCommand}"
                                    Style="{StaticResource ActionButton}" Width="50"/>

                            <TextBlock Grid.Row="1" Grid.Column="0" Text="波特率" Style="{StaticResource ConfigLabel}"/>
                            <ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
                                      ItemsSource="{Binding BaudRates}"
                                      SelectedItem="{Binding BaudRate, UpdateSourceTrigger=PropertyChanged}"
                                      Style="{StaticResource ConfigComboBox}"/>

                            <TextBlock Grid.Row="2" Grid.Column="0" Text="校验位" Style="{StaticResource ConfigLabel}"/>
                            <ComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"
                                      ItemsSource="{Binding ParityOptions}"
                                      SelectedIndex="{Binding ParityIndex, UpdateSourceTrigger=PropertyChanged}"
                                      Style="{StaticResource ConfigComboBox}"/>

                            <TextBlock Grid.Row="3" Grid.Column="0" Text="数据位" Style="{StaticResource ConfigLabel}"/>
                            <ComboBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2"
                                      ItemsSource="{Binding DataBitsOptions}"
                                      SelectedItem="{Binding DataBits, UpdateSourceTrigger=PropertyChanged}"
                                      Style="{StaticResource ConfigComboBox}"/>

                            <TextBlock Grid.Row="4" Grid.Column="0" Text="停止位" Style="{StaticResource ConfigLabel}"/>
                            <ComboBox Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2"
                                      ItemsSource="{Binding StopBitsOptions}"
                                      SelectedIndex="{Binding StopBitsIndex, UpdateSourceTrigger=PropertyChanged}"
                                      Style="{StaticResource ConfigComboBox}"/>

                            <Button Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3"
                                    Content="{Binding ConnectButtonText}"
                                    Command="{Binding ToggleConnectCommand}"
                                    Style="{StaticResource SendButton}" Foreground="White"
                                    Background="{Binding IsConnected, Converter={x:Null}}"/>
                        </Grid>
                    </StackPanel>
                </Border>

                <!-- 发送配置 -->
                <Border Style="{StaticResource GroupBorder}">
                    <StackPanel>
                        <TextBlock Text="发送配置" Style="{StaticResource SectionTitle}"/>

                        <CheckBox Content="十六进制发送" IsChecked="{Binding HexSend}" Margin="4,2"/>

                        <TextBox Text="{Binding SendText, UpdateSourceTrigger=PropertyChanged}"
                                 Style="{StaticResource ConfigTextBox}" Height="80"
                                 AcceptsReturn="True" TextWrapping="Wrap"/>

                        <Button Content="发送" Command="{Binding SendCommand}"
                                Style="{StaticResource SendButton}" Foreground="White"
                                Background="#218868"/>
                    </StackPanel>
                </Border>

                <!-- 统计信息 -->
                <Border Style="{StaticResource GroupBorder}">
                    <StackPanel>
                        <TextBlock Text="统计" Style="{StaticResource SectionTitle}"/>
                        <TextBlock Margin="4,2">
                            <Run FontWeight="Bold">已发送:</Run>
                            <Run Text="{Binding BytesSent}"/> 字节
                        </TextBlock>
                        <TextBlock Margin="4,2">
                            <Run FontWeight="Bold">已接收:</Run>
                            <Run Text="{Binding BytesReceived}"/> 字节
                        </TextBlock>
                    </StackPanel>
                </Border>
            </StackPanel>
        </ScrollViewer>

        <!-- ============ 右侧:日志显示 ============ -->
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Border Grid.Row="0" Style="{StaticResource GroupBorder}" Background="#FFF5E6">
                <Grid>
                    <TextBlock Text="日志" Style="{StaticResource SectionTitle}" VerticalAlignment="Center"/>
                    <Button HorizontalAlignment="Right" Content="清空" Command="{Binding ClearLogCommand}"
                            Style="{StaticResource ActionButton}" Margin="0,2,4,2"/>
                </Grid>
            </Border>

            <Border Grid.Row="1" Style="{StaticResource GroupBorder}">
                <RichTextBox x:Name="txtLog" IsReadOnly="True" VerticalScrollBarVisibility="Auto"
                             FontFamily="Consolas" FontSize="12" Background="#1E1E1E" Foreground="#D4D4D4"/>
            </Border>
        </Grid>
    </Grid>
</Window>