From 5a01014dc116813fd4ea05b61e1eb6eec669f95d Mon Sep 17 00:00:00 2001 From: wangbin <lan@DESKTOP-MG88OFJ> Date: 星期一, 08 七月 2024 09:25:48 +0800 Subject: [PATCH] 1 --- WarM/条码打印/Kf_WeighToBarCode.cs | 340 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 338 insertions(+), 2 deletions(-) diff --git "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Kf_WeighToBarCode.cs" "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Kf_WeighToBarCode.cs" index 42fd702..6f1e97c 100644 --- "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Kf_WeighToBarCode.cs" +++ "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Kf_WeighToBarCode.cs" @@ -12,6 +12,9 @@ using System.IO; using Pub_Class; +using System.IO.Ports; +using System.Linq; + namespace WarM { public partial class Kf_WeighToBarCode : Form @@ -49,6 +52,13 @@ public DAL.ClsGy_BarCodeBill_Ctl oBar = new DAL.ClsGy_BarCodeBill_Ctl(); public DBUtility.ClsPub.Enum_BillStatus BillStatus; SQLHelper.ClsCN oCn = new SQLHelper.ClsCN(); + + + private static string repeatData = string.Empty; + private SerialPort ComDevice = null; + private int HGetWeight = 0; + private string Message = ""; + private int timeIndex = 0; #region //鍥哄畾浠g爜 @@ -176,6 +186,228 @@ DBUtility.Xt_BaseBillFun.DefaultGridView(grdMain, this.Name); DBUtility.Xt_BaseBillFun.DefaultGridView(grdSub, this.Name + "grdSub"); } + + + //鎵撳紑涓插彛 + private void button3_Click(object sender, EventArgs e) + { + OpenCom(); + } + public void OpenCom() + { + //鍒濆鍖栦覆鍙� + ComDevice = new SerialPort(); + ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);//缁戝畾浜嬩欢 + string[] PortNames = SerialPort.GetPortNames(); + + + if (!ComDevice.IsOpen) + { + + try + { + ComDevice.PortName = txtPortName.Text; + ComDevice.BaudRate = int.Parse(txtBaudRate.Text); + switch (cmbParity.SelectedItem.ToString()) + { + case "0": + ComDevice.Parity = Parity.None; + break; + case "1": + ComDevice.Parity = Parity.Odd; + break; + case "2": + ComDevice.Parity = Parity.Even; + break; + case "3": + ComDevice.Parity = Parity.Mark; + break; + case "4": + ComDevice.Parity = Parity.Space; + break; + default: + break; + } + + ComDevice.DataBits = int.Parse(txtDataBits.Text); + switch (cmbStopBits.SelectedItem.ToString()) + { + case "0": + ComDevice.StopBits = StopBits.None; + break; + case "1": + ComDevice.StopBits = StopBits.One; + break; + case "2": + ComDevice.StopBits = StopBits.Two; + break; + case "1.5": + ComDevice.StopBits = StopBits.OnePointFive; + break; + default: + break; + } + + ComDevice.Open(); + + //lblComStatus.Text = "涓插彛鐘舵�侊細寮�鍚�"; + timer3.Enabled = true; + } + catch (Exception ex) + { + MessageBox.Show("鎵撳紑涓插彛澶辫触锛�" + ex.Message, "閿欒", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + else + { + + } + } + + //鍏抽棴涓插彛 + private void button2_Click(object sender, EventArgs e) + { + CloseCom(); + } + public void CloseCom() + { + if (!ComDevice.IsOpen) + { + + } + else + { + try + { + timer3.Enabled = false; + Thread.Sleep(100); + ComDevice.Close(); + // valueCounts = new Dictionary<string, int>(); + // lblComStatus.Text = "涓插彛鐘舵�侊細鍏抽棴"; + + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "閿欒", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + } + + //鍙戦�佹暟鎹� + public bool SendData(byte[] data) + { + if (ComDevice.IsOpen) + { + try + { + ComDevice.Write(data, 0, data.Length);//鍙戦�佹暟鎹� + return true; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "閿欒", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + finally + { + + } + } + else + { + MessageBox.Show("涓插彛鏈墦寮�", "閿欒", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + return false; + } + + //鎺ユ敹鏁版嵁 + private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + //if (!ComDevice.IsOpen) + // { + // return; + // } + // Thread.Sleep(100); + if (HGetWeight == 1) + { + byte[] ReDatas = new byte[ComDevice.BytesToRead]; + ComDevice.Read(ReDatas, 0, ReDatas.Length);//璇诲彇鏁版嵁 + this.AddData(ReDatas);//杈撳嚭鏁版嵁 + } + } + + //澶勭悊鎺ユ敹鍒扮殑鏁版嵁 + public void AddData(byte[] data) + { + //Thread.Sleep(100); + StringBuilder sb = new StringBuilder(); + sb.Append(Encoding.ASCII.GetString(data)); + + var str = sb.ToString(); + str = str.Replace("w", "").Replace("n", "").Replace("kg\r\n", ";"); + + + if (str != repeatData) + { + repeatData = str; + string[] strArray = str.Split(';'); + + for (int i = 0; i < strArray.Length; i++) + { + if (double.TryParse(strArray[i], out double ret))//鍒ゆ柇鏄惁鏄暟瀛� + { + + Message += double.Parse(strArray[i]) + ";"; + + //UpdateLabel(ret.ToString()); + } + } + } + } + + //鑾峰彇鏁扮粍涓嚭鐜伴鐜囨渶楂樼殑鏁版嵁 + private string getMostStr(string message) + { + string[] strArray = message.Split(';'); + // 浣跨敤LINQ鏌ヨ鍑虹幇娆℃暟鏈�澶氱殑鍏冪礌 + var maxElement = strArray.GroupBy(x => x) + .OrderByDescending(g => g.Count()) + .First() + .Key; + + + return maxElement; + } + + //渚濇嵁绾跨▼锛屽皢鏁版嵁鍐欏叆鐣岄潰缁勪欢 + private void UpdateLabel(string text) + { + if (txtHWeightQyt.InvokeRequired) + { + // 鍦║I绾跨▼涓婅皟鐢� UpdateLabel + txtHWeightQyt.Invoke(new Action<string>(UpdateLabel), text); + } + else + { + // 鐩存帴鏇存柊 txtHWeightQyt.Text + txtHWeightQyt.Text = text; + } + } + + //鑾峰彇鏁版嵁鎸夐挳锛屽惎鍔ㄨ鏃跺櫒 + private void button4_Click(object sender, EventArgs e) + { + HGetWeight = 1; + timer2.Enabled = true; + } + + //璁℃椂鍣ㄤ簨浠� + private void timer2_Tick(object sender, EventArgs e) + { + + } + #endregion @@ -1030,13 +1262,117 @@ } } + + + + + + #endregion + private void txtHWeightQyt_TextChanged(object sender, EventArgs e) + { + + } + + private void label24_Click(object sender, EventArgs e) + { + + } + + #region 鏃犵敤浠g爜 + private void label30_Click(object sender, EventArgs e) + { + + } + + private void Form1_Load(object sender, EventArgs e) + { + + } + + + private void txtBaudRate_TextChanged(object sender, EventArgs e) + { + + } + + private void lblHBWeight_Click(object sender, EventArgs e) + { + + } + + private void cmbStopBits_SelectedIndexChanged(object sender, EventArgs e) + { + + } + + private void txtDataBits_TextChanged(object sender, EventArgs e) + { + + } + + private void txtPortName_TextChanged(object sender, EventArgs e) + { + + } + + private void cmbParity_SelectedIndexChanged(object sender, EventArgs e) + { + + } + + private void txtHSend0_TextChanged(object sender, EventArgs e) + { + + } + + private void txtHSend_TextChanged(object sender, EventArgs e) + { + + } + + private void label26_Click(object sender, EventArgs e) + { + + } + + private void label20_Click(object sender, EventArgs e) + { + + } + + private void label23_Click(object sender, EventArgs e) + { + + } + + private void label25_Click(object sender, EventArgs e) + { + + } + + private void cmdHSaveInfo_Click(object sender, EventArgs e) + { + //ClsIni.WriteIni("COMINFO", "txtPortName", DBUtility.ClsPub.isStrNull(txtPortName.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini"); + //ClsIni.WriteIni("COMINFO", "txtBaudRate", DBUtility.ClsPub.isStrNull(txtBaudRate.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini"); + //ClsIni.WriteIni("COMINFO", "cmbParity", DBUtility.ClsPub.isStrNull(cmbParity.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini"); + //ClsIni.WriteIni("COMINFO", "txtDataBits", DBUtility.ClsPub.isStrNull(txtDataBits.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini"); + //ClsIni.WriteIni("COMINFO", "cmbStopBits", DBUtility.ClsPub.isStrNull(cmbStopBits.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini"); + //ClsIni.WriteIni("COMINFO", "txtHSend0", DBUtility.ClsPub.isStrNull(txtHSend0.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini"); + //ClsIni.WriteIni("COMINFO", "txtHSend", DBUtility.ClsPub.isStrNull(txtHSend.Text), DBUtility.ClsPub.AppPath + @"/HXERP.ini"); + } + + private void richTextBox1_TextChanged(object sender, EventArgs e) + { + } + #endregion + private void button4_Click_1(object sender, EventArgs e) + { - - + } } } \ No newline at end of file -- Gitblit v1.9.1