From 006c98e5650b530e5220f279c1e803fefe69d40d Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期二, 25 十一月 2025 15:47:33 +0800
Subject: [PATCH] 优化一些东西;
---
Plugin.TestResult/Scanner.xaml.cs | 206 +++++++++++++++++++++++++++++++++++----------------
1 files changed, 142 insertions(+), 64 deletions(-)
diff --git a/Plugin.TestResult/Scanner.xaml.cs b/Plugin.TestResult/Scanner.xaml.cs
index 7e80db7..d8fa36e 100644
--- a/Plugin.TestResult/Scanner.xaml.cs
+++ b/Plugin.TestResult/Scanner.xaml.cs
@@ -1,19 +1,15 @@
-锘縰sing System;
+锘縰sing HMI.Core.Util;
+using HMI.UI.MDI;
+using HMI.UI.Util;
+using HMI.Util;
+using Panuon.WPF.UI;
+using Plugin.TestResult.Model;
+using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
-using System.Linq;
-using System.Runtime.CompilerServices;
-using System.Text;
-using System.Threading.Tasks;
using System.Windows;
-using System.Windows.Controls;
-using System.Windows.Data;
-using System.Windows.Documents;
using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using System.Windows.Shapes;
namespace Plugin.TestResult_WPF
{
@@ -22,44 +18,37 @@
public string name { get; private set; }
public string positionCode { get; private set; }
public string snrCode { get; private set; }
+ public string oldPosition { get; private set; }
+ public int oldIndex { get; private set; }
- public BarcodeEventArg(string name, string positionCode, string snrCode)
+ public BarcodeEventArg(string name, string positionCode, string snrCode, string oldPosition, int oldIndex)
{
this.name = name;
this.positionCode = positionCode;
this.snrCode = snrCode;
+ this.oldPosition = oldPosition;
+ this.oldIndex = oldIndex;
}
}
/// <summary>
/// Scanner.xaml 鐨勪氦浜掗�昏緫
/// </summary>
- public partial class Scanner : Window, INotifyPropertyChanged
+ public partial class Scanner : WindowX
{
- #region ...
- public event PropertyChangedEventHandler PropertyChanged;
-
- public virtual void RaisePropertyChanged(string name)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
-
- public void RaiseAndSetIfchanged<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
- {
- old = @new;
- if (propertyName != null) RaisePropertyChanged(propertyName);
- }
- #endregion
-
private string _title;
public string title
{
get { return _title; }
- set => RaiseAndSetIfchanged(ref _title, value);
+ set => Set(ref _title, value);
}
public event EventHandler<BarcodeEventArg> FinishBarcode;
+ public event EventHandler<RoutedEventArgs> ComplateClick;
+
+ private static Dictionary<string, string> snrCache = new Dictionary<string, string>();
+ private static Dictionary<string, int> snrIndex = new Dictionary<string, int>();
private DataTable dataTable;
SerialPort serialPort;
@@ -68,7 +57,13 @@
this.title = title;
this.DataContext = this;
InitializeComponent();
+ Loaded += Scanner_Loaded;
+ }
+
+ private void Scanner_Loaded(object sender, RoutedEventArgs e)
+ {
InitScanner();
+ Loaded -= Scanner_Loaded;
}
/// <summary>
@@ -78,26 +73,36 @@
{
string comStr = "";
if (serialPort != null) return;
- if (this.title == "涓�鍖烘壂鐮�")
+
+ string section = this.Tag.ToString();
+ ScannerConfig common = ScannerConfig.ReadFromIni(ViewControl.SannerIniFile, "Config");
+
+ string portName = IniFileHelper.ReadIniData(section, "PortName", "", ViewControl.SannerIniFile);
+ if (string.IsNullOrEmpty(portName))
{
- comStr = "COM1";
+ if (section.StartsWith("A"))
+ {
+ portName = "COM1";
+ }
+ else
+ {
+ portName = "COM2";
+ }
}
- else
- {
- comStr = "COM2";
- }
+ common.PortName = portName;
+
// 閰嶇疆涓插彛
serialPort = new SerialPort
{
- PortName = comStr,
- BaudRate = 115200,
+ PortName = common.PortName,
+ BaudRate = common.BaudRate,
Parity = Parity.None,
- DataBits = 8,
- StopBits = StopBits.One,
+ DataBits = common.DataBits,
+ StopBits = common.StopBits,
Handshake = Handshake.None,
- DtrEnable = true,
- RtsEnable = true
+ DtrEnable = common.DtrEnable,
+ RtsEnable = common.RtsEnable
//ReadTimeout = 500,
//WriteTimeout = 500,
//ReciveBufferSize=4096
@@ -112,7 +117,8 @@
}
catch (Exception ex)
{
- MessageBox.Show($"鍒濆鍖栨壂鐮佹灙澶辫触锛亄ex.StackTrace}");
+ ViewControl.Logger.Error("Init Scan Gun Failed.", ex);
+ MsgBoxHelper.Error(this, $"{this.lang("Tr.InitGunFailed")}: {ex.StackTrace}");
}
}
@@ -142,7 +148,46 @@
}
catch (Exception ex)
{
- MessageBox.Show($"鑾峰彇鎵爜鏋暟鎹け璐ワ紒{ex.StackTrace}");
+ ViewControl.Logger.Error("Get Scan Gun Data Failed.", ex);
+ MessageBox.Show($"{this.lang("Tr.GetGunData")}: {ex.StackTrace}");
+ }
+ }
+
+ public static void InsertNewCode(string snr, string position, int index)
+ {
+ snrCache[snr] = position;
+ snrIndex[snr] = index;
+ }
+
+ public static void RemoveSNR(string snr)
+ {
+ snrCache.Remove(snr);
+ snrIndex.Remove(snr);
+ }
+
+ private void SendCode(string tag, string position, string snr)
+ {
+ if (snrCache.ContainsKey(snr))
+ {
+ var oldPosition = snrCache[snr];
+ if (MsgBoxHelper2.WarnChoose($"鏉$爜[{snr}]宸叉斁缃簬[{oldPosition}]涓紝鏄惁娓呴櫎锛�") == MessageBoxResult.OK)
+ {
+ try
+ {
+ FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, null, oldPosition, snrIndex[snr]));
+ snrCache.Remove(snr);
+ snrIndex.Remove(snr);
+ FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, snr, null, -1));
+ }
+ catch (Exception ex)
+ {
+ ToastHelper.Error(ex.Message);
+ }
+ }
+ }
+ else
+ {
+ FinishBarcode.Invoke(this, new BarcodeEventArg(tag, position, snr, null, -1));
}
}
@@ -153,40 +198,53 @@
string position = positionText.Text, snr = snrText.Text;
if (string.IsNullOrEmpty(position) || string.IsNullOrEmpty(snr))
{
- MessageBox.Show("璇疯緭鍏ュ偍浣嶆潯鐮佸拰浜у搧鏉$爜");
+ ToastHelper.Warning(this, this.lang("Tr.PleaseInputSnrs"));
return;
}
try
{
- FinishBarcode.Invoke(this, new BarcodeEventArg(title, position, snr));
- //positionText.Text = "";
- //snrText.Text = "";
+ SendCode(Tag.ToString(), position, snr);
+ positionText.Text = "";
+ snrText.Text = "";
positionText.Focus();
}
catch (Exception ex)
{
- MessageBox.Show(ex.Message);
+ 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)
{
- string position = positionText.Text, snr = snrText.Text;
- if (string.IsNullOrEmpty(position) || string.IsNullOrEmpty(snr))
- {
- MessageBox.Show("璇疯緭鍏ュ偍浣嶆潯鐮佸拰浜у搧鏉$爜");
- return;
- }
- try
- {
- FinishBarcode.Invoke(this, new BarcodeEventArg(title, position, snr));
- this.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
+ ComplateClick?.Invoke(this, e);
+ this.Close();
}
private string ReadDataFrom(SerialPort sp)
@@ -196,7 +254,7 @@
{
byte[] buffer = new byte[sp.BytesToRead];
sp.Read(buffer, 0, buffer.Length);
- barcode += System.Text.Encoding.UTF8.GetString(buffer);
+ barcode += System.Text.Encoding.ASCII.GetString(buffer);
System.Threading.Thread.Sleep(50);
}
return barcode;
@@ -207,10 +265,30 @@
try
{
serialPort.Close();
+ 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
+ {
+ snrText.Focus();
+ }
+ });
+ }
+ }
}
}
--
Gitblit v1.9.1