串口逻辑,数据库读写逻辑修改

This commit is contained in:
sc committed 2025-09-10 21:42:52 +08:00
commit 4af9042d5f
18 files changed
+658 -599

No files matched your search

+45 -5
View File
@@ -1,7 +1,9 @@
using System;
using ScottPlot.Plottables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.SerialPorts;
@@ -18,10 +20,13 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
portclient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到端口
portclient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从端口断开连接。此处仅主动断开才有效。
portclient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从端口断开连接,当连接不成功时不会触发。
portclient.Received = async (c, e) =>
portclient.Received = (client, e) =>
{
await Console.Out.WriteLineAsync(e.ByteBlock.Span.ToString(Encoding.UTF8));
_responseEvent.Set();
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
//string DAT1 = CRCcheck16.ToCRC16(e.ByteBlock.Span.ToString(Encoding.UTF8));
return EasyTask.CompletedTask; ;
};
await portclient.SetupAsync(new TouchSocketConfig()
@@ -31,7 +36,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
DataBits = 8,//数据位
Parity = System.IO.Ports.Parity.None,//校验位
PortName = com,//COM
StopBits = System.IO.Ports.StopBits.One,//停止位
StopBits = System.IO.Ports.StopBits.One,//停止位
})
.SetSerialDataHandlingAdapter(() => new PeriodPackageAdapter() { CacheTimeout = TimeSpan.FromMilliseconds(100) })
.ConfigurePlugins(a =>
@@ -41,5 +46,40 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
await portclient.ConnectAsync();
}
private static readonly ManualResetEventSlim _responseEvent = new ManualResetEventSlim(false);
private static byte[] _receivedData;
private static readonly object _sendLock = new object(); // 确保发送-等待过程原子性
/// <summary>
/// 发送指令并等待响应,超时时间为500ms
/// </summary>
/// <param name="command">要发送的指令字节数组</param>
/// <returns>从机的响应数据,超时则为null</returns>
public static byte[] SendCommandAndWait(SerialPortClient serialPortClients, string command)
{
// lock (_sendLock) // 防止并发发送
{
_receivedData = null;
_responseEvent.Reset(); // 重置事件状态
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
bool signaled = _responseEvent.Wait(TimeSpan.FromMilliseconds(1000));
if (signaled)
{
return _receivedData; // 返回收到的数据
}
else
{
// 超时处理,记录日志或抛出异常
//Console.WriteLine("警告: 等待从机响应超时 (500ms)。");
return null; // 或者可以抛出 TimeoutException
}
}
}
}
}
+14 -31
View File
@@ -39,8 +39,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
/// </summary>
public class AsyncTcpClient
{
private static SQLiteHelper SQLiteHelpers = null; //定义数据库
private static readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\SCCM.db"; //数据库路径
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)
{
@@ -149,12 +149,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库
SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 111 } },
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 111 } },
"WorkOrder ='" + DAT + "'", null);
SQLiteHelpers.Close();
}
catch (Exception ex)
{
@@ -166,11 +163,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库
SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 113 } },
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 113 } },
"WorkOrder ='" + DAT + "'", null);
SQLiteHelpers.Close();
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}
@@ -178,12 +172,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库
SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 101 } },
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 101 } },
"WorkOrder ='" + DAT + "'", null);
SQLiteHelpers.Close();
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}
@@ -220,11 +211,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
newRow["State"] = 202;
newRow.EndEdit();
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库
SQLiteHelpers.InsertData("DyelotHistory", SQLiteHelpers.ToDictionary(newRow));// 执行插入
SQLiteHelpers.Close();
MainWindowViewModel.SQLiteHelpers.InsertData("DyelotHistory", MainWindowViewModel.SQLiteHelpers.ToDictionary(newRow));// 执行插入
MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString
{
ID = Convert.ToInt16(MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines,
@@ -281,10 +268,10 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
Chart_new.Add("STTC", _new.GetValue("STTC"));
Chart_new.Add("STLC", _new.GetValue("STLC"));
SQLiteHelpers = new SQLiteHelper(ChartAdress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库
SQLiteHelpers.InsertData("Chart", Chart_new);// 执行插入
SQLiteHelpers.Close();
SQLiteChartAdress = new SQLiteHelper(ChartAdress); //数据库连接路径
SQLiteChartAdress.Open(); //打开数据库
SQLiteChartAdress.InsertData("Chart", Chart_new);// 执行插入
SQLiteChartAdress.Close();
}
}
@@ -367,10 +354,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
SQLiteHelpers.Open(); //打开数据库
SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 109 } },
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 109 } },
"WorkOrder ='" + DAT + "'", null);
SQLiteHelpers.Close();
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//发布失败
@@ -379,10 +364,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
SQLiteHelpers.Open(); //打开数据库
SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 119 } },
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 119 } },
"WorkOrder ='" + DAT + "'", null);
SQLiteHelpers.Close();
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//细节错误