扫码枪二维码一般格式解析Demo
1、背景,测试Demo如下图所示
1.1、单击网格,文本框获取选中的二维码;
1.2、点击【OK】按钮根据条件解析字符串,呈现于列表框中;
2、测试数据类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Threading.Tasks; namespace Test.Barcode { #region Model public class Sample { public Sample() { } private string strSN = string.Empty; private string strPKGID = string.Empty; private string strBarcode = string.Empty; public string SN { get { return this.strSN; } set { this.strSN = value; } } public string PKGID { get { return this.strPKGID; } set { this.strPKGID = value; } } public string Barcode { get { return this.strBarcode; } set { this.strBarcode = value; } } } #endregion public class TestData { /// <summary> /// 获取测试数据表信息 /// </summary> /// <returns></returns> public static DataTable GetData() { DataTable dt = new DataTable(); dt.Columns.Add("SN"); dt.Columns.Add("PKGID"); dt.Columns.Add("Barcode"); DataRow dr = dt.NewRow(); dr[0] = "21136267"; dr[1] = "LA1011321948AB9412"; dr[2]= "[)>06F01001P52SLA1011321948AB941218VLEHWTF02010I1P211362672PDKHW20747420-141V10113210D1948(140)1T1948Q140"; dt.Rows.Add(dr); DataRow dr2 = dt.NewRow(); dr2[0] = "21138120-009"; dr2[1] = "LA1011321948AB9409"; dr2[2]= "[)>06F01001P52SLA1011321948AB940918VLEHWTF02010I1P21138120-0092PBKHW20708834-131V10113210D1948(40)1T1948Q40"; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow(); dr3[0] = "21241207"; dr3[1] = "LA1011321947AE1218"; dr3[2]= "[)>06F01001P52SLA1011321947AE121818VLEHWTF02010I1P212412072PBKHW20709231-121V10113210D1947(210)1Q8.641T1947Q210"; dt.Rows.Add(dr3); return dt; } public static List<Sample> GetList() { List<Sample> samples =new List<Sample> { new Sample {SN="21136267",PKGID="LA1011321948AB9412",Barcode="[)>06F01001P52SLA1011321948AB941218VLEHWTF02010I1P211362672PDKHW20747420-141V10113210D1948(140)1T1948Q140"}, new Sample{SN="21138120-009",PKGID="LA1011321948AB9409",Barcode="[)>06F01001P52SLA1011321948AB940918VLEHWTF02010I1P21138120-0092PBKHW20708834-131V10113210D1948(40)1T1948Q40"}, new Sample{SN="21241207",PKGID="LA1011321947AE1218",Barcode="[)>06F01001P52SLA1011321947AE121818VLEHWTF02010I1P212412072PBKHW20709231-121V10113210D1947(210)1Q8.641T1947Q210"} }; return samples; } } }
3、窗体实现
/* 要响应GridView的单击或者双击事件,要设置GridView的OptionsBehavior.Editable=false。 如果为true,它是不会响应这这两个事件的。 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Test.Barcode { public partial class FrmTest : Form { bool bo = false; public FrmTest() { InitializeComponent(); #region Timer timer = new Timer(); timer.Interval = 1000; timer.Tick += new EventHandler(RealTimer); timer.Start(); #endregion } void RealTimer(object sender,EventArgs e) { barHeaderItem1.Caption = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } private void FrmTest_FormClosing(object sender, FormClosingEventArgs e) { if (bo) { Application.Exit(); return; } DialogResult dialog = DevExpress.XtraEditors.XtraMessageBox.Show("Are you sure exit the system?", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dialog == DialogResult.OK) { try { bo = true; } catch (Exception) { } finally { Application.Exit(); } } else { e.Cancel = true; } } private void FrmTest_Load(object sender, EventArgs e) { //gridControl1.DataSource = TestData.GetData(); gridControl1.DataSource = TestData.GetList(); gridView1.Columns[0].Width = 100; gridView1.Columns[1].Width = 150; gridView1.Columns[2].Width = 360; gridView1.Columns[0].Caption = "编号"; gridView1.Columns[1].Caption = "箱号"; gridView1.Columns[2].Caption = "二维码"; } private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { // memoStr.Text = GetSelect("Barcode"); string aa = gridView1.GetFocusedRowCellValue("Barcode").ToString(); memoStr.Text = aa; memoStr.Focus(); } /// <summary> /// 获取单元格值 /// </summary> /// <param name="filedName">filedName为要获取列的列名</param> /// <returns></returns> public string GetSelect(string filedName) { int[] pRows = this.gridView1.GetSelectedRows();//传递实体类过去 获取选中的行 if (pRows.GetLength(0) > 0) return gridView1.GetRowCellValue(pRows[0], filedName).ToString(); else return null; } /// <summary> /// asciiCode转换 /// </summary> /// <param name="asciiCode"></param> /// <returns></returns> public static char[] Chr(int asciiCode) { if (asciiCode >= 0 && asciiCode <= 255) { System.Text.ASCIIEncoding ascii = new ASCIIEncoding(); byte[] byteArray = new byte[] { (byte)asciiCode }; char[] aa = ascii.GetChars(byteArray); return aa; } else { throw new Exception("ASCII Code is not valid."); } } private void btnOk_Click(object sender, EventArgs e) { lstItem.Items.Clear(); MaterialBarcode mbarcode = new MaterialBarcode(memoStr.Text); #region string s1 = mbarcode.Barcode; string s2 = mbarcode.BoxName; string s3 = mbarcode.ArrayItemCode; string s4 = mbarcode.ArrayQty; string s5 = mbarcode.VerSion; string s6 = mbarcode.SN; string s7 = mbarcode.MAC; string s8 = mbarcode.GPON; string s9 = mbarcode.ClientItemCode; string s10 = mbarcode.CLEI; string s11 = mbarcode.Model; //string s12 = mbarcode.SupplyCode; string s13 = mbarcode.CompanyCode; string s14 = mbarcode.ClientPO; string s15 = mbarcode.BatchNumber; string s16 = mbarcode.GW; #endregion char[] c = Chr(29); string[] myArr = s1.Split(c); if (string.IsNullOrEmpty(s5)) { MessageBox.Show("The version is not allow empty!"); return; } if (string.IsNullOrEmpty(s3)) { MessageBox.Show("The PN is not allow empty!"); return; } if (string.IsNullOrEmpty(s15)) { MessageBox.Show("The Lot No is not allow empty!"); return; } for (int i = 0; i < myArr.Length; i++) { if (!myArr[0].StartsWith("[)>")) { MessageBox.Show("The type of string is wrong!"); return; } if (i > 2 && myArr[2].Length != 21 && myArr[2].StartsWith("52S")) { if (myArr[2].Length > 21) { MessageBox.Show("The length more than 18!"); return; } else { MessageBox.Show("The length less than 18!"); return; } } if (i > 5 && (!myArr[5].StartsWith("1P"))) { MessageBox.Show("The number of matirial is not allow empty!"); return; } lstItem.Items.Add(myArr[i]); } } private void memoStr_KeyDown(object sender, KeyEventArgs e) { if (e.KeyValue == 13) { btnOk_Click(sender, e); } } private void gridControl1_Click(object sender, EventArgs e) { //No.1 //memoStr.Text = GetSelect("Barcode"); //No.2 //string aa = gridView1.GetFocusedRowCellValue("Barcode").ToString(); //memoStr.Text = aa; //memoStr.Focus(); } } }
4、 结构体
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Threading.Tasks; namespace Test.Barcode { [StructLayout(LayoutKind.Sequential)] public struct MaterialBarcode { private string barcode; private string strBoxName; private string strArrayItemCode; private string strArrayQty; private string strVerSion; private string strSN; private string strMAC; private string strGPON; private string strClientItemCode; private string strCLEI; private string strModel; private string strSupplyCode; private string strCompanyCode; private string strClientPO; private string strBatchNumber; private string strGW; public MaterialBarcode(string barcode) { this.barcode = barcode; this.strBoxName = string.Empty; this.strArrayItemCode = string.Empty; this.strArrayQty = string.Empty; this.strVerSion = string.Empty; this.strSN = string.Empty; this.strMAC = string.Empty; this.strGPON = string.Empty; this.strClientItemCode = string.Empty; this.strCLEI = string.Empty; this.strModel = string.Empty; this.strSupplyCode = string.Empty; this.strCompanyCode = string.Empty; this.strClientPO = string.Empty; this.strBatchNumber = string.Empty; this.strGW = string.Empty; } public string GW { get { return this.strGW; } set { this.strGW = value; } } public string BoxName { get { return this.GetBoxName(this.barcode); } set { } } public string ArrayItemCode { get { return this.strArrayItemCode; } set { this.strArrayItemCode = value; } } public string ArrayQty { get { return this.strArrayQty; } set { this.strArrayQty = value; } } public string VerSion { get { return this.strVerSion; } set { this.strVerSion = value; } } public string SN { get { return this.strSN; } set { this.strSN = value; } } public string MAC { get { return this.strMAC; } set { this.strMAC = value; } } public string GPON { get { return this.strGPON; } set { this.strGPON = value; } } public string ClientItemCode { get { return this.strClientItemCode; } set { this.strClientItemCode = value; } } public string CLEI { get { return this.strCLEI; } set { this.strCLEI = value; } } public string Model { get { return this.strModel; } set { this.strModel = value; } } public string SupplyCode { get { return this.SupplyCode; } set { this.SupplyCode = value; } } public string CompanyCode { get { return this.strCompanyCode; } set { this.strCompanyCode = value; } } public string ClientPO { get { return this.strClientPO; } set { this.strClientPO = value; } } public string BatchNumber { get { return this.strBatchNumber; } set { this.strBatchNumber = value; } } private string GetBoxName(string barcode) { this.GetItemInfoByBarcode(barcode); return this.strBoxName; } private void GetItemInfoByBarcode(string barcode) { barcode = barcode.Replace(Convert.ToString('\x001d'), "&"); barcode = barcode.Replace(Convert.ToString('\x001e'), "&"); string[] strArray = barcode.Split(new char[] { '&' }); for (int i = 1; i < (strArray.Length - 1); i++) { if ((strArray[i].StartsWith("3S") || strArray[i].StartsWith("4S")) || (strArray[i].StartsWith("5S") || strArray[i].StartsWith("52S"))) { if (strArray[i].StartsWith("52S")) { this.strBoxName = strArray[i].Substring(3); } else { this.strBoxName = strArray[i].Substring(2); } } else if (strArray[i].StartsWith("23S")) { this.strMAC = strArray[i].Substring(3); } else if (strArray[i].StartsWith("24S")) { this.strGPON = strArray[i].Substring(3); } else if (strArray[i].StartsWith("P")) { this.strClientItemCode = strArray[i].Substring(1); } else if (strArray[i].StartsWith("1P")) { this.strArrayItemCode = strArray[i].Substring(2); } else if (strArray[i].StartsWith("2P")) { this.strVerSion = strArray[i].Substring(2); } else if (strArray[i].StartsWith("Q")) { this.strArrayQty = strArray[i].Substring(1); } else if (strArray[i].StartsWith("S")) { this.strSN = strArray[i].Substring(1); } else if (strArray[i].StartsWith("K")) { this.strClientPO = strArray[i].Substring(1); } else if (strArray[i].StartsWith("1T")) { this.strBatchNumber = strArray[i].Substring(2); } else if (strArray[i].StartsWith("1Q")) { this.strGW = strArray[i].Substring(2); } } } public string Barcode { get { if (this.barcode == null) { return string.Empty; } return this.barcode; } set { this.barcode = value; } } public string Itemcode { get { return GetCode(this.Barcode); } } public string Qty { get { return GetQty(this.Barcode); } } private static string GetQty(string barcode) { if (!barcode.StartsWith("Q")) { if (barcode.StartsWith("TQ")) { barcode = barcode.Substring(2, barcode.Length - 2).Trim(); } } else { StringBuilder builder = new StringBuilder(); barcode = barcode.Substring(1, barcode.Length - 1).Trim(); foreach (char ch in barcode) { if (char.IsLetter(ch)) { break; } builder.Append(ch); } barcode = builder.ToString(); } if ((barcode.IndexOf('E') <= 0) && (barcode.IndexOf('P') <= 0)) { if (barcode.StartsWith("(") || barcode.StartsWith("(")) { barcode = barcode.Substring(3, barcode.Length - 3); if (barcode.IndexOf(',') > 0) { barcode = barcode.Remove(barcode.IndexOf(','), 1); } return barcode.Trim(); } foreach (char ch3 in barcode) { if (!char.IsLetterOrDigit(ch3)) { return string.Empty; } } return barcode.Trim(); } string str = string.Empty; foreach (char ch2 in barcode) { if (ch2.Equals('E') || ch2.Equals('P')) { break; } if (char.IsNumber(ch2)) { str = str + ch2; } else { str = string.Empty; } } return str.Trim(); } public static bool IsValid(string barcode) { return (GetCode(barcode) != null); } public static string GetCode(string barcode) { if ((barcode.Length == 8) || ((barcode.Length == 12) && (barcode.IndexOf('-') > -1))) { return barcode; } if (barcode.Length < 8) { return string.Empty; } string str = string.Empty; switch (barcode.Substring(0, 2)) { case "09": if (barcode.Length != 20) { if ((barcode.Length > 20) && (barcode.IndexOf('/') != -1)) { str = barcode.Substring(2, barcode.IndexOf('/') - 2); } return str; } return barcode.Substring(2, 8); case "(P": if (barcode.Length >= 3) { return barcode.Substring(3, barcode.Length - 3); } return string.Empty; case "10": if (barcode.Length == 0x12) { return barcode.Substring(2, 8); } return string.Empty; case "21": if (barcode.Length == 20) { return barcode.Substring(2, 8); } return string.Empty; case "02": if (barcode.Length == 0x10) { return ("0302" + barcode.Substring(2, 4)); } return string.Empty; case "03": if (barcode.Length == 0x10) { return ("0303" + barcode.Substring(2, 4)); } return string.Empty; case "19": if (barcode.Length != 20) { if ((barcode.Length > 20) && (barcode.IndexOf('/') != -1)) { str = barcode.Substring(2, barcode.IndexOf('/') - 2); } return str; } return barcode.Substring(2, 8); case "29": if (barcode.Length >= 9) { return barcode.Substring(2, 8); } return string.Empty; case "39": if (barcode.Length >= 9) { return barcode.Substring(2, 8); } return string.Empty; } if (barcode.ToUpper().StartsWith("L")) { return string.Copy(barcode); } if (barcode.ToUpper().StartsWith("P")) { if (barcode.Length == 0x13) { return barcode.Substring(1, 8); } if ((barcode.Length > 9) && (barcode.IndexOf("-") != -1)) { return barcode.Substring(1, 12); } if ((barcode.Length > 0x13) && (barcode.IndexOf('/') != -1)) { str = barcode.Substring(1, barcode.IndexOf('/') - 1); } return str; } if (char.IsNumber(barcode, 0)) { str = barcode.Substring(0, 8); } return str; } public override string ToString() { return this.Barcode; } } }