yxj
2024-09-20 f34c6a21dc58bc0d40a011d6d7366c5eb3f15f15
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
{
@@ -35,7 +37,7 @@
        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;         //物料克重
@@ -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
@@ -523,6 +534,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或单据号失败,请重新刷新界面!", "提示");
@@ -567,6 +589,38 @@
            {
                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
@@ -574,7 +628,10 @@
                    return false;
                }
            }
            return true;
            else
            {
                return true;
            }
        }
        //生成条码
@@ -641,7 +698,7 @@
            string HRemark = "";
            string HMaterName = "";
            string HMaterModel = "";
            string ProcessExchangeBillNo = "";
            string HProcessExchangeBillNo = "";
            Int64 HBarcodeNo = 0;       //包数
            Int64 HBadReasonID = 0;     //不良原因ID
            string HCusMaterName = "";  //客户物料名称
@@ -657,7 +714,7 @@
            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);
@@ -683,14 +740,14 @@
                + ", 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"
                + ", " + ProcessExchangeInterID.ToString() + ",0,'" + ProcessExchangeBillNo + "'," + HBadReasonID.ToString() + ",'" + HCusMaterName + "','" + HCusModel + "'"
                + ", " + HProcessExchangeInterID.ToString() + ",0,'" + HProcessExchangeBillNo + "'," + HBadReasonID.ToString() + ",'" + HCusMaterName + "','" + HCusModel + "'"
                + ",'" + HMaterName + "','" + HMaterModel + "',''," + HAuxPropID.ToString() + ",'','','1')");
                //回填最大流水号
                oCn.RunProc("exec h_p_WMS_SetMaxNo_QTY '" + sTMNumber + "',1 ");
                //回填源单临时表HInterID
                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='" + ProcessExchangeBillNo + "'");
                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 " +
@@ -746,9 +803,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);
        }
        //返回源单列表信息
@@ -988,8 +1046,304 @@
        #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
    }
}