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.StandardTest/Scanner.xaml.cs |  209 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 209 insertions(+), 0 deletions(-)

diff --git a/Plugin.StandardTest/Scanner.xaml.cs b/Plugin.StandardTest/Scanner.xaml.cs
new file mode 100644
index 0000000..b268a0b
--- /dev/null
+++ b/Plugin.StandardTest/Scanner.xaml.cs
@@ -0,0 +1,209 @@
+锘縰sing HMI.Core.Util;
+using HMI.UI.Entity;
+using HMI.UI.Util;
+using HMI.Util;
+using Panuon.WPF.UI;
+using System;
+using System.ComponentModel;
+using System.IO.Ports;
+using System.Windows;
+
+namespace Plugin.StandardTest
+{
+    public class BarcodeEventArg
+    {
+        public string name { get; private set; }
+        public string positionCode { get; private set; }
+        public string snrCode { get; private set; }
+
+        public BarcodeEventArg(string name, string positionCode, string snrCode)
+        {
+            this.name = name;
+            this.positionCode = positionCode;
+            this.snrCode = snrCode;
+        }
+    }
+
+    /// <summary>
+    /// Scanner.xaml 鐨勪氦浜掗�昏緫
+    /// </summary>
+    public partial class Scanner : WindowX
+    {
+        private string _title;
+
+        public string title
+        {
+            get { return _title; }
+            set => Set(ref _title, value);
+        }
+
+        public event EventHandler<BarcodeEventArg> FinishBarcode;
+        SerialPort serialPort;
+        public Scanner(string title)
+        {
+            this.title = title;
+            this.DataContext = this;
+            InitializeComponent();
+            Loaded += Scanner_Loaded;
+        }
+
+        private void Scanner_Loaded(object sender, RoutedEventArgs e)
+        {
+            InitScanner();
+            Loaded -= Scanner_Loaded;
+        }
+
+        /// <summary>
+        /// 璁剧疆鎵爜鏋�
+        /// </summary>
+        private void InitScanner()
+        {
+            string comStr = "";
+            if (serialPort != null) return;
+
+            string section = this.Tag.ToString();
+            ScannerConfig common = ScannerConfig.ReadFromIni("", "Config");
+
+            string portName = IniFileHelper.ReadIniData(section, "PortName", "","");
+            if (string.IsNullOrEmpty(portName))
+            {
+                if (section.StartsWith("A"))
+                {
+                    portName = "COM1";
+                }
+                else
+                {
+                    portName = "COM2";
+                }
+            }
+            common.PortName = portName;
+
+
+            // 閰嶇疆涓插彛
+            serialPort = new SerialPort
+            {
+                PortName = common.PortName,
+                BaudRate = common.BaudRate,
+                Parity = Parity.None,
+                DataBits = common.DataBits,
+                StopBits = common.StopBits,
+                Handshake = Handshake.None,
+                DtrEnable = common.DtrEnable,
+                RtsEnable = common.RtsEnable
+                //ReadTimeout = 500,
+                //WriteTimeout = 500,
+                //ReciveBufferSize=4096
+            };
+
+            try
+            {
+                // 鎵撳紑涓插彛
+                serialPort.Open();
+                // 娉ㄥ唽鎺ユ敹鏁版嵁浜嬩欢
+                serialPort.DataReceived += SerialPort_DataReceived;
+            }
+            catch (Exception ex)
+            {
+                MsgBoxHelper.Error(this, $"{this.lang("Tr.InitGunFailed")}: {ex.StackTrace}");
+            }
+        }
+
+        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
+        {
+            try
+            {
+                SerialPort sp = (SerialPort)sender;
+                string barcode = ReadDataFrom(sp);
+
+                //string barcode = 
+                Dispatcher.Invoke(() =>
+                {
+                    if (snrText.IsFocused)
+                    {
+                        snrText.Text = barcode;
+                        Con_Click(null, null);
+                    }
+                    else
+                    {
+                        positionText.Text = barcode;
+                        //positionText.Focus();
+                        //snrText.Text = "";
+                        snrText.Focus();
+                    }
+                });
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show($"{this.lang("Tr.GetGunData")}: {ex.StackTrace}");
+            }
+        }
+
+        private void Con_Click(object sender, RoutedEventArgs e)
+        {
+            Dispatcher.Invoke(() =>
+            {
+                string position = positionText.Text, snr = snrText.Text;
+                if (string.IsNullOrEmpty(position) || string.IsNullOrEmpty(snr))
+                {
+                    ToastHelper.Warning(this, this.lang("Tr.PleaseInputSnrs"));
+                    return;
+                }
+                try
+                {
+                    FinishBarcode.Invoke(this, new BarcodeEventArg(Tag.ToString(), position, snr));
+                    //positionText.Text = "";
+                    //snrText.Text = "";
+                    positionText.Focus();
+                }
+                catch (Exception ex)
+                {
+                    ToastHelper.Warning(this, ex.Message);
+                }
+            });
+        }
+
+        private void Ok_Click(object sender, RoutedEventArgs e)
+        {
+            string position = positionText.Text, snr = snrText.Text;
+            if (string.IsNullOrEmpty(position) || string.IsNullOrEmpty(snr))
+            {
+                ToastHelper.Warning(this, this.lang("Tr.PleaseInputSnrs"));
+                return;
+            }
+            try
+            {
+                FinishBarcode.Invoke(this, new BarcodeEventArg(Tag.ToString(), position, snr));
+                this.Close();
+            }
+            catch (Exception ex)
+            {
+                ToastHelper.Warning(this, ex.Message);
+            }
+        }
+
+        private 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 void Window_Closing(object sender, CancelEventArgs e)
+        {
+            try
+            {
+                serialPort.Close();
+                serialPort = null;
+            }
+            catch (Exception ex)
+            {
+            }
+        }
+    }
+}

--
Gitblit v1.9.1