pzy
2024-09-18 c4f47bbec99d71eba138745e7f16c03400d88ad1
WarM/ÌõÂë´òÓ¡/Gy_BarCodeBill_automaticallyByPLC_New.cs
@@ -13,6 +13,7 @@
using System.Drawing.Printing;
using System.IO;
using System.Management;
using System.Net.NetworkInformation;
namespace WarM
{
@@ -223,14 +224,14 @@
                //系统参数,生成条码数量可否超源单数量控制(N为不可超源单数量)
                if (SourceQtyCtl == "N")
                {
                    sWhere = " and HOrgID =" + HOrgID.ToString() + " and ä»»åŠ¡æ•°é‡>0 and ç”Ÿäº§çº¿='" + comboBox_SourceNameParams.Text + "' ";
                    sWhere = " and HOrgID =" + HOrgID.ToString() + " and ä»»åŠ¡æ•°é‡>0 and çŠ¶æ€ = '开工' and ç”Ÿäº§çº¿='" + comboBox_SourceNameParams.Text + "' ";
                }
                else
                {
                    sWhere = " and HOrgID =" + HOrgID.ToString();
                    sWhere = " and HOrgID =" + HOrgID.ToString() + " and çŠ¶æ€ = '开工' and ç”Ÿäº§çº¿='" + comboBox_SourceNameParams.Text + "' "; ;
                }
                DAL.Cls_S_IFCLD_ICMOList oIFCLD_ICMOList = new DAL.Cls_S_IFCLD_ICMOList();
                DAL.Cls_S_IFCLD_ICMOList1 oIFCLD_ICMOList = new DAL.Cls_S_IFCLD_ICMOList1();
                if (oIFCLD_ICMOList.Refresh(sWhere))  //选择原单
                {
                    FillSelectData(oIFCLD_ICMOList.oBillSelectColl);
@@ -408,6 +409,19 @@
        {
            this.Sub_SaveBill();
            Display();
            //设置打印模板,打印
            grdList.Rows[0].Cells[0].Value = "*";
            Report = new GridppReport();
            Report.LoadFromFile(DBUtility.ClsPub.AppPath + @"\" + textBox_PrintModelParams.Text + ".grf");  //here .
            Report.BeforePostRecord += new _IGridppReportEvents_BeforePostRecordEventHandler(ReportBeforePostRecord);
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecordByDataTable);
            Report.PrintEnd += new _IGridppReportEvents_PrintEndEventHandler(ReportPrintEnd);
            if (comboBox_PrinterParams.Text != "")
            {
                Report.Printer.PrinterName = comboBox_PrinterParams.Text.Replace("(默认)", "");
            }
            Report.Print(false);
            Display4();
        }
@@ -966,28 +980,40 @@
            string LIU = "";
            int LEN = 4;
            //获取当日本产品条码编号最大的记录
            DataSet ds;
            string sql = "select * from h_v_IF_BarCodeBillList Where HMaterID = " + grdMain.Rows[0].Cells[HMaterIDCol].Value + " and HSTOCKORGID = " + HOrgID + " and CONVERT(varchar(100),条码日期, 23) = '" + HDate + "' order by æ¡ç ç¼–号 desc";
            ds = oCn.RunProcReturn(sql, "h_v_IF_BarCodeBillList");
            if (ds != null)
            //获取产品条码备份表中当日本产品条码编号最大的记录
            long HMaxNo_Bak = 0;                //记录产品条码备份表中当日本产品条码编号最大的记录的条码编号的流水号
            DataSet ds1;
            string sql1 = "select * from Gy_BarCodeBill_SWELL_SubBarCodeBill Where HMaterID = " + grdMain.Rows[0].Cells[HMaterIDCol].Value + " and HSTOCKORGID = " + HOrgID + " and CONVERT(varchar(100),HBarCodeDate, 23) = '" + HDate + "' and HInterID <>0 order by HBarCode desc";
            ds1 = oCn.RunProcReturn(sql1, "Gy_BarCodeBill_SWELL_SubBarCodeBill");
            if (ds1 != null && ds1.Tables[0].Rows.Count>0)
            {
                long count = 0;
                if (ds.Tables[0].Rows.Count > 0)
                {
                    string HBarCodeMaxNo = ds.Tables[0].Rows[0]["条码编号"].ToString();
                    HBarCodeMaxNo = HBarCodeMaxNo.Substring(HBarCodeMaxNo.Length - LEN, LEN);
                    count = ClsPub.isLong(HBarCodeMaxNo);
                }
                LIU += count + 1;
                while (LIU.Length < LEN)  //如果流水号小于6位数前面补0
                {
                    LIU = "0" + LIU;
                }
                string HBarCodeMaxNo = ds1.Tables[0].Rows[0]["HBarCode"].ToString();
                HBarCodeMaxNo = HBarCodeMaxNo.Substring(HBarCodeMaxNo.Length - LEN, LEN);
                HMaxNo_Bak = ClsPub.isLong(HBarCodeMaxNo);
            }
            else
            ////获取条码档案中当日本产品条码编号最大的记录
            long HMaxNo = 0;                //记录条码档案中当日本产品条码编号最大的记录的条码编号的流水号
            DataSet ds;
            string sql = "select * from h_v_IF_BarCodeBillList Where HMaterID = " + grdMain.Rows[0].Cells[HMaterIDCol].Value + " and HSTOCKORGID = " + HOrgID + " and CONVERT(varchar(100),条码日期, 23) = '" + HDate + "' and HInterID <> 0 order by æ¡ç ç¼–号 desc";
            ds = oCn.RunProcReturn(sql, "h_v_IF_BarCodeBillList");
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return "";
                string HBarCodeMaxNo = ds.Tables[0].Rows[0]["条码编号"].ToString();
                HBarCodeMaxNo = HBarCodeMaxNo.Substring(HBarCodeMaxNo.Length - LEN, LEN);
                HMaxNo = ClsPub.isLong(HBarCodeMaxNo);
            }
            //获取当日本产品 åœ¨æ¡ç æ¡£æ¡ˆå’Œäº§å“æ¡ç å¤‡ä»½è¡¨ä¸­ æœ€å¤§çš„æµæ°´å·
            long count = 0;
            count = HMaxNo >= HMaxNo_Bak ? HMaxNo : HMaxNo_Bak;
            //为流水号前面补零
            LIU += count + 1;
            while (LIU.Length < LEN)  //如果流水号小于6位数前面补0
            {
                LIU = "0" + LIU;
            }
            //拼接批号
@@ -1005,6 +1031,12 @@
        {
            try
            {
                if (socket != null && socket.Connected)
                {
                    MessageBox.Show("已建立连接,不可重复建立连接!");
                    return;
                }
                if (grdMain.Rows.Count == 0 || grdMain.Rows[0].Cells[HMaterIDCol].Value == null || grdMain.Rows[0].Cells[HMaterIDCol].Value.ToString() == "" || grdMain.Rows[0].Cells[HMaterIDCol].Value.ToString() == "0")
                {
                    MessageBox.Show("请选择源单!");
@@ -1048,6 +1080,8 @@
                Thread thread = new Thread(ReceiveMess);
                thread.Start();
                MessageBox.Show("连接成功!");
                timer3.Enabled = true;
            }
            catch (Exception ex)
            {
@@ -1072,13 +1106,17 @@
                    {
                        string RMess = Encoding.UTF8.GetString(buffer, 0, length);
                        produceQty = ClsPub.isLong(RMess);
                        if (produceQty == 0)
                        {
                            produceQty = 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("接口异常,已关闭连接:" + ex.Message);
                socket.Close();
                //MessageBox.Show("接口异常,已关闭连接:" + ex.Message);
                //socket.Close();
            }
        }
@@ -1220,6 +1258,7 @@
                if (socket.Connected)
                {
                    socket.Close();
                    MessageBox.Show("连接关闭成功!");
                }
                else
                {
@@ -1230,6 +1269,10 @@
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                timer3.Enabled = false;
            }
        }
        #endregion
@@ -2337,5 +2380,90 @@
            }
        }
        #region æ–­çº¿é‡è¿ž
        private int maxRepeatConnectTimes = 5;
        private int currentRepeatConnectTimes = 0;
        #region å®šæ—¶å™¨æ£€æµ‹è¿žæŽ¥çŠ¶æ€ã€æ–­çº¿é‡è¿ž
        private void timer3_Tick(object sender, EventArgs e)
        {
            if (currentRepeatConnectTimes < maxRepeatConnectTimes)
            {
                //判断客户端是否可以访问服务器,若不可以访问,关闭socket连接
                if (!getTargetInternetStatus())
                {
                    if (socket != null && socket.Connected)
                    {
                        socket.Close();
                    }
                    currentRepeatConnectTimes += 1;
                }
                else if (getTargetInternetStatus() && !socket.Connected)
                {
                    //若客户端可以访问服务器,socket重新连接服务器
                    try
                    {
                        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        IPAddress iPAddress = IPAddress.Parse(textBox_IPParams.Text);
                        IPEndPoint point = new IPEndPoint(iPAddress, Convert.ToInt32(textBox_PortParams.Text));
                        socket.Connect(point);
                        Thread thread = new Thread(ReceiveMess);
                        thread.Start();
                        currentRepeatConnectTimes = 0;
                    }
                    catch (Exception ex)
                    {
                        currentRepeatConnectTimes += 1;
                    }
                }
            }
            else
            {
                //timer3.Enabled = false;
                currentRepeatConnectTimes = 0;
                MessageBox.Show("连接已断开,请重新连接!!");
            }
        }
        #endregion
        #region èŽ·å–ç›®æ ‡è®¾å¤‡ç½‘ç»œè¿žæŽ¥çŠ¶æ€
        private bool getTargetInternetStatus()
        {
            try
            {
                string targetIp = textBox_IPParams.Text; // æ›¿æ¢ä¸ºç›®æ ‡ç”µè„‘çš„IP地址
                Ping pingSender = new Ping();
                PingOptions options = new PingOptions();
                // ä½¿ç”¨64字节的数据包,‌你可以根据需要调整这个值
                string data = "Hello from Ping!";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeout = 1200; // è¶…时时间,‌单位为毫秒
                PingReply reply = pingSender.Send(targetIp, timeout, buffer, options);
                if (reply.Status == IPStatus.Success)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion
        #endregion
    }
}