diff --git a/SunlightCentralizedControlManagement_SCCM_.csproj b/SunlightCentralizedControlManagement_SCCM_.csproj index bde8f8a..92b0e4a 100644 --- a/SunlightCentralizedControlManagement_SCCM_.csproj +++ b/SunlightCentralizedControlManagement_SCCM_.csproj @@ -480,9 +480,6 @@ 5.2.1 - - 1.0.3405.78 - 3.0.81 diff --git a/UserClass/AsyncSerialPortClient.cs b/UserClass/AsyncSerialPortClient.cs index d98ba60..0e9c546 100644 --- a/UserClass/AsyncSerialPortClient.cs +++ b/UserClass/AsyncSerialPortClient.cs @@ -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 _new = new Dictionary();//缓存函数 + _new = JsonConvert.DeserializeObject>(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 { { "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 { { "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 { { "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 _new = new Dictionary();//缓存函数 + _new = JsonConvert.DeserializeObject>(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 Chart_new = new Dictionary();//缓存函数 + 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); /// /// 发送指令并等待响应,超时时间为500ms /// @@ -162,35 +388,55 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass /// 从机的响应数据,超时则为null 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) { } } } } diff --git a/UserClass/AsyncTcpClient.cs b/UserClass/AsyncTcpClient.cs index 3b88264..15ac300 100644 --- a/UserClass/AsyncTcpClient.cs +++ b/UserClass/AsyncTcpClient.cs @@ -38,10 +38,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass /// 异步TCP客户端 /// 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);// 执行插入 } } diff --git a/ViewModel/MainWindowViewModel.cs b/ViewModel/MainWindowViewModel.cs index a1c4b14..98c8736 100644 --- a/ViewModel/MainWindowViewModel.cs +++ b/ViewModel/MainWindowViewModel.cs @@ -2,6 +2,7 @@ using LiveChartsCore.Geo; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using NModbus; using OpenTK.Graphics.ES11; using ScottPlot.Palettes; using ScottPlot.TickGenerators.TimeUnits; @@ -40,6 +41,7 @@ using static MaterialDesignThemes.Wpf.Theme; using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper; using static SunlightCentralizedControlManagement_SCCM_.View.MachinesView; using static SunlightCentralizedControlManagement_SCCM_.ViewModel.ManualDyelotModel; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView; namespace SunlightCentralizedControlManagement_SCCM_.ViewModel { @@ -81,6 +83,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel public static SQLiteHelper SQLiteHelpers = null; //定义数据库 private BDC_SQL.BDCSqlHelper bDCSqlHelper = new BDC_SQL.BDCSqlHelper(); private 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 DataTable Machines = new DataTable(); //设备表缓存 // public static DataRow MachinesROW; public static DataTable USER_data = new DataTable(); @@ -106,6 +110,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel } catch (Exception ex) { LogGing.ERRDATA(ex); } + SQLiteChartAdress = new SQLiteHelper(ChartAdress); //数据库连接路径 + SQLiteChartAdress.Open(); //打开数据库 SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 SQLiteHelpers.Open(); //打开数据库 Machines = SQLiteHelpers.ExecuteDataSet("select * from Machines Order by id", null).Tables[0]; //读取表写入缓存 @@ -642,47 +648,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel else { Time30++; } } async void Tick_Event_5S()//Tick_Event周期执行事件5S - { - DataTable Workorddr = SQLiteHelpers.ExecuteDataSet("select * from WorkorderSteps ", null).Tables[0]; //读取表写入缓存 - int y = Workorddr.Rows.Count; - object[] datal = new object[19]; - for (int i = 0; i < Workorddr.Rows.Count; i++) - { - - /*AsyncSerialPortClient.SendCommandAndWait( - MachiensPortClient[2], - "SC800[" + MachinesRow["Station"] + "]" + f);*/ - if (i >= 20) break; - - datal[0] = Workorddr.Rows[i]["Step"]; - datal[1] = Workorddr.Rows[i]["StepID"]; - datal[2] = Workorddr.Rows[i]["Parameter1"] ?? 0; - datal[3] = Workorddr.Rows[i]["Parameter2"] ?? 0; - datal[4] = Workorddr.Rows[i]["Parameter3"] ?? 0; - datal[5] = Workorddr.Rows[i]["Parameter4"] ?? 0; - datal[6] = Workorddr.Rows[i]["Parameter5"] ?? 0; - datal[7] = Workorddr.Rows[i]["StepID_S1"]; - datal[8] = Workorddr.Rows[i]["StepID_S2"]; - datal[9] = Workorddr.Rows[i].Field("Parameter1_S1") ?? 0; - datal[10] = Workorddr.Rows[i].Field("Parameter1_S2") ?? 0; - datal[11] = Workorddr.Rows[i].Field("Parameter2_S1") ?? 0; - datal[12] = Workorddr.Rows[i].Field("Parameter2_S2") ?? 0; - datal[13] = Workorddr.Rows[i].Field("Parameter3_S1") ?? 0; - datal[14] = Workorddr.Rows[i].Field("Parameter3_S2") ?? 0; - datal[15] = Workorddr.Rows[i].Field("Parameter4_S1") ?? 0; - datal[16] = Workorddr.Rows[i].Field("Parameter4_S2") ?? 0; - datal[17] = Workorddr.Rows[i].Field("Parameter5_S1") ?? 0; - datal[18] = Workorddr.Rows[i].Field("Parameter5_S2") ?? 0; - - stringQueueSerial_3.Enqueue(new QueueSerial - { - ID = 45, - DAT = "SC810[" + 1 + "]" + datal.ToJsonString() - });//信息插入队列 - } - - - + { foreach (DataRow MachinesRow in Machines.Rows) { /**发送800指令**/ @@ -1035,7 +1001,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel /*曲线图*/ for (int i = 0; i < Machines.Rows.Count; i++)//曲线图 { - if (Selet_Machines(Machines, "State", "ID='" + i + "'").ToString() !="800") + string st = Selet_Machines(Machines, "State", "ID='" + i + "'").ToString(); + if (st != "800" && st != "801" && st != "802") { DataRow whole_Dat = Whole_dat.NewRow(); whole_Dat["Machine"] = Selet_Machines(Machines, "NAME", "ID='" + i + "'").ToString(); @@ -1089,7 +1056,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel await AsyncSerialPortClient.PortClient(MachiensPortClient[0] //建立连接 , Configini.IniReadvalue("SYS", "COMP1") , Convert.ToInt32(Configini.IniReadvalue("SYS", "BAUD1"))); - Thread myThread1 = new Thread(Port1_link); + AsyncSerialPortClient._responseEvent.Set(); + + Thread myThread1 = new Thread(Port1_link); myThread1.Start(); } if (Machines.Select("Type='false' AND Serial='PORT2'").Length > 0) @@ -1100,7 +1069,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel await AsyncSerialPortClient.PortClient2(MachiensPortClient[1] //建立连接 , Configini.IniReadvalue("SYS", "COMP2") , Convert.ToInt32(Configini.IniReadvalue("SYS", "BAUD2"))); - + AsyncSerialPortClient._responseEvent2.Set(); + Thread myThread2 = new Thread(Port2_link); myThread2.Start(); } @@ -1112,7 +1082,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel await AsyncSerialPortClient.PortClient3(MachiensPortClient[2] //建立连接 , Configini.IniReadvalue("SYS", "COMP3") , Convert.ToInt32(Configini.IniReadvalue("SYS", "BAUD3"))); - + AsyncSerialPortClient._responseEvent3.Set(); + Thread myThread3 = new Thread(Port3_link); myThread3.Start(); } @@ -1124,126 +1095,195 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel await AsyncSerialPortClient.PortClient4(MachiensPortClient[3] //建立连接 , Configini.IniReadvalue("SYS", "COMP4") , Convert.ToInt32(Configini.IniReadvalue("SYS", "BAUD4"))); - + AsyncSerialPortClient._responseEvent4.Set(); + Thread myThread4 = new Thread(Port4_link); myThread4.Start(); } } private void Port1_link() { - try + while (true) { - foreach (DataRow MachinesRow in Machines.Select("Type='false' AND Serial='PORT1'")) + try { if (stringQueueSerial_1.Count > 0) //信息发送队列 { - QueueSerial t = stringQueueSerial_1.Dequeue(); - - AsyncSerialPortClient.SendCommandAndWait(MachiensPortClient[0], t.DAT+"\n"); - //MachiensPortClient[t.ID].Send(t.DAT); + if (AsyncSerialPortClient._responseEvent.IsSet) + { + QueueSerial t = stringQueueSerial_1.Dequeue(); + AsyncSerialPortClient.SendCommandAndWait(MachiensPortClient[0], t.DAT + "\n"); + //MachiensPortClient[t.ID].Send(t.DAT); + } + else + { + Thread.Sleep(TimeSpan.FromMilliseconds(50)); + } } - if (MachinesRow["State"].ToString() == "800") + else { - AsyncSerialPortClient.SendCommandAndWait( - MachiensPortClient[0], - "SC800[" + MachinesRow["Station"] + "]\n"); + foreach (DataRow MachinesRow in Machines.Select("Type='false' AND Serial='PORT1'")) + { + if (MachinesRow["State"].ToString() == "800") + { + stringQueueSerial_1.Enqueue(new QueueSerial + { + ID = Convert.ToInt16(MachinesRow["ID"]), + DAT = "SC800[" + MachinesRow["Station"] + "]" + }); + } + else + { + Thread.Sleep(TimeSpan.FromMilliseconds(5)); + } + } } } - } - catch (Exception) - { - } - finally - { - Port1_link();//回调 - } + catch (Exception) + { + } + finally + { + } + } }//发送 private void Port2_link() { - try + while (true) { - foreach (DataRow MachinesRow in Machines.Select("Type='false' AND Serial='PORT2'")) + try { if (stringQueueSerial_2.Count > 0) //信息发送队列 { - QueueSerial t = stringQueueSerial_2.Dequeue(); - - AsyncSerialPortClient.SendCommandAndWait2(MachiensPortClient[1], t.DAT+ "\n"); - //MachiensPortClient[t.ID].Send(t.DAT); + if (AsyncSerialPortClient._responseEvent2.IsSet) + { + QueueSerial t = stringQueueSerial_2.Dequeue(); + AsyncSerialPortClient.SendCommandAndWait2(MachiensPortClient[1], t.DAT + "\n"); + //MachiensPortClient[t.ID].Send(t.DAT); + } + else + { + Thread.Sleep(TimeSpan.FromMilliseconds(50)); + } } - if (MachinesRow["State"].ToString() == "800") + else { - AsyncSerialPortClient.SendCommandAndWait2( - MachiensPortClient[1], - "SC800[" + MachinesRow["Station"] + "]\n"); + foreach (DataRow MachinesRow in Machines.Select("Type='false' AND Serial='PORT2'")) + { + if (MachinesRow["State"].ToString() == "800") + { + stringQueueSerial_2.Enqueue(new QueueSerial + { + ID = Convert.ToInt16(MachinesRow["ID"]), + DAT = "SC800[" + MachinesRow["Station"] + "]" + }); + } + else + { + Thread.Sleep(TimeSpan.FromMilliseconds(5)); + } + } } } - } - catch (Exception) - { - } - finally - { - Port2_link();//回调 - } + catch (Exception) + { + } + finally + { + } + } }//发送 private void Port3_link() { - try + while (true) { - foreach (DataRow MachinesRow in Machines.Select("Type='false' AND Serial='PORT3'")) + try { if (stringQueueSerial_3.Count > 0) //信息发送队列 { - QueueSerial t = stringQueueSerial_3.Dequeue(); - - AsyncSerialPortClient.SendCommandAndWait3(MachiensPortClient[2],t.DAT+ "\n"); - //MachiensPortClient[t.ID].Send(t.DAT); + if (AsyncSerialPortClient._responseEvent3.IsSet) + { + QueueSerial t = stringQueueSerial_3.Dequeue(); + AsyncSerialPortClient.SendCommandAndWait3(MachiensPortClient[2], t.DAT + "\n"); + //MachiensPortClient[t.ID].Send(t.DAT); + } + else + { + Thread.Sleep(TimeSpan.FromMilliseconds(50)); + } } - else if (MachinesRow["State"].ToString() == "800") + else { - AsyncSerialPortClient.SendCommandAndWait3( - MachiensPortClient[2], - "SC800[" + MachinesRow["Station"] + "]\n"); + foreach (DataRow MachinesRow in Machines.Select("Type='false' AND Serial='PORT3'")) + { + if (MachinesRow["State"].ToString() == "800") + { + stringQueueSerial_3.Enqueue(new QueueSerial + { + ID = Convert.ToInt16(MachinesRow["ID"]), + DAT = "SC800[" + MachinesRow["Station"] + "]" + }); + } + else + { + Thread.Sleep(TimeSpan.FromMilliseconds(50)); + } + } } } - } - catch (Exception) - { - } - finally - { - Port3_link();//回调 - } + catch (Exception) + { + } + finally + { + } + } }//发送 private void Port4_link() { - try + while (true) { - foreach (DataRow MachinesRow in Machines.Select("Type='false' AND Serial='PORT4'")) + try { if (stringQueueSerial_4.Count > 0) //信息发送队列 { - QueueSerial t = stringQueueSerial_4.Dequeue(); - - AsyncSerialPortClient.SendCommandAndWait4(MachiensPortClient[3],t.DAT+ "\n"); - //MachiensPortClient[t.ID].Send(t.DAT); + if (AsyncSerialPortClient._responseEvent4.IsSet) + { + QueueSerial t = stringQueueSerial_4.Dequeue(); + AsyncSerialPortClient.SendCommandAndWait4(MachiensPortClient[3], t.DAT + "\n"); + //MachiensPortClient[t.ID].Send(t.DAT); + } + else + { + Thread.Sleep(TimeSpan.FromMilliseconds(50)); + } } - if (MachinesRow["State"].ToString() == "800") + else { - AsyncSerialPortClient.SendCommandAndWait4( - MachiensPortClient[3], - "SC800[" + MachinesRow["Station"] + "]\n"); + foreach (DataRow MachinesRow in Machines.Select("Type='false' AND Serial='PORT4'")) + { + if (MachinesRow["State"].ToString() == "800") + { + stringQueueSerial_4.Enqueue(new QueueSerial + { + ID = Convert.ToInt16(MachinesRow["ID"]), + DAT = "SC800[" + MachinesRow["Station"] + "]" + }); + } + else + { + Thread.Sleep(TimeSpan.FromMilliseconds(50)); + } + } } } - } - catch (Exception) - { - } - finally - { - Port4_link();//回调 - } + catch (Exception) + { + } + finally + { + } + } }//发送 public static string SYS_WorkNumder; //工单号