feat - 添加串口 添加软件称量逻辑 添加网络文件交互

master
SoulStar 2 days ago
parent 9f05c6a601
commit 0d742100c5

@ -47,7 +47,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="LANConnectConfig\LANConnect.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="XmlConfig\XmlUtil.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HighWayIot.Log4net\HighWayIot.Log4net.csproj">

@ -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
{
/// <summary>
/// 局域网连接配置类
/// </summary>
public class LANConnect
{
private static readonly Lazy<LANConnect> lazy = new Lazy<LANConnect>(() => new LANConnect());
public static LANConnect Instance
{
get
{
return lazy.Value;
}
}
private static LogHelper _logHelper = LogHelper.Instance;
XmlUtil xmlConfig = XmlUtil.Instance;
/// <summary>
/// 读取网络文件夹中的txt文件
/// </summary>
/// <param name="path">路径</param>
/// <param name="txtName">txt名称</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 向网络文件夹内写入txt文件
/// </summary>
/// <param name="path">写入路径</param>
/// <param name="txtName">txt名称</param>
/// <param name="content">写入内容</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 移动文件到一个新位置
/// </summary>
/// <param name="sourcePath">源路径</param>
/// <param name="targetPath">目标路径</param>
/// <param name="fileName">文件名称</param>
/// <returns></returns>
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;
}
}
}
}

@ -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
{
/// <summary>
/// XML读写类
/// </summary>
public class XmlUtil
{
private static readonly Lazy<XmlUtil> lazy = new Lazy<XmlUtil>(() => new XmlUtil());
public static XmlUtil Instance
{
get
{
return lazy.Value;
}
}
/// <summary>
/// XML读写实例
/// </summary>
XmlDocument xmlDocument = new XmlDocument();
/// <summary>
/// 运行时路径
/// </summary>
private string Path = System.Environment.CurrentDirectory;
/// <summary>
/// 设备配置读取
/// </summary>
/// <returns></returns>
public List<string> ConfigReader()
{
List<string> list = new List<string>();
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;
}
/// <summary>
/// 初级目录InnerText配置读取
/// </summary>
/// <returns></returns>
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
{
/// <summary>
/// 页面名称
/// </summary>
public string PageName { get; set; }
/// <summary>
/// 规则编号
/// </summary>
public int RoleIndex { get; set; }
}
public class ExportPathConfig
{
public string ExportConfig { get; set; }
public string Config { get; set; }
}
}

@ -13,6 +13,8 @@
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -40,6 +42,9 @@
<HintPath>..\packages\System.Buffers.4.6.1\lib\net462\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.IO.Ports, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Ports.8.0.0\lib\net462\System.IO.Ports.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.6.3\lib\net462\System.Memory.dll</HintPath>
</Reference>
@ -59,16 +64,19 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="TouchSocket, Version=3.0.12.0, Culture=neutral, PublicKeyToken=5f39d7da98dac6a9, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.3.0.12\lib\net472\TouchSocket.dll</HintPath>
<Reference Include="TouchSocket, Version=3.1.15.0, Culture=neutral, PublicKeyToken=5f39d7da98dac6a9, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.3.1.15\lib\net472\TouchSocket.dll</HintPath>
</Reference>
<Reference Include="TouchSocket.Core, Version=3.0.12.0, Culture=neutral, PublicKeyToken=d6c415a2f58eda72, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.Core.3.0.12\lib\net472\TouchSocket.Core.dll</HintPath>
<Reference Include="TouchSocket.Core, Version=3.1.15.0, Culture=neutral, PublicKeyToken=d6c415a2f58eda72, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.Core.3.1.15\lib\net472\TouchSocket.Core.dll</HintPath>
</Reference>
<Reference Include="TouchSocket.SerialPorts, Version=3.1.15.0, Culture=neutral, PublicKeyToken=b0dfdf1c6b51c716, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.SerialPorts.3.1.15\lib\net472\TouchSocket.SerialPorts.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TouchSocketTcpClient.cs" />
<Compile Include="TouchSocketCOM.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
@ -81,7 +89,14 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\TouchSocket.Core.3.0.12\analyzers\dotnet\cs\TouchSocket.Core.SourceGenerator.dll" />
<Analyzer Include="..\packages\TouchSocket.Core.3.1.15\analyzers\dotnet\cs\TouchSocket.Core.SourceGenerator.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets" Condition="Exists('..\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.ValueTuple.4.6.1\build\net471\System.ValueTuple.targets'))" />
</Target>
</Project>

@ -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
{
/// <summary>
/// 日志
/// </summary>
private static LogHelper _logHelper = LogHelper.Instance;
/// <summary>
/// 懒加载
/// </summary>
private static readonly Lazy<TouchSocketCOM> lazy = new Lazy<TouchSocketCOM>(() => new TouchSocketCOM());
public static TouchSocketCOM Instance => lazy.Value;
public Action<string> ComAction;
/// <summary>
/// 创建串口连接
/// </summary>
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<MyPlugin>();
}));
await client.ConnectAsync();
_logHelper.Info("COM5连接成功");
}
catch (Exception ex)
{
_logHelper.Error("连接失败", ex);
}
}
}
}

@ -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
{
/// <summary>
/// 日志
/// </summary>
private static LogHelper _logHelper = LogHelper.Instance;
/// <summary>
/// 懒加载
/// </summary>
private static readonly Lazy<TouchSocketTcpClient> lazy = new Lazy<TouchSocketTcpClient>(() => new TouchSocketTcpClient());
public static TouchSocketTcpClient Instance => lazy.Value;
/// <summary>
/// 所有的客户端
/// </summary>
public Dictionary<string, TcpClient> Clients = new Dictionary<string, TcpClient>();
public int ClientsCount = 0;
public Action<byte[], string> 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;
}
/// <summary>
/// 信息发送
/// </summary>
/// <param name="message">Bytes</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 客户端释放
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
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;
}
}
}
}

@ -12,7 +12,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.0" newVersion="4.0.4.0" />
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />

@ -2,10 +2,13 @@
<packages>
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
<package id="System.Buffers" version="4.6.1" targetFramework="net48" />
<package id="System.IO.Ports" version="8.0.0" targetFramework="net48" />
<package id="System.Memory" version="4.6.3" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.6.1" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.1.2" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.6.3" targetFramework="net48" />
<package id="TouchSocket" version="3.0.12" targetFramework="net48" />
<package id="TouchSocket.Core" version="3.0.12" targetFramework="net48" />
<package id="System.ValueTuple" version="4.6.1" targetFramework="net48" />
<package id="TouchSocket" version="3.1.15" targetFramework="net48" />
<package id="TouchSocket.Core" version="3.1.15" targetFramework="net48" />
<package id="TouchSocket.SerialPorts" version="3.1.15" targetFramework="net48" />
</packages>

@ -1,23 +1,27 @@
<?xml version="1.0" encoding="utf-8" ?>
<root>
<!--PageName是页面名称-->
<RoleConfig>
<Role PageName = "角色管理" RoleIndex = "0" />
<Role PageName = "用户管理" RoleIndex = "1" />
<Role PageName = "操作日志" RoleIndex = "2" />
<Role PageName = "报警日志" RoleIndex = "3" />
<Role PageName = "监控主界面" RoleIndex = "4" />
<Role PageName = "班时间维护" RoleIndex = "5" />
<Role PageName = "物料管理" RoleIndex = "6" />
<Role PageName = "物料类型管理" RoleIndex = "7" />
<Role PageName = "配方管理" RoleIndex = "8" />
<Role PageName = "日报表" RoleIndex = "9" />
<Role PageName = "机台物料信息绑定" RoleIndex = "10" />
<Role PageName = "硫化排程" RoleIndex = "11" />
<Role PageName = "RFID参数配置" RoleIndex = "12" />
<Role PageName = "PLC测试页面" RoleIndex = "14" />
</RoleConfig>
<ExportConfig Path =""/>
<!--串口配置-->
<COMConfig COMName="COM5"/>
<!--设备配置-->
<DeviceConfig>
<DeviceName Name="D1#"/>
<DeviceName Name="D2#"/>
<DeviceName Name="D3#"/>
<DeviceName Name="D4#"/>
<DeviceName Name="D5#"/>
<DeviceName Name="D6#"/>
<DeviceName Name="D7#"/>
<DeviceName Name="D8#"/>
<DeviceName Name="D9#"/>
<DeviceName Name="D10#"/>
<DeviceName Name="D11#"/>
<DeviceName Name="D12#"/>
</DeviceConfig>
<!--网盘文件路径设置-->
<InputPath>\\RK90004438\Users\ADMIN\Desktop\pod\Imadumb\input</InputPath>
<ArchievePath>\\RK90004438\Users\ADMIN\Desktop\pod\Imadumb\archive</ArchievePath>
<OutputPath>\\RK90004438\Users\ADMIN\Desktop\pod\Imadumb\output</OutputPath>
<LANUserName>administrator</LANUserName>
<LANPassword>cc123456@</LANPassword>
</root>

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura />
</Weavers>

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Costura.Fody.6.0.0\build\Costura.Fody.props" Condition="Exists('..\packages\Costura.Fody.6.0.0\build\Costura.Fody.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -28,6 +29,8 @@
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -70,6 +73,9 @@
<Reference Include="BouncyCastle.Cryptography, Version=2.0.0.0, Culture=neutral, PublicKeyToken=072edcf4a5328938, processorArchitecture=MSIL">
<HintPath>..\packages\BouncyCastle.Cryptography.2.3.1\lib\net461\BouncyCastle.Cryptography.dll</HintPath>
</Reference>
<Reference Include="Costura, Version=6.0.0.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>..\packages\Costura.Fody.6.0.0\lib\netstandard2.0\Costura.dll</HintPath>
</Reference>
<Reference Include="Enums.NET, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7ea1c1650d506225, processorArchitecture=MSIL">
<HintPath>..\packages\Enums.NET.4.0.1\lib\net45\Enums.NET.dll</HintPath>
</Reference>
@ -147,16 +153,16 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainForm\LoginForm.cs">
<Compile Include="MainForm\MainForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MainForm\LoginForm.Designer.cs">
<DependentUpon>LoginForm.cs</DependentUpon>
<Compile Include="MainForm\MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="MainForm\LoginForm.resx">
<DependentUpon>LoginForm.cs</DependentUpon>
<EmbeddedResource Include="MainForm\MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
@ -168,7 +174,6 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="HighWayIot.Winform_TemporaryKey.pfx" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
@ -187,6 +192,10 @@
<None Include="Static\MesnacLogoHighPixel.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HighWayIot.Common\HighWayIot.Common.csproj">
<Project>{89a1edd9-d79e-468d-b6d3-7d07b8843562}</Project>
<Name>HighWayIot.Common</Name>
</ProjectReference>
<ProjectReference Include="..\HighWayIot.Controls\HighWayIot.Controls.csproj">
<Project>{DDEE27AA-0694-47A6-9335-E9308261F63A}</Project>
<Name>HighWayIot.Controls</Name>
@ -240,8 +249,16 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Folder Include="Business\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Fody.6.9.3\build\Fody.targets" Condition="Exists('..\packages\Fody.6.9.3\build\Fody.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Fody.6.9.3\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.6.9.3\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\Costura.Fody.6.0.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.6.0.0\build\Costura.Fody.props'))" />
<Error Condition="!Exists('..\packages\Costura.Fody.6.0.0\build\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.6.0.0\build\Costura.Fody.targets'))" />
</Target>
<Import Project="..\packages\Costura.Fody.6.0.0\build\Costura.Fody.targets" Condition="Exists('..\packages\Costura.Fody.6.0.0\build\Costura.Fody.targets')" />
</Project>

@ -1,55 +0,0 @@

using System.Drawing;
using System.Windows.Forms;
namespace HighWayIot.Winform.MainForm
{
partial class LoginForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
}
}

@ -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();
}
/// <summary>
/// 登陆验证
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Login_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 自动登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoginForm_Load(object sender, EventArgs e)
{
}
}
}

@ -0,0 +1,239 @@

using System.Drawing;
using System.Windows.Forms;
namespace HighWayIot.Winform.MainForm
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

@ -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<double> WeightResults = new List<double>();
private Action<string> action;
private string AverageResult;
/// <summary>
/// 登陆验证
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Login_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 自动登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
/// <summary>
/// 处理串口数据
/// </summary>
/// <param name="data"></param>
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(); // 清空结果列表,准备下一次称重
}
}
}
/// <summary>
/// 向ListBox中添加数据
/// </summary>
/// <param name="data"></param>
private void AddWeightData(string data)
{
LogBox.Items.Add(data);
LogBox.TopIndex = LogBox.Items.Count - 1;
}
/// <summary>
/// 按的是回车键时清空当前选中的 TextBox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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如果有
}
}
}
/// <summary>
/// 写入测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WriteInTestButton_Click(object sender, EventArgs e)
{
AddWeightData(LANConnect.Instance.WriteTxt(XmlUtil.Instance.BaseInnerTextReader("OutputPath"), $"{RollNumberTextBox.Text}.txt", "测试11111").ToString());
}
/// <summary>
/// 剪切测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CutTestButton_Click(object sender, EventArgs e)
{
AddWeightData(LANConnect.Instance.MoveFile(XmlUtil.Instance.BaseInnerTextReader("InputPath"), XmlUtil.Instance.BaseInnerTextReader("ArchievePath"), $"{RollNumberTextBox.Text}.txt").ToString());
}
/// <summary>
/// 读取测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ReadTestButton_Click(object sender, EventArgs e)
{
AddWeightData(LANConnect.Instance.ReadTxt(XmlUtil.Instance.BaseInnerTextReader("InputPath"), $"{RollNumberTextBox.Text}.txt"));
}
}
}

@ -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();//释放控制台

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BouncyCastle.Cryptography" version="2.3.1" targetFramework="net48" />
<package id="Costura.Fody" version="6.0.0" targetFramework="net48" developmentDependency="true" />
<package id="Enums.NET" version="4.0.1" targetFramework="net48" />
<package id="ExtendedNumerics.BigDecimal" version="2025.1001.2.129" targetFramework="net48" />
<package id="Fody" version="6.9.3" targetFramework="net48" developmentDependency="true" />
<package id="MathNet.Numerics.Signed" version="5.0.0" targetFramework="net48" />
<package id="Microsoft.IO.RecyclableMemoryStream" version="3.0.0" targetFramework="net48" />
<package id="NPOI" version="2.7.3" targetFramework="net48" />

Loading…
Cancel
Save