using HMI.Core.Util; using HMI.UI.Util; using Panuon.WPF.UI; using System; using System.ComponentModel; using System.IO.Ports; using System.Windows; 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 BarcodeEventArg(string name, string positionCode, string snrCode) { this.name = name; this.positionCode = positionCode; this.snrCode = snrCode; } } /// /// Scanner.xaml 的交互逻辑 /// public partial class Scanner : WindowX { private string _title; public string title { get { return _title; } set => Set(ref _title, value); } public event EventHandler FinishBarcode; private DataTable dataTable; 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; } /// /// 设置扫码枪 /// private void InitScanner() { string comStr = ""; if (serialPort != null) return; if (this.Tag == "Zone1") { comStr = "COM1"; } else { comStr = "COM2"; } // 配置串口 serialPort = new SerialPort { PortName = comStr, BaudRate = 115200, Parity = Parity.None, DataBits = 8, StopBits = StopBits.One, Handshake = Handshake.None, DtrEnable = true, RtsEnable = true //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); //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) { ViewControl.Logger.Error("Get Scan Gun Data Failed.", 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) { 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)) { ToastHelper.Warning(this, this.lang("Tr.PleaseInputSnrs")); return; } try { FinishBarcode.Invoke(this, new BarcodeEventArg(Tag.ToString(), position, snr)); this.Close(); } catch (Exception ex) { ViewControl.Logger.Error("Con_Click Failed.", 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(); } catch (Exception ex) { } } } }