| | |
| | | using 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.Windows; |
| | | using System.Windows.Input; |
| | | |
| | | namespace Plugin.TestResult_WPF |
| | | { |
| | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | 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; |
| | |
| | | { |
| | | string comStr = ""; |
| | | if (serialPort != null) return; |
| | | if (this.Tag == "Zone1") |
| | | |
| | | 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 |
| | |
| | | } |
| | | } |
| | | |
| | | 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)); |
| | | } |
| | | } |
| | | |
| | | private void Con_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | Dispatcher.Invoke(() => |
| | |
| | | } |
| | | try |
| | | { |
| | | FinishBarcode.Invoke(this, new BarcodeEventArg(Tag.ToString(), position, snr)); |
| | | //positionText.Text = ""; |
| | | //snrText.Text = ""; |
| | | SendCode(Tag.ToString(), position, snr); |
| | | positionText.Text = ""; |
| | | snrText.Text = ""; |
| | | positionText.Focus(); |
| | | } |
| | | catch (Exception ex) |
| | |
| | | }); |
| | | } |
| | | |
| | | /// <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)) |
| | | { |
| | | 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); |
| | | } |
| | | ComplateClick?.Invoke(this, e); |
| | | this.Close(); |
| | | } |
| | | |
| | | private string ReadDataFrom(SerialPort sp) |
| | |
| | | { |
| | | 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; |
| | |
| | | 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(); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | } |