diff --git a/NDSD-Screwdriver/DOperateEntity.cs b/NDSD-Screwdriver/DOperateEntity.cs index eda50aa..fa8443e 100644 --- a/NDSD-Screwdriver/DOperateEntity.cs +++ b/NDSD-Screwdriver/DOperateEntity.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace NDSD_Screwdriver { - internal class DOperateEntity + public class DOperateEntity { public int port { get; set; } diff --git a/NDSD-Screwdriver/MainForm.cs b/NDSD-Screwdriver/MainForm.cs index 7131922..97fe3d4 100644 --- a/NDSD-Screwdriver/MainForm.cs +++ b/NDSD-Screwdriver/MainForm.cs @@ -18,6 +18,7 @@ using System.Timers; using System.Windows.Forms; using System.Xml.Serialization; using NewLife.Caching; +using NewLife.Collections; namespace NDSD_Screwdriver { @@ -68,25 +69,30 @@ namespace NDSD_Screwdriver private CancellationTokenSource cancellationTokenSource; - private CancellationTokenSource sendCancellationTokenSource; + public int textRFID = 0; - private Queue _methodQueue; + private ICache cache = Cache.Default; int FlashIndex = 0; bool _flashflag = true; + + private DengTool tool; + public MainForm() { InitializeComponent(); - _methodQueue = new Queue(); + tool = new DengTool(); + tool.Start(); + FlashThreadTime(); @@ -109,7 +115,7 @@ namespace NDSD_Screwdriver { serialPort = new SerialPortFactory(memorySetting.Com); StartLongRunningTask(); - SendRunningTask(); + } catch (Exception e) { @@ -139,10 +145,8 @@ namespace NDSD_Screwdriver } } - if (sendCancellationTokenSource != null) - { - sendCancellationTokenSource.Cancel(); - } + + tool.Stop(); if (cancellationTokenSource != null) { @@ -456,7 +460,7 @@ namespace NDSD_Screwdriver if (RFIDStatesEntities[i].RFIDState == 1) { // DOperateInfo.DOpen(lightsEntities[i].Yellow); - _methodQueue.Enqueue(new DOperateEntity() + tool.AddMq(new DOperateEntity() { port = lightsEntities[i].Yellow, delay = 0 @@ -465,13 +469,13 @@ namespace NDSD_Screwdriver } else if (RFIDStatesEntities[i].RFIDState == 3) { - _methodQueue.Enqueue(new DOperateEntity() + tool.AddMq(new DOperateEntity() { port = lightsEntities[i].Green, delay = 0 }); - _methodQueue.Enqueue(new DOperateEntity() + tool.AddMq(new DOperateEntity() { port = lightsEntities[i].Yellow, delay = -1 @@ -481,13 +485,13 @@ namespace NDSD_Screwdriver else if (RFIDStatesEntities[i].RFIDState == 4) { - _methodQueue.Enqueue(new DOperateEntity() + tool.AddMq(new DOperateEntity() { port = lightsEntities[i].Red, delay = 0 }); - _methodQueue.Enqueue(new DOperateEntity() + tool.AddMq(new DOperateEntity() { port = lightsEntities[i].Yellow, delay = -1 @@ -605,52 +609,7 @@ namespace NDSD_Screwdriver } - /// - /// RFID刷新任务 - /// - private void SendRunningTask() - { - sendCancellationTokenSource = new CancellationTokenSource(); - Task.Run(() => - { - while (!sendCancellationTokenSource.Token.IsCancellationRequested) - { - - if (!cache.ContainsKey("Test")) - { - - while (_methodQueue.Count > 0) - { - var q = _methodQueue.Dequeue(); - // -1 DClose > 0 DTimeOpen =0 Dopen - - if (q.delay == -1) - { - DOperateInfo.DClose(q.port); - } - - if (q.delay == 0) - { - DOperateInfo.DOpen(q.port); - } - - if (q.delay > 0) - { - DOperateInfo.DTimeOpen(q.port, q.delay); - } - - Thread.Sleep(210); // 模拟一些长时间的工作 - } - } - else - { - _methodQueue.Clear(); - } - - Thread.Sleep(100); // 模拟一些长时间的工作 - } - }, cancellationTokenSource.Token); - } + ///// ///// 等待工作指示灯闪烁进程 @@ -687,7 +646,7 @@ namespace NDSD_Screwdriver if (step == 0) { step = 1; - _methodQueue.Enqueue(new DOperateEntity() + tool.AddMq(new DOperateEntity() { port = lightsEntities[FlashIndex].Yellow, delay = 1 @@ -775,20 +734,20 @@ namespace NDSD_Screwdriver for (int i = 0; i < RFIDStatesEntities.Count; i++) { - _methodQueue.Enqueue(new DOperateEntity() + tool.AddMq(new DOperateEntity() { port = lightsEntities[i].Yellow, delay = -1 }); - _methodQueue.Enqueue(new DOperateEntity() + tool.AddMq(new DOperateEntity() { port = lightsEntities[i].Red, delay = -1 }); - _methodQueue.Enqueue(new DOperateEntity() + tool.AddMq(new DOperateEntity() { port = lightsEntities[i].Green, delay = -1 diff --git a/NDSD-Screwdriver/NDSD_Screwdriver.csproj b/NDSD-Screwdriver/NDSD_Screwdriver.csproj index e0b0bda..2819a31 100644 --- a/NDSD-Screwdriver/NDSD_Screwdriver.csproj +++ b/NDSD-Screwdriver/NDSD_Screwdriver.csproj @@ -122,6 +122,7 @@ + diff --git a/NDSD-Screwdriver/Tool/DengTool.cs b/NDSD-Screwdriver/Tool/DengTool.cs new file mode 100644 index 0000000..999877e --- /dev/null +++ b/NDSD-Screwdriver/Tool/DengTool.cs @@ -0,0 +1,99 @@ +using NDSD_TouchSocket; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace NDSD_Screwdriver.Tool +{ + + public class DengTool + { + private Queue _methodQueue; + + private CancellationTokenSource sendCancellationTokenSource; + + + + /// + /// 输出端口操作 + /// + private DOperate DOperateInfo; + + public DengTool() + { + + DOperateInfo = new DOperate(); + _methodQueue =new Queue(); + + } + + + /// + /// RFID刷新任务 + /// + private void SendRunningTask() + { + sendCancellationTokenSource = new CancellationTokenSource(); + Task.Run(() => + { + while (!sendCancellationTokenSource.Token.IsCancellationRequested) + { + while (_methodQueue.Count > 0) + { + var q = _methodQueue.Dequeue(); + // -1 DClose > 0 DTimeOpen =0 Dopen + + if (q.delay == -1) + { + DOperateInfo.DClose(q.port); + } + + if (q.delay == 0) + { + DOperateInfo.DOpen(q.port); + } + + if (q.delay > 0) + { + DOperateInfo.DTimeOpen(q.port, q.delay); + } + + Thread.Sleep(210); // 模拟一些长时间的工作 + } + + Thread.Sleep(100); // 模拟一些长时间的工作 + } + }, sendCancellationTokenSource.Token); + } + + + + public void AddMq(DOperateEntity entity) + { + _methodQueue.Enqueue(entity); + } + + public void Stop() + { + _methodQueue.Clear(); + sendCancellationTokenSource.Cancel(); + + } + + public void Start() + { + SendRunningTask(); + } + + public void Restart() + { + Stop(); + Thread.Sleep(200); + Start(); + } + } +}