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
|
{
|
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 : Window, INotifyPropertyChanged
|
{
|
#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);
|
}
|
|
public event EventHandler<BarcodeEventArg> FinishBarcode;
|
|
private DataTable dataTable;
|
SerialPort serialPort;
|
public Scanner(string title)
|
{
|
this.title = title;
|
this.DataContext = this;
|
InitializeComponent();
|
InitScanner();
|
}
|
|
/// <summary>
|
/// 设置扫码枪
|
/// </summary>
|
private void InitScanner()
|
{
|
string comStr = "";
|
if (serialPort != null) return;
|
if (this.title == "一区扫码")
|
{
|
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)
|
{
|
MessageBox.Show($"初始化扫码枪失败!{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($"获取扫码枪数据失败!{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))
|
{
|
MessageBox.Show("请输入储位条码和产品条码");
|
return;
|
}
|
try
|
{
|
FinishBarcode.Invoke(this, new BarcodeEventArg(title, position, snr));
|
//positionText.Text = "";
|
//snrText.Text = "";
|
positionText.Focus();
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show(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);
|
}
|
}
|
|
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)
|
{
|
}
|
}
|
}
|
}
|