diff --git a/UserClass/AsyncSerialPortClient.cs b/UserClass/AsyncSerialPortClient.cs index 8431047..ec6d651 100644 --- a/UserClass/AsyncSerialPortClient.cs +++ b/UserClass/AsyncSerialPortClient.cs @@ -2,7 +2,6 @@ using ScottPlot.Plottables; using SunlightCentralizedControlManagement_SCCM_.View; using SunlightCentralizedControlManagement_SCCM_.ViewModel; -using SunlightCentralizedControlManagement_SCCM_.WindowsView; using System; using System.Collections.Generic; using System.Data; @@ -16,10 +15,8 @@ using System.Windows; using TouchSocket.Core; using TouchSocket.SerialPorts; using TouchSocket.Sockets; -using static OpenTK.Graphics.OpenGL.GL; using static SkiaSharp.HarfBuzz.SKShaper; using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper; -using static SunlightCentralizedControlManagement_SCCM_.ViewModel.MainWindowViewModel; using static System.Net.Mime.MediaTypeNames; namespace SunlightCentralizedControlManagement_SCCM_.UserClass @@ -47,7 +44,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass 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; } @@ -63,17 +60,19 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass portclient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从端口断开连接,当连接不成功时不会触发。 portclient.Received = (client, e) => { + //接受重置事件状态 _responseEvent.Set(); string[] sArray = Regex.Split(e.ByteBlock.Span.ToString(Encoding.UTF8), @"\n", RegexOptions.IgnoreCase); 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 Station = ClipBetween(sArray[i], '[', ']'); string DAT = sArray[i].Substring(sArray[i].IndexOf("]") + 1); + if (SYSAPI == "SC800") { try @@ -98,10 +97,11 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { + DAT = DAT.Substring(DAT.IndexOf("]") + 1); Dictionary _dat; _dat = SerializeConvert.JsonDeserializeFromString>(DAT); MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary { { "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) { @@ -112,14 +112,15 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { + DAT = DAT.Substring(DAT.IndexOf("]") + 1); Dictionary _dat; - _dat = SerializeConvert.JsonDeserializeFromString>(DAT); + _dat = SerializeConvert.JsonDeserializeFromString>(DAT); MainWindowViewModel.SQLiteHelpers.Update("WorkorderSteps", new Dictionary { { "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); - 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; + int x = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("select * from WorkorderSteps where WorkOrder='" + + _dat.GetValue("Work") + "' AND ReDye ='" + _dat.GetValue("ReDye") + "' AND Mode <> '1'", null).Tables[0].Rows.Count; if (x == 0) { MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary { { "State", 113 } }, @@ -132,6 +133,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { + DAT = DAT.Substring(DAT.IndexOf("]") + 1); Dictionary _dat; _dat = SerializeConvert.JsonDeserializeFromString>(DAT); MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary { { "State", 101 } }, @@ -143,79 +145,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { - Dictionary _dat; - _dat = SerializeConvert.JsonDeserializeFromString>(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 Product_ = new Dictionary();//缓存函数 - Product_.Add("State", 202); - Product_.Add("Dyelot", newRow.Field("Dyelot")); - Product_.Add("Step", newRow.Field("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("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++; } }//获取呼叫领料单 - else if (SYSAPI == "SC822") - { - try - { - DataTable dataTable = JsonConvert.DeserializeObject(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") { try @@ -236,7 +168,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass drEmployee["WaterLevel"] = _new.GetValue("MTL"); drEmployee["Process"] = _new.GetValue("Process"); drEmployee["Step"] = _new.GetValue("Step"); - drEmployee["UserButton"] = _new.GetValue("User"); + drEmployee["UserButton"] = _new.GetValue("UserB"); if (_new.GetValue("InfoS").ToString() != "900") { drEmployee["UserInfoStart"] = _new.GetValue("InfoS"); } drEmployee["UserInfo"] = _new.GetValue("Info"); @@ -246,13 +178,13 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass drEmployee.EndEdit(); drEmployee.AcceptChanges(); } - + if (_new.GetValue("Status").ToString() != "----------") { Dictionary Chart_new = new Dictionary();//缓存函数 Chart_new.Add("WorkOrder", _new.GetValue("Work")); - Chart_new.Add("Machine", mac); - Chart_new.Add("Time", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); + //Chart_new.Add("Machine", mac); + //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")); @@ -268,20 +200,18 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass 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") { try { - Dictionary _dat; - _dat = SerializeConvert.JsonDeserializeFromString>(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++; } - }//当前步骤信息 + } else if (SYSAPI == "SC832") { try @@ -294,11 +224,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { - Dictionary _dat; - _dat = SerializeConvert.JsonDeserializeFromString>(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++; } }//当前领料单信息 @@ -306,34 +231,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { - Dictionary _dat; - _dat = SerializeConvert.JsonDeserializeFromString>(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++; } }//数字开关表 @@ -341,34 +238,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { - Dictionary _dat; - _dat = SerializeConvert.JsonDeserializeFromString>(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++; } }//寄存器表 @@ -376,23 +245,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { - Dictionary _dat; - _dat = SerializeConvert.JsonDeserializeFromString>(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++; } @@ -425,8 +277,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { - MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary { { "State", 109 } }, - "WorkOrder ='" + DAT + "'", null); + } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//发布失败 @@ -434,21 +285,13 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { - MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary { { "State", 119 } }, - "WorkOrder ='" + DAT + "'", null); + } 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") { - 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), @"\n", RegexOptions.IgnoreCase); - - return EasyTask.CompletedTask; ; }; await portclient.SetupAsync(new TouchSocketConfig() @@ -577,7 +418,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass PortName = com,//COM StopBits = System.IO.Ports.StopBits.One,//停止位 }) - .SetSerialDataHandlingAdapter(() => new PeriodPackageAdapter() { CacheTimeout = TimeSpan.FromMilliseconds(100) }) + .SetSerialDataHandlingAdapter(() => new PeriodPackageAdapter() { CacheTimeout = TimeSpan.FromMilliseconds(500) }) .ConfigurePlugins(a => { //a.Add(); @@ -602,14 +443,15 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass { try { - // var t = CRCcheck16.ToCRC16(command); + var t = CRCcheck16.ToCRC16(command); _responseEvent.Reset(); serialPortClients.Send(command); // 发送指令 - // 等待500毫秒或直到收到响应 - _responseEvent.Wait(TimeSpan.FromMilliseconds(1000)); - if (!_responseEvent.IsSet) - { - //清机台状态 + + // 等待500毫秒或直到收到响应 + if (!_responseEvent.Wait(TimeSpan.FromMilliseconds(1000))) + { + _responseEvent.Set(); + //清楚了解状态 string Station = ClipBetween(command, '[', ']'); lock (MainWindowViewModel.Machines.Rows.SyncRoot) { @@ -634,8 +476,6 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass } } } - // 重置事件状态 - _responseEvent.Set(); } catch (Exception) { } }