Compare commits

...

2 Commits

Author SHA1 Message Date
bunny 6fc5ca253e 🎉 数据源展示 2025-06-23 23:05:34 +08:00
bunny 4e1d211e40 🎉 串口基础API 2025-06-23 22:26:07 +08:00
13 changed files with 285 additions and 0 deletions

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>Base_1_Create</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.IO.Ports" Version="5.0.1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,35 @@
using System;
using System.IO.Ports;
namespace Base_1_Create
{
internal class Program
{
private static void Main(string[] args)
{
var serialPort = new SerialPort
{
// 串口名称
PortName = "COM1",
// 值越大传输越快默认9600
BaudRate = 9600,
// 数据位
DataBits = 8,
// 默认是 1
StopBits = StopBits.One,
// 校验位,校验位 1 的个数
Parity = Parity.Odd,
// 读取时缓冲区字节数
ReadBufferSize = 4096
};
// 打开串口
serialPort.Open();
if (serialPort.IsOpen) Console.WriteLine("串口打开");
// 关闭串口
// serialPort.Close();
}
}
}

View File

@ -0,0 +1,9 @@
<Application x:Class="Base_2_Window.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Base_2_Window"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

View File

@ -0,0 +1,12 @@
using System.Configuration;
using System.Data;
using System.Windows;
namespace Base_2_Window;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

View File

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

View File

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<RootNamespace>Base_2_Window</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.IO.Ports">
<HintPath>..\..\..\..\..\..\..\software\Plugins\nuget\system.io.ports\5.0.1\ref\netstandard2.0\System.IO.Ports.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.IO.Ports" Version="6.0.0"/>
</ItemGroup>
</Project>

View File

@ -0,0 +1,14 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Base_2_Window.MVVM;
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

View File

@ -0,0 +1,73 @@
<Window x:Class="Base_2_Window.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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Width="200" Margin="5">
<StackPanel Orientation="Horizontal" Margin="0,0,0,5">
<Label Width="60" VerticalAlignment="Center">串口:</Label>
<ComboBox Width="100" Height="28" ItemsSource="{Binding SerialPortNames}" SelectedIndex="0" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,5">
<Label Width="60" VerticalAlignment="Center">波特率:</Label>
<ComboBox Width="100" Height="28" ItemsSource="{Binding BaudRates}" SelectedIndex="0" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,5">
<Label Width="60" VerticalAlignment="Center">数据位:</Label>
<ComboBox Width="100" Height="28" ItemsSource="{Binding DataBits}" SelectedIndex="0" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,5">
<Label Width="60" VerticalAlignment="Center">校验位:</Label>
<ComboBox Width="100" Height="28" ItemsSource="{Binding ParityList}" SelectedIndex="0" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,10">
<Label Width="60" VerticalAlignment="Center">停止位:</Label>
<ComboBox Width="100" Height="28" ItemsSource="{Binding StopBitsList}" SelectedIndex="0" />
</StackPanel>
<Button Height="32" Margin="0,5,0,0" FontWeight="Bold" Command="{Binding }">打开串口</Button>
</StackPanel>
<!-- 右侧通信面板 -->
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- 消息显示区域 -->
<GroupBox Grid.Row="0" Header="消息显示" Margin="0,0,0,5">
<TextBox Name="MessageBox"
AcceptsReturn="True"
IsReadOnly="True"
VerticalScrollBarVisibility="Auto"
FontFamily="Consolas" />
</GroupBox>
<!-- 消息发送区域 -->
<GroupBox Grid.Row="1" Header="发送消息">
<DockPanel>
<Button DockPanel.Dock="Right"
Width="80"
Margin="5,0,0,0"
FontWeight="Bold">
发送
</Button>
<TextBox AcceptsReturn="True" VerticalScrollBarVisibility="Auto" />
</DockPanel>
</GroupBox>
</Grid>
</Grid>
</Window>

View File

@ -0,0 +1,26 @@
using System.IO.Ports;
using System.Windows;
using Base_2_Window.ViewModel;
namespace Base_2_Window;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel
{
SerialPortNames = SerialPort.GetPortNames().Select(s => s).ToList(),
BaudRates = new List<int> { 9600 },
DataBits = new List<int> { 6, 7, 8 },
StopBitsList = new List<StopBits> { StopBits.None, StopBits.One, StopBits.Two, StopBits.OnePointFive },
ParityList = new List<Parity>
{ Parity.None, Parity.Even, Parity.Mark, Parity.None, Parity.Odd, Parity.Space }
};
}
}

View File

@ -0,0 +1,14 @@
using System.IO.Ports;
namespace Base_2_Window.Model;
public class SerialPortModel
{
public List<string>? SerialPortNames { get; set; }
public int? BaudRate { get; set; }
public int? DataBits { get; set; }
public StopBits? StopBits { get; set; }
}

View File

@ -0,0 +1,17 @@
using System.IO.Ports;
using Base_2_Window.MVVM;
namespace Base_2_Window.ViewModel;
public class MainWindowViewModel : ViewModelBase
{
public List<string>? SerialPortNames { get; set; }
public List<int>? BaudRates { get; set; }
public List<StopBits>? StopBitsList { get; set; }
public List<Parity>? ParityList { get; set; }
public List<int>? DataBits { get; set; }
}

View File

@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36212.18 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ModuleBus", "ModuleBus", "{6C2EABF6-5D1B-41D1-9BA2-3AF2798FF5BB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Base", "Base", "{62565C31-44DB-4369-A8B1-76772A3CFA22}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Base-1-Create", "Base-1-Create\Base-1-Create.csproj", "{82D48C39-53F1-42D1-8EF7-74D589C06A64}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Base-2-Window", "Base-2-Window\Base-2-Window.csproj", "{09CDF9A7-1DA1-4452-94F5-E150B4AE15F4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{82D48C39-53F1-42D1-8EF7-74D589C06A64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{82D48C39-53F1-42D1-8EF7-74D589C06A64}.Debug|Any CPU.Build.0 = Debug|Any CPU
{82D48C39-53F1-42D1-8EF7-74D589C06A64}.Release|Any CPU.ActiveCfg = Release|Any CPU
{82D48C39-53F1-42D1-8EF7-74D589C06A64}.Release|Any CPU.Build.0 = Release|Any CPU
{09CDF9A7-1DA1-4452-94F5-E150B4AE15F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{09CDF9A7-1DA1-4452-94F5-E150B4AE15F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09CDF9A7-1DA1-4452-94F5-E150B4AE15F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09CDF9A7-1DA1-4452-94F5-E150B4AE15F4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{82D48C39-53F1-42D1-8EF7-74D589C06A64} = {62565C31-44DB-4369-A8B1-76772A3CFA22}
{09CDF9A7-1DA1-4452-94F5-E150B4AE15F4} = {62565C31-44DB-4369-A8B1-76772A3CFA22}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AParity_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Feabc9cb4ff56410badff95faf89658e09778_003F32_003F33c960a1_003FParity_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASerialPort_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Feabc9cb4ff56410badff95faf89658e09778_003Ff6_003F34911093_003FSerialPort_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStopBits_002Ecs_002Fl_003AC_0021_003FUsers_003F13199_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Feabc9cb4ff56410badff95faf89658e09778_003Fc5_003F355efbd9_003FStopBits_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>