From 243e86af1a7c74c8bfda3bb41f29a11fef13c065 Mon Sep 17 00:00:00 2001
From: zrg <z18737863051@163.com>
Date: 星期三, 09 十月 2024 16:32:52 +0800
Subject: [PATCH] Merge branch 'master' of http://101.37.171.70:10101/r/MES-WEB-API
---
WarM/条码打印/Kf_WeighToBarCode.cs | 566 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 560 insertions(+), 6 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..06a734f 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"
@@ -11,6 +11,8 @@
using System.Threading;
using System.IO;
using Pub_Class;
+using System.IO.Ports;
+using System.Linq;
namespace WarM
{
@@ -62,6 +64,9 @@
grdMain.DataSource = null;
grdSub.DataSource = null;
chkHDYFlag.Checked = true;
+
+ //璇诲彇 鎵撳紑涓插彛鐨勯厤缃俊鎭�
+ readParams();
}
//绐椾綋鍔犺浇
@@ -383,7 +388,13 @@
private void tc_Click(object sender, EventArgs e)
{
- this.Close();
+ //閫�鍑烘椂鍏抽棴涓插彛
+ if (ComDevice != null)
+ {
+ CloseCom();
+ }
+
+ this.Close();
}
#endregion
@@ -692,6 +703,7 @@
Int64 HBadReasonID = 0; //涓嶈壇鍘熷洜ID
string HCusMaterName = ""; //瀹㈡埛鐗╂枡鍚嶇О
string HCusModel = ""; //瀹㈡埛瑙勬牸鍨嬪彿
+ string HBarCode_Pack = ""; //鎵樼洏鏉$爜
HBarCode = TM;
HBarCodeType = DBUtility.ClsPub.isStrNull(cmbHBarCodeType.Text);
@@ -708,6 +720,7 @@
HBadReasonID = DBUtility.ClsPub.isLong(txtHRemark.Tag);
HCusMaterName = DBUtility.ClsPub.isStrNull(txtHCusMaterName.Text);
HCusModel = DBUtility.ClsPub.isStrNull(txtHCusModel.Text);
+ HBarCode_Pack = DBUtility.ClsPub.isStrNull(txtHBarCode_Pack.Text);
try
{
@@ -719,7 +732,7 @@
",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HEndQty " +
",HBarcodeQtys,HBarcodeNo,HDeptID,HWhID,HSPID,HRemark " +
",HCusID,HCusType,HEndDate,HWorkLineName,HBarCodeDate,HMaterialJQty,HMaterialMQty,HNowQty " +
- ",HSTOCKORGID,HOWNERID,HSeOrderBillNo,HInterID,HGiveAwayFlag " +
+ ",HSTOCKORGID,HOWNERID,HSeOrderBillNo,HInterID,HGiveAwayFlag,HBarCode_Pack " +
",HOrderInterID,HOrderEntryID,HOrderBillNo,HCustomQty1,HMaterialModel,HCusModel " +
",HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo,HInnerBillNo,HLogo " +
") Values ("
@@ -728,7 +741,7 @@
+ ", " + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + ",'" + HSourceBillNo + "','" + HSourceBillType + "',''"
+ ", 1," + HBarcodeNo.ToString() + "," + HDeptID.ToString() + "," + HWhID.ToString() + "," + HSPID.ToString() + ",'" + HRemark + "'"
+ ", 0,'',getdate(),'','" + sDate + "'," + HMaterialJQty.ToString() + "," + HMaterialMQty.ToString() + "," + HNowQty.ToString()
- + ", " + HStockOrgID.ToString() + "," + HStockOrgID.ToString() + ",''," + HInterID.ToString() + ",0"
+ + ", " + HStockOrgID.ToString() + "," + HStockOrgID.ToString() + ",''," + HInterID.ToString() + ",0,'" + HBarCode_Pack + "'"
+ ", " + HProcessExchangeInterID.ToString() + ",0,'" + HProcessExchangeBillNo + "'," + HBadReasonID.ToString() + ",'" + HCusMaterName + "','" + HCusModel + "'"
+ ",'" + HMaterName + "','" + HMaterModel + "',''," + HAuxPropID.ToString() + ",'','','1')");
@@ -750,8 +763,8 @@
" Values("
+ HInterID.ToString() + ",'" + HBillNo + "','" + HBillType + "'," + HMaterID.ToString() + "," + HAuxPropID.ToString() + ",0"
+ "," + HWhID.ToString() + ",0," + HSPID.ToString() + ",0,0,''"
- +",0," + HQty.ToString() + ",1,'" + HBatchNo + "','" + HBarCode + "',''"
- +",'" + HMaker + "',getdate()," + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + ",'" + HSourceBillType + "','" + HSourceBillNo + "'"
+ +",0," + HQty.ToString() + ",1,'" + HBatchNo + "','" + HBarCode + "','" + HBarCode_Pack + "'"
+ + ",'" + HMaker + "',getdate()," + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + ",'" + HSourceBillType + "','" + HSourceBillNo + "'"
+",0,0,'',0,'',0"
+"," + HStockOrgID.ToString() + "," + HStockOrgID.ToString() + "," + HStockOrgID.ToString() + ",'','绉伴噸鍏ュ簱鐢熸垚','2'"
+",0,0,'',0)");
@@ -877,6 +890,11 @@
{
HRemark = HRemark + "銆�" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("鏉$爜缂栧彿")].Value);
}
+ if (DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("鏉$爜绫诲瀷")].Value) == "鎵樼洏鏉$爜")
+ {
+ MessageBox.Show("鎵�閫夋潯鐮佺紪鍙凤細" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("鏉$爜缂栧彿")].Value) + " 涓烘墭鐩樻潯鐮侊紝涓嶅厑璁镐綔搴燂紒", "鎻愮ず");
+ return;
+ }
}
if (HRemark != "")
{
@@ -939,6 +957,7 @@
string HItemID = "";
string HBarCode = "";
string HBarCode2 = "";
+ string HBarCode_Pack = DBUtility.ClsPub.isStrNull(txtHBarCode_Pack.Text);
for (int i = 0; i <= grdMain.SelectedRows.Count - 1; i++)
{
HItemID = HItemID + "," + DBUtility.ClsPub.isLong(grdMain.SelectedRows[i].Cells[Fun_GetCol("hmainid")].Value.ToString());
@@ -954,6 +973,17 @@
if (DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("浣滃簾鏍囪")].Value) == "")
{
HRemark = HRemark + "銆�" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("鏉$爜缂栧彿")].Value);
+ }
+ if (DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("鏉$爜绫诲瀷")].Value) == "鎵樼洏鏉$爜")
+ {
+ MessageBox.Show("鎵�閫夋潯鐮佺紪鍙凤細" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("鏉$爜缂栧彿")].Value) + " 涓烘墭鐩樻潯鐮侊紝涓嶅厑璁稿弽浣滃簾锛�", "鎻愮ず");
+ return;
+ }
+ if (DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("HBarCode_Pack")].Value) != ""
+ && DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("HBarCode_Pack")].Value) != HBarCode_Pack)
+ {
+ MessageBox.Show("鎵�閫夋潯鐮佺紪鍙凤細" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("鏉$爜缂栧彿")].Value) + " 宸茬敓鎴愬搴旂粍鎵樺崟锛屼笉鍏佽鍙嶄綔搴燂紒", "鎻愮ず");
+ return;
}
}
if (HRemark != "")
@@ -988,7 +1018,7 @@
",HCusID,HDeptID,HCusBarCode,HMulSourceBill) " +
" select " + HInterID.ToString() + ",'" + HBillNo + "','" + HBillType + "',HMaterID,HAuxPropID,0" +
",HWhID,0,HSPID,0,0,''" +
- ",0,HQty,1,HBatchNo,HBarCode,''" +
+ ",0,HQty,1,HBatchNo,HBarCode,HBarCode_Pack" +
",'" + HMaker + "',getdate(),HSourceInterID,HSourceEntryID,HSourceBillType,HSourceBillNo" +
",0,0,'',0,'',0" +
",HSTOCKORGID,HSTOCKORGID,HSTOCKORGID,'','绉伴噸鍏ュ簱鐢熸垚-鍙嶄綔搴�','2'" +
@@ -1032,9 +1062,533 @@
#endregion
+ #region //寮�濮嬬粍鎵�
+ private void cmdHBPack_Click(object sender, EventArgs e)
+ {
+ if (!PackSub_AllowSave())
+ {
+ return;
+ }
+ SavePackBarCode();
+ BarCode_Display();
+ grdMain.Rows[0].Selected = true;
+
+ txtHBarCode_Pack.Text = DBUtility.ClsPub.isStrNull(grdMain.Rows[grdMain.SelectedRows[0].Index].Cells[Fun_GetCol("鏉$爜缂栧彿")].Value);
+ cmdHBPack.Enabled = false;
+
+ sBarCodeItemID = DBUtility.ClsPub.isLong(grdMain.Rows[grdMain.SelectedRows[0].Index].Cells[Fun_GetCol("HItemID")].Value).ToString();
+ //鑷姩鎵撳嵃
+ if (chkHDYFlag.Checked == true)
+ {
+ //鎵撳嵃鏉$爜
+ Sub_SetReport("鎵樼洏鏉$爜");
+ Report.Print(false);
+ Thread.Sleep(1000);
+ }
+ else
+ {
+ //閫夋嫨鎵撳嵃妯℃澘
+ BLL.Gy_OpenTmp oFrm = new BLL.Gy_OpenTmp();
+ oFrm.sBillName = ModName;
+ oFrm.sBillModel = ModCaption;
+ oFrm.ShowDialog();
+ if (oFrm.OKTag == Pub_Class.ClsPub.Enum_OKTag.OKTag_OK)
+ {
+ Sub_SetReport(oFrm.sOpenTmp);
+ Report.Print(false);
+ Thread.Sleep(1000);
+ }
+ }
+ }
+
+ //鏉$爜鐢熸垚鍓嶅垽鏂�
+ private bool PackSub_AllowSave()
+ {
+ //鍒ゆ柇褰撳墠鍗曟嵁ID鏄惁宸蹭笂浼犵敓鎴愬崟鎹�
+ DataSet Ds = oCn.RunProcReturn("select top 1 1 from KF_WeighSource_Temp with(nolock) where HInterID=" + HInterID.ToString() + " and HRelationInterID=1 ", "KF_WeighSource_Temp", ref DBUtility.ClsPub.sExeReturnInfo);
+ if (Ds == null || Ds.Tables[0].Rows.Count == 0)
+ {
+ }
+ else
+ {
+ MessageBox.Show("褰撳墠鏉$爜璁板綍宸叉崲鐝笂浼犵敓鎴愬崟鎹紝璇烽��鍑哄悗閲嶆柊杩涘叆璇ユā鍧楄繘琛屾潯鐮佺敓鎴愭搷浣滐紒", "鎻愮ず");
+ return false;
+ }
+
+ if (HInterID == 0 || HBillNo == "")
+ {
+ MessageBox.Show("鑾峰彇鏉$爜瀵瑰簲鍗曟嵁ID鎴栧崟鎹彿澶辫触锛岃閲嶆柊鍒锋柊鐣岄潰锛�", "鎻愮ず");
+ return false;
+ }
+ return true;
+ }
+
+ //鐢熸垚鎵樼洏鏉$爜
+ private void SavePackBarCode()
+ {
+ int LSHlen = 6; //娴佹按鍙烽暱搴�
+ Int64 LSH = 0; //娴佹按鍙�
+ string LSH2 = ""; //娴佹按鍙疯浆鎹㈡垚瀛楃
+ string sDate = ""; //鏃ユ湡
+ string sYear = ""; //骞�
+ string sPeriod = ""; //鏈�
+ string sDay = ""; //鏃�
+ string sTMNumber = ""; //鏉$爜鑷畾涔夊墠缂�
+ string TM = ""; //鏉$爜缂栧彿
+ DataSet Ds;
+
+ //鏃ユ湡鑾峰彇鏂瑰紡
+ sDate = dtpHDate.Value.ToShortDateString();
+ sYear = DBUtility.ClsPub.isDate(sDate).Year.ToString().Substring(2, 2);
+ sPeriod = "0" + DBUtility.ClsPub.isDate(sDate).Month.ToString();
+ sPeriod = sPeriod.Substring(sPeriod.Length - 2, 2);
+ sDay = "0" + DBUtility.ClsPub.isDate(sDate).Day.ToString();
+ sDay = sDay.Substring(sDay.Length - 2, 2);
+ //==================================
+ //鏉$爜鍓嶇紑 = 'TP' + 缁勭粐浠g爜 + 骞� + 鏈� + 鏃�
+ sTMNumber = "TP" + DBUtility.ClsPub.HOrgNumber + sYear + sPeriod + sDay;
+ Ds = oCn.RunProcReturn("exec h_p_WMS_GetMaxNo '" + sTMNumber + "'", "h_p_WMS_GetMaxNo"); //鑾峰彇鏈�澶ф祦姘村彿
+ LSH = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0][0]);
+ LSH = LSH + 1;
+ LSH2 = LSH.ToString();
+ while (LSH2.Length < LSHlen) //濡傛灉娴佹按鍙峰皬浜�6浣嶆暟鍓嶉潰琛�0
+ {
+ LSH2 = "0" + LSH2;
+ }
+ TM = sTMNumber + LSH2;
+
+ string HBarCode = "";
+ string HBarCodeType = "鎵樼洏鏉$爜";
+
+ HBarCode = TM;
+ HBarCodeType = "";
+
+ try
+ {
+ oCn.BeginTran();
+ //鍐欏叆鏉$爜妗f
+ oCn.RunProc("Insert into Gy_BarCodeBill " +
+ "(HBarCode,HBarCodeType,HMaterID,HUnitID,HQty,HBarCode_Pack" +
+ ",HBatchNo,HSupID,HGroupID,HMaker,HMakeDate,HPrintQty,HinitQty" +
+ ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HEndQty " +
+ ",HBarcodeQtys,HBarcodeNo,HDeptID,HWhID,HSPID,HRemark " +
+ ",HCusID,HCusType,HEndDate,HWorkLineName,HBarCodeDate,HMaterialJQty,HMaterialMQty,HNowQty " +
+ ",HSTOCKORGID,HOWNERID,HSeOrderBillNo,HInterID,HGiveAwayFlag " +
+ ",HOrderInterID,HOrderEntryID,HOrderBillNo,HCustomQty1,HMaterialModel,HCusModel " +
+ ",HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo,HInnerBillNo,HLogo " +
+ ") Values ("
+ + "'" + HBarCode + "','" + HBarCodeType + "',0,0,1,'" + HBarCode + "'"
+ + ",'',0,0,'" + HMaker + "',getdate(),0,1"
+ + ",0,0,'','',''"
+ + ",1,0,0,0,0,''"
+ + ", 0,'',getdate(),'','" + sDate + "',0,0,0"
+ + ", " + HStockOrgID.ToString() + "," + HStockOrgID.ToString() + ",''," + HInterID.ToString() + ",0"
+ + ", " + HProcessExchangeInterID.ToString() + ",0,'',0,'',''"
+ + ",'','','',0,'','','1')");
+
+ //鍥炲~鏈�澶ф祦姘村彿
+ oCn.RunProc("exec h_p_WMS_SetMaxNo_QTY '" + sTMNumber + "',1 ");
+ oCn.Commit();
+ }
+ catch (Exception e)
+ {
+ oCn.RollBack();
+ MessageBox.Show("鎵樼洏鏉$爜鐢熸垚澶辫触锛�" + e.Message);
+ }
+ }
+
+
+ #endregion
+
+ #region //缁撴潫缁勬墭
+ private void cmdHEPack_Click(object sender, EventArgs e)
+ {
+ string HBarCode_Pack = DBUtility.ClsPub.isStrNull(txtHBarCode_Pack.Text);
+
+ //鐢熸垚缁勬墭鍗曞墠鍒ゆ柇
+ if(HBarCode_Pack=="")
+ {
+ MessageBox.Show("璇峰厛鐐瑰嚮寮�濮嬬粍鎵橈紝杩涜鏉$爜缁勬墭鎿嶄綔锛�", "鎻愮ず");
+ return;
+ }
+ bool b = false;
+ for (int i = 0; i < grdMain.RowCount; i++)
+ {
+ if (DBUtility.ClsPub.isStrNull(grdMain.Rows[i].Cells[Fun_GetCol("鏉$爜绫诲瀷")].Value) == "鍞竴鏉$爜"
+ && DBUtility.ClsPub.isStrNull(grdMain.Rows[i].Cells[Fun_GetCol("HBarCode_Pack")].Value) == HBarCode_Pack)
+ {
+ b = true;
+ break;
+ }
+ }
+ if (b == false)
+ {
+ MessageBox.Show("褰撳墠鎵樼洏鏉$爜涓嬭繕娌$敓鎴愬搴旀潯鐮侊紝涓嶅厑璁哥敓鎴愮粍鎵樺崟锛�", "鎻愮ず");
+ return;
+ }
+
+ //鍒ゆ柇浼氳鏈熸槸鍚﹀悎鐞�
+ string s = "";
+ int sYear = 0;
+ int sPeriod = 0;
+ if (DBUtility.Xt_BaseBillFun.Fun_AllowYearPeriod(HDate, ref sYear, ref sPeriod, ref s) == false)
+ {
+ MessageBox.Show(s, "鎻愮ず");
+ return;
+ }
+
+ try
+ {
+ oCn.BeginTran();
+ //鐢熸垚鐢熶骇缁勬墭鍗�
+ Int64 HPUInterID = DBUtility.ClsPub.CreateBillID_Prod("3783", ref DBUtility.ClsPub.sExeReturnInfo); //寰楀埌鏈�鏂扮敓浜х粍鎵樺崟ID
+ string HPUBillNo = DBUtility.ClsPub.CreateBillCode_Prod("3783", ref DBUtility.ClsPub.sExeReturnInfo, true); //寰楀埌鏈�鏂扮敓浜х粍鎵樺崟鍙�
+
+ //鍐欏叆鐢熶骇缁勬墭鍗曚富琛�
+ oCn.RunProc("Insert Into Sc_PackUnionBillMain " +
+ "(HInterID,HBillNo,HBillType,HBillSubType,HDate,HRemark" +
+ ",HYear,HPeriod,HChecker,HCheckDate,HMaker,HMakeDate" +
+ ",HICMOInterID,HICMOBillNo,HBarCode_Pack,HMaterID,HUnitID,HProdOrgID" +
+ ") Values ("
+ + " " + HPUInterID.ToString() + ",'" + HPUBillNo + "','3783','3783',convert(varchar(10),getdate(),120),''"
+ + "," + sYear.ToString() + "," + sPeriod.ToString() + ",'" + HMaker + "',getdate(),'" + HMaker + "',getdate()"
+ + ",0,'','" + HBarCode_Pack + "',0,0," + HStockOrgID.ToString() + ") ");
+
+ //鍐欏叆鐢熶骇瑁呯鍗曞瓙琛�
+ oCn.RunProc("Insert into Sc_PackUnionBillSub " +
+ "(HInterID,HEntryID,HRelationQty,HRelationMoney,HRemark" +
+ ",HMaterID,HUnitID,HQty,HBarCode,HBarCode_Pack,HScanDate" +
+ ",HAuxPropID,HBatchNo,HMTONo" +
+ ",HSourceID,HEquipID,HGroupID,HWorkerID,HStatus" +
+ ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType) " +
+ " select " + HPUInterID.ToString() + ",HItemID,'0,0,''" +
+ ",HMaterID,HUnitID,HQty,HBarCode,HBarCode_Pack,getdate()" +
+ ",HAuxPropID,HBatchNo,HMTONo" +
+ ",0,0,0,0,0" +
+ ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType" +
+ "from Gy_BarCodeBill with(nolock) where HInterID=" + HInterID.ToString() + " and HBarCode_Pack='" + HBarCode_Pack + "' and HStopflag=0"
+ );
+
+ MessageBox.Show("鐢熸垚鐢熶骇缁勬墭鍗曪細" + HPUBillNo + " 鎴愬姛锛�", "鎻愮ず");
+
+ txtHBarCode_Pack.Text = "";
+ cmdHBPack.Enabled = true;
+ oCn.Commit();
+ return;
+ }
+ catch (Exception e2)
+ {
+ oCn.RollBack();
+ MessageBox.Show("鐢熸垚鐢熶骇缁勬墭鍗曞け璐ワ紒" + e2.Message);
+ return;
+ }
+ }
+
+ #endregion
+ #region 璇诲彇璁惧绉伴噸淇℃伅
+
+
+
+ private static string repeatData = string.Empty;
+ private SerialPort ComDevice = null;
+
+ private int HGetWeight = 0;
+ private string Message = "";
+
+ private int timeIndex = 0;
+
+ private string value_COM = ""; //COM
+ private string value_BaudRate = "9600"; //姣旂壒鐜�
+ private string value_StopBits = "1"; //鍋滄浣�
+ private string value_Parity = "0"; //鏍¢獙浣�
+ private string value_DataBits = "8"; //鏁版嵁浣�
+
+ #region 鎵撳紑涓插彛鎸夐挳瑙﹀彂浜嬩欢
+ private void button_OpenCom_Click(object sender, EventArgs e)
+ {
+ value_COM = textBox_COM.Text;
+ OpenCom();
+ }
+ #endregion
+
+ #region 鎵撳紑涓插彛
+ public void OpenCom()
+ {
+ //鍒濆鍖栦覆鍙�
+ ComDevice = new SerialPort();
+ ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);//缁戝畾浜嬩欢
+ string[] PortNames = SerialPort.GetPortNames();
+
+ if (!ComDevice.IsOpen)
+ {
+ try
+ {
+ ComDevice.PortName = value_COM;
+ ComDevice.BaudRate = int.Parse(value_BaudRate);
+ switch (value_Parity)
+ {
+ 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(value_DataBits);
+ switch (value_StopBits)
+ {
+ 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();
+
+ MessageBox.Show("涓插彛鎵撳紑鎴愬姛锛�");
+
+ //淇濆瓨涓插彛鐨勯厤缃俊鎭�
+ saveParams();
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("鎵撳紑涓插彛澶辫触锛�" + ex.Message, "閿欒", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ return;
+ }
+ }
+ else
+ {
+
+ }
+ }
+ #endregion
+
+ #region 鍏抽棴涓插彛
+ public void CloseCom()
+ {
+ if (!ComDevice.IsOpen)
+ {
+ MessageBox.Show("涓插彛鍏抽棴澶辫触锛佸綋鍓嶄覆鍙f湭鎵撳紑锛�");
+ }
+ else
+ {
+ try
+ {
+ Thread.Sleep(100);
+ ComDevice.Close();
+
+ MessageBox.Show("涓插彛鍏抽棴鎴愬姛锛�");
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(ex.Message, "閿欒", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ return;
+ }
+ }
+ }
+ #endregion
+
+ #region 鎺ユ敹鏁版嵁
+ private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
+ {
+ if (HGetWeight == 1)
+ {
+ byte[] ReDatas = new byte[ComDevice.BytesToRead];
+ ComDevice.Read(ReDatas, 0, ReDatas.Length);//璇诲彇鏁版嵁
+ this.AddData(ReDatas);//杈撳嚭鏁版嵁
+ }
+ }
+ #endregion
+
+ #region 澶勭悊鎺ユ敹鍒扮殑鏁版嵁
+ public void AddData(byte[] data)
+ {
+ 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))//鍒ゆ柇鏄惁鏄暟瀛�
+ {
+ //淇濈暀涓�浣嶅皬鏁�
+ double temp = double.Parse(strArray[i]);
+ temp = Math.Round(temp,1);
+
+ Message += double.Parse(strArray[i]) + ";";
+ }
+ }
+ }
+ }
+ #endregion
+
+ #region 鑾峰彇鏁扮粍涓嚭鐜伴鐜囨渶楂樼殑鏁版嵁
+ private string getMostStr(string message)
+ {
+ string[] strArray = message.Split(';');
+ // 浣跨敤LINQ鏌ヨ鍑虹幇娆℃暟鏈�澶氱殑鍏冪礌
+ var maxElement = strArray.GroupBy(x => x)
+ .OrderByDescending(g => g.Count())
+ .First()
+ .Key;
+
+
+ return maxElement;
+ }
+ #endregion
+
+ #region 璁℃椂鍣ㄨЕ鍙戜簨浠讹紝鐢ㄤ簬鑾峰彇鏃堕棿闂撮殧鍐呮帴鏀跺埌鐨勬暟鎹�
+ private void timer2_Tick(object sender, EventArgs e)
+ {
+ if (timeIndex == 0)
+ {
+ timeIndex = 1;
+ }
+ else
+ {
+ timer2.Enabled = false;
+
+ string str = getMostStr(Message);
+
+ txtHWeightQyt.Text = str;
+
+
+ Message = "";
+ timeIndex = 0;
+ HGetWeight = 0;
+ }
+ }
+ #endregion
+
+ #region 绉伴噸鎸夐挳瑙﹀彂浜嬩欢
+ private void button_getHWeight_Click(object sender, EventArgs e)
+ {
+ HGetWeight = 1;
+ timer2.Enabled = true;
+ }
+ #endregion
+
+
+ #region 淇濆瓨閰嶇疆淇℃伅
+ //鍐欏叆閰嶇疆鍙傛暟
+ private void saveParams()
+ {
+ //鍒ゆ柇鏂囦欢鏄惁瀛樺湪
+ if (!File.Exists(Application.StartupPath + "//Kf_WeightToBarCodeParams.txt"))
+ {
+ FileStream fs1 = new FileStream(Application.StartupPath + "//Kf_WeightToBarCodeParams.txt", FileMode.Create, FileAccess.Write);//鍒涘缓鍐欏叆鏂囦欢
+ StreamWriter sw = new StreamWriter(fs1);
+ sw.WriteLine(value_COM);//寮�濮嬪啓鍏ュ��
+ sw.WriteLine(value_BaudRate);
+
+ sw.WriteLine(value_StopBits);
+
+ sw.WriteLine(value_Parity);
+ sw.WriteLine(value_DataBits);
+ sw.Close();
+ fs1.Close();
+ }
+ else
+ {
+ FileStream fs1 = new FileStream(Application.StartupPath + "//Kf_WeightToBarCodeParams.txt", FileMode.Create, FileAccess.Write);//鍒涘缓鍐欏叆鏂囦欢
+ StreamWriter sw = new StreamWriter(fs1);
+ sw.WriteLine(value_COM);//寮�濮嬪啓鍏ュ��
+ sw.WriteLine(value_BaudRate);
+
+ sw.WriteLine(value_StopBits);
+
+ sw.WriteLine(value_Parity);
+ sw.WriteLine(value_DataBits);
+ sw.Close();
+ fs1.Close();
+ }
+ }
+ #endregion
+
+ #region 璇诲彇閰嶇疆淇℃伅
+ //璇诲彇閰嶇疆鏂囦欢
+ private void readParams()
+ {
+ //鍏抽棴涓插彛
+ //CloseCom();
+
+ if (File.Exists(Application.StartupPath + "//Kf_WeightToBarCodeParams.txt"))
+ {
+ //璇诲彇鏂囦欢鍊煎苟鏄剧ず鍒扮獥浣�
+ FileStream fs = new FileStream(Application.StartupPath + "//Kf_WeightToBarCodeParams.txt", FileMode.Open, FileAccess.ReadWrite);
+ StreamReader sr = new StreamReader(fs);
+ string line = sr.ReadLine();
+ int curLine = 0;
+ while (line != null)
+ {
+ if (++curLine == 1)
+ {
+ value_COM = line;
+ textBox_COM.Text = value_COM;
+ }
+ else if (curLine == 2)
+ {
+ value_BaudRate = line;
+ }
+ else if (curLine == 3)
+ {
+ value_StopBits = line;
+ }
+ else if (curLine == 4)
+ {
+ value_Parity = line;
+ }
+ else if (curLine == 5)
+ {
+ value_DataBits = line;
+ }
+ else
+ {
+
+ }
+ line = sr.ReadLine();
+ }
+ sr.Close();
+ fs.Close();
+ }
+ }
+
+ #endregion
+
+ #endregion
--
Gitblit v1.9.1