sc 6 months ago
committed by 忱 沈
parent
commit
437b8957ce
  1. 212
      UserClass/AsyncSerialPortClient.cs

212
UserClass/AsyncSerialPortClient.cs

@ -2,7 +2,6 @@
using ScottPlot.Plottables; using ScottPlot.Plottables;
using SunlightCentralizedControlManagement_SCCM_.View; using SunlightCentralizedControlManagement_SCCM_.View;
using SunlightCentralizedControlManagement_SCCM_.ViewModel; using SunlightCentralizedControlManagement_SCCM_.ViewModel;
using SunlightCentralizedControlManagement_SCCM_.WindowsView;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -16,10 +15,8 @@ using System.Windows;
using TouchSocket.Core; using TouchSocket.Core;
using TouchSocket.SerialPorts; using TouchSocket.SerialPorts;
using TouchSocket.Sockets; using TouchSocket.Sockets;
using static OpenTK.Graphics.OpenGL.GL;
using static SkiaSharp.HarfBuzz.SKShaper; using static SkiaSharp.HarfBuzz.SKShaper;
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper; using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
using static SunlightCentralizedControlManagement_SCCM_.ViewModel.MainWindowViewModel;
using static System.Net.Mime.MediaTypeNames; using static System.Net.Mime.MediaTypeNames;
namespace SunlightCentralizedControlManagement_SCCM_.UserClass namespace SunlightCentralizedControlManagement_SCCM_.UserClass
@ -47,7 +44,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
int contentLength = endIndex - startIndex - 1; int contentLength = endIndex - startIndex - 1;
return contentLength > 0 ? source.Substring(contentStart, contentLength) : string.Empty; return contentLength > 0 ? source.Substring(contentStart, contentLength) : string.Empty;
} }
}//获取字符串指定字符的范围的值 }
public static string PortCOM1 { get; set; } public static string PortCOM1 { get; set; }
public static string PortCOM2 { get; set; } public static string PortCOM2 { get; set; }
@ -63,17 +60,19 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
portclient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从端口断开连接,当连接不成功时不会触发。 portclient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从端口断开连接,当连接不成功时不会触发。
portclient.Received = (client, e) => portclient.Received = (client, e) =>
{ {
//接受重置事件状态
_responseEvent.Set(); _responseEvent.Set();
string[] sArray = Regex.Split(e.ByteBlock.Span.ToString(Encoding.UTF8), string[] sArray = Regex.Split(e.ByteBlock.Span.ToString(Encoding.UTF8),
@"\n", RegexOptions.IgnoreCase); @"\n", RegexOptions.IgnoreCase);
for (int i = 0; i < sArray.Length; i++) for (int i = 0; i < sArray.Length; i++)
{ {
if (sArray[i].Length>5) if (sArray[i].Length > 5)
{ {
string SYSAPI = sArray[i].Substring(0, 5); string SYSAPI = sArray[i].Substring(0, 5);
string Station = ClipBetween(sArray[i], '[', ']'); string Station = ClipBetween(sArray[i], '[', ']');
string DAT = sArray[i].Substring(sArray[i].IndexOf("]") + 1); string DAT = sArray[i].Substring(sArray[i].IndexOf("]") + 1);
if (SYSAPI == "SC800") if (SYSAPI == "SC800")
{ {
try try
@ -98,10 +97,11 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
Dictionary<string, object> _dat; Dictionary<string, object> _dat;
_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT); _dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT);
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 111 } }, MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 111 } },
"WorkOrder ='" + _dat.GetValue("Work").ToString() + "' AND ReDye ='" + _dat.GetValue("ReDye") + "'", null); "WorkOrder ='" + _dat.GetValue("Work") + "' AND ReDye ='" + _dat.GetValue("ReDye") + "'", null);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -112,14 +112,15 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
Dictionary<string, object> _dat; Dictionary<string, object> _dat;
_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT); _dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT);
MainWindowViewModel.SQLiteHelpers.Update("WorkorderSteps", new Dictionary<string, object> { { "Mode", 1 } }, MainWindowViewModel.SQLiteHelpers.Update("WorkorderSteps", new Dictionary<string, object> { { "Mode", 1 } },
"WorkOrder ='" + _dat.GetValue("Work") + "' AND ReDye ='" + _dat.GetValue("ReDye") + "' AND Step ='" + "WorkOrder ='" + _dat.GetValue("Work") + "' AND ReDye ='" + _dat.GetValue("ReDye") + "' AND Step ='" +
_dat.GetValue("Step") + "'", null); _dat.GetValue("Step") + "'", null);
int x = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("select * from WorkorderSteps where WorkOrder='"+ int x = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("select * from WorkorderSteps where WorkOrder='" +
_dat.GetValue("Order") + "' AND ReDye ='" + _dat.GetValue("ReDye") + "' AND Mode <> '1'", null).Tables[0].Rows.Count; _dat.GetValue("Work") + "' AND ReDye ='" + _dat.GetValue("ReDye") + "' AND Mode <> '1'", null).Tables[0].Rows.Count;
if (x == 0) if (x == 0)
{ {
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 113 } }, MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 113 } },
@ -132,6 +133,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
Dictionary<string, object> _dat; Dictionary<string, object> _dat;
_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT); _dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT);
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 101 } }, MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 101 } },
@ -143,79 +145,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
Dictionary<string, object> _dat;
_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT);
DataTable dataTable = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("select * from Dyelot where WorkOrder='" +
_dat.GetValue("WorkOrder") + "' AND ReDye ='" + _dat.GetValue("ReDye") + "' AND Step = '"+ _dat.GetValue("Step") + "'", null).Tables[0];
foreach (DataRow sourceRow in dataTable.Rows)
{
lock (MainWindowViewModel.Dyelot_CALL.Rows.SyncRoot)
{
// 创建新行(基于目标表结构)
DataRow newRow = MainWindowViewModel.Dyelot_CALL.NewRow();
// 复制源表中存在的列数据
foreach (DataColumn column in dataTable.Columns)
{
if (MainWindowViewModel.Dyelot_CALL.Columns.Contains(column.ColumnName))
{
newRow[column.ColumnName] = sourceRow[column.ColumnName];
}
}
MainWindowViewModel.Dyelot_CALL.Rows.Add(newRow);
Dictionary<string, object> Product_ = new Dictionary<string, object>();//缓存函数
Product_.Add("State", 202);
Product_.Add("Dyelot", newRow.Field<string>("Dyelot"));
Product_.Add("Step", newRow.Field<int>("Step"));
newRow.BeginEdit();
newRow["State"] = 202;
newRow.EndEdit();
MainWindowViewModel.SQLiteHelpers.InsertData("DyelotHistory", MainWindowViewModel.SQLiteHelpers.ToDictionary(newRow));// 执行插入
int index = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("select * from Machines where Serial='PORT1' AND Station ='" +
Station + "' ", null).Tables[0].Rows[0].Field<int>("ID");
string dat_ = "SC828[" + Station + "]" + Product_.ToJsonString();
bool exists = stringQueueSerial_1.Any(item => item.ID == index && item.DAT == dat_);
if (!exists)
{
stringQueueSerial_1.Enqueue(new QueueSerial
{
ID = index,
DAT = dat_
});
}
}
}
} }
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//获取呼叫领料单 }//获取呼叫领料单
else if (SYSAPI == "SC822")
{
try
{
DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(DAT).Copy();//反序列化
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
UserWorkOrder userWorkOrder = new UserWorkOrder();
userWorkOrder.WorkOrderTable = dataTable;
userWorkOrder.Show();
});
}
catch (Exception)
{
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
UserInf userInf = new UserInf();
userInf.Inf_DAT = "没有等待中的工艺";
userInf.Show();
userInf.Activate();
});
// LogGing.LogGingDATA("[ERR='" + ex + "']=Exception");
MainWindowViewModel.ERR_c++;
}
}//可用工单
else if (SYSAPI == "SC830") else if (SYSAPI == "SC830")
{ {
try try
@ -236,7 +168,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
drEmployee["WaterLevel"] = _new.GetValue("MTL"); drEmployee["WaterLevel"] = _new.GetValue("MTL");
drEmployee["Process"] = _new.GetValue("Process"); drEmployee["Process"] = _new.GetValue("Process");
drEmployee["Step"] = _new.GetValue("Step"); drEmployee["Step"] = _new.GetValue("Step");
drEmployee["UserButton"] = _new.GetValue("User"); drEmployee["UserButton"] = _new.GetValue("UserB");
if (_new.GetValue("InfoS").ToString() != "900") if (_new.GetValue("InfoS").ToString() != "900")
{ drEmployee["UserInfoStart"] = _new.GetValue("InfoS"); } { drEmployee["UserInfoStart"] = _new.GetValue("InfoS"); }
drEmployee["UserInfo"] = _new.GetValue("Info"); drEmployee["UserInfo"] = _new.GetValue("Info");
@ -251,8 +183,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
Dictionary<string, object> Chart_new = new Dictionary<string, object>();//缓存函数 Dictionary<string, object> Chart_new = new Dictionary<string, object>();//缓存函数
Chart_new.Add("WorkOrder", _new.GetValue("Work")); Chart_new.Add("WorkOrder", _new.GetValue("Work"));
Chart_new.Add("Machine", mac); //Chart_new.Add("Machine", mac);
Chart_new.Add("Time", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); //Chart_new.Add("Time", _new.GetValue("Time"));
Chart_new.Add("MST", _new.GetValue("MST")); Chart_new.Add("MST", _new.GetValue("MST"));
Chart_new.Add("MTT", _new.GetValue("MTT")); Chart_new.Add("MTT", _new.GetValue("MTT"));
Chart_new.Add("MTL", _new.GetValue("MTL")); Chart_new.Add("MTL", _new.GetValue("MTL"));
@ -268,20 +200,18 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
MainWindowViewModel.SQLiteChartAdress.InsertData("Chart", Chart_new);// 执行插入 MainWindowViewModel.SQLiteChartAdress.InsertData("Chart", Chart_new);// 执行插入
} }
} }
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; catch (Exception ex)
{
LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++;
} }
}//当前信息 }//当前信息
else if (SYSAPI == "SC831") else if (SYSAPI == "SC831")
{ {
try try
{ {
Dictionary<string, object> _dat;
_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT);
MainWindowViewModel.dt_TP = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("select * WorkorderSteps Dyelot where WorkOrder='" +
_dat.GetValue("Work") + "' AND ReDye ='" + _dat.GetValue("Re") + "'", null).Tables[0];
} }
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//当前步骤信息 }
else if (SYSAPI == "SC832") else if (SYSAPI == "SC832")
{ {
try try
@ -294,11 +224,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
Dictionary<string, object> _dat;
_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT);
MonitorView.Product_DAT = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("select * from Dyelot where WorkOrder='" +
_dat.GetValue("WorkOrder") + "' AND ReDye ='" + _dat.GetValue("ReDye") + "'", null).Tables[0];
MonitorView.Product_bool = true;
} }
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//当前领料单信息 }//当前领料单信息
@ -306,34 +231,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
Dictionary<string, object> _dat;
_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT);
string[] DO = _dat.GetValue("DO").ToString().Split(',');
string[] DQ = _dat.GetValue("DQ").ToString().Split(',');
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID", Type.GetType("System.String"));
dataTable.Columns.Add("type", Type.GetType("System.String"));
dataTable.Columns.Add("DIO", Type.GetType("System.Bool"));
dataTable.Columns.Add("PLC", Type.GetType("System.String"));
for (int DOi = 0; DOi < DO.Length; DOi++)
{
DataRow newRow = dataTable.NewRow();
newRow["ID"] = DOi+2001;
newRow["type"] = "DO";
newRow["DIO"] = DO[DOi];
newRow["PLC"] = DOi + 2001;
dataTable.Rows.Add(newRow);
}
for (int DQi = 0; DQi < DO.Length; DQi++)
{
DataRow newRow = dataTable.NewRow();
newRow["ID"] = DQi + 3001;
newRow["type"] = "DQ";
newRow["DIO"] = DQ[DQi];
newRow["PLC"] = DQi + 3001;
dataTable.Rows.Add(newRow);
}
MainWindowViewModel.dt_d = dataTable;
} }
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//数字开关表 }//数字开关表
@ -341,34 +238,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
Dictionary<string, object> _dat;
_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT);
string[] AI = _dat.GetValue("DO").ToString().Split(',');
string[] AO = _dat.GetValue("DQ").ToString().Split(',');
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID", Type.GetType("System.String"));
dataTable.Columns.Add("type", Type.GetType("System.String"));
dataTable.Columns.Add("AIO", Type.GetType("System.Int"));
dataTable.Columns.Add("PLC", Type.GetType("System.String"));
for (int AIi = 0; AIi < AI.Length; AIi++)
{
DataRow newRow = dataTable.NewRow();
newRow["ID"] = AIi + 4001;
newRow["type"] = "DO";
newRow["AIO"] = AI[AIi];
newRow["PLC"] = AIi + 4001;
dataTable.Rows.Add(newRow);
}
for (int AOi = 0; AOi < AO.Length; AOi++)
{
DataRow newRow = dataTable.NewRow();
newRow["ID"] = AOi + 5001;
newRow["type"] = "DQ";
newRow["AIO"] = AO[AOi];
newRow["PLC"] = AOi + 5001;
dataTable.Rows.Add(newRow);
}
MainWindowViewModel.dt_a = dataTable;
} }
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//寄存器表 }//寄存器表
@ -376,23 +245,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
Dictionary<string, object> _dat;
_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(DAT);
string[] M = _dat.GetValue("M").ToString().Split(',');
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID", Type.GetType("System.String"));
dataTable.Columns.Add("type", Type.GetType("System.String"));
dataTable.Columns.Add("Value", Type.GetType("System.Double"));
dataTable.Columns.Add("PLC", Type.GetType("System.String"));
for (int Mi = 0; Mi < M.Length; Mi++)
{
DataRow newRow = dataTable.NewRow();
newRow["ID"] = Mi + 1001;
newRow["type"] = "M";
newRow["Value"] = M[Mi];
newRow["PLC"] = Mi + 1001;
dataTable.Rows.Add(newRow);
}
} }
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
@ -425,8 +277,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 109 } },
"WorkOrder ='" + DAT + "'", null);
} }
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//发布失败 }//发布失败
@ -434,21 +285,13 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 119 } },
"WorkOrder ='" + DAT + "'", null);
} }
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//细节错误 }//细节错误
else if (SYSAPI == "SC921")
{
try
{
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//错误
else if (SYSAPI == "SC980") else if (SYSAPI == "SC980")
{ {
MessageBox.Show(DAT, "SCCM", MessageBoxButton.OK, MessageBoxImage.Question);
} }
} }
} }
@ -564,8 +407,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
string[] sArray = Regex.Split(e.ByteBlock.Span.ToString(Encoding.UTF8), string[] sArray = Regex.Split(e.ByteBlock.Span.ToString(Encoding.UTF8),
@"\n", RegexOptions.IgnoreCase); @"\n", RegexOptions.IgnoreCase);
return EasyTask.CompletedTask; ; return EasyTask.CompletedTask; ;
}; };
await portclient.SetupAsync(new TouchSocketConfig() await portclient.SetupAsync(new TouchSocketConfig()
@ -577,7 +418,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
PortName = com,//COM PortName = com,//COM
StopBits = System.IO.Ports.StopBits.One,//停止位 StopBits = System.IO.Ports.StopBits.One,//停止位
}) })
.SetSerialDataHandlingAdapter(() => new PeriodPackageAdapter() { CacheTimeout = TimeSpan.FromMilliseconds(100) }) .SetSerialDataHandlingAdapter(() => new PeriodPackageAdapter() { CacheTimeout = TimeSpan.FromMilliseconds(500) })
.ConfigurePlugins(a => .ConfigurePlugins(a =>
{ {
//a.Add<MyPlugin>(); //a.Add<MyPlugin>();
@ -602,14 +443,15 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{ {
try try
{ {
// var t = CRCcheck16.ToCRC16(command); var t = CRCcheck16.ToCRC16(command);
_responseEvent.Reset(); _responseEvent.Reset();
serialPortClients.Send(command); // 发送指令 serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应 // 等待500毫秒或直到收到响应
_responseEvent.Wait(TimeSpan.FromMilliseconds(1000)); if (!_responseEvent.Wait(TimeSpan.FromMilliseconds(1000)))
if (!_responseEvent.IsSet)
{ {
//清机台状态 _responseEvent.Set();
//清楚了解状态
string Station = ClipBetween(command, '[', ']'); string Station = ClipBetween(command, '[', ']');
lock (MainWindowViewModel.Machines.Rows.SyncRoot) lock (MainWindowViewModel.Machines.Rows.SyncRoot)
{ {
@ -634,8 +476,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
} }
} }
} }
// 重置事件状态
_responseEvent.Set();
} }
catch (Exception) { } catch (Exception) { }
} }

Loading…
Cancel
Save