From 0d742100c5d753d597bbd2ba4308c77c49060ff3 Mon Sep 17 00:00:00 2001 From: SoulStar Date: Tue, 26 Aug 2025 10:16:00 +0800 Subject: [PATCH] =?UTF-8?q?feat=20-=20=E6=B7=BB=E5=8A=A0=E4=B8=B2=E5=8F=A3?= =?UTF-8?q?=20=E6=B7=BB=E5=8A=A0=E8=BD=AF=E4=BB=B6=E7=A7=B0=E9=87=8F?= =?UTF-8?q?=E9=80=BB=E8=BE=91=20=E6=B7=BB=E5=8A=A0=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=BA=A4=E4=BA=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HighWayIot.Common/HighWayIot.Common.csproj | 2 + .../LANConnectConfig/LANConnect.cs | 124 +++++++++ HighWayIot.Common/XmlConfig/XmlUtil.cs | 135 ++++++++++ .../HighWayIot.TouchSocket.csproj | 27 +- HighWayIot.TouchSocket/TouchSocketCOM.cs | 73 ++++++ .../TouchSocketTcpClient.cs | 152 ----------- HighWayIot.TouchSocket/app.config | 2 +- HighWayIot.TouchSocket/packages.config | 7 +- HighWayIot.Winform/Configuration.xml | 42 +-- HighWayIot.Winform/FodyWeavers.xml | 3 + .../HighWayIotWeightUpload.csproj | 35 ++- .../MainForm/LoginForm.Designer.cs | 55 ---- HighWayIot.Winform/MainForm/LoginForm.cs | 45 ---- .../MainForm/MainForm.Designer.cs | 239 ++++++++++++++++++ HighWayIot.Winform/MainForm/MainForm.cs | 174 +++++++++++++ .../{LoginForm.resx => MainForm.resx} | 0 HighWayIot.Winform/Program.cs | 2 +- HighWayIot.Winform/packages.config | 2 + 18 files changed, 829 insertions(+), 290 deletions(-) create mode 100644 HighWayIot.Common/LANConnectConfig/LANConnect.cs create mode 100644 HighWayIot.Common/XmlConfig/XmlUtil.cs create mode 100644 HighWayIot.TouchSocket/TouchSocketCOM.cs delete mode 100644 HighWayIot.TouchSocket/TouchSocketTcpClient.cs create mode 100644 HighWayIot.Winform/FodyWeavers.xml delete mode 100644 HighWayIot.Winform/MainForm/LoginForm.Designer.cs delete mode 100644 HighWayIot.Winform/MainForm/LoginForm.cs create mode 100644 HighWayIot.Winform/MainForm/MainForm.Designer.cs create mode 100644 HighWayIot.Winform/MainForm/MainForm.cs rename HighWayIot.Winform/MainForm/{LoginForm.resx => MainForm.resx} (100%) diff --git a/HighWayIot.Common/HighWayIot.Common.csproj b/HighWayIot.Common/HighWayIot.Common.csproj index 5f13f11..5672ec6 100644 --- a/HighWayIot.Common/HighWayIot.Common.csproj +++ b/HighWayIot.Common/HighWayIot.Common.csproj @@ -47,7 +47,9 @@ + + diff --git a/HighWayIot.Common/LANConnectConfig/LANConnect.cs b/HighWayIot.Common/LANConnectConfig/LANConnect.cs new file mode 100644 index 0000000..a3d2f9d --- /dev/null +++ b/HighWayIot.Common/LANConnectConfig/LANConnect.cs @@ -0,0 +1,124 @@ +using HighWayIot.Common.XmlConfig; +using HighWayIot.Log4net; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Common.LANConnectConfig +{ + /// + /// 局域网连接配置类 + /// + public class LANConnect + { + + private static readonly Lazy lazy = new Lazy(() => new LANConnect()); + + public static LANConnect Instance + { + get + { + return lazy.Value; + } + } + + private static LogHelper _logHelper = LogHelper.Instance; + + XmlUtil xmlConfig = XmlUtil.Instance; + + /// + /// 读取网络文件夹中的txt文件 + /// + /// 路径 + /// txt名称 + /// + public string ReadTxt(string path, string txtName) + { + string sourceFile = Path.Combine(path, txtName); + try + { + if (File.Exists(sourceFile)) + { + string content = File.ReadAllText(sourceFile); + _logHelper.Info("读取到内容:" + content); + return content; + } + else + { + _logHelper.Error("源文件不存在: " + sourceFile); + return string.Empty; + } + } + catch (Exception ex) + { + _logHelper.Error("读取文件发生错误", ex); + return string.Empty; + } + } + + /// + /// 向网络文件夹内写入txt文件 + /// + /// 写入路径 + /// txt名称 + /// 写入内容 + /// + public bool WriteTxt(string path, string txtName, string content) + { + string newFile = Path.Combine(path, txtName); + try + { + File.WriteAllText(newFile, content); + _logHelper.Info("已写入文件:" + newFile); + return true; + } + catch (Exception ex) + { + _logHelper.Error("写入文件发生错误", ex); + return false; + } + } + + /// + /// 移动文件到一个新位置 + /// + /// 源路径 + /// 目标路径 + /// 文件名称 + /// + public bool MoveFile(string sourcePath, string targetPath, string fileName) + { + // 3. 剪切(移动)文件到另一个网络文件夹 + string moveFileSource = Path.Combine(sourcePath, fileName); + string moveFileTarget = Path.Combine(targetPath, fileName); + try + { + // 先创建一个待移动的文件 + if (File.Exists(moveFileSource)) + { + if (File.Exists(moveFileTarget)) + { + File.Delete(moveFileTarget); // 目标存在就先删除,避免冲突 + } + File.Move(moveFileSource, moveFileTarget); + _logHelper.Info("已移动文件到:" + moveFileTarget); + return true; + } + else + { + _logHelper.Error("源文件不存在"); + return false; + } + } + catch (Exception ex) + { + _logHelper.Error("移动文件发生错误", ex); + return false; + } + } + } +} diff --git a/HighWayIot.Common/XmlConfig/XmlUtil.cs b/HighWayIot.Common/XmlConfig/XmlUtil.cs new file mode 100644 index 0000000..4960a30 --- /dev/null +++ b/HighWayIot.Common/XmlConfig/XmlUtil.cs @@ -0,0 +1,135 @@ +using HighWayIot.Log4net; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace HighWayIot.Common.XmlConfig +{ + /// + /// XML读写类 + /// + public class XmlUtil + { + private static readonly Lazy lazy = new Lazy(() => new XmlUtil()); + + public static XmlUtil Instance + { + get + { + return lazy.Value; + } + } + + /// + /// XML读写实例 + /// + XmlDocument xmlDocument = new XmlDocument(); + + /// + /// 运行时路径 + /// + private string Path = System.Environment.CurrentDirectory; + + /// + /// 设备配置读取 + /// + /// + public List ConfigReader() + { + List list = new List(); + + xmlDocument.Load($"{Path}\\Configuration.xml"); + XmlNode root = xmlDocument.DocumentElement; + XmlNode node = root.SelectSingleNode("DeviceConfig"); + foreach (XmlNode role in node) + { + XmlAttribute pageName = (XmlAttribute)role.Attributes.GetNamedItem("Name"); + list.Add(pageName.Value); + } + + return list; + } + + /// + /// 初级目录InnerText配置读取 + /// + /// + public string BaseInnerTextReader(string label) + { + xmlDocument.Load($"{Path}\\Configuration.xml"); + XmlNode root = xmlDocument.DocumentElement; + XmlNode node = root.SelectSingleNode(label); + string path = node.InnerText; + return path; + } + + public void ExportPathWriter(string path) + { + //xmlDocument.Load($"{Path}\\Configuration.xml"); + //XmlNode root = xmlDocument.DocumentElement; + //XmlNode node = root.SelectSingleNode("ExportConfig"); + //node.Attributes["Path"].Value = path; + //xmlDocument.Save($"{Path}\\Configuration.xml"); + + // 参数验证 + if (string.IsNullOrWhiteSpace(path)) + { + throw new ArgumentNullException(nameof(path), "导出路径不能为空"); + } + + try + { + // 加载XML文档 + xmlDocument.Load($"{Path}\\Configuration.xml"); + + // 获取根节点 + XmlNode root = xmlDocument.DocumentElement ?? + throw new InvalidOperationException("XML文档没有根元素"); + + // 查找ExportConfig节点 + XmlNode node = root.SelectSingleNode("ExportConfig") ?? + throw new InvalidOperationException("找不到ExportConfig节点"); + + // 获取Path属性 + XmlAttribute pathAttribute = node.Attributes?["Path"] ?? + throw new InvalidOperationException("ExportConfig节点没有Path属性"); + + // 设置新值 + pathAttribute.Value = path; + + // 保存修改 + xmlDocument.Save($"{Path}\\Configuration.xml"); + } + catch (Exception ex) + { + // 记录日志或处理特定异常 + LogHelper.Instance.Error("保存导出路径配置失败", ex); + } + } + } + + + public class RoleConfig + { + /// + /// 页面名称 + /// + public string PageName { get; set; } + + /// + /// 规则编号 + /// + public int RoleIndex { get; set; } + } + + public class ExportPathConfig + { + public string ExportConfig { get; set; } + + public string Config { get; set; } + } +} diff --git a/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj b/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj index c323ccd..06a0fd8 100644 --- a/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj +++ b/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj @@ -13,6 +13,8 @@ 512 true + + true @@ -40,6 +42,9 @@ ..\packages\System.Buffers.4.6.1\lib\net462\System.Buffers.dll + + ..\packages\System.IO.Ports.8.0.0\lib\net462\System.IO.Ports.dll + ..\packages\System.Memory.4.6.3\lib\net462\System.Memory.dll @@ -59,16 +64,19 @@ - - ..\packages\TouchSocket.3.0.12\lib\net472\TouchSocket.dll + + ..\packages\TouchSocket.3.1.15\lib\net472\TouchSocket.dll - - ..\packages\TouchSocket.Core.3.0.12\lib\net472\TouchSocket.Core.dll + + ..\packages\TouchSocket.Core.3.1.15\lib\net472\TouchSocket.Core.dll + + + ..\packages\TouchSocket.SerialPorts.3.1.15\lib\net472\TouchSocket.SerialPorts.dll - + @@ -81,7 +89,14 @@ - + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + \ No newline at end of file diff --git a/HighWayIot.TouchSocket/TouchSocketCOM.cs b/HighWayIot.TouchSocket/TouchSocketCOM.cs new file mode 100644 index 0000000..f2399bf --- /dev/null +++ b/HighWayIot.TouchSocket/TouchSocketCOM.cs @@ -0,0 +1,73 @@ +using HighWayIot.Log4net; +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core; +using TouchSocket.SerialPorts; +using TouchSocket.Sockets; + +namespace HighWayIot.TouchSocket +{ + public class TouchSocketCOM + { + /// + /// 日志 + /// + private static LogHelper _logHelper = LogHelper.Instance; + + /// + /// 懒加载 + /// + private static readonly Lazy lazy = new Lazy(() => new TouchSocketCOM()); + + public static TouchSocketCOM Instance => lazy.Value; + + public Action ComAction; + + /// + /// 创建串口连接 + /// + public async Task CreateConnection() + { + try + { + var client = new SerialPortClient(); + client.Connecting = (c, e) => { return EasyTask.CompletedTask; };//即将连接到端口 + client.Connected = (c, e) => { return EasyTask.CompletedTask; };//成功连接到端口 + client.Closing = (c, e) => { return EasyTask.CompletedTask; };//即将从端口断开连接。此处仅主动断开才有效。 + client.Closed = (c, e) => { return EasyTask.CompletedTask; };//从端口断开连接,当连接不成功时不会触发。 + client.Received = (c, e) => + { + //收到信息 + string mes = e.ByteBlock.Span.ToString(Encoding.UTF8); + ComAction.Invoke(mes); + return EasyTask.CompletedTask; + }; + + await client.SetupAsync(new TouchSocketConfig() + .SetSerialPortOption(new SerialPortOption() + { + BaudRate = 9600,//波特率 + DataBits = 8,//数据位 + Parity = System.IO.Ports.Parity.None,//校验位 + PortName = "COM5",//COM + StopBits = System.IO.Ports.StopBits.One//停止位 + }) + .SetSerialDataHandlingAdapter(() => new PeriodPackageAdapter() { CacheTimeout = TimeSpan.FromMilliseconds(100) }) + .ConfigurePlugins(a => + { + //a.Add(); + })); + + await client.ConnectAsync(); + _logHelper.Info("COM5连接成功"); + } + catch (Exception ex) + { + _logHelper.Error("连接失败", ex); + } + } + } +} diff --git a/HighWayIot.TouchSocket/TouchSocketTcpClient.cs b/HighWayIot.TouchSocket/TouchSocketTcpClient.cs deleted file mode 100644 index 13a3900..0000000 --- a/HighWayIot.TouchSocket/TouchSocketTcpClient.cs +++ /dev/null @@ -1,152 +0,0 @@ -using HighWayIot.Log4net; -using System; -using System.Collections.Generic; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; -using TouchSocket.Core; -using TouchSocket.Sockets; -using TcpClient = TouchSocket.Sockets.TcpClient; - -namespace HighWayIot.TouchSocket -{ - public class TouchSocketTcpClient - { - /// - /// 日志 - /// - private static LogHelper _logHelper = LogHelper.Instance; - - /// - /// 懒加载 - /// - private static readonly Lazy lazy = new Lazy(() => new TouchSocketTcpClient()); - - public static TouchSocketTcpClient Instance => lazy.Value; - - /// - /// 所有的客户端 - /// - public Dictionary Clients = new Dictionary(); - - public int ClientsCount = 0; - - public Action GetMessageAction; - - public bool CreateTcpClient(string ip, string port) - { - TcpClient tcpClient = new TcpClient(); - - tcpClient.Connecting = (client, e) => - { - return EasyTask.CompletedTask; - };//有客户端正在连接 - tcpClient.Connected = (client, e) => - { - return EasyTask.CompletedTask; - };//有客户端成功连接 - tcpClient.Closing = (client, e) => - { - return EasyTask.CompletedTask; - };//有客户端正在断开连接,只有当主动断开时才有效。 - tcpClient.Closed = (client, e) => - { - return EasyTask.CompletedTask; - };//有客户端断开连接 - tcpClient.Received = (client, e) => - { - GetMessageAction.Invoke(e.ByteBlock.Span.ToArray(), client.IP); - return EasyTask.CompletedTask; - }; //接收信号 - - tcpClient.SetupAsync(new TouchSocketConfig() - .SetRemoteIPHost($"{ip}:{port}") - .ConfigureContainer(a => - { - a.AddConsoleLogger();//添加一个日志注入 - }) - .ConfigurePlugins(a => - { - a.UseTcpReconnection() - .UsePolling(TimeSpan.FromSeconds(1)); - }) - ); - - Result result = Result.Default; //不断尝试重连 - do - { - _logHelper.Info($"连接{ip}:{port}"); - result = tcpClient.TryConnect(); - Task.Delay(1000).Wait(); - } - while (!result.IsSuccess); - _logHelper.Info($"{ip}:{port}连接成功 {++ClientsCount}"); - if (Clients.ContainsKey(ip)) - { - Clients.Remove(ip); - Clients.Add(ip, tcpClient); - } - else - { - Clients.Add(ip, tcpClient); - } - - return true; - } - - /// - /// 信息发送 - /// - /// Bytes - /// - public bool Send(string ip, byte[] message) - { - try - { - if (Clients.ContainsKey(ip)) - { - Clients[ip].SendAsync(message); - return true; - } - else - { - return false; - } - } - catch (Exception e) - { - _logHelper.Error("发送数据发生错误" + e); - return false; - } - } - - /// - /// 客户端释放 - /// - /// - /// - public bool DisposeClient(string ip) - { - try - { - if (Clients.ContainsKey(ip)) - { - Clients[ip].Close(); - Clients[ip].Dispose(); - Clients.Remove(ip); - } - else - { - return false; - } - return true; - } - catch (Exception e) - { - _logHelper.Error("释放客户端发生错误" + e); - return false; - } - } - - } -} diff --git a/HighWayIot.TouchSocket/app.config b/HighWayIot.TouchSocket/app.config index 6e60b15..2e0e972 100644 --- a/HighWayIot.TouchSocket/app.config +++ b/HighWayIot.TouchSocket/app.config @@ -12,7 +12,7 @@ - + diff --git a/HighWayIot.TouchSocket/packages.config b/HighWayIot.TouchSocket/packages.config index a22d9c4..f425c44 100644 --- a/HighWayIot.TouchSocket/packages.config +++ b/HighWayIot.TouchSocket/packages.config @@ -2,10 +2,13 @@ + - - + + + + \ No newline at end of file diff --git a/HighWayIot.Winform/Configuration.xml b/HighWayIot.Winform/Configuration.xml index 4ffb70a..6957aa3 100644 --- a/HighWayIot.Winform/Configuration.xml +++ b/HighWayIot.Winform/Configuration.xml @@ -1,23 +1,27 @@  - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + \\RK90004438\Users\ADMIN\Desktop\pod\Imadumb\input + \\RK90004438\Users\ADMIN\Desktop\pod\Imadumb\archive + \\RK90004438\Users\ADMIN\Desktop\pod\Imadumb\output + administrator + cc123456@ diff --git a/HighWayIot.Winform/FodyWeavers.xml b/HighWayIot.Winform/FodyWeavers.xml new file mode 100644 index 0000000..5029e70 --- /dev/null +++ b/HighWayIot.Winform/FodyWeavers.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/HighWayIot.Winform/HighWayIotWeightUpload.csproj b/HighWayIot.Winform/HighWayIotWeightUpload.csproj index e2b1d49..9c6536d 100644 --- a/HighWayIot.Winform/HighWayIotWeightUpload.csproj +++ b/HighWayIot.Winform/HighWayIotWeightUpload.csproj @@ -1,5 +1,6 @@  + Debug @@ -28,6 +29,8 @@ false true true + + AnyCPU @@ -70,6 +73,9 @@ ..\packages\BouncyCastle.Cryptography.2.3.1\lib\net461\BouncyCastle.Cryptography.dll + + ..\packages\Costura.Fody.6.0.0\lib\netstandard2.0\Costura.dll + ..\packages\Enums.NET.4.0.1\lib\net45\Enums.NET.dll @@ -147,16 +153,16 @@ - + Form - - LoginForm.cs + + MainForm.cs - - LoginForm.cs + + MainForm.cs ResXFileCodeGenerator @@ -168,7 +174,6 @@ Resources.resx True - SettingsSingleFileGenerator @@ -187,6 +192,10 @@ + + {89a1edd9-d79e-468d-b6d3-7d07b8843562} + HighWayIot.Common + {DDEE27AA-0694-47A6-9335-E9308261F63A} HighWayIot.Controls @@ -240,8 +249,16 @@ false - - - + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + + + \ No newline at end of file diff --git a/HighWayIot.Winform/MainForm/LoginForm.Designer.cs b/HighWayIot.Winform/MainForm/LoginForm.Designer.cs deleted file mode 100644 index 881fb88..0000000 --- a/HighWayIot.Winform/MainForm/LoginForm.Designer.cs +++ /dev/null @@ -1,55 +0,0 @@ - -using System.Drawing; -using System.Windows.Forms; - -namespace HighWayIot.Winform.MainForm -{ - partial class LoginForm - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LoginForm)); - this.SuspendLayout(); - // - // LoginForm - // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(413, 407); - this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); - this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); - this.Name = "LoginForm"; - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "基材重量上传"; - this.Load += new System.EventHandler(this.LoginForm_Load); - this.ResumeLayout(false); - - } - - #endregion - } -} diff --git a/HighWayIot.Winform/MainForm/LoginForm.cs b/HighWayIot.Winform/MainForm/LoginForm.cs deleted file mode 100644 index 629eafd..0000000 --- a/HighWayIot.Winform/MainForm/LoginForm.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace HighWayIot.Winform.MainForm -{ - public partial class LoginForm : Form - { - - public LoginForm() - { - InitializeComponent(); - - } - - - - /// - /// 登陆验证 - /// - /// - /// - private void Login_Click(object sender, EventArgs e) - { - - } - - /// - /// 自动登录 - /// - /// - /// - private void LoginForm_Load(object sender, EventArgs e) - { - - } - } -} diff --git a/HighWayIot.Winform/MainForm/MainForm.Designer.cs b/HighWayIot.Winform/MainForm/MainForm.Designer.cs new file mode 100644 index 0000000..8ea36b0 --- /dev/null +++ b/HighWayIot.Winform/MainForm/MainForm.Designer.cs @@ -0,0 +1,239 @@ + +using System.Drawing; +using System.Windows.Forms; + +namespace HighWayIot.Winform.MainForm +{ + partial class MainForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); + this.StartWeightButon = new System.Windows.Forms.Button(); + this.UploadResultButton = new System.Windows.Forms.Button(); + this.LogBox = new System.Windows.Forms.ListBox(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.label1 = new System.Windows.Forms.Label(); + this.WeightResultTextBox = new System.Windows.Forms.TextBox(); + this.WeightCountComboBox = new System.Windows.Forms.ComboBox(); + this.label2 = new System.Windows.Forms.Label(); + this.RollNumberTextBox = new System.Windows.Forms.TextBox(); + this.label3 = new System.Windows.Forms.Label(); + this.TestButton = new System.Windows.Forms.Button(); + this.ReadTestButton = new System.Windows.Forms.Button(); + this.CutTestButton = new System.Windows.Forms.Button(); + this.WriteInTestButton = new System.Windows.Forms.Button(); + this.groupBox1.SuspendLayout(); + this.SuspendLayout(); + // + // StartWeightButon + // + this.StartWeightButon.Font = new System.Drawing.Font("宋体", 12F); + this.StartWeightButon.Location = new System.Drawing.Point(108, 333); + this.StartWeightButon.Name = "StartWeightButon"; + this.StartWeightButon.Size = new System.Drawing.Size(93, 46); + this.StartWeightButon.TabIndex = 0; + this.StartWeightButon.Text = "开始称重"; + this.StartWeightButon.UseVisualStyleBackColor = true; + // + // UploadResultButton + // + this.UploadResultButton.Font = new System.Drawing.Font("宋体", 12F); + this.UploadResultButton.Location = new System.Drawing.Point(253, 333); + this.UploadResultButton.Name = "UploadResultButton"; + this.UploadResultButton.Size = new System.Drawing.Size(93, 46); + this.UploadResultButton.TabIndex = 1; + this.UploadResultButton.Text = "上传结果"; + this.UploadResultButton.UseVisualStyleBackColor = true; + // + // LogBox + // + this.LogBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.LogBox.FormattingEnabled = true; + this.LogBox.ItemHeight = 12; + this.LogBox.Location = new System.Drawing.Point(3, 17); + this.LogBox.Name = "LogBox"; + this.LogBox.Size = new System.Drawing.Size(466, 363); + this.LogBox.TabIndex = 2; + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.LogBox); + this.groupBox1.Location = new System.Drawing.Point(466, 12); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(472, 383); + this.groupBox1.TabIndex = 3; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "称量结果"; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Font = new System.Drawing.Font("宋体", 12F); + this.label1.Location = new System.Drawing.Point(114, 292); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(71, 16); + this.label1.TabIndex = 4; + this.label1.Text = "称量结果"; + // + // WeightResultTextBox + // + this.WeightResultTextBox.Font = new System.Drawing.Font("宋体", 12F); + this.WeightResultTextBox.Location = new System.Drawing.Point(191, 288); + this.WeightResultTextBox.Name = "WeightResultTextBox"; + this.WeightResultTextBox.Size = new System.Drawing.Size(145, 26); + this.WeightResultTextBox.TabIndex = 5; + // + // WeightCountComboBox + // + this.WeightCountComboBox.Font = new System.Drawing.Font("宋体", 12F); + this.WeightCountComboBox.FormattingEnabled = true; + this.WeightCountComboBox.Location = new System.Drawing.Point(191, 219); + this.WeightCountComboBox.Name = "WeightCountComboBox"; + this.WeightCountComboBox.Size = new System.Drawing.Size(145, 24); + this.WeightCountComboBox.TabIndex = 6; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Font = new System.Drawing.Font("宋体", 12F); + this.label2.Location = new System.Drawing.Point(114, 222); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(71, 16); + this.label2.TabIndex = 7; + this.label2.Text = "称量片数"; + // + // RollNumberTextBox + // + this.RollNumberTextBox.Font = new System.Drawing.Font("宋体", 12F); + this.RollNumberTextBox.Location = new System.Drawing.Point(132, 61); + this.RollNumberTextBox.Name = "RollNumberTextBox"; + this.RollNumberTextBox.Size = new System.Drawing.Size(257, 26); + this.RollNumberTextBox.TabIndex = 9; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Font = new System.Drawing.Font("宋体", 12F); + this.label3.Location = new System.Drawing.Point(71, 65); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(55, 16); + this.label3.TabIndex = 8; + this.label3.Text = "膜卷号"; + // + // TestButton + // + this.TestButton.Font = new System.Drawing.Font("宋体", 12F); + this.TestButton.Location = new System.Drawing.Point(9, 333); + this.TestButton.Name = "TestButton"; + this.TestButton.Size = new System.Drawing.Size(93, 46); + this.TestButton.TabIndex = 10; + this.TestButton.Text = "测试按钮"; + this.TestButton.UseVisualStyleBackColor = true; + this.TestButton.Click += new System.EventHandler(this.TestButton_Click); + // + // ReadTestButton + // + this.ReadTestButton.Font = new System.Drawing.Font("宋体", 12F); + this.ReadTestButton.Location = new System.Drawing.Point(352, 333); + this.ReadTestButton.Name = "ReadTestButton"; + this.ReadTestButton.Size = new System.Drawing.Size(93, 46); + this.ReadTestButton.TabIndex = 11; + this.ReadTestButton.Text = "读取测试"; + this.ReadTestButton.UseVisualStyleBackColor = true; + this.ReadTestButton.Click += new System.EventHandler(this.ReadTestButton_Click); + // + // CutTestButton + // + this.CutTestButton.Font = new System.Drawing.Font("宋体", 12F); + this.CutTestButton.Location = new System.Drawing.Point(352, 281); + this.CutTestButton.Name = "CutTestButton"; + this.CutTestButton.Size = new System.Drawing.Size(93, 46); + this.CutTestButton.TabIndex = 12; + this.CutTestButton.Text = "剪切测试"; + this.CutTestButton.UseVisualStyleBackColor = true; + this.CutTestButton.Click += new System.EventHandler(this.CutTestButton_Click); + // + // WriteInTestButton + // + this.WriteInTestButton.Font = new System.Drawing.Font("宋体", 12F); + this.WriteInTestButton.Location = new System.Drawing.Point(352, 229); + this.WriteInTestButton.Name = "WriteInTestButton"; + this.WriteInTestButton.Size = new System.Drawing.Size(93, 46); + this.WriteInTestButton.TabIndex = 13; + this.WriteInTestButton.Text = "写入测试"; + this.WriteInTestButton.UseVisualStyleBackColor = true; + this.WriteInTestButton.Click += new System.EventHandler(this.WriteInTestButton_Click); + // + // MainForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(950, 403); + this.Controls.Add(this.WriteInTestButton); + this.Controls.Add(this.CutTestButton); + this.Controls.Add(this.ReadTestButton); + this.Controls.Add(this.TestButton); + this.Controls.Add(this.RollNumberTextBox); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.WeightCountComboBox); + this.Controls.Add(this.WeightResultTextBox); + this.Controls.Add(this.label1); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.UploadResultButton); + this.Controls.Add(this.StartWeightButon); + this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "MainForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "基材重量上传"; + this.Load += new System.EventHandler(this.LoginForm_Load); + this.groupBox1.ResumeLayout(false); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button StartWeightButon; + private Button UploadResultButton; + private GroupBox groupBox1; + private Label label1; + private TextBox WeightResultTextBox; + private ComboBox WeightCountComboBox; + private Label label2; + private TextBox RollNumberTextBox; + private Label label3; + private Button TestButton; + private ListBox LogBox; + private Button ReadTestButton; + private Button CutTestButton; + private Button WriteInTestButton; + } +} diff --git a/HighWayIot.Winform/MainForm/MainForm.cs b/HighWayIot.Winform/MainForm/MainForm.cs new file mode 100644 index 0000000..f75b5bb --- /dev/null +++ b/HighWayIot.Winform/MainForm/MainForm.cs @@ -0,0 +1,174 @@ +using HighWayIot.Common.LANConnectConfig; +using HighWayIot.Common.XmlConfig; +using HighWayIot.TouchSocket; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace HighWayIot.Winform.MainForm +{ + public partial class MainForm : Form + { + + public MainForm() + { + InitializeComponent(); + CheckForIllegalCrossThreadCalls = false; + TouchSocketCOM.Instance.ComAction += WeightData; + action += AddWeightData; + WeightCountComboBox.DataSource = Enumerable.Range(1, 24).ToList(); + RollNumberTextBox.KeyDown += TextBox_KeyDown; + } + + private string WholeData = string.Empty; + + private string TempData = string.Empty; + + private List WeightResults = new List(); + + private Action action; + + private string AverageResult; + + /// + /// 登陆验证 + /// + /// + /// + private void Login_Click(object sender, EventArgs e) + { + + } + + /// + /// 自动登录 + /// + /// + /// + private void LoginForm_Load(object sender, EventArgs e) + { + TouchSocketCOM.Instance.CreateConnection(); + } + + private void TestButton_Click(object sender, EventArgs e) + { + try + { + AddWeightData(XmlUtil.Instance.BaseInnerTextReader("InputPath")); + AddWeightData(XmlUtil.Instance.BaseInnerTextReader("OutputPath")); + foreach (var s in XmlUtil.Instance.ConfigReader()) + { + AddWeightData(s); + } + } + catch + { + MessageBox.Show("配置文件读取失败,请检查配置文件是否存在或格式是否正确。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + /// + /// 处理串口数据 + /// + /// + public void WeightData(string data) + { + data.Trim(); + TempData = TempData + data; + // 检查数据是否包含单次称量结束标志(g) + if (TempData.Contains("g")) + { + // 提取数据,去掉结束标志(g)后的部分 + WholeData = TempData.Substring(0, TempData.IndexOf("g")).Trim(); + TempData = string.Empty; + // 检查数据是否为数字(浮点数)格式 + if (double.TryParse(WholeData, out double result)) + { + WeightResults.Add(result); // 将结果添加到列表中 + } + else + { + action.Invoke($"第[{WeightResults.Count}]次称重数据格式转换失败"); + return; + } + action.Invoke($"第{WeightResults.Count}次称重:{WholeData}g"); + // 检查是否达到了预期的称重次数 + if (WeightResults.Count.ToString() == WeightCountComboBox.Text) + { + double resultAvg = WeightResults.Average(); // 计算平均值 + AverageResult = resultAvg.ToString("F5"); // 保留5位小数 + action.Invoke($"膜卷号[{RollNumberTextBox.Text}]称重结束,结果为[{AverageResult}],请上传结果"); + action.Invoke($"-----------------------------------------------------"); + WeightResultTextBox.Text = AverageResult; // 结果输出到文本框 + WeightResults.Clear(); // 清空结果列表,准备下一次称重 + } + } + } + + /// + /// 向ListBox中添加数据 + /// + /// + private void AddWeightData(string data) + { + LogBox.Items.Add(data); + LogBox.TopIndex = LogBox.Items.Count - 1; + } + + /// + /// 按的是回车键时清空当前选中的 TextBox + /// + /// + /// + private void TextBox_KeyDown(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Enter) + { + TextBox currentTextBox = sender as TextBox; + if (currentTextBox != null) + { + //回车后的业务逻辑 + //先核对膜卷号 + + //准备开始称重 + action.Invoke($"膜卷号[{RollNumberTextBox.Text}]开始称重"); + WeightResults.Clear(); + WeightResultTextBox.Clear(); + e.Handled = true; // 阻止触发 AcceptButton(如果有) + } + } + } + + /// + /// 写入测试 + /// + /// + /// + private void WriteInTestButton_Click(object sender, EventArgs e) + { + AddWeightData(LANConnect.Instance.WriteTxt(XmlUtil.Instance.BaseInnerTextReader("OutputPath"), $"{RollNumberTextBox.Text}.txt", "测试11111").ToString()); + } + + /// + /// 剪切测试 + /// + /// + /// + private void CutTestButton_Click(object sender, EventArgs e) + { + AddWeightData(LANConnect.Instance.MoveFile(XmlUtil.Instance.BaseInnerTextReader("InputPath"), XmlUtil.Instance.BaseInnerTextReader("ArchievePath"), $"{RollNumberTextBox.Text}.txt").ToString()); + } + + /// + /// 读取测试 + /// + /// + /// + private void ReadTestButton_Click(object sender, EventArgs e) + { + AddWeightData(LANConnect.Instance.ReadTxt(XmlUtil.Instance.BaseInnerTextReader("InputPath"), $"{RollNumberTextBox.Text}.txt")); + } + } +} diff --git a/HighWayIot.Winform/MainForm/LoginForm.resx b/HighWayIot.Winform/MainForm/MainForm.resx similarity index 100% rename from HighWayIot.Winform/MainForm/LoginForm.resx rename to HighWayIot.Winform/MainForm/MainForm.resx diff --git a/HighWayIot.Winform/Program.cs b/HighWayIot.Winform/Program.cs index ec08896..d175bc4 100644 --- a/HighWayIot.Winform/Program.cs +++ b/HighWayIot.Winform/Program.cs @@ -37,7 +37,7 @@ namespace HighWayIot.Winform Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //创建窗体 - LoginForm loginform = new LoginForm(); + MainForm.MainForm loginform = new MainForm.MainForm(); logger.Info("程序初始化成功"); Application.Run(loginform); FreeConsole();//释放控制台 diff --git a/HighWayIot.Winform/packages.config b/HighWayIot.Winform/packages.config index 7d4c479..25b2877 100644 --- a/HighWayIot.Winform/packages.config +++ b/HighWayIot.Winform/packages.config @@ -1,8 +1,10 @@  + +