/// <summary> /// 扫描事件 /// </summary> /// <param name="obj"></param> void scanner_OnScannerReaderEvent(object obj, BarcodeReadEventArgs args)//扫描事件响应函数,返回扫描到条码相关信息 { try { if (this.InvokeRequired) { Action <object, BarcodeReadEventArgs> fun = new Action <object, BarcodeReadEventArgs>(scanner_OnScannerReaderEvent); this.Invoke(fun, new object[] { obj, args }); } else { if (string.IsNullOrEmpty(args.strDataBuffer) || args.strDataBuffer.Length == 0) { this.lblTip.Text = "扫描的条码错误"; Audio.SoundTip(0);//错误提示音 return; } SymbologyOptions.SymbologyType t = (SymbologyOptions.SymbologyType)args.SymbologyDetail; ScanOptFun(args.strDataBuffer, t);//扫描的执行方法 } } catch (Exception ex) { this.lblTip.Text = "扫描失败"; CLog.WriteErrLog("[Frmbatch.ScanEvent]" + ex.Message); } }
/// <summary> /// 扫描事件 /// </summary> /// <param name="obj"></param> void scanner_OnScannerReaderEvent(object obj, BarcodeReadEventArgs args)//扫描事件响应函数,返回扫描到条码相关信息 { try { if (this.InvokeRequired) { Action <object, BarcodeReadEventArgs> fun = new Action <object, BarcodeReadEventArgs>(scanner_OnScannerReaderEvent); this.Invoke(fun, new object[] { obj, args }); } else { this.lblTip.Text = ""; if (Opt.FeatureCodeList == null || Opt.FeatureCodeList.Count < 1) { this.lblTip.Text = "加载数据失败"; Audio.SoundTip(0);//错误提示音 return; } string barcode = ""; if (string.IsNullOrEmpty(args.strDataBuffer) || args.strDataBuffer.Length == 0) { this.lblTip.Text = "请重新扫描"; Audio.SoundTip(0);//错误提示音 return; } barcode = args.strDataBuffer; if (IsHjScan) { //合件扫描 this.ValidateHj(barcode); } else { //零件扫描 SymbologyOptions.SymbologyType t = (SymbologyOptions.SymbologyType)args.SymbologyDetail; ScanValidate(barcode, t); } } } catch (Exception ex) { CLog.WriteErrLog(ex.Message); } }
/// <summary> /// 扫描验证 /// </summary> /// <param name="barcode"></param> private void ScanValidate(string barcode, SymbologyOptions.SymbologyType t) { try { barcode = barcode.Replace("<cr><lf>", "").Replace("\r\n", "").Replace("]C1", ""); //获取条码/二维码,过滤回车换行 bool IsExist = false; //标记是否存在 foreach (ListViewItem item in this.lvMaterial.Items) { string tmpBarcode = barcode; string ItemCode = item.Text.ToString(); //当前的零件编码 string FeatureCode = item.SubItems[3].Text; //特征码 string FeatureIndex = item.SubItems[4].Text; //特征位 string TraceType = item.SubItems[4].Text; //追溯类型 bool flag = false; if (FeatureCode == null || FeatureCode.Trim() == "") { if (tmpBarcode.Length < ItemCode.Length) { continue; } string tmpCode = ""; if ((t == SymbologyOptions.SymbologyType.QR_Code || t == SymbologyOptions.SymbologyType.DataMatrix || t == SymbologyOptions.SymbologyType.Maxicode || t == SymbologyOptions.SymbologyType.PDF417 || t == SymbologyOptions.SymbologyType.Aztec) && barcode.Trim().Length >= 38) //if (t == SymbologyOptions.SymbologyType.QR_Code && barcode.Trim().Length >= 38) //if (tmpBarcode.Length >= 38) { tmpCode = tmpBarcode.Substring(0, 10); string supplier = tmpBarcode.Substring(15, 10); //供应商代码 string bno = tmpBarcode.Substring(25, 13); //批次号 tmpBarcode = bno + ";" + supplier; } //else //{ // tmpCode = tmpBarcode.Substring(0, ItemCode.Length); //} //flag = tmpCode == ItemCode;//判断特征码 } else { if (tmpBarcode.Length < FeatureCode.Length) { continue; } flag = Opt.ValidateFeatureCode(tmpBarcode, FeatureIndex, FeatureCode);//判断特征码 } if (flag) { materialCode = ItemCode; item.ForeColor = Color.Green; //当防错正常时字体为绿色 item.SubItems[2].Text = tmpBarcode; //扫描条码 this.lblMaterialCode.Text = ItemCode; this.lblBarCode.Text = tmpBarcode; item.Selected = true; RecordScanInfo(); //记录扫描信息 this.lblTip.Text = "扫描零件条码或零件批次条码"; Audio.SoundTip(1); //扫描提示音 IsExist = true; break; } else { string code = tmpBarcode.Substring(1); bool IsOk = false; switch (scantype) { case ScanType.MATERIALCODE: if (ItemCode.Equals(code)) { this.lblMaterialCode.Text = code; //扫描的条码 this.lblBarCode.Text = ""; materialCode = ItemCode; item.Selected = true; //选中扫描的材料 listViewItemIndex = item.Index; Audio.SoundTip(1); //扫描提示音 scantype = ScanType.BARCODE; //修改扫描类型为批次条码 this.lblTip.Text = "扫描零件批次条码"; IsExist = true; IsOk = true; } break; case ScanType.BARCODE: this.lblBarCode.Text = tmpBarcode; //扫描的条码 this.lvMaterial.Items[listViewItemIndex].ForeColor = Color.Green; //当防错正常时字体为绿色 this.lvMaterial.Items[listViewItemIndex].SubItems[2].Text = tmpBarcode; //扫描条码 scantype = ScanType.MATERIALCODE; RecordScanInfo(); //记录扫描信息 this.lblTip.Text = "扫描零件条码或零件批次条码"; Audio.SoundTip(1); //扫描提示音 IsExist = true; IsOk = true; break; default: break; } if (IsOk) { break; } } } if (!IsExist && scantype != ScanType.BARCODE)//不存在的情况 { this.lblTip.Text = "扫描零件编码不在此产品之列"; Audio.SoundTip(0);//错误提示音 } } catch (Exception ex) { CLog.WriteErrLog(ex.Message); } }
/// <summary> /// 扫描执行操作的方法 /// </summary> /// <param name="obj"></param> private void ScanOptFun(string barcode, SymbologyOptions.SymbologyType t) { try { if (!IsEdit) { #region 扫描录入 this.lblTip.Text = ""; switch (scantype) { case ScanType.MATERIALCODE: //材料编码扫描 { #region //材料编码扫描 this.cmbMaterialCode.SelectedIndex = -1; this.lblMaterialName.Text = ""; this.tbMaterialBatchNum.Text = ""; this.tbMaterialBatchNo.Text = ""; this.tbSupplier.Text = ""; this.tbBarCode.Text = ""; string pcode = ""; //判断是否为二维码 #region // 二维码 if ((t == SymbologyOptions.SymbologyType.QR_Code || t == SymbologyOptions.SymbologyType.DataMatrix || t == SymbologyOptions.SymbologyType.Maxicode || t == SymbologyOptions.SymbologyType.PDF417 || t == SymbologyOptions.SymbologyType.Aztec) && barcode.Trim().Length >= 38) { pcode = barcode.Substring(0, 10); if (MaterialInfoList.ContainsKey(pcode)) { //能够匹配 Audio.SoundTip(1); //扫描提示音 string bnumstr = barcode.Substring(10, 5); //包装量 string supplier = barcode.Substring(15, 10); //供应商代码 string bno = barcode.Substring(25, 13); //批次号 BatchNoMDL model = MaterialInfoList[pcode] as BatchNoMDL; int batchnum = model.BatchNum; try { int bnum = int.Parse(bnumstr); batchnum = bnum; } catch (Exception) { } this.cmbMaterialCode.Text = model.MaterialCode; this.lblMaterialName.Text = model.MaterialName; this.tbMaterialBatchNum.Text = batchnum.ToString(); this.tbSupplier.Text = supplier; this.tbMaterialBatchNo.Text = bno; IsOK = false; scantype = ScanType.HJBARCODE; this.tbBarCode.Focus(); this.lblTip.Text = "请扫描合件条码"; } else { //不能够匹配 Audio.SoundTip(0); //错误提示音 scantype = ScanType.MATERIALCODE; this.lblTip.Text = "扫描的条码的零件不符当前产品"; this.tbMaterialBatchNo.Text = barcode; IsOK = true; } return; } #endregion string code = barcode.Substring(1); this.cmbMaterialCode.Text = code; //扫描的条码 if (MaterialInfoList.ContainsKey(code)) { Audio.SoundTip(1); //扫描提示音 scantype = ScanType.PacketNumber; //修改扫描类型为供应商条码 BatchNoMDL model = MaterialInfoList[code] as BatchNoMDL; this.cmbMaterialCode.Text = model.MaterialCode; this.lblMaterialName.Text = model.MaterialName; //this.tbMaterialBatchNum.Text = model.BatchNum.ToString(); this.tbMaterialBatchNum.Focus(); this.lblTip.Text = "请扫描包装数量"; //this.lblTip.Text = "请扫描供应商编码"; } else { Audio.SoundTip(0); //错误提示音 scantype = ScanType.MATERIALCODE; this.lblTip.Text = "扫描的条码的零件不符当前产品"; } #endregion } break; case ScanType.Supplier: //供应商编码 { Audio.SoundTip(1); //扫描提示音 this.tbSupplier.Text = barcode; scantype = ScanType.PacketNumber; this.tbMaterialBatchNum.Focus(); this.lblTip.Text = "请扫描包装数量"; } break; case ScanType.PacketNumber: //包装数量 { this.tbMaterialBatchNum.Text = barcode.ToUpper().Replace("Q", ""); int num = 0; try { num = int.Parse(this.tbMaterialBatchNum.Text); } catch (Exception) { num = 0; scantype = ScanType.PacketNumber; } if (num == 0) { this.lblTip.Text = "扫描包装数量不是数字或=0"; return; } scantype = ScanType.BARCODE; this.tbMaterialBatchNo.Focus(); this.lblTip.Text = "请扫描批次条码"; } break; case ScanType.BARCODE: //批次条码 { string code = barcode.Substring(1); if (barcode.Substring(0, 1).Equals("P") && code == this.cmbMaterialCode.Text) { Audio.SoundTip(0); //错误提示音 this.lblTip.Text = "重复扫描零件条码"; scantype = ScanType.BARCODE; } else { Audio.SoundTip(1); //扫描提示音 this.tbMaterialBatchNo.Text = barcode; //扫描的条码 this.lblTip.Text = "请扫描合件条码"; scantype = ScanType.HJBARCODE; this.tbBarCode.Focus(); } } break; case ScanType.HJBARCODE: //合件条码扫描 { this.tbBarCode.Text = barcode; #region //添加合件验证 if (Opt.ValidatePartCode(this.cmbMaterialCode.Text, barcode)) { Audio.SoundTip(1); scantype = ScanType.MATERIALCODE; this.lblTip.Text = "更新信息或者是重新录入"; return; } #endregion Audio.SoundTip(0); scantype = ScanType.HJBARCODE; this.lblTip.Text = "扫描的合件不匹配,请重新录入"; } break; } #endregion } else { #region 手动录入 if (this.MaterialInfoList != null && this.cmbMaterialCode.SelectedIndex > 0) { //判断是否为二维码 if (barcode.Trim().Length >= 38) { var pcode = barcode.Substring(0, 10); if (MaterialInfoList.ContainsKey(pcode)) { //能够匹配 Audio.SoundTip(1);//扫描提示音 SetQrCode(barcode); } return; } if (this.tbBarCode.Focused) { this.tbBarCode.Text = barcode; Audio.SoundTip(1); } else if (this.tbSupplier.Focused) { this.tbSupplier.Text = barcode; Audio.SoundTip(1); this.tbMaterialBatchNum.Focus(); } else if (this.tbMaterialBatchNum.Focused) { this.tbMaterialBatchNum.Text = barcode.ToUpper().Replace("Q", ""); int num = 0; try { num = int.Parse(this.tbMaterialBatchNum.Text); } catch (Exception) { num = 0; } if (num == 0) { this.lblTip.Text = "扫描包装数量不是数字或=0"; Audio.SoundTip(0); return; } Audio.SoundTip(1); this.tbMaterialBatchNo.Focus(); } else if (this.tbMaterialBatchNo.Focused) { this.tbMaterialBatchNo.Text = barcode; Audio.SoundTip(1); this.tbBarCode.Focus(); } else { return; } int ret = 0; if (string.IsNullOrEmpty(this.tbBarCode.Text)) { ret += 1; this.lblTip.Text = "请录入合件条码"; this.tbBarCode.Focus(); } if (string.IsNullOrEmpty(this.tbMaterialBatchNo.Text)) { ret += 1; this.lblTip.Text = "请录入批次流水号"; this.tbMaterialBatchNo.Focus(); } if (string.IsNullOrEmpty(this.tbMaterialBatchNum.Text)) { ret += 1; this.lblTip.Text = "请录入包装数量"; this.tbMaterialBatchNum.Focus(); } //if (string.IsNullOrEmpty(this.tbSupplier.Text)) //{ // ret += 1; // this.lblTip.Text = "请录入供应商编码"; // this.tbSupplier.Focus(); //} if (ret == 0) { this.lblTip.Text = "更新信息或者是重新录入"; } } else { Audio.SoundTip(0);//错误提示音 this.lblTip.Text = "请选择零件编码"; this.cmbMaterialCode.Focus(); } #endregion } } catch (Exception ex) { CLog.WriteErrLog(ex.Message); } }