diff --git a/UserClass/AsyncSerialPortClient.cs b/UserClass/AsyncSerialPortClient.cs index 04ec44d..98a96f9 100644 --- a/UserClass/AsyncSerialPortClient.cs +++ b/UserClass/AsyncSerialPortClient.cs @@ -427,7 +427,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass _responseEvent.Reset(); serialPortClients.Send(command); // 发送指令 // 等待500毫秒或直到收到响应 - _responseEvent.Wait(TimeSpan.FromMilliseconds(1000)); + _responseEvent.Wait(TimeSpan.FromMilliseconds(500)); // 重置事件状态 _responseEvent.Set(); //清楚了解状态 diff --git a/ViewModel/MainWindowViewModel.cs b/ViewModel/MainWindowViewModel.cs index f6a1a78..1d9345a 100644 --- a/ViewModel/MainWindowViewModel.cs +++ b/ViewModel/MainWindowViewModel.cs @@ -95,7 +95,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel public static int ERR_c = 0;//错误计数器 public bool UPort1, UPort2, UPort3; public static bool[] USER_Capacity { set; get; } = new bool[99]; - + public MainWindowViewModel() { try @@ -264,7 +264,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel stringQueueSerial_1.Enqueue(new QueueSerial { ID = index, - DAT = "SC811" + Selet_Machines(Machines, "SYSKEY", "ID='" + index + "'") + dataTable.Rows[0].ToJsonString() + DAT = "SC811[" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + + dataTable.Rows[0].ToJsonString() }); } else if (Selet_Machines(Machines, "Serial", "Name='" + WorkOrderdata_m + "'").ToString() == "PORT2") @@ -272,7 +274,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel stringQueueSerial_2.Enqueue(new QueueSerial { ID = index, - DAT = "SC811" + Selet_Machines(Machines, "SYSKEY", "ID='" + index + "'") + dataTable.Rows[0].ToJsonString() + DAT = "SC811[" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + + dataTable.Rows[0].ToJsonString() }); } else if (Selet_Machines(Machines, "Serial", "Name='" + WorkOrderdata_m + "'").ToString() == "PORT3") @@ -280,7 +284,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel stringQueueSerial_3.Enqueue(new QueueSerial { ID = index, - DAT = "SC811" + Selet_Machines(Machines, "SYSKEY", "ID='" + index + "'") + dataTable.Rows[0].ToJsonString() + DAT = "SC811[" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + + dataTable.Rows[0].ToJsonString() }); } else if (Selet_Machines(Machines, "Serial", "Name='" + WorkOrderdata_m + "'").ToString() == "PORT4") @@ -288,14 +294,16 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel stringQueueSerial_4.Enqueue(new QueueSerial { ID = index, - DAT = "SC811" + Selet_Machines(Machines, "SYSKEY", "ID='" + index + "'") + dataTable.Rows[0].ToJsonString() + DAT = "SC811[" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + + dataTable.Rows[0].ToJsonString() }); } } } } /*下传工单总览*/ - DataTable WorkOrderdata = SQLiteHelpers.ExecuteDataSet("select * from WorkOrder where (State='100' or State='110') AND EndTime >'" + + DataTable WorkOrderdata = SQLiteHelpers.ExecuteDataSet("select * from WorkOrder where (State='100' or State='110') AND EndTime >'" + DateTime.Now.AddHours(-8).ToString("yyyy/MM/dd HH:mm:ss") + "'", null).Tables[0]; //读取表写入缓存 for (int k = 0; k < WorkOrderdata.Rows.Count; k++) { @@ -349,7 +357,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel stringQueueSerial_1.Enqueue(new QueueSerial { ID = index, - DAT = "SC810" + Selet_Machines(Machines, "SYSKEY", "ID='" + index + "'") + DataTableToDictionary(dataTable).ToJsonString() + DAT = "SC810[" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + + DataTableToDictionary(dataTable).ToJsonString() }); } else if (Selet_Machines(Machines, "Serial", "Name='" + WorkOrderdata_m + "'").ToString() == "PORT2") @@ -357,7 +367,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel stringQueueSerial_2.Enqueue(new QueueSerial { ID = index, - DAT = "SC810" + Selet_Machines(Machines, "SYSKEY", "ID='" + index + "'") + DataTableToDictionary(dataTable).ToJsonString() + DAT = "SC810[" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + + DataTableToDictionary(dataTable).ToJsonString() }); } else if (Selet_Machines(Machines, "Serial", "Name='" + WorkOrderdata_m + "'").ToString() == "PORT3") @@ -365,7 +377,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel stringQueueSerial_3.Enqueue(new QueueSerial { ID = index, - DAT = "SC810" + Selet_Machines(Machines, "SYSKEY", "ID='" + index + "'") + DataTableToDictionary(dataTable).ToJsonString() + DAT = "SC810[" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + + DataTableToDictionary(dataTable).ToJsonString() }); } else if (Selet_Machines(Machines, "Serial", "Name='" + WorkOrderdata_m + "'").ToString() == "PORT4") @@ -373,7 +387,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel stringQueueSerial_4.Enqueue(new QueueSerial { ID = index, - DAT = "SC810" + Selet_Machines(Machines, "SYSKEY", "ID='" + index + "'") + DataTableToDictionary(dataTable).ToJsonString() + DAT = "SC810[" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + + DataTableToDictionary(dataTable).ToJsonString() }); } @@ -384,7 +400,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel } } /*下传工单设置*/ - DataTable WorkOrderset_ = SQLiteHelpers.ExecuteDataSet("select * from WorkOrder where (State='113'or State='114') AND EndTime >'" + + DataTable WorkOrderset_ = SQLiteHelpers.ExecuteDataSet("select * from WorkOrder where (State='113'or State='114') AND EndTime >'" + DateTime.Now.AddHours(-8).ToString("yyyy/MM/dd HH:mm:ss") + "'", null).Tables[0]; //读取表写入缓存 for (int k = 0; k < WorkOrderset_.Rows.Count; k++) { @@ -428,7 +444,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel { ID = index, DAT = "SC812[" + - Selet_Machines(Machines, "Station", "ID='" + k + "'") + "]" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + DataTableToDictionary(WorkOrder_set).ToJsonString() }); } @@ -438,7 +454,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel { ID = index, DAT = "SC812[" + - Selet_Machines(Machines, "Station", "ID='" + k + "'") + "]" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + DataTableToDictionary(WorkOrder_set).ToJsonString() }); } @@ -448,7 +464,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel { ID = index, DAT = "SC812[" + - Selet_Machines(Machines, "Station", "ID='" + k + "'") + "]" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + DataTableToDictionary(WorkOrder_set).ToJsonString() }); } @@ -458,7 +474,7 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel { ID = index, DAT = "SC812[" + - Selet_Machines(Machines, "Station", "ID='" + k + "'") + "]" + + Selet_Machines(Machines, "Station", "ID='" + index + "'") + "]" + DataTableToDictionary(WorkOrder_set).ToJsonString() }); } @@ -535,16 +551,11 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel } else {//串口发送 - if (MachinesRow["State"].ToString() == "802") + string mac_s = MachinesRow["State"].ToString(); + if (mac_s == "802") { if (MachinesRow["Serial"].ToString() == "PORT1") { - stringQueueSerial_1.Enqueue(new QueueSerial - { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC830[" + MachinesRow["Station"] + "]" - }); - bool mode = Convert.ToBoolean(MachinesRow["LOCK"]);//发送锁定状态 if (mode) { @@ -552,21 +563,23 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel dat_829.Clear(); dat_829.Add("NAME", "LOCK"); dat_829.Add("VALUE", "True"); - stringQueueSerial_1.Enqueue(new QueueSerial + + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC829[" + MachinesRow["Station"] + "]" + dat_829.ToJsonString(); + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_1.Any(item => item.ID == id && item.DAT == dat); + if (!exists) { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC829[" + MachinesRow["Station"] + "]" + dat_829.ToJsonString() - }); + stringQueueSerial_1.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } } } else if (MachinesRow["Serial"].ToString() == "PORT2") { - stringQueueSerial_2.Enqueue(new QueueSerial - { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC830[" + MachinesRow["Station"] + "]" - }); - bool mode = Convert.ToBoolean(MachinesRow["LOCK"]);//发送锁定状态 if (mode) { @@ -574,21 +587,23 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel dat_829.Clear(); dat_829.Add("NAME", "LOCK"); dat_829.Add("VALUE", "True"); - stringQueueSerial_2.Enqueue(new QueueSerial + + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC829[" + MachinesRow["Station"] + "]" + dat_829.ToJsonString(); + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_2.Any(item => item.ID == id && item.DAT == dat); + if (!exists) { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC829[" + MachinesRow["Station"] + "]" + dat_829.ToJsonString() - }); + stringQueueSerial_2.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } } } else if (MachinesRow["Serial"].ToString() == "PORT3") { - stringQueueSerial_3.Enqueue(new QueueSerial - { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC830[" + MachinesRow["Station"] + "]" - }); - bool mode = Convert.ToBoolean(MachinesRow["LOCK"]);//发送锁定状态 if (mode) { @@ -596,21 +611,23 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel dat_829.Clear(); dat_829.Add("NAME", "LOCK"); dat_829.Add("VALUE", "True"); - stringQueueSerial_3.Enqueue(new QueueSerial + + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC829[" + MachinesRow["Station"] + "]" + dat_829.ToJsonString(); + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_3.Any(item => item.ID == id && item.DAT == dat); + if (!exists) { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC829[" + MachinesRow["Station"] + "]" + dat_829.ToJsonString() - }); + stringQueueSerial_3.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } } } else if (MachinesRow["Serial"].ToString() == "PORT4") { - stringQueueSerial_4.Enqueue(new QueueSerial - { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC830[" + MachinesRow["Station"] + "]" - }); - bool mode = Convert.ToBoolean(MachinesRow["LOCK"]);//发送锁定状态 if (mode) { @@ -618,69 +635,168 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel dat_829.Clear(); dat_829.Add("NAME", "LOCK"); dat_829.Add("VALUE", "True"); - stringQueueSerial_4.Enqueue(new QueueSerial + + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC829[" + MachinesRow["Station"] + "]" + dat_829.ToJsonString(); + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_4.Any(item => item.ID == id && item.DAT == dat); + if (!exists) { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC829[" + MachinesRow["Station"] + "]" + dat_829.ToJsonString() - }); + stringQueueSerial_4.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } } + } } - int m_run = Convert.ToInt16(MachinesRow["WORK_RUN"]); - if (m_run == 0) - { - Updata_Machines(Machines, "State", "ID='" + MachinesRow["ID"] + "'", "101");//停止状态 - } - else if (m_run == 1) - { - Updata_Machines(Machines, "State", "ID='" + MachinesRow["ID"] + "'", "101");//暂停状态 - } - else if (m_run == 2) + else if (mac_s == "800") { - Updata_Machines(Machines, "State", "ID='" + MachinesRow["ID"] + "'", "202");//运行 - } - - if (Convert.ToBoolean(MachinesRow["ERR"])) - { - Updata_Machines(Machines, "State", "ID='" + MachinesRow["ID"] + "'", "309");//错误 + if (MachinesRow["Serial"].ToString() == "PORT1") + { + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC800[" + MachinesRow["Station"] + "]"; + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_1.Any(item => item.ID == id && item.DAT == dat); + if (!exists) + { + stringQueueSerial_1.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } + } + else if (MachinesRow["Serial"].ToString() == "PORT2") + { + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC800[" + MachinesRow["Station"] + "]"; + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_2.Any(item => item.ID == id && item.DAT == dat); + if (!exists) + { + stringQueueSerial_2.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } + } + else if (MachinesRow["Serial"].ToString() == "PORT3") + { + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC800[" + MachinesRow["Station"] + "]"; + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_3.Any(item => item.ID == id && item.DAT == dat); + if (!exists) + { + stringQueueSerial_3.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } + } + else if (MachinesRow["Serial"].ToString() == "PORT4") + { + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC800[" + MachinesRow["Station"] + "]"; + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_4.Any(item => item.ID == id && item.DAT == dat); + if (!exists) + { + stringQueueSerial_4.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } + } } - - string mac_s = MachinesRow["State"].ToString(); if ((mac_s == "101") || (mac_s == "201") || (mac_s == "202") || (mac_s == "301") || (mac_s == "309"))//获取信息 { if (MachinesRow["Serial"].ToString() == "PORT1") { - stringQueueSerial_1.Enqueue(new QueueSerial + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC830[" + MachinesRow["Station"] + "]"; + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_1.Any(item => item.ID == id && item.DAT == dat); + if (!exists) { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC830[" + MachinesRow["Station"] + "]" - }); + stringQueueSerial_1.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } } else if (MachinesRow["Serial"].ToString() == "PORT2") { - stringQueueSerial_2.Enqueue(new QueueSerial + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC830[" + MachinesRow["Station"] + "]"; + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_2.Any(item => item.ID == id && item.DAT == dat); + if (!exists) { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC830[" + MachinesRow["Station"] + "]" - }); + stringQueueSerial_2.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } } else if (MachinesRow["Serial"].ToString() == "PORT3") { - stringQueueSerial_3.Enqueue(new QueueSerial + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC830[" + MachinesRow["Station"] + "]"; + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_3.Any(item => item.ID == id && item.DAT == dat); + if (!exists) { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC830[" + MachinesRow["Station"] + "]" - }); + stringQueueSerial_3.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } } else if (MachinesRow["Serial"].ToString() == "PORT4") { - stringQueueSerial_4.Enqueue(new QueueSerial + int id = Convert.ToInt16(MachinesRow["ID"]); + string dat = "SC830[" + MachinesRow["Station"] + "]"; + // 检查队列中是否已存在相同项 + bool exists = stringQueueSerial_4.Any(item => item.ID == id && item.DAT == dat); + if (!exists) { - ID = Convert.ToInt16(MachinesRow["ID"]), - DAT = "SC830[" + MachinesRow["Station"] + "]" - }); + stringQueueSerial_4.Enqueue(new QueueSerial + { + ID = id, + DAT = dat + }); + } } } + + int m_run = Convert.ToInt16(MachinesRow["WORK_RUN"]); + if (m_run == 0) + { + Updata_Machines(Machines, "State", "ID='" + MachinesRow["ID"] + "'", "101");//停止状态 + } + else if (m_run == 1) + { + Updata_Machines(Machines, "State", "ID='" + MachinesRow["ID"] + "'", "101");//暂停状态 + } + else if (m_run == 2) + { + Updata_Machines(Machines, "State", "ID='" + MachinesRow["ID"] + "'", "202");//运行 + } + + if (Convert.ToBoolean(MachinesRow["ERR"])) + { + Updata_Machines(Machines, "State", "ID='" + MachinesRow["ID"] + "'", "309");//错误 + } } } }