扫描到二维码后,需要将读出的二维码进行整理,将机型和日期显示出来进行确定,设计如下:
一、添加一个窗口类
二、调整窗口二的大小,并添加控件,布局如下,调整Form属性,不显示最大、最小化图框,设定左上角的起始坐标。
三、将二维码数据传到子窗口,网上关于C#之间的窗体之间互相传值都很麻烦,大概都是重构、委托等,最主要还是都只对控件进行操作,查阅大神操作后,这里用字典在窗体间进行互相传值,(关键点:form2.ShowDialog();之后,form1的代码是在这里中断、形成一个断点的,直到form2被关闭之后,代码才继续向下执行的),因此先定义一个公有的Intent全局类来存放数据。
详细参考:http://t.csdnimg.cn/aTB11
四、在新类里面,创建一个静态,将这个字典dict直接设置成public static,调用不用这么麻烦。
正常情况dict左边的key用string,右边的values用Object,这样就是面对不同的数值传递都可以用上。但用VS2008在取值的时候对象转化方面有问题,所以values也用string了。
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace BT_W155GProject2
{
class Intent
{
public static Dictionary<string,string> dict = new Dictionary<string,string>();
}
}
五、将料框信息及二维码信息存入字典,Form1里面添加方法,并在读码程序里调用
//二维码存储
public static void CodeSave(string codeNum, int frameNum)
{
Intent.dict["currentFrame"] = frameNum.ToString(); //当前启动扫描的料框
Intent.dict["FrameState"] = "1"; //当前状态
if (frameNum == 1)
{
Intent.dict["firstCode"] = codeNum; //1号框的码
Intent.dict["firstState"] = "1"; //1号框的状态
}
if (frameNum == 2)
{
Intent.dict["secondCode"] = codeNum; //2号框的码
Intent.dict["secondState"] = "1"; //2号框的状态
}
if (frameNum == 3)
{
Intent.dict["thirdCode"] = codeNum; //3号框的码
Intent.dict["thirdState"] = "1"; //3号框的状态
}
if (frameNum == 4)
{
Intent.dict["fourthCode"] = codeNum; //4号框的码
Intent.dict["fourthState"] = "1"; //4号框的状态
}
}
六、在Form2内,添加方法将二维码内的编号及生产日期、编号提取出来,这里用到了string.Substring()方法。substring是用来截取字符串的,根据参数的个数不同,方法含义也不同
Substring(1, 5):从第二个字符开始,截取5个;
Substring(1):去掉第一个字符,从第二个字符开始,截取到最后;
//拆分二维码的部品番号
public string PartNumberRead(string str)
{
string str1 = string.Empty;
str1 = str.Substring(1, 5) + " - " + str.Substring(6, 5);
return str1;
}
//拆分二维码的生产编号
public static string SerialNumberRead(string str)
{
string str1 = string.Empty;
string str2 = string.Empty;
string str3 = string.Empty;
string str4 = string.Empty;
string producedDate = string.Empty; //生产日期
string producedNum = string.Empty; //生产编号
str1 = str.Substring(25, 2);
str2 = str.Substring(27, 1);
str3 = str.Substring(28, 2);
producedNum = str.Substring(30, 3);
if (str2 == "A") //10月
{
str2 = "10";
}
else if (str2 == "B")//11月
{
str2 = "11";
}
else if (str2 == "C")//12月
{
str2 = "12";
}
else
{
str2 = "0" + str2;//1-9月前端补零
}
producedDate = str1 + str2 + str3;
str4 = producedDate + "--" + producedNum;
return str4;
}
七、在Form2内创建一个显示 防范,将番号及编号输出到Form2界面
//番号及编号显示
private void codeShow()
{
string PartNumberText;
string SerialNumberText;
string CodeData = null;
curFrame = Intent.dict["currentFrame"];
if (curFrame == "1")
{
CodeData = (string)Intent.dict["firstCode"];
}
else if (curFrame == "2")
{
CodeData = (string)Intent.dict["secondCode"];
}
else if (curFrame == "3")
{
CodeData = (string)Intent.dict["thirdCode"];
}
else if (curFrame == "4")
{
CodeData = (string)Intent.dict["fourthCode"];
}
else
{
return;
}
PartNumberText = PartNumberRead(CodeData);
SerialNumberText = SerialNumberRead(CodeData);
this.label4.Text = PartNumberText;
this.label5.Text = SerialNumberText;
}
八、Form2窗口读取时,调用该方法
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BT_W155GProject2
{
public partial class Form2 : Form
{
string curFrame;//当前料框
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
codeShow();//显示
}
效果如下:
九、将异常和正确按钮程序写入
//扫码异常
private void button1_Click_1(object sender, EventArgs e)
{
if (curFrame == "1")
{
Intent.dict["firstState"] = "0";
}
if (curFrame == "2")
{
Intent.dict["secondState"] = "0";
}
if (curFrame == "3")
{
Intent.dict["thirdState"] = "0";
}
if (curFrame == "4")
{
Intent.dict["fourthState"] = "0";
}
Intent.dict["FrameState"] = "2";
this.DialogResult = DialogResult.OK;
this.Close();
}
//读码正确
private void button2_Click(object sender, EventArgs e)
{
Intent.dict["FrameState"] = "0";
this.DialogResult = DialogResult.OK;
this.Close();
}
十、当直接按右上角×关闭时,添加一个窗口关闭事件,判单FrameState是否为1,不为1时说明是NG码:
//按下窗口× 关闭窗口时,判断状态
private void Form2_Closed(object sender, EventArgs e)
{
string str = Intent.dict["FrameState"];
if (str=="1")
{
if (curFrame == "1")
{
Intent.dict["firstState"] = "0";
}
if (curFrame == "2")
{
Intent.dict["secondState"] = "0";
}
if (curFrame == "3")
{
Intent.dict["thirdState"] = "0";
}
if (curFrame == "4")
{
Intent.dict["fourthState"] = "0";
}
Intent.dict["FrameState"] = "2";
this.DialogResult = DialogResult.OK;
}
}
接下来调整Form1的程序,当Form2传过来OK时,将二维码输出的信息栏,NG时,将"请重新扫码"输出到信息栏;
另外之前出现的MessgaeBox弹窗闪退的问题也找到了,按下扫码枪后,PDA一直处于扫码状态,此时会强制关闭弹窗,需要调用Bt.ScanLib.Control.btScanDisable()推出扫码模式
//确定读取的是二维码而不是条形码
if (symbolGet==11)
{
disp = "料框" + ScanFrameNum.ToString() + "二维码:" + strCodedata + "\r\n";
CodeSave(strCodedata, ScanFrameNum); //将当前料框号及二维码存入字典
//退出读码模式
ret = Bt.ScanLib.Control.btScanDisable();//使得代码读取功能不可用
if (ret != LibDef.BT_OK)
{
disp = "btScanDisable error ret[" + ret + "]";
MessageBox.Show(disp, "错误");
}
}
else
{
disp = "料框" + ScanFrameNum.ToString() + "读码不正确,请扫二维码" + "\r\n";
ScanMessageShow(disp); //异常信息存入textBox
Color color_NG = Color.Silver;
buttonBackColor(ScanFrameNum, color_NG);
ScanFrameNum = 0;
goto L_END;
}
Form2 f2 = new Form2();
f2.ShowDialog();
disp = Intent.dict["FrameState"];
if (disp == "2")
{
string str = "请重新扫码" + "\r\n";
ScanMessageShow(str); //异常信息存入textBox
Color color_NG = Color.Silver;
buttonBackColor(ScanFrameNum, color_NG);
ScanFrameNum = 0;
return;
}
else
{
string str = "料框" + ScanFrameNum.ToString() + "二维码:" + strCodedata + "\r\n";
ScanMessageShow(str);
Color color_OK = Color.Lime;
buttonBackColor(ScanFrameNum, color_OK);
ScanFrameNum = 0;
}
按钮颜色程序修改了一下,颜色由外部传参进来
//按钮背景颜色恢复
private static void buttonBackColor(int FrameNum,Color color)
{
if (FrameNum==1)
{
_Form1Instance.button1.BackColor = color;
}
if (FrameNum == 2)
{
_Form1Instance.button2.BackColor = color;
}
if (FrameNum == 3)
{
_Form1Instance.button3.BackColor = color;
}
if (FrameNum == 4)
{
_Form1Instance.button4.BackColor = color;
}
}