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

diff --git a/Plugin.TestResult/Scanner.xaml.cs b/Plugin.TestResult/Scanner.xaml.cs
new file mode 100644
index 0000000..aeba0b8
--- /dev/null
+++ b/Plugin.TestResult/Scanner.xaml.cs
@@ -0,0 +1,469 @@
+锘縰sing HMI.Core.Manager;
+using HMI.Core.Setting;
+using HMI.Core.Util;
+using HMI.UI.Util;
+using HMI.Util;
+using Panuon.WPF.UI;
+using Plugin.TestResult.Lixun;
+using Plugin.TestResult.Model;
+using Plugin.TestResult.Util;
+using Plugin.TestResult_WPF.Model;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.IO.Ports;
+using System.Windows;
+using System.Windows.Input;
+
+namespace Plugin.TestResult_WPF
+{
+    public class BarcodeEventArg
+    {
+        public string name { get; private set; }
+        public string positionCode { get; private set; }
+        public string snrCode { get; private set; }
+        public CellInfo lastInfo { get; private set; }
+
+        public BarcodeEventArg(string name, string positionCode, string snrCode, CellInfo lastInfo)
+        {
+            this.name = name;
+            this.positionCode = positionCode;
+            this.snrCode = snrCode;
+            this.lastInfo = lastInfo;
+        }
+    }
+
+    /// <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;
+        public event EventHandler<RoutedEventArgs> ComplateClick;
+
+        private static Dictionary<string, CellInfo> snrCache = new Dictionary<string, CellInfo>();
+        SerialPort serialPort;
+
+        private string model;
+        private int columns;
+        private int rows;
+        private int ovenIndex;
+        private CellInfo targetCellInfo;
+        public Scanner(int index, string title, string model, int columns, int rows)
+        {
+            this.ovenIndex = index;
+            this.title = title;
+            this.model = model;
+            this.columns = columns;
+            this.rows = rows;
+            this.DataContext = this;
+            InitializeComponent();
+            Loaded += Scanner_Loaded;
+        }
+
+        private void Scanner_Loaded(object sender, RoutedEventArgs e)
+        {
+            try
+            {
+                InitScanner();
+                Loaded -= Scanner_Loaded;
+            }
+            catch (Exception ex)
+            {
+                ViewControl.Logger.Error("Open Scanner Window Error.", ex);
+                MsgBoxHelper.Error(this, $"{ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 璁剧疆鎵爜鏋�
+        /// </summary>
+        private void InitScanner()
+        {
+            string comStr = "";
+            if (serialPort != null) return;
+
+            string section = this.Tag.ToString();
+            ScannerConfig common = ScannerConfig.ReadFromIni(ViewControl.SannerIniFile, "Config");
+
+            string portName = IniFileHelper.ReadIniData(section, "PortName", "", ViewControl.SannerIniFile);
+            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)
+            {
+                ViewControl.Logger.Error("Init Scan Gun Failed.", 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).Trim();
+
+                //string barcode = 
+                Dispatcher.Invoke(() =>
+                {
+                    if (snrText.IsFocused)
+                    {
+                        snrText.Text = barcode;
+                        Con_Click(null, null);
+                    }
+                    else
+                    {
+                        try
+                        {
+                            var targetCellInfo = ScannerParser.GetCellInfoByCode(this.model, this.columns, this.rows, barcode);
+                            if (!TestResultView.nameTables.ContainsKey(targetCellInfo.OvenName))
+                            {
+                                MsgBoxHelper2.Error($"鑰佸寲鐐� [{targetCellInfo.OvenName}] 涓嶅瓨鍦紝璇锋鏌ラ厤缃�");
+                                return;
+                            }
+                            var sourceTable = TestResultView.tables[this.ovenIndex];
+                            if (sourceTable.title != targetCellInfo.OvenName)
+                            {
+                                MsgBoxHelper2.Error($"璇峰垏鎹㈣嚦 [{targetCellInfo.OvenName}] 鍖烘壂鐮侊紝褰撳墠涓� [{sourceTable.title}] 鍖猴紒");
+                                return;
+                            }
+
+                            var oven = TestResultView.nameTables[targetCellInfo.OvenName];
+                            if (oven.cells.Length < targetCellInfo.Index + 1)
+                            {
+                                MsgBoxHelper2.Error($"绌翠綅鍙� [{barcode}] 瓒呭嚭绱㈠紩 {targetCellInfo.Index}");
+                                return;
+                            }
+
+                            this.targetCellInfo = targetCellInfo;
+                            positionText.Text = barcode;
+                            //positionText.Focus();
+                            //snrText.Text = "";
+                            snrText.Focus();
+                        }
+                        catch (Exception ex)
+                        {
+                            ViewControl.Logger.Error("Parse Position Code Error.", ex);
+                            MsgBoxHelper2.Error($"绌翠綅鍙� [{barcode}] 瑙f瀽澶辫触: {ex.Message}");
+                        }
+                    }
+                });
+            }
+            catch (Exception ex)
+            {
+                ViewControl.Logger.Error("Get Scan Gun Data Failed.", ex);
+                MessageBox.Show($"{this.lang("Tr.GetGunData")}: {ex.StackTrace}");
+            }
+        }
+
+        public static void InsertNewCode(string snr, CellInfo info)
+        {
+            snrCache[snr] = info;
+        }
+
+        public static void RemoveSNR(string snr)
+        {
+            snrCache.Remove(snr);
+        }
+
+        private void SendCode(string tag, string position, string snr)
+        {
+            if (snrCache.ContainsKey(snr))
+            {
+                string oldPosition = snrCache[snr].OriginCode;
+                if (MsgBoxHelper2.WarnChoose($"鏉$爜[{snr}]宸叉斁缃簬[{oldPosition}]涓紝鏄惁娓呴櫎锛�") == MessageBoxResult.OK)
+                {
+                    try
+                    {
+                        FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, null, snrCache[snr]));
+                        snrCache.Remove(snr);
+                        FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, snr, null));
+                    }
+                    catch (Exception ex)
+                    {
+                        ToastHelper.Error(ex.Message);
+                    }
+                }
+            }
+            else
+            {
+                FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, snr, null));
+            }
+        }
+
+        private void Con_Click(object sender, RoutedEventArgs e)
+        {
+            Dispatcher.Invoke(() =>
+            {
+                string position = positionText.Text, snr = snrText.Text;
+                try
+                {
+                    var EnableMes = ParameterManager.GetCommonParameterByName<bool>("EnableMes", false);
+                    if (ParamSetting.CustomerCode == "LIXUN" && EnableMes)
+                    {
+                        var table = TestResultView.nameTables[targetCellInfo.OvenName];
+                        // TestResultView.tables[this.ovenIndex]
+                        string currentPn = table.CbProduct.Text;
+
+                        /// 妫�鏌ユ牸寮�
+                        if (!SNValidator.ValidateSN(currentPn, snr, out string msg))
+                        {
+                            table.GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                            MsgBoxHelper2.Error($"妫�鏌ヤ骇鍝佹潯鐮佸紓甯革細{msg}");
+                            snrText.Text = string.Empty;
+                            snrText.Focus();
+                            return;
+                        }
+
+                        /// 鑾峰彇绔欏悕
+                        string stationName = ProductManager.GetParameterByProductAndName(currentPn, "MES绔欎綅")?.Value;
+                        if (string.IsNullOrEmpty(stationName))
+                        {
+                            table.GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                            MsgBoxHelper2.Error($"妫�鏌ヤ骇鍝佹潯鐮佸紓甯革紝褰撳墠浜у搧[{currentPn}]鏈缃弬鏁癕ES绔欎綅");
+                            snrText.Text = string.Empty;
+                            snrText.Focus();
+                            return;
+                        }
+
+                        /// 妫�鏌N
+                        if (PNUtil.GetPN(ParamSetting.MesAddress, stationName, snr, out string pn, out msg))
+                        {
+
+                            if (pn != currentPn)
+                            {
+                                table.GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                                MsgBoxHelper2.Error($"褰撳墠娴嬭瘯鏂欏彿涓� [{currentPn}]锛屾壂鐮佷骇鍝佹枡鍙蜂负 [{pn}] 涓嶅厑璁告祴璇�");
+                                snrText.Text = string.Empty;
+                                snrText.Focus();
+                                return;
+                            }
+                        }
+                        else
+                        {
+                            table.GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                            MsgBoxHelper2.Error($"妫�鏌ヤ骇鍝佹潯鐮佸紓甯革細{msg}");
+                            snrText.Text = string.Empty;
+                            snrText.Focus();
+                            return;
+                        }
+
+                        /// 妫�鏌ュ伐搴�
+                        if (PNUtil.CheckIn(ViewControl.Logger, ParamSetting.MesAddress, stationName, snr, out bool needCheckIn, out msg))
+                        {
+                            if (needCheckIn != true)
+                            {
+                                table.GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                                MsgBoxHelper2.Error($"鏉$爜[{snr}]宸ュ簭涓嶅湪鏈珯锛歿msg}");
+                                snrText.Text = string.Empty;
+                                snrText.Focus();
+                                return;
+                            }
+                        }
+                        else
+                        {
+                            table.GetSingleCell(targetCellInfo.Index).LightRed(true, out _);
+                            MsgBoxHelper2.Error($"鏉$爜[{snr}]CheckIn澶辫触锛歿msg}");
+                            snrText.Text = string.Empty;
+                            snrText.Focus();
+                            return;
+                        }
+                    }
+                    //var _ = ScannerParser.GetCellInfoByCode(this.model, this.columns, this.rows, snr);
+                    //ToastHelper.Warning(this, "浜у搧鐮佹牸寮忓紓甯�");
+                    //snrText.Text = string.Empty;
+                    //snrText.Focus();
+                    //return;
+                }
+                catch (Exception ex)
+                {
+                    ViewControl.Logger.Error("Scanner Check Error.", ex);
+                    MsgBoxHelper2.Error($"鎵爜妫�鏌ュ紓甯革細{ex.Message}");
+                    return;
+                }
+
+                if (string.IsNullOrEmpty(position) || string.IsNullOrEmpty(snr))
+                {
+                    ToastHelper.Warning(this, this.lang("Tr.PleaseInputSnrs"));
+                    return;
+                }
+                try
+                {
+                    SendCode(Tag.ToString(), position, snr);
+                    positionText.Text = "";
+                    snrText.Text = "";
+                    positionText.Focus();
+                }
+                catch (Exception ex)
+                {
+                    ViewControl.Logger.Error("Con_Click Failed.", ex);
+                    ToastHelper.Warning(this, ex.Message);
+                }
+            });
+        }
+
+        /// <summary>
+        /// 鍘熸潵鐨勫畬鎴愭壂鎻忛�昏緫
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        //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
+        //    {
+        //        SendCode(Tag.ToString(), position, snr);
+        //        this.Close();
+        //    }
+        //    catch (Exception ex)
+        //    {
+        //        ViewControl.Logger.Error("Con_Click Failed.", ex);
+        //        ToastHelper.Warning(this, ex.Message);
+        //    }
+        //}
+
+        private void Ok_Click(object sender, RoutedEventArgs e)
+        {
+            ComplateClick?.Invoke(this, e);
+            this.Close();
+        }
+
+        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.ASCII.GetString(buffer);
+                System.Threading.Thread.Sleep(50);
+            }
+            return barcode;
+        }
+
+        private void Window_Closing(object sender, CancelEventArgs e)
+        {
+            try
+            {
+                targetCellInfo = null;
+                serialPort.Dispose();
+                serialPort = null;
+            }
+            catch (Exception ex)
+            {
+            }
+        }
+
+        private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
+        {
+            if (e.Key == Key.Enter)
+            {
+                //string barcode = 
+                Dispatcher.Invoke(() =>
+                {
+                    if (snrText.IsFocused)
+                    {
+                        Con_Click(null, null);
+                    }
+                    else
+                    {
+                        //if (ParamSetting.CustomerCode != "LIXUN")
+                        //{
+                        //    snrText.Focus();
+                        //    return;
+                        //}
+                        string barcode = positionText.Text;
+                        try
+                        {
+                            targetCellInfo = ScannerParser.GetCellInfoByCode(this.model, this.columns, this.rows, barcode);
+                            if (!TestResultView.nameTables.ContainsKey(targetCellInfo.OvenName))
+                            {
+                                MsgBoxHelper2.Error($"鑰佸寲鐐� [{targetCellInfo.OvenName}] 涓嶅瓨鍦紝璇锋鏌ラ厤缃�");
+                                positionText.Text = string.Empty;
+                                positionText.Focus();
+                                targetCellInfo = null;
+                                return;
+                            }
+
+                            var sourceTable = TestResultView.tables[this.ovenIndex];
+                            if (sourceTable.title != targetCellInfo.OvenName)
+                            {
+                                MsgBoxHelper2.Error($"璇峰垏鎹㈣嚦 [{targetCellInfo.OvenName}] 鍖烘壂鐮侊紝褰撳墠涓� [{sourceTable.title}] 鍖猴紒");
+                                positionText.Text = string.Empty;
+                                positionText.Focus();
+                                targetCellInfo = null;
+                                return;
+                            }
+
+                            var oven = TestResultView.nameTables[targetCellInfo.OvenName];
+                            if (oven.cells.Length < targetCellInfo.Index + 1)
+                            {
+                                MsgBoxHelper2.Error($"绌翠綅鍙� [{barcode}] 瓒呭嚭绱㈠紩 {targetCellInfo.Index}");
+                                positionText.Text = string.Empty;
+                                positionText.Focus();
+                                targetCellInfo = null;
+                                return;
+                            }
+                            snrText.Focus();
+                        }
+                        catch (Exception ex)
+                        {
+                            positionText.Text = string.Empty;
+                            positionText.Focus();
+                            ViewControl.Logger.Error("Parse Position Code Error.", ex);
+                            MsgBoxHelper2.Error($"绌翠綅鍙� [{barcode}] 瑙f瀽澶辫触: {ex.Message}");
+                        }
+                    }
+                });
+            }
+        }
+    }
+}

--
Gitblit v1.9.1