From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点

---
 Plugin.TsExecution/ScannerManager.cs |  101 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 101 insertions(+), 0 deletions(-)

diff --git a/Plugin.TsExecution/ScannerManager.cs b/Plugin.TsExecution/ScannerManager.cs
new file mode 100644
index 0000000..00ac8c1
--- /dev/null
+++ b/Plugin.TsExecution/ScannerManager.cs
@@ -0,0 +1,101 @@
+锘縰sing HMI.UI.Entity;
+using System;
+using System.Collections.Generic;
+using System.IO.Ports;
+using System.Windows.Controls;
+
+namespace Plugin.TsExecution
+{
+    public class ScannerManager
+    {
+        public static Dictionary<string, SerialPort> map = new Dictionary<string, SerialPort>();
+        public static Dictionary<SerialPort, Queue<TextBox>> controlMap = new Dictionary<SerialPort, Queue<TextBox>>();
+
+        public static Dictionary<SerialPort, TextBox> focusMap = new Dictionary<SerialPort, TextBox>();
+        public static Dictionary<TextBox, SerialPort> reverseMap = new Dictionary<TextBox, SerialPort>();
+
+
+        public static void RegisterCom(ScannerConfig setting, TextBox tb)
+        {
+            if (!map.ContainsKey(setting.PortName))
+            {
+                // 閰嶇疆涓插彛
+                var serialPort = new SerialPort
+                {
+                    PortName = setting.PortName,
+                    BaudRate = setting.BaudRate,
+                    Parity = Parity.None,
+                    DataBits = setting.DataBits,
+                    StopBits = setting.StopBits,
+                    Handshake = Handshake.None,
+                    DtrEnable = setting.DtrEnable,
+                    RtsEnable = setting.RtsEnable
+                    //ReadTimeout = 500,
+                    //WriteTimeout = 500,
+                    //ReciveBufferSize=4096
+                };
+
+                try
+                {
+                    // 鎵撳紑涓插彛
+                    serialPort.Open();
+                    map[setting.PortName] = serialPort;
+                    controlMap[serialPort] = new Queue<TextBox>();
+                    reverseMap[tb] = serialPort;
+                    tb.GotFocus += Tb_GotFocus;
+                    controlMap[serialPort].Enqueue(tb);
+
+                    // 娉ㄥ唽鎺ユ敹鏁版嵁浜嬩欢
+                    serialPort.DataReceived += SerialPort_DataReceived; ;
+                }
+                catch (Exception ex)
+                {
+
+                }
+            }
+            else { 
+                
+            }
+        }
+
+        private static string ReadDataFrom(SerialPort sp)
+        {
+            string barcode = "";
+            while (sp.BytesToRead > 0)
+            {
+                byte[] buffer = new byte[sp.BytesToRead];
+                sp.Read(buffer, 0, buffer.Length);
+                barcode += System.Text.Encoding.UTF8.GetString(buffer);
+                System.Threading.Thread.Sleep(50);
+            }
+            return barcode;
+        }
+
+        private static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
+        {
+            var sp = sender as SerialPort;
+            string barcode = ReadDataFrom(sp);
+            TextBox tb;
+            if (focusMap.ContainsKey(sp))
+            {
+                tb = focusMap[sp];
+            }
+            else
+            {
+                tb = controlMap[sp].Dequeue();
+            }
+            //string barcode = 
+            tb.Dispatcher.Invoke(() =>
+                {
+                    tb.Text = barcode;
+                });
+        }
+
+        private static void Tb_GotFocus(object sender, System.Windows.RoutedEventArgs e)
+        {
+            var tb = sender as TextBox;
+            SerialPort sp = reverseMap[tb];
+            focusMap[sp] = tb;
+        }
+    }
+}

--
Gitblit v1.9.1