From dd2a0f3d7b8542873ee2f286c1c7d376eb7fc018 Mon Sep 17 00:00:00 2001 From: jasonchenwork Date: Tue, 24 Jun 2025 10:57:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=81=99=E6=8E=A7=E5=99=A8?= =?UTF-8?q?=E8=A8=8A=E8=99=9F=E5=BA=8F=E5=88=97(=E5=A4=9A=E7=B7=92?= =?UTF-8?q?=E6=8E=92=E7=A8=8B)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SerialPortManager.cs | 79 +++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/SerialPortManager.cs b/SerialPortManager.cs index 36f1cd1..bf2aabe 100644 --- a/SerialPortManager.cs +++ b/SerialPortManager.cs @@ -1,5 +1,6 @@ using System.IO.Ports; using System.Text; +using System.Collections.Concurrent; namespace DualScreenDemo { @@ -48,18 +49,26 @@ namespace DualScreenDemo // 綁定資料接收事件 mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); + AppDomain.CurrentDomain.ProcessExit += (s, e) => + { + cts.Cancel(); + }; try - { - mySerialPort.Open(); - Console.WriteLine($"{selectedPort} 串列埠已成功開啟。"); - } - catch (Exception ex) - { - MessageBox.Show($"開啟 {selectedPort} 串列埠時發生錯誤: {ex.Message}"); - } + { + mySerialPort.Open(); + Console.WriteLine($"{selectedPort} 串列埠已成功開啟。"); + } + catch (Exception ex) + { + MessageBox.Show($"開啟 {selectedPort} 串列埠時發生錯誤: {ex.Message}"); + } } + + private readonly ConcurrentQueue commandQueue = new(); + public readonly CancellationTokenSource cts = new(); + private bool isProcessing = false; private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { @@ -68,19 +77,14 @@ namespace DualScreenDemo SerialPort sp = (SerialPort)sender; if (!sp.IsOpen) - { - // Console.WriteLine("串列埠未開啟,無法接收資料。"); return; - } int bytesToRead = sp.BytesToRead; if (bytesToRead > 0) { - byte[] buffer = new byte[bytesToRead]; int bytesRead = sp.Read(buffer, 0, bytesToRead); - StringBuilder hexData = new StringBuilder(bytesRead * 2); for (int i = 0; i < bytesRead; i++) { @@ -88,31 +92,46 @@ namespace DualScreenDemo } string indata = hexData.ToString(); - // Console.WriteLine($"接收到的資料 (Hex): {indata}"); - - Task.Run(() => + Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] 遙控器: {indata}"); + + // 將資料放入佇列 + commandQueue.Enqueue(indata); + + // 如果尚未啟動處理迴圈,則啟動 + if (!isProcessing) { - try - { - commandHandler.ProcessData(indata); - } - catch (Exception processEx) - { - Console.WriteLine($"處理資料時發生錯誤: {processEx.Message}"); - } - }); - } - else - { - // Console.WriteLine("未接收到任何資料。"); + isProcessing = true; + Task.Run(ProcessQueueAsync); + } } } catch (Exception ex) { - Console.WriteLine($"接收資料時發生錯誤: {ex.Message}"); + Console.WriteLine($"接收資料時發生錯誤: {ex.Message}"); } } + private async Task ProcessQueueAsync() + { + while (!cts.Token.IsCancellationRequested) + { + while (commandQueue.TryDequeue(out var cmd)) + { + try + { + commandHandler.ProcessData(cmd); + } + catch (Exception processEx) + { + Console.WriteLine($"處理資料時發生錯誤: {processEx.Message}"); + } + } + + await Task.Delay(10); // 避免 CPU 空轉 + } + cts.Cancel(); + isProcessing = false; + }