using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Lskj.Control; using System.IO; using Lskj.Util; using System.Security.Cryptography; using Lskj.Core; using ZXing; using Lskj.Control.Model; using ZXing.Presentation; using com.google.zxing.common; using com.google.zxing; using Lskj.Model; namespace Lskj.ReBurnCode { public partial class FrmMain : BaseForm { #region 公有字段 public int comm; public int band; public const int WM_DEVICECHANGE = 0x219; public const int DBT_DEVICEARRIVAL = 0x8000; public const int DBT_DEVICEREMOVECOMPLETE = 0x8004; public bool IsCopy = false; /// /// 选中类型的数据值 /// public string EquipmentNo; /// /// 反写密码 /// public string Password; /// /// 保存版本号 /// public string VerNum; /// /// 序列号 /// public string SeqNum; #endregion public FrmMain() { InitializeComponent(); } /// /// 说明:初始化设置密码控件 /// 创建人:王一帆 /// 创建日期:2020-11-05 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void BurnCode_Load(object sender, EventArgs e) { if (DBConfig.Instance.CreateConnection()) { SetCommandBand(); SetProductBand(); SetAuthorizationBand(); } else { MessageUtil.Show("请检查连接!"); } } #region 事件 #region usb插入拔出事件 /// /// 说明:usb插入拔出事件 /// 创建人:王一帆 /// 创建日期:2020-11-05 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// protected override void WndProc(ref Message m) { try { if (m.Msg == WM_DEVICECHANGE) { switch (m.WParam.ToInt32()) { // USB插上 case DBT_DEVICEARRIVAL: if (!IsCopy) // 如果不用这个判断,会被多次调用,原因不明 { IsCopy = true; //WaitForm.ShowForm("正在连接串口请稍后...."); //if (Program.OpenComm(this.comm, this.band, 8, 0, 1) == 0) //{ // MessageUtil.Show("串口打开失败!"); // this.label3.Visible = true; // this.label3.ForeColor = Color.Red; // this.label2.Visible = false; // this.label3.Text = "串口打开失败"; // this.progressBar1.Value = 0; // return; //} //// 其他处理 //this.Ver.Visible = this.power.Visible = this.days.Visible = true; //this.Ver.Text = "版本号:" + Program.GetVer();//获取版本号 //this.power.Text = "权限:" + Program.GetRun_status(0);//获取权限 //this.days.Text = "天数:" + Program.GetRun_status(1);//获取天数 } break; // USB移除 case DBT_DEVICEREMOVECOMPLETE: IsCopy = false; this.Ver.Visible = this.power.Visible = this.days.Visible = false; break; default: break; } } } catch (Exception ex) { MessageUtil.Show(ex); } finally { WaitForm.HideForm(); } base.WndProc(ref m); } #endregion /// /// 说明:扫码获取工号 /// 创建人:王一帆 /// 创建日期:2020-11-05 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void OnbtnScanCode(object sender, EventArgs e) { FrmCamera frmCamera = new FrmCamera(); // 保存流转步骤 if (frmCamera.ShowDialog() == DialogResult.OK) { this.FuNumber.Text = frmCamera.resultText; this.ScanCodeText.Text = "1"; } } #region #endregion #region 通过串口保存密码事件 /// /// 说明:通过串口保存密码 /// 创建人:王一帆 /// 创建日期:2020-11-05 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void OnbtnSaveClick(object sender, EventArgs e) { //if (!IsCopy) //{ // MessageUtil.Show("请检查usb连接!"); // return; //} try { this.label2.Visible = false; this.FuNumber.Text = this.ScanCodeText.Text == "1" ? this.FuNumber.Text : string.Empty; SeqNum = string.Empty; WritebackInformation(); if (string.IsNullOrEmpty(FuNumber.Text)) { MessageUtil.Show("数据配置不全!请检查"); return; } UInt16 num = 998; if (this.comboBox1.Text == "") { MessageBox.Show("授权方式未选择"); return; } if (this.comboBox1.Text == "限时运行") { if (this.textBox1.Text == "") { MessageBox.Show("授权天数未填"); return; } try { num = Convert.ToUInt16(this.textBox1.Text); if (num < 0 || num > 999) { MessageBox.Show("天数超出范围"); this.textBox1.Text = ""; return; } } catch (Exception) { MessageBox.Show("天数应为数字!"); this.textBox1.Text = ""; return; } } this.label3.BackColor = Color.Transparent; this.progressBar1.Minimum = 0; this.progressBar1.Maximum = 100; string text = this.GeneratePassword(this.FuNumber.Text + this.comboBox1.SelectedValue); if (MessageUtil.Show("确定要设置密码为:" + text+"吗?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { this.label2.Visible = true; this.Password = text; this.label2.Text = "密码为" + text; this.progressBar1.Value = 10; if (Program.OpenComm(this.comm, this.band, 8, 0, 1) == 0) { MessageUtil.Show("串口打开失败!"); this.label3.Visible = true; this.label3.ForeColor = Color.Red; this.label2.Visible = false; this.label3.Text = "串口打开失败"; this.progressBar1.Value = 0; return; } VerNum = GetVer(ConvertHex(Program.GetVer() + "")); this.Ver.Text = "版本号:" + VerNum;//获取版本号 for (UInt16 i = 0; i < 6; i++) { SeqNum += ConvertHex(Program.GetUid(i) + "") + ","; } SeqNum = SeqNum.TrimEnd(','); //this.Ver.Visible = true; string text2 = this.comboBox1.Text; UInt16 permission = 0; //if (!(text2 == "完全授权")) //{ if (text2 == "限时运行") { permission = 4; } else if (text2 == "开机管控") { permission = 2; } // } // else //{ // permission = 2; //} this.progressBar1.Value = 30; int q = Program.SetPassword(Convert.ToUInt32(text) & 0xFFFFF, permission, num); if (q == 0) { WriteAfterInformation(); } else { this.label3.Visible = true; this.label3.ForeColor = Color.Red; this.label2.Visible = false; this.label3.Text = "写入失败"; this.btn_Save.Text = "再次写入"; //SqlHelper.ExecuteNonQuery(string.Format("update MES_SerialNumberInterFaceTab set isok='-1' where EquipmentNo='{0}'", EquipmentNo)); this.progressBar1.Value = 0; this.label3.Text = q + ""; } this.ScanCodeText.Text = "0"; } } catch (Exception ex) { MessageUtil.Show(ex); } } #endregion #region 授权机器下拉改变时候触发 /// /// 说明:授权机器下拉改变时候触发 /// 创建人:王一帆 /// 创建日期:2020-11-05 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void selectChageProduct(object sender, EventArgs e) { EquipmentNo = this.product.SelectedValue.ToString(); if (string.IsNullOrWhiteSpace(EquipmentNo)) { MessageUtil.Show("请选择产品类型!"); } try { ByteMatrix byteMatrix = new MultiFormatWriter().encode(EquipmentNo, BarcodeFormat.QR_CODE, 200, 150); Bitmap bitmap = toBitmap(byteMatrix); pictureBox1.Image = bitmap; } catch (Exception ex) { } } #endregion #endregion #region 方法 /// /// 返回版本号分割字符 /// /// value /// public static string GetVer(string value) { string first = System.Convert.ToInt32(value.Substring(0, 1), 16) + ""; first = first.Length == 1 ? "0" + first : first; string bottom = System.Convert.ToInt32(value.Substring(1, 2), 16) + ""; bottom = bottom.Length == 1 ? "0" + bottom : bottom; bottom = bottom.Length == 2 ? "0" + bottom : bottom; string last = System.Convert.ToInt32(value.Substring(3), 16) + ""; last = last.Length == 1 ? "0" + last : last; return first + "," + bottom + "," + last; } /// /// 返回16进制字符 /// /// value /// public static string GetHexChar(string value) { string sReturn = string.Empty; switch (value) { case "10": sReturn = "A"; break; case "11": sReturn = "B"; break; case "12": sReturn = "C"; break; case "13": sReturn = "D"; break; case "14": sReturn = "E"; break; case "15": sReturn = "F"; break; default: sReturn = value; break; } return sReturn; } /// /// 返回16进制 /// /// value /// public static string ConvertHex(string value) { string sReturn = string.Empty; try { while (int.Parse(value) > 16) { int v = int.Parse(value); sReturn = GetHexChar((v % 16).ToString()) + sReturn; value = Math.Floor(Convert.ToDouble(v / 16)).ToString(); } sReturn = GetHexChar(value) + sReturn; } catch { sReturn = "###Valid Value!###"; } return sReturn; } #region 通过文本生成二维码 /// /// 说明:通过下拉框生成二维码 /// 创建人:王一帆 /// 创建日期:2020-11-05 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// public static Bitmap toBitmap(ByteMatrix matrix) { int width = matrix.Width; int height = matrix.Height; Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF")); } } return bmap; } #endregion #region 初始化设置密码控件 /// /// 说明:初始化设置密码控件 /// 创建人:王一帆 /// 创建日期:2020-11-05 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private string GeneratePassword(string mac) { HashAlgorithm hashAlgorithm = new SHA256Managed(); byte[] array = Encoding.Default.GetBytes(mac); array = hashAlgorithm.ComputeHash(array); string text = Math.Abs(BitConverter.ToInt32(array, 0) % 999999).ToString(); if (text.Length < 6) { text = ("00000" + text).Substring(text.Length - 1, 6); } return text; } #endregion #region 获取本地存储的串口和波率 /// /// 说明:获取本地存储的串口和波率 /// 创建人:王一帆 /// 创建日期:2020-11-06 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetCommandBand() { comm = Convert.ToInt32(IniHelper.Read("test.ini", "comm")); band = Convert.ToInt32(IniHelper.Read("test.ini", "band")); } #endregion #region 获取本地机器类配置 /// /// 说明:获取本地机器类配置 /// 创建人:王一帆 /// 创建日期:2020-11-06 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetProductBand() { this.product.ValueMember = "no"; this.product.DisplayMember = "name"; DataTable productTable = SqlHelper.ExecuteDataTable(string.Format("select no,name from MES_EquipmentInterFaceTab where computerName='{0}'", ERPInfo.Instance.WindowName)); if (productTable.Rows.Count > 0) { this.product.DataSource = productTable; } else { MessageUtil.Show("未配置机器类型表!"); } } #endregion #region 获取授权类型配置 /// /// 说明:获取授权类型配置 /// 创建人:王一帆 /// 创建日期:2020-11-06 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetAuthorizationBand() { this.comboBox1.ValueMember = "CCode"; this.comboBox1.DisplayMember = "CName"; DataTable authorizationTable = new DataTable(); authorizationTable.Columns.Add("CCode"); authorizationTable.Columns.Add("CName"); authorizationTable.Rows.Add("2", "开机管控"); authorizationTable.Rows.Add("4", "限时运行"); this.comboBox1.DataSource = authorizationTable; } #endregion #region 通过类型反写信息号 /// /// 说明:通过类型反写信息号 /// 创建人:王一帆 /// 创建日期:2020-11-06 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void WritebackInformation() { DataTable dt = SqlHelper.ExecuteDataTable(string.Format("select * from MES_SerialNumberInterFaceTab where EquipmentNo='{0}' and isok=0", EquipmentNo)); if (dt.Rows.Count > 0 && this.ScanCodeText.Text!="1") { FuNumber.Text = dt.Rows[0]["barcode"] + ""; } } #endregion #region 写入成功后更新数据反写密码 /// /// 说明:写入成功后更新数据反写密码 /// 创建人:王一帆 /// 创建日期:2020-11-06 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void WriteAfterInformation() { this.label3.Visible = true; this.label3.ForeColor = Color.Green; this.label3.Text = "写入成功"; this.label2.Visible = true; SqlHelper.ExecuteNonQuery(string.Format("update MES_SerialNumberInterFaceTab set password='{0}',isok='1',writeDt='{2}',ismyself='{3}',versionNum='{5}',motherboardNum='{6}' where EquipmentNo='{1}' and barcode='{4}'", Password, EquipmentNo, DateTime.Now.ToString(), this.ScanCodeText.Text, this.FuNumber.Text, VerNum,SeqNum)); this.progressBar1.Value = 100; } #endregion private void btn_Close_Click(object sender, EventArgs e) { // 关闭所有的线程 System.Environment.Exit(0); } #endregion private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { // 关闭所有的线程 System.Environment.Exit(0); } } }