部分数据库及串口逻辑修改

This commit is contained in:
sc committed 2025-09-13 01:45:28 +08:00
commit 48eb8a0ad1
4 files changed
+451 -174

No files matched your search

+291 -45
View File
@@ -1,27 +1,54 @@
using ScottPlot.Plottables;
using Newtonsoft.Json;
using ScottPlot.Plottables;
using SunlightCentralizedControlManagement_SCCM_.View;
using SunlightCentralizedControlManagement_SCCM_.ViewModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using TouchSocket.Core;
using TouchSocket.SerialPorts;
using TouchSocket.Sockets;
using static SkiaSharp.HarfBuzz.SKShaper;
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
using static System.Net.Mime.MediaTypeNames;
namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{
public class AsyncSerialPortClient
{
public static string ClipBetween(string source, char startChar, char endChar, bool includeBounds = false)
{
if (string.IsNullOrEmpty(source))
return string.Empty;
int startIndex = source.IndexOf(startChar);
if (startIndex == -1) return string.Empty;
int endIndex = source.IndexOf(endChar, startIndex + 1);
if (endIndex == -1) return string.Empty;
if (includeBounds)
{
return source.Substring(startIndex, endIndex - startIndex + 1);
}
else
{
int contentStart = startIndex + 1;
int contentLength = endIndex - startIndex - 1;
return contentLength > 0 ? source.Substring(contentStart, contentLength) : string.Empty;
}
}
public static string PortCOM1 { get; set; }
public static string PortCOM2 { get; set; }
public static string PortCOM3 { get; set; }
public static string PortCOM4 { get; set; }
public static void SCAPI(string dat)
{
}
public static async Task PortClient(SerialPortClient portclient, string com, int BAUD)
{
@@ -31,13 +58,209 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
portclient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从端口断开连接。此处仅主动断开才有效。
portclient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从端口断开连接,当连接不成功时不会触发。
portclient.Received = (client, e) =>
{
{
_responseEvent.Set();
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
SCAPI(DAT);
string[] sArray = Regex.Split(e.ByteBlock.Span.ToString(Encoding.UTF8),
@"\n", RegexOptions.IgnoreCase);
for (int i = 0; i < sArray.Length; i++)
{
if (!string.IsNullOrEmpty(sArray[i]))
{
string SYSAPI = sArray[i].Substring(0, 5);
string Station = ClipBetween(sArray[i], '[', ']');
string DAT = sArray[i].Substring(sArray[i].IndexOf("]") + 1);
if (SYSAPI == "SC800")
{
try
{
Dictionary<string, object> _new = new Dictionary<string, object>();//缓存函数
_new = JsonConvert.DeserializeObject<Dictionary<string, object>>(DAT);//反序列化
DataRow drEmployee = MainWindowViewModel.Machines.Select("Station='" +
Station + "' AND Serial = 'PORT1'").First();
drEmployee.BeginEdit();
drEmployee["State"] = "802";
drEmployee.EndEdit();
drEmployee.AcceptChanges();
}
catch (Exception ex)
{
LogGing.ERRDATA(ex); MainWindowViewModel.ERR_c++;
}
}
else if (SYSAPI == "SC810")
{
try
{
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 111 } },
"WorkOrder ='" + DAT + "'", null);
}
catch (Exception ex)
{
LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++;
}
}
else if (SYSAPI == "SC811")
{
try
{
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 113 } },
"WorkOrder ='" + DAT + "'", null);
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}
else if (SYSAPI == "SC812")
{
try
{
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 101 } },
"WorkOrder ='" + DAT + "'", null);
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}
else if (SYSAPI == "SC827")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//获取呼叫领料单
else if (SYSAPI == "SC830")
{
try
{
return EasyTask.CompletedTask; ;
Dictionary<string, object> _new = new Dictionary<string, object>();//缓存函数
_new = JsonConvert.DeserializeObject<Dictionary<string, object>>(DAT);//反序列化
DataRow drEmployee = MainWindowViewModel.Machines.Select("Station='" +
Station + "' AND Serial = 'PORT1'").First();
drEmployee.BeginEdit();
drEmployee["ERR"] = _new.GetValue("ERR");
drEmployee["LOCK"] = _new.GetValue("LOCK");
drEmployee["Message"] = _new.GetValue("Status");
drEmployee["WorkOrder"] = _new.GetValue("WorkNumder");
drEmployee["Temperature"] = _new.GetValue("MTT");
drEmployee["WaterLevel"] = _new.GetValue("MTL");
drEmployee["Process"] = _new.GetValue("Process");
drEmployee["Step"] = _new.GetValue("Step");
drEmployee["UserButton"] = _new.GetValue("UserButton");
if (_new.GetValue("UserInfoStart").ToString() != "900")
{ drEmployee["UserInfoStart"] = _new.GetValue("UserInfoStart"); }
drEmployee["UserInfo"] = _new.GetValue("UserInfo");
drEmployee["WORK_RUN"] = _new.GetValue("WORK_RUN");
drEmployee["RUN_STEPID"] = _new.GetValue("RUN_STEPID");
drEmployee["CALL"] = _new.GetValue("CALL");
drEmployee.EndEdit();
drEmployee.AcceptChanges();
if (_new.GetValue("Status").ToString() != "----------")
{
Dictionary<string, object> Chart_new = new Dictionary<string, object>();//缓存函数
Chart_new.Add("WorkOrder", _new.GetValue("WorkNumder"));
Chart_new.Add("Machine", _new.GetValue("Machine"));
Chart_new.Add("Time", _new.GetValue("Time"));
Chart_new.Add("MST", _new.GetValue("MST"));
Chart_new.Add("MTT", _new.GetValue("MTT"));
Chart_new.Add("MTL", _new.GetValue("MTL"));
Chart_new.Add("MTH", _new.GetValue("MTH"));
Chart_new.Add("MUT", _new.GetValue("MUT"));
Chart_new.Add("STTA", _new.GetValue("STTA"));
Chart_new.Add("STLA", _new.GetValue("STLA"));
Chart_new.Add("STTB", _new.GetValue("STTB"));
Chart_new.Add("STLB", _new.GetValue("STLB"));
Chart_new.Add("STTC", _new.GetValue("STTC"));
Chart_new.Add("STLC", _new.GetValue("STLC"));
MainWindowViewModel.SQLiteChartAdress.InsertData("Chart", Chart_new);// 执行插入
}
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//当前信息
else if (SYSAPI == "SC831")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}
else if (SYSAPI == "SC832")
{
try
{
MainWindowViewModel.MachineLOG = DAT;
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//当前细节信息
else if (SYSAPI == "SC833")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//当前领料单信息
else if (SYSAPI == "SC851")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//数字开关表
else if (SYSAPI == "SC852")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//寄存器表
else if (SYSAPI == "SC853")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//缓存表
else if (SYSAPI == "SC854")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//程序设置表
else if (SYSAPI == "SC855")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//系统设置表
else if (SYSAPI == "SC910")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//发布失败
else if (SYSAPI == "SC911")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//细节错误
else if (SYSAPI == "SC980")
{
}
}
}
return EasyTask.CompletedTask; ;
};
await portclient.SetupAsync(new TouchSocketConfig()
.SetSerialPortOption(new SerialPortOption()
{
@@ -64,12 +287,13 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
portclient.Received = (client, e) =>
{
_responseEvent2.Set();
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
//string DAT1 = CRCcheck16.ToCRC16(e.ByteBlock.Span.ToString(Encoding.UTF8));
SCAPI(DAT);
string[] sArray = Regex.Split(e.ByteBlock.Span.ToString(Encoding.UTF8),
@"\n", RegexOptions.IgnoreCase);
return EasyTask.CompletedTask; ;
};
await portclient.SetupAsync(new TouchSocketConfig()
.SetSerialPortOption(new SerialPortOption()
{
@@ -96,12 +320,13 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
portclient.Received = (client, e) =>
{
_responseEvent3.Set();
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
//string DAT1 = CRCcheck16.ToCRC16(e.ByteBlock.Span.ToString(Encoding.UTF8));
SCAPI(DAT);
string[] sArray = Regex.Split(e.ByteBlock.Span.ToString(Encoding.UTF8),
@"\n", RegexOptions.IgnoreCase);
return EasyTask.CompletedTask; ;
};
await portclient.SetupAsync(new TouchSocketConfig()
.SetSerialPortOption(new SerialPortOption()
{
@@ -128,12 +353,13 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
portclient.Received = (client, e) =>
{
_responseEvent4.Set();
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
//string DAT1 = CRCcheck16.ToCRC16(e.ByteBlock.Span.ToString(Encoding.UTF8));
SCAPI(DAT);
string[] sArray = Regex.Split(e.ByteBlock.Span.ToString(Encoding.UTF8),
@"\n", RegexOptions.IgnoreCase);
return EasyTask.CompletedTask; ;
};
await portclient.SetupAsync(new TouchSocketConfig()
.SetSerialPortOption(new SerialPortOption()
{
@@ -151,10 +377,10 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
await portclient.ConnectAsync();
}
private static readonly ManualResetEventSlim _responseEvent = new ManualResetEventSlim(false);
private static readonly ManualResetEventSlim _responseEvent2 = new ManualResetEventSlim(false);
private static readonly ManualResetEventSlim _responseEvent3 = new ManualResetEventSlim(false);
private static readonly ManualResetEventSlim _responseEvent4 = new ManualResetEventSlim(false);
public static readonly ManualResetEventSlim _responseEvent = new ManualResetEventSlim(false);
public static readonly ManualResetEventSlim _responseEvent2 = new ManualResetEventSlim(false);
public static readonly ManualResetEventSlim _responseEvent3 = new ManualResetEventSlim(false);
public static readonly ManualResetEventSlim _responseEvent4 = new ManualResetEventSlim(false);
/// <summary>
/// 发送指令并等待响应,超时时间为500ms
/// </summary>
@@ -162,35 +388,55 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
/// <returns>从机的响应数据,超时则为null</returns>
public static void SendCommandAndWait(SerialPortClient serialPortClients, string command)
{
_responseEvent.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
try
{
_responseEvent.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
_responseEvent.Set();
}
catch (Exception) { }
}
public static void SendCommandAndWait2(SerialPortClient serialPortClients, string command)
{
_responseEvent2.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent2.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
try
{
_responseEvent2.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent2.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
_responseEvent2.Set();
}
catch (Exception) { }
}
public static void SendCommandAndWait3(SerialPortClient serialPortClients, string command)
{
_responseEvent3.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent3.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
try
{
_responseEvent3.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent3.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
_responseEvent3.Set();
}
catch (Exception) { }
}
public static void SendCommandAndWait4(SerialPortClient serialPortClients, string command)
{
_responseEvent4.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent4.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
try
{
_responseEvent4.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent4.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
_responseEvent4.Set();
}
catch (Exception) { }
}
}
}
+2 -8
View File
@@ -38,10 +38,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
/// 异步TCP客户端
/// </summary>
public class AsyncTcpClient
{
public static SQLiteHelper SQLiteChartAdress = null; //定义数据库
private static readonly string ChartAdress = Environment.CurrentDirectory + "\\DataBase\\Chart.db"; //数据库路径
{
public static async Task TcpClient(TcpClient tcpClient, string ip, string port)
{
//TcpClient tcpClient = new TcpClient();
@@ -268,10 +265,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
Chart_new.Add("STTC", _new.GetValue("STTC"));
Chart_new.Add("STLC", _new.GetValue("STLC"));
SQLiteChartAdress = new SQLiteHelper(ChartAdress); //数据库连接路径
SQLiteChartAdress.Open(); //打开数据库
SQLiteChartAdress.InsertData("Chart", Chart_new);// 执行插入
SQLiteChartAdress.Close();
MainWindowViewModel.SQLiteChartAdress.InsertData("Chart", Chart_new);// 执行插入
}
}