C# 第二阶段 modbus

1:nuget添加依赖

新建项目后,添加NModbus,

2:测试软件:Modbus slave ,新建一个从站,并连接

3:通信测试

4:本次学到的内容

4.1:用的DockPanel标签,使其能自适应。

4.2:ContentControl标签,切换主页面布局

4.3:mvvm架构,绑定数据以及操作

cs 复制代码
<Window x:Class="Device_Data.MainWindow"
        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"
        xmlns:local="clr-namespace:Device_Data"
        mc:Ignorable="d"
        Title="自动化设备" Height="auto" Width="auto" MinWidth="800" MinHeight="800" Background="#E1F5FE">
    <Grid>
        <DockPanel LastChildFill="true">
            <!--LastChildFill="true" true 最后一个子元素填充剩余空间-->
            <Border DockPanel.Dock="Left" BorderBrush="Black" BorderThickness="1" Width="100">
                <StackPanel>
                    <Button  Content="主页面" Height="30" Width="75" Margin="0,20,0,0"/>
                    <Button Click="show_console" Command="{Binding consoleView}" Content="输出" Height="30" Width="75" Margin="0,20,0,0"/>
                    <Button Click="show_data" Command="{Binding dataView}" Content="看板" Height="30" Width="75" Margin="0,20,0,0"/>
                </StackPanel>
            </Border>
            <Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1" Width="auto" Height="50">
                <StackPanel Orientation="Horizontal">
                    <Label Content="IP:" Height="auto" Width="auto" Margin="10"/>
                    <TextBox Name="IP_Data" Height="20" TextWrapping="Wrap" Text="192.168.10.246" Width="auto" MinWidth="100"/>
                    <Label Content="端口:" Height="auto" Width="auto" Margin="10"/>
                    <TextBox Name="Port" Height="20" TextWrapping="Wrap" Text="502" Width="auto" MinWidth="50"/>
                    <Button Click="link" Content="连接" Height="20" Width="56" Margin="50,10,1,10"/>
                    <Button Click="stop_link" Content="断开连接" Height="20" Width="56" Margin="20,10,1,10"/>
                    <Button Click="read_data" Content="开始接收" Height="20" Width="56" Margin="20,10,1,10"/>
                    <Button Click="stop_read" Content="停止接收" Height="20" Width="56" Margin="20,10,1,10"/>
                </StackPanel>
            </Border>
            <Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1" Width="auto" MinHeight="500">
                <ContentControl x:Name="mainConten"/>
            </Border>
            <Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1" Height="40">
                <StackPanel HorizontalAlignment="Left"  VerticalAlignment="Bottom" Height="30"  Width="auto" Orientation="Horizontal">
                    <Label Content="控制台" Height="28" Width="87" FontSize="15"/>
                    <Button Click="clear" Content="清空" Height="30" Width="87"/>
                </StackPanel>
            </Border>
            <Border DockPanel.Dock="Bottom" Background="White" BorderBrush="Black" BorderThickness="1" Width="auto" Height="auto"  Margin="10,10,10,10">
                <TextBox Name="result" TextWrapping="Wrap" Text="" TextChanged="TextBox_TextChanged"/>
            </Border>
        </DockPanel>

    </Grid>
</Window>

源码地址:c#WPFModbusmvvm应用资源-CSDN文库

相关推荐
熊猫钓鱼>_>22 分钟前
2025反爬虫之战札记:从robots.txt到多层防御的攻防进化史
开发语言·c++·爬虫
牛十二1 小时前
mac-intel操作系统go-stock项目(股票分析工具)安装与配置指南
开发语言·前端·javascript
励志不掉头发的内向程序员1 小时前
从零开始的python学习——文件
开发语言·python·学习
恒森宇电子有限公司1 小时前
IP5326_BZ 支持C同口输入输出的移动电源芯片 2.4A的充放电电流 支持4LED指示灯
c语言·开发语言·单片机
曼巴UE51 小时前
UE5.3 C++ 接口初步使用
开发语言·jvm·c++
奔跑的石头_2 小时前
GO语言的主要语法和特性
开发语言
liulilittle2 小时前
UNIX/macOS路由表查询原理与实现
服务器·开发语言·c++·macos·unix·编程语言
HUST2 小时前
C语言 第三讲:分支和循环(上)
c语言·开发语言
Dovis(誓平步青云)3 小时前
《探索C++11:现代语法的性能优化策略(中篇)》
开发语言·c++
再努力"亿"点点3 小时前
爬取m3u8视频完整教程
开发语言·python