duhe
2024-10-31 33253e48c0f67a177cb0e41459b2a2785b49c5aa
WarM/ÌõÂë´òÓ¡/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
{
@@ -26,6 +28,8 @@
        public Int64 HInterID = 0;
        public string HBillNo = "";
        public string HBillType = "1202";
        public Int64 HMaterID = 0;
        public string HBatchNo = "";
        public Int64 HAuxPropID = 0;
        public Int64 HUnitID = 0;
        public Int64 HWhID = 0;
@@ -35,9 +39,10 @@
        public Int64 HSourceEntryID = 0;
        public string HSourceBillNo = "";
        public string HSourceBillType = "";
        public Int64 ProcessExchangeInterID = 0;
        public Int64 HProcessExchangeInterID = 0;
        public Int64 HSupID = 0;
        public double HSourceQty = 0;
        public double HMaterWeight = 0;         //物料克重
        public string HMaker = DBUtility.ClsPub.CurUserName;
        public Int64 HStockOrgID = DBUtility.ClsPub.HOrgID;
        public DateTime HDate = DateTime.Today;
@@ -45,6 +50,8 @@
        public string PrintQtyCtl = "";         //条码打印次数控制
        public string UpdatePrintQtyCtl = "";   //条码打印次数更新
        public string sBarCodeItemID = "";      //条码自增列
        public string HBarCode_Pack = "";       //托条码
        public long HSNo = 0;                   //组托计数用
        public DAL.ClsGy_BarCodeBill_Ctl oBar = new DAL.ClsGy_BarCodeBill_Ctl();
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
        SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
@@ -61,6 +68,9 @@
            grdMain.DataSource = null;
            grdSub.DataSource = null;
            chkHDYFlag.Checked = true;
            //读取 æ‰“开串口的配置信息
            readParams();
        }
        //窗体加载
@@ -107,11 +117,15 @@
            {
                HInterID = DBUtility.ClsPub.CreateBillID_Prod(HBillType, ref DBUtility.ClsPub.sExeReturnInfo);           //得到新单据ID
                HBillNo = DBUtility.ClsPub.CreateBillCode_Prod(HBillType, ref DBUtility.ClsPub.sExeReturnInfo, true);    //得到新单据号
                HSNo = 0;
                this.lblSNo.Text = "";
            }
            else if (DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HBack"]) == 1)
            {
                HInterID = DBUtility.ClsPub.CreateBillID_Prod(HBillType, ref DBUtility.ClsPub.sExeReturnInfo);           //得到新单据ID
                HBillNo = DBUtility.ClsPub.CreateBillCode_Prod(HBillType, ref DBUtility.ClsPub.sExeReturnInfo, true);    //得到新单据号
                HSNo = 0;
                this.lblSNo.Text = "";
            }
            else
            {
@@ -123,7 +137,7 @@
            if (grdSub.Rows.Count > 0)
            {
                grdSub.Rows[0].Selected = true;
                SourceBill();
                SourceBill(1);
                txtHQty.Focus();
            }
            else
@@ -313,12 +327,18 @@
            bool b = false;
            for (int i = 0; i < grdMain.RowCount; i++)
            {
                    b = true;
                    break;
                b = true;
                break;
            }
            if (b == false)
            {
                MessageBox.Show("当前称重记录列表无数据,不允许换班生成单据!", "提示");
                return false;
            }
            if (DBUtility.ClsPub.isStrNull(txtHBarCode_Pack.Text) != "")
            {
                MessageBox.Show("当前组托未结束,请结束组托后再点击换班!", "提示");
                return false;
            }
@@ -337,7 +357,16 @@
                oCn.BeginTran();
                //生成出入库单据
                //插入子表
                oCn.RunProc("EXEC h_p_Kf_ProductInBillSub_Insert_New " + HInterID.ToString() + ",'" + HBillNo + "','" + HSourceBillType + "'");
                DataSet ds = oCn.RunProcReturn("EXEC h_p_Kf_ProductInBillSub_Insert_New " + HInterID.ToString() + ",'" + HBillNo + "','" + HSourceBillType + "'", "h_p_Kf_ProductInBillSub_Insert_New");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                }
                else if (DBUtility.ClsPub.isStrNull(ds.Tables[0].Rows[0][0]) != "OK")
                {
                    oCn.RollBack();
                    MessageBox.Show("生成入库单失败,当前称重记录列表无数据,不允许换班生成单据!", "提示");
                    return false;
                }
                //插入主表
                oCn.RunProc("Insert Into Kf_ICStockBillMain " +
                    "(HBillType,HBillSubType,HInterID,HBillNo,HDate,HMainSourceBillType" +
@@ -353,6 +382,8 @@
                    + ", " + HStockOrgID.ToString() + "," + HStockOrgID.ToString() + ") ");
                //更新关联数量
                oCn.RunProc("exec h_p_Sc_UpDateRelation_ICMOToProductIn_Add " + HInterID.ToString());
                //回填已生单状态
                oCn.RunProc("Update KF_WeighSource_Temp set HRelationInterID=1 where HInterID= " + HInterID.ToString());
                MessageBox.Show("生成生产入库单:" + HBillNo + " æˆåŠŸï¼", "提示");
                oCn.Commit();
                return true;
@@ -371,7 +402,13 @@
        private void tc_Click(object sender, EventArgs e)
        {
            this.Close();
            //退出时关闭串口
            if (ComDevice != null)
            {
                CloseCom();
            }
            this.Close();
        }
        #endregion
@@ -390,8 +427,7 @@
                }
                Source_Display();
                grdSub.Rows[0].Selected = true;
                DBUtility.Xt_BaseBillFun.Sub_ClearBill(gbUp);
                SourceBill();
                SourceBill(2);
                txtHBarCode.Text = "";
                txtHQty.Focus();
            }
@@ -407,8 +443,7 @@
            }
            Source_Display();
            grdSub.Rows[0].Selected = true;
            DBUtility.Xt_BaseBillFun.Sub_ClearBill(gbUp);
            SourceBill();
            SourceBill(2);
            txtHBarCode.Text = "";
            txtHQty.Focus();
        }
@@ -439,11 +474,6 @@
        private void cmdSourceBillNo_Click(object sender, EventArgs e)
        {
            //初始化右边表头信息
            cmbHBarCodeType.Items.Clear();
            cmbHBarCodeType.Items.Add("唯一条码");
            DBUtility.Xt_BaseBillFun.Sub_ClearBill(gbUp);
            if (grdSub.CurrentRow == null)
            {
                MessageBox.Show("请在左边单据列表中选择一行单据!", "提示");
@@ -456,7 +486,7 @@
            }
            else
            {
                SourceBill();
                SourceBill(3);
                txtHQty.Focus();
            }
        }
@@ -473,6 +503,10 @@
            }
            SaveBarCode();
            BarCode_Display();
            if (DBUtility.ClsPub.isStrNull(txtHBarCode_Pack.Text) != "")
            {
                this.lblSNo.Text = "当前已组 " + HSNo + " ä»¶";
            }
            grdMain.Rows[0].Selected = true;
            sBarCodeItemID = DBUtility.ClsPub.isLong(grdMain.Rows[grdMain.SelectedRows[0].Index].Cells[Fun_GetCol("HItemID")].Value).ToString();
@@ -511,6 +545,17 @@
        //条码生成前判断
        private bool Sub_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或单据号失败,请重新刷新界面!", "提示");
@@ -524,6 +569,11 @@
            else if (DBUtility.ClsPub.isDoule( txtHWeightQyt.Text) == 0)
            {
                MessageBox.Show("请输入正确重量!", "提示");
                return false;
            }
            else if (DBUtility.ClsPub.isDoule(txtHQty.Text)- DBUtility.ClsPub.isDoule(txtHKDQty.Text) <= 0)
            {
                MessageBox.Show("输入的扣点数不能大于米数!", "提示");
                return false;
            }
            else if (DBUtility.ClsPub.isInt(txtHBarcodeNo.Text) == 0)
@@ -541,7 +591,58 @@
                MessageBox.Show("物料默认仓库启用了仓位,但未设置仓位,获取仓位信息失败,请先设置物料默认仓位!", "提示");
                return false;
            }
            return true;
            //实际克重=重量/米数*1000,实际克重超出物料维护克重的百分之二十或不足物料维护克重的百分之八十,预警提示
            double sQty = 0;    //实际克重
            sQty = DBUtility.ClsPub.isDoule(txtHWeightQyt.Text) / DBUtility.ClsPub.isDoule(txtHQty.Text) * 1000;
            if (sQty > HMaterWeight * 1.2 || sQty < HMaterWeight * 0.8)
            {
                if (MessageBox.Show("物料维护克重为:"+HMaterWeight.ToString()+ ",实际克重为:" + sQty.ToString() + ",所输入实际克重超出物料维护克重的百分之二十或不足物料维护克重的百分之八十,请检查输入的数据是否正确!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    //return true;
                }
                else
                {
                    return false;
                }
            }
            //条码生成前控制判断
            double HNowQty = 0;     //米数
            double HWeightQyt = 0;  //重量
            double HKDQty = 0;      //扣点数
            HNowQty = DBUtility.ClsPub.isDoule(txtHQty.Text);
            HWeightQyt = DBUtility.ClsPub.isDoule(txtHWeightQyt.Text);
            HKDQty = DBUtility.ClsPub.isDoule(txtHKDQty.Text);
            DataSet oDsCheck = oCn.RunProcReturn("exec h_p_Kf_WeighToBarCode_AddBarCodeCheck " + HInterID.ToString() + "," + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + "," + HProcessExchangeInterID.ToString() + "," + HSourceQty.ToString() + "," + HNowQty.ToString() + "," + HWeightQyt.ToString() + "," + HKDQty.ToString(), "h_p_Kf_WeighToBarCode_AddBarCodeCheck");
            //
            if (oDsCheck == null && oDsCheck.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("条码生成前判断错误!");
                return false;
            }
            else if (DBUtility.ClsPub.isStrNull(oDsCheck.Tables[0].Rows[0][0]) == "1")
            {
                MessageBox.Show(DBUtility.ClsPub.isStrNull(oDsCheck.Tables[0].Rows[0]["HRemark"]), "提示");
                return false;
            }
            else if (DBUtility.ClsPub.isStrNull(oDsCheck.Tables[0].Rows[0][0]) == "2")
            {
                if (MessageBox.Show(DBUtility.ClsPub.isStrNull(oDsCheck.Tables[0].Rows[0]["HRemark"]), "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        //生成条码
@@ -600,58 +701,68 @@
            string HBarCode = "";
            string HBarCodeType = "";
            double HQty = 0;            //米数
            double HNowQty = 0;         //米数
            double HMaterialJQty = 0;   //重量
            double HMaterialMQty = 0;   //扣点数
            double HQty = 0;            //数量
            Int64 HDeptID = 0;
            string HRemark = "";
            string HMaterName = "";
            string HMaterModel = "";
            string ProcessExchangeBillNo = "";
            string HProcessExchangeBillNo = "";
            Int64 HBarcodeNo = 0;       //包数
            Int64 HBadReasonID = 0;     //不良原因ID
            string HCusMaterName = "";  //客户物料名称
            string HCusModel = "";      //客户规格型号
            HBarCode = TM;
            HBarCodeType = DBUtility.ClsPub.isStrNull(cmbHBarCodeType.Text);
            HQty = DBUtility.ClsPub.isDoule(txtHQty.Text);
            HNowQty = DBUtility.ClsPub.isDoule(txtHQty.Text);
            HMaterialJQty = DBUtility.ClsPub.isDoule(txtHWeightQyt.Text);
            HMaterialMQty = DBUtility.ClsPub.isDoule(txtHKDQty.Text);
            HQty = HNowQty- HMaterialMQty;
            HDeptID = DBUtility.ClsPub.isLong(txtHDeptName.Tag);
            HRemark = DBUtility.ClsPub.isStrNull(txtHRemark.Text);
            HMaterName = DBUtility.ClsPub.isStrNull(txtHMaterName.Text);
            HMaterModel = DBUtility.ClsPub.isStrNull(txtHMaterModel.Text);
            ProcessExchangeBillNo = DBUtility.ClsPub.isStrNull(txtHBillNo.Text);
            HProcessExchangeBillNo = DBUtility.ClsPub.isStrNull(txtHBillNo.Text);
            HBarcodeNo = DBUtility.ClsPub.isLong(txtHBarcodeNo.Text);
            HBadReasonID = DBUtility.ClsPub.isLong(txtHRemark.Tag);
            HCusMaterName = DBUtility.ClsPub.isStrNull(txtHCusMaterName.Text);
            HCusModel = DBUtility.ClsPub.isStrNull(txtHCusModel.Text);
            if (HBarCode_Pack != "")
            {
                HSNo = HSNo + 1;
            }
            try
            {
                oCn.BeginTran();
                //写入条码档案
                //写入条码档案    (客户物料名称HCusMaterName存入HMaterialModel字段,客户规格型号HCusModel存入HCusModel字段,不良原因ID存入HCustomQty1字段)
                oCn.RunProc("Insert into Gy_BarCodeBill " +
                "(HBarCode,HBarCodeType,HMaterID,HUnitID,HQty" +
                ",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 " +
                ",HSTOCKORGID,HOWNERID,HSeOrderBillNo,HInterID,HGiveAwayFlag " +
                ",HOrderInterID,HOrderEntryID,HOrderBillNo,HCustomQty1 " +
                ",HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo,HInnerBillNo " +
                ",HCusID,HCusType,HEndDate,HWorkLineName,HBarCodeDate,HMaterialJQty,HMaterialMQty,HNowQty " +
                ",HSTOCKORGID,HOWNERID,HSeOrderBillNo,HInterID,HGiveAwayFlag,HBarCode_Pack " +
                ",HOrderInterID,HOrderEntryID,HOrderBillNo,HCustomQty1,HMaterialModel,HCusModel " +
                ",HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo,HInnerBillNo,HLogo,HPackflag " +
                ") Values ("
                + "'" + HBarCode + "','" + HBarCodeType + "'," + HMaterID.ToString() + "," + HUnitID.ToString() + "," + HQty.ToString()
                + ",'" + HBatchNo + "'," + HSupID.ToString() + ",0,'" + HMaker + "',getdate(),0," + HQty.ToString()
                + ", " + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + ",'" + HSourceBillNo + "','" + HSourceBillType + "',''"
                + ", 1," + HBarcodeNo.ToString() + "," + HDeptID.ToString() + "," + HWhID.ToString() + "," + HSPID.ToString() + ",'" + HRemark + "'"
                + ", 0,'',getdate(),'','" + sDate + "'," + HMaterialJQty.ToString() + "," + HMaterialMQty.ToString()
                + ", " + HStockOrgID.ToString() + "," + HStockOrgID.ToString() + ",''," + HInterID.ToString() + ",0"
                + ", " + ProcessExchangeInterID.ToString() + ",0,'" + ProcessExchangeBillNo + "'," + HBadReasonID.ToString()
                + ",'" + HMaterName + "','" + HMaterModel + "',''," + HAuxPropID.ToString() + ",'','')");
                + ", 0,'',getdate(),'','" + sDate + "'," + HMaterialJQty.ToString() + "," + HMaterialMQty.ToString() + "," + HNowQty.ToString()
                + ", " + HStockOrgID.ToString() + "," + HStockOrgID.ToString() + ",''," + HInterID.ToString() + ",0,'" + HBarCode_Pack + "'"
                + ", " + HProcessExchangeInterID.ToString() + ",0,'" + HProcessExchangeBillNo + "'," + HBadReasonID.ToString() + ",'" + HCusMaterName + "','" + HCusModel + "'"
                + ",'" + HMaterName + "','" + HMaterModel + "',''," + HAuxPropID.ToString() + ",'','','1',0)");
                //回填最大流水号
                oCn.RunProc("exec h_p_WMS_SetMaxNo_QTY '" + sTMNumber + "',1 ");
                //回填源单临时表HInterID
                oCn.RunProc("update a set a.HInterID=" + HInterID.ToString() + ",a.HBillNo='" + HBillNo + "' from KF_WeighSource_Temp a where a.HInterID<>" + HInterID.ToString() + " and a.HMaker='" + HMaker + "' and convert(varchar(10),a.HMakeDate,120)=convert(varchar(10),getdate(),120)");
                oCn.RunProc("update a set a.HInterID=" + HInterID.ToString() + ",a.HBillNo='" + HBillNo +"',a.HRelationInterID= 0 from KF_WeighSource_Temp a where a.HInterID<>" + HInterID.ToString() + " and a.HMaker='" + HMaker + "' and a.HSourceBillNo='" + HProcessExchangeBillNo + "'");
                //写入条码出入库临时表
                oCn.RunProc("Insert into KF_PonderationBillMain_Temp " +
@@ -665,8 +776,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)");
@@ -684,9 +795,53 @@
        #region  //返回信息
        //返回源单列表信息
        private void SourceBill()
        //返回源单列表信息(num=1,打开模块时加载显示;num=2,扫流转卡显示;num=3,选单按钮显示)
        private void SourceBill(int num)
        {
            //打开模块时加载显示
            if (num == 1)
            {
                txtHBarCode_Pack.Text = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HBarCode_Pack")].Value);
                HBarCode_Pack = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HBarCode_Pack")].Value);
                HSNo = DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HSNo")].Value);
                if (HBarCode_Pack == "")
                {
                    this.lblSNo.Text = "";
                }
                else
                {
                    this.lblSNo.Text = "当前已组 " + HSNo + " ä»¶";
                    cmdHBPack.Enabled = false;
                }
            }
            //扫流转卡显示
            if (num == 2)
            {
                //初始化右边表头信息
                DBUtility.Xt_BaseBillFun.Sub_ClearBill(gbUp);
                if (HBarCode_Pack != "")
                {
                    txtHBarCode_Pack.Text = HBarCode_Pack;
                    this.lblSNo.Text = "当前已组 " + HSNo + " ä»¶";
                }
            }
            //选单按钮显示
            if (num == 3)
            {
                if (HBarCode_Pack != "" && (HMaterID != DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HMaterID")].Value)
                    || HBatchNo != DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("批号")].Value)))
                {
                    MessageBox.Show("所扫描流转卡对应物料、批号与当前正在组托物料、批号不一致,不同物料、批号不允许组托在同一托上,如需换物料生成条码请先结束当前组托!", "提示");
                    return;
                }
                //初始化右边表头信息
                DBUtility.Xt_BaseBillFun.Sub_ClearBill(gbUp);
                if (HBarCode_Pack != "")
                {
                    txtHBarCode_Pack.Text = HBarCode_Pack;
                    this.lblSNo.Text = "当前已组 " + HSNo + " ä»¶";
                }
            }
            txtHBillNo.Text = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("单据号")].Value);
            txtHDeptName.Tag = DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HDeptID")].Value);
            txtHDeptName.Text = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("车间")].Value);
@@ -695,6 +850,10 @@
            txtHMaterName.Text = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("物料名称")].Value);
            txtHMaterModel.Text = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("规格型号")].Value);
            txtHBatchNo.Text = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("批号")].Value);
            txtHCusMaterName.Text = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HCusMaterName")].Value);
            txtHCusModel.Text = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HCusModel")].Value);
            HMaterID = DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HMaterID")].Value);
            HBatchNo = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("批号")].Value);
            HAuxPropID = DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HAuxPropID")].Value);
            HUnitID = DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HUnitID")].Value);
            HWhID = DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HWhID")].Value);
@@ -705,8 +864,10 @@
            HSourceBillNo = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("生产订单")].Value);
            HSourceBillType = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HSourceBillType")].Value);
            HSourceQty = DBUtility.ClsPub.isDoule(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("数量")].Value);
            ProcessExchangeInterID = DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("ProcessExchangeInterID")].Value);
            HProcessExchangeInterID = DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("ProcessExchangeInterID")].Value);
            HSupID = DBUtility.ClsPub.isLong(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HSupID")].Value);
            HMaterWeight = DBUtility.ClsPub.isDoule(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("HMaterWeight")].Value);
            txtHOrderPickRemark.Text = DBUtility.ClsPub.isStrNull(grdSub.SelectedRows[0].Cells[Fun_GetSubCol("订单包装备注")].Value);
        }
        //返回源单列表信息
@@ -772,6 +933,7 @@
                    string HItemID = "";
                    string HBarCode = "";
                    string HBarCode2 = "";
                    long HSNo_Temp = HSNo;
                    for (int i = 0; i <= grdMain.SelectedRows.Count - 1; i++)
                    {
                        HItemID = HItemID + "," + DBUtility.ClsPub.isLong(grdMain.SelectedRows[i].Cells[Fun_GetCol("hmainid")].Value.ToString());
@@ -788,9 +950,35 @@
                        {
                            HRemark = HRemark + "、" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条码编号")].Value);
                        }
                        //更新当前已组托件数
                        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
                            && DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条码类型")].Value) != "托盘条码")
                        {
                            HSNo = HSNo - 1;
                        }
                        if (DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条码类型")].Value) == "托盘条码")
                        {
                            if (DBUtility.ClsPub.isBool(grdMain.SelectedRows[i].Cells[Fun_GetCol("HPackflag")].Value))
                            {
                                MessageBox.Show("所选条码编号:" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条码编号")].Value) + " ä¸ºæ‰˜ç›˜æ¡ç ï¼Œä¸”已组托生成对应组托单,不允许作废!", "提示");
                                return;
                            }
                            else if (HSNo > 0)
                            {
                                MessageBox.Show("所选条码编号:" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条码编号")].Value) + " ä¸ºæ‰˜ç›˜æ¡ç ï¼Œä¸”当前托盘条码下已生成对应条码,不允许作废!", "提示");
                                return;
                            }
                            else
                            {
                                HBarCode_Pack = "";
                            }
                        }
                    }
                    if (HRemark != "")
                    {
                        HSNo = HSNo_Temp;
                        HBarCode_Pack = DBUtility.ClsPub.isStrNull(txtHBarCode_Pack.Text);
                        MessageBox.Show("所选条码编号:"+ HRemark.Remove(0, 1)+" å·²ä½œåºŸï¼Œä¸å…è®¸é‡å¤ä½œåºŸï¼", "提示");
                        return;
                    }
@@ -818,11 +1006,23 @@
                    catch (Exception e2)
                    {
                        oCn.RollBack();
                        HSNo = HSNo_Temp;
                        HBarCode_Pack = DBUtility.ClsPub.isStrNull(txtHBarCode_Pack.Text);
                        MessageBox.Show("作废条码失败!" + e2.Message, "提示");
                        return;
                    }
                    //刷新称重记录列表信息
                    BarCode_Display();
                    if (HBarCode_Pack != "")
                    {
                        this.lblSNo.Text = "当前已组 " + HSNo + " ä»¶";
                    }
                    else
                    {
                        txtHBarCode_Pack.Text = "";
                        this.lblSNo.Text = "";
                        cmdHBPack.Enabled = true;
                    }
                    MessageBox.Show("所选条码编号:" + HBarCode.Remove(0, 1) + " ä½œåºŸæˆåŠŸï¼", "提示");
                }
            }
@@ -850,6 +1050,27 @@
                    string HItemID = "";
                    string HBarCode = "";
                    string HBarCode2 = "";
                    long HSNo_Temp = HSNo;
                    bool HFlag = false;
                    //界面上为未启用组托状态
                    if (HBarCode_Pack == "")
                    {
                        for (int i = 0; i <= grdMain.SelectedRows.Count - 1; i++)
                        {
                            //判断条码是否为托盘条码,未生成组托单,已作废状态,界面上为未组托状态
                            if (DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条码类型")].Value) == "托盘条码"
                                && DBUtility.ClsPub.isBool(grdMain.SelectedRows[i].Cells[Fun_GetCol("HPackflag")].Value) == false
                                && DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("作废标记")].Value) != ""
                                )
                            {
                                HFlag = true;
                                HBarCode_Pack = DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("HBarCode_Pack")].Value);
                                break;
                            }
                        }
                    }
                    for (int i = 0; i <= grdMain.SelectedRows.Count - 1; i++)
                    {
                        HItemID = HItemID + "," + DBUtility.ClsPub.isLong(grdMain.SelectedRows[i].Cells[Fun_GetCol("hmainid")].Value.ToString());
@@ -866,9 +1087,44 @@
                        {
                            HRemark = HRemark + "、" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条码编号")].Value);
                        }
                        //条码为组托条码
                        if (DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("HBarCode_Pack")].Value) != "")
                        {
                            //已上传生成组托单
                            if (DBUtility.ClsPub.isBool(grdMain.SelectedRows[i].Cells[Fun_GetCol("HPackflag")].Value))
                            {
                            }
                            //未上传生成组托单
                            else
                            {
                                //条码对应托条码和界面上的托条码一致
                                if (DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("HBarCode_Pack")].Value) == HBarCode_Pack)
                                {
                                    //更新当前已组托件数
                                    if (DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条码类型")].Value) != "托盘条码")
                                    {
                                        HSNo = HSNo + 1;
                                    }
                                }
                                //条码对应托条码和界面上的托条码不一致
                                else
                                {
                                    MessageBox.Show("所选条码编号:" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("条码编号")].Value) + " å¯¹åº”托条码:" + DBUtility.ClsPub.isStrNull(grdMain.SelectedRows[i].Cells[Fun_GetCol("HBarCode_Pack")].Value) + " å·²ç”Ÿæˆç»„托单,或已作废,不允许反作废!", "提示");
                                    return;
                                }
                            }
                        }
                    }
                    if (HRemark != "")
                    {
                        HSNo = HSNo_Temp;
                        if(HFlag)
                        {
                            HBarCode_Pack = "";
                        }
                        MessageBox.Show("所选条码编号:" + HRemark.Remove(0, 1) + " æœªä½œåºŸï¼Œä¸å…è®¸åä½œåºŸï¼", "提示");
                        return;
                    }
@@ -899,12 +1155,12 @@
                        ",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'" +
                        ",0,0,'',0" +
                        "from Gy_BarCodeBill with(nolock) where HItemID in (" + HItemID + ")"
                        "from Gy_BarCodeBill with(nolock) where HItemID in (" + HItemID + ") and HBarCodeType='唯一条码' "
                        );
                        //写入系统日志
@@ -917,11 +1173,28 @@
                    catch (Exception e2)
                    {
                        oCn.RollBack();
                        HSNo = HSNo_Temp;
                        if (HFlag)
                        {
                            HBarCode_Pack = "";
                        }
                        MessageBox.Show("反作废条码失败!" + e2.Message, "提示");
                        return;
                    }
                    //刷新称重记录列表信息
                    BarCode_Display();
                    if (HBarCode_Pack != "")
                    {
                        txtHBarCode_Pack.Text = HBarCode_Pack;
                        this.lblSNo.Text = "当前已组 " + HSNo + " ä»¶";
                        cmdHBPack.Enabled = false;
                    }
                    else
                    {
                        txtHBarCode_Pack.Text = "";
                        this.lblSNo.Text = "";
                        cmdHBPack.Enabled = true;
                    }
                    MessageBox.Show("所选条码编号:" + HBarCode.Remove(0, 1) + " åä½œåºŸæˆåŠŸï¼", "提示");
                }
            }
@@ -943,9 +1216,560 @@
        #endregion
        #region  //开始组托
        private void cmdHBPack_Click(object sender, EventArgs e)
        {
            if (!PackSub_AllowSave())
            {
                return;
            }
            HSNo = 0;
            SavePackBarCode();
            BarCode_Display();
            this.lblSNo.Text = "当前已组 " + HSNo + " ä»¶";
            grdMain.Rows[0].Selected = true;
            txtHBarCode_Pack.Text = DBUtility.ClsPub.isStrNull(grdMain.Rows[grdMain.SelectedRows[0].Index].Cells[Fun_GetCol("条码编号")].Value);
            HBarCode_Pack = 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;
            }
            if (HProcessExchangeInterID == 0)
            {
                MessageBox.Show("请先扫描流转卡号!", "提示");
                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' + ç»„织代码 + å¹´ + æœˆ + æ—¥
            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 = "";
            string HMaterID = "";       //物料内码
            string HBatchNo = "";       //批次
            string HMaterName = "";
            string HMaterModel = "";
            string HProcessExchangeBillNo = "";
            string HCusMaterName = "";  //客户物料名称
            string HCusModel = "";      //客户规格型号
            HBarCode = TM;
            HBarCodeType = "托盘条码";
            HMaterID = DBUtility.ClsPub.isStrNull(txtHMaterNumber.Tag);
            HBatchNo = DBUtility.ClsPub.isStrNull(txtHBatchNo.Text);
            HMaterName = DBUtility.ClsPub.isStrNull(txtHMaterName.Text);
            HMaterModel = DBUtility.ClsPub.isStrNull(txtHMaterModel.Text);
            HProcessExchangeBillNo = DBUtility.ClsPub.isStrNull(txtHBillNo.Text);
            HCusMaterName = DBUtility.ClsPub.isStrNull(txtHCusMaterName.Text);
            HCusModel = DBUtility.ClsPub.isStrNull(txtHCusModel.Text);
            try
            {
                oCn.BeginTran();
                //写入条码档案
                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,HPackflag " +
                ") Values ("
                + "'" + HBarCode + "','" + HBarCodeType + "'," + HMaterID.ToString() + "," + HUnitID.ToString() + ",1,'" + HBarCode + "'"
                + ",'" + HBatchNo + "'," + HSupID.ToString() + ",0,'" + HMaker + "',getdate(),0,1"
                + ", " + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + ",'" + HSourceBillNo + "','" + HSourceBillType + "',''"
                + ",1,0,0,0,0,''"
                + ", 0,'',getdate(),'','" + sDate + "',0,0,0"
                + ", " + HStockOrgID.ToString() + "," + HStockOrgID.ToString() + ",''," + HInterID.ToString() + ",0"
                + ", " + HProcessExchangeInterID.ToString() + ",0,'" + HProcessExchangeBillNo + "',0,'" + HCusMaterName + "','" + HCusModel + "'"
                + ",'" + HMaterName + "','" + HMaterModel + "',''," + HAuxPropID.ToString() + ",'','','1',0)");
                //回填最大流水号
                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)
        {
            //生成组托单前判断
            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
                    && DBUtility.ClsPub.isStrNull(grdMain.Rows[i].Cells[Fun_GetCol("作废标记")].Value) == "")
                {
                    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 and HBarCodeType='唯一条码' "
                );
                //回填条码档案组托标记
                oCn.RunProc("update Gy_BarCodeBill set HPackflag=1 where HInterID=" + HInterID.ToString() + " and HBarCode_Pack='" + HBarCode_Pack + "' and HStopflag=0", ref DBUtility.ClsPub.sExeReturnInfo);
                MessageBox.Show("生成生产组托单:" + HPUBillNo + " æˆåŠŸï¼", "提示");
                txtHBarCode_Pack.Text = "";
                HBarCode_Pack = "";
                cmdHBPack.Enabled = true;
                HSNo = 0;
                this.lblSNo.Text = "";
                BarCode_Display();
                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("串口关闭失败!当前串口未打开!");
            }
            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