From 990d27d32fb8422259fc007498047da686d472e8 Mon Sep 17 00:00:00 2001 From: wenjy Date: Mon, 23 Jun 2025 15:58:06 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E7=A8=8B=E5=BA=8F=E8=B0=83?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Business/SerialBusiness.cs | 134 ++++++ SlnMesnac.Config/RfidConfig.cs | 5 + SlnMesnac.TouchSocket/TouchSocketSetup.cs | 11 +- SlnMesnac.WPF/App.xaml.cs | 28 ++ SlnMesnac.WPF/MainWindow.xaml | 10 +- SlnMesnac.WPF/MainWindow.xaml.cs | 8 +- SlnMesnac.WPF/Page/IndexControl.xaml | 54 +-- .../ProductDetails/DetailsEditControl.xaml | 91 ++-- .../ProductDetails/ProductDetailsControl.xaml | 108 +++-- SlnMesnac.WPF/SlnMesnac.WPF.csproj | 11 +- .../Templates/icon/certification.jpg | Bin 0 -> 1860 bytes .../ViewModel/Index/IndexViewModel.cs | 435 +++++++++++++++++- SlnMesnac.WPF/appsettings.json | 38 +- 13 files changed, 775 insertions(+), 158 deletions(-) create mode 100644 SlnMesnac.Business/SerialBusiness.cs create mode 100644 SlnMesnac.WPF/Templates/icon/certification.jpg diff --git a/SlnMesnac.Business/SerialBusiness.cs b/SlnMesnac.Business/SerialBusiness.cs new file mode 100644 index 0000000..b65687f --- /dev/null +++ b/SlnMesnac.Business/SerialBusiness.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.IO.Ports; +using System.Text; +using System.Threading; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2025 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:T14-GEN3-7895 +* 命名空间:SlnMesnac.Business +* 唯一标识:a0333022-6db6-4dd1-8aa7-bc46b57b6050 +* +* 创建者:WenJY +* 电子邮箱: +* 创建时间:2025-06-23 13:50:07 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Business +{ + public class SerialBusiness + { + private SerialPort serialPort; + + public SerialBusiness() + { + serialPort = new SerialPort("COM6", 115200, Parity.None, 8, StopBits.One); + //serialPort.DataReceived += SerialPort_DataReceived; + } + + private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + // 数据接收事件处理 + // 这里可以根据需要处理接收到的数据 + } + + public void OpenPort() + { + try + { + if (!serialPort.IsOpen) + { + serialPort.Open(); + } + } + catch (Exception ex) + { + Console.WriteLine($"打开串口时出错: {ex.Message}"); + } + } + + public void ClosePort() + { + if (serialPort.IsOpen) + { + serialPort.Close(); + } + } + + private byte[] receiveBuffer = new byte[1024]; + private int bufferIndex = 0; + + public byte[] SendDataAndWaitForResponse(byte[] dataToSend) + { + if (!serialPort.IsOpen) + { + throw new InvalidOperationException("串口未打开。"); + } + + // 清空接收缓冲区 + serialPort.DiscardInBuffer(); + bufferIndex = 0; + + byte[] receivedData = new byte[0]; + AutoResetEvent dataReceivedEvent = new AutoResetEvent(false); + + serialPort.DataReceived += (sender, e) => + { + int bytesToRead = serialPort.BytesToRead; + serialPort.Read(receiveBuffer, bufferIndex, bytesToRead); + bufferIndex += bytesToRead; + + + if (bufferIndex >= 5) + { + receivedData = new byte[bufferIndex]; + Array.Copy(receiveBuffer, receivedData, bufferIndex); + dataReceivedEvent.Set(); + } + }; + + try + { + // 发送数据 + serialPort.Write(dataToSend, 0, dataToSend.Length); + + // 等待数据返回,最多等待 5 秒 + if (!dataReceivedEvent.WaitOne(5000)) + { + throw new TimeoutException("在 5 秒内未收到响应。"); + } + + return receivedData; + } + finally + { + // 移除事件处理程序 + serialPort.DataReceived -= (sender, e) => + { + int bytesToRead = serialPort.BytesToRead; + serialPort.Read(receiveBuffer, bufferIndex, bytesToRead); + bufferIndex += bytesToRead; + + if (bufferIndex >= 10) + { + receivedData = new byte[bufferIndex]; + Array.Copy(receiveBuffer, receivedData, bufferIndex); + dataReceivedEvent.Set(); + } + }; + } + } + } +} diff --git a/SlnMesnac.Config/RfidConfig.cs b/SlnMesnac.Config/RfidConfig.cs index 87622ea..3fb6f04 100644 --- a/SlnMesnac.Config/RfidConfig.cs +++ b/SlnMesnac.Config/RfidConfig.cs @@ -43,6 +43,11 @@ namespace SlnMesnac.Config /// 设备 Key /// public string? equipKey { get; set; } + + /// + /// 连接会话 + /// + public object? session { get;set; } /// /// 是否启用 diff --git a/SlnMesnac.TouchSocket/TouchSocketSetup.cs b/SlnMesnac.TouchSocket/TouchSocketSetup.cs index 637d77f..9b7549e 100644 --- a/SlnMesnac.TouchSocket/TouchSocketSetup.cs +++ b/SlnMesnac.TouchSocket/TouchSocketSetup.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using System.Text; using Microsoft.AspNetCore.Builder; using TouchSocket.Sockets; +using TouchSocket.Core; +using SlnMesnac.Config; #region << 版 本 注 释 >> /*-------------------------------------------------------------------- @@ -35,14 +37,11 @@ namespace SlnMesnac.TouchSocket public static class TouchSocketSetup { - public static IApplicationBuilder UseTouchSocketExtensions(this IApplicationBuilder app) + public static async void UseTouchSocketExtensions(this IServiceProvider service) { - var _server = app.ApplicationServices.GetService(); - _server.Init(20108); + var ap = service.GetService(); - var _apiServer = app.ApplicationServices.GetService(); - _apiServer.Init(); - return app; + } } } \ No newline at end of file diff --git a/SlnMesnac.WPF/App.xaml.cs b/SlnMesnac.WPF/App.xaml.cs index 09d82d0..3eeba18 100644 --- a/SlnMesnac.WPF/App.xaml.cs +++ b/SlnMesnac.WPF/App.xaml.cs @@ -7,6 +7,7 @@ using System.Windows; using Microsoft.Extensions.Configuration; using SlnMesnac.Extensions; using SlnMesnac.Serilog; +using SlnMesnac.TouchSocket; using System.Reflection; using TouchSocket.Sockets; using SlnMesnac.WPF.Attribute; @@ -14,6 +15,7 @@ using SlnMesnac.WPF.Page.Login; using Prism.Events; using SlnMesnac.WPF.Event; using AduSkin.Controls.Metro; +using TouchSocket.Core; namespace SlnMesnac.WPF { @@ -58,6 +60,8 @@ namespace SlnMesnac.WPF // 配置Serilog和其他扩展 ServiceProvider.UseSerilogExtensions(); + ServiceProvider.UseTouchSocketExtensions(); + //通知弹窗 NoticeManager.Initialize(); @@ -85,9 +89,33 @@ namespace SlnMesnac.WPF .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfiguration configuration = configurationBuilder.Build(); var ap = configuration.GetSection("AppConfig").Get(); + + foreach (var item in ap.rfidConfig) + { + if (item.isFlage) + { + TcpClient tcpClient = new TcpClient(); + + var hashcode = tcpClient.GetHashCode(); + + tcpClient.Setup(new TouchSocketConfig() + .SetRemoteIPHost($"{item.equipIp}:{item.equipPort}")); + + tcpClient.Connect(); + + Result result = tcpClient.TryConnect(); + if (result.IsSuccess()) + { + item.session = tcpClient; + } + var hashcode2 = tcpClient.GetHashCode(); + } + } + return ap; }); + services.AddSingleton(typeof(SerilogHelper)); Assembly[] assemblies = { diff --git a/SlnMesnac.WPF/MainWindow.xaml b/SlnMesnac.WPF/MainWindow.xaml index 63e83bd..ea0ada2 100644 --- a/SlnMesnac.WPF/MainWindow.xaml +++ b/SlnMesnac.WPF/MainWindow.xaml @@ -30,7 +30,7 @@ - + @@ -38,8 +38,8 @@ x:Name="Index" Command="{Binding ControlOnClickCommand}" CommandParameter="{Binding Name,ElementName=Index}"> - - + + @@ -49,8 +49,8 @@ x:Name="Set" Command="{Binding ControlOnClickCommand}" CommandParameter="{Binding Name,ElementName=Set}"> - - + + diff --git a/SlnMesnac.WPF/MainWindow.xaml.cs b/SlnMesnac.WPF/MainWindow.xaml.cs index 0e790b5..658fceb 100644 --- a/SlnMesnac.WPF/MainWindow.xaml.cs +++ b/SlnMesnac.WPF/MainWindow.xaml.cs @@ -2,6 +2,7 @@ using SlnMesnac.WPF.ViewModel; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -26,8 +27,13 @@ namespace SlnMesnac.WPF public MainWindow(MainWindowViewModel mainWindowViewModel) { InitializeComponent(); - + this.Closing += Application_Closing; this.DataContext = mainWindowViewModel; } + private void Application_Closing(object sender, CancelEventArgs e) + { + Application.Current.Shutdown(); + // 在此处执行退出事件处理逻辑 + } } } diff --git a/SlnMesnac.WPF/Page/IndexControl.xaml b/SlnMesnac.WPF/Page/IndexControl.xaml index 8769c88..77e31cc 100644 --- a/SlnMesnac.WPF/Page/IndexControl.xaml +++ b/SlnMesnac.WPF/Page/IndexControl.xaml @@ -52,32 +52,32 @@ --> - + - + -