duhe
2024-10-31 33253e48c0f67a177cb0e41459b2a2785b49c5aa
WarM/ÌõÂë´òÓ¡/Gy_BarCodeBill_automaticallyByPLC_New.cs
@@ -13,6 +13,8 @@
using System.Drawing.Printing;
using System.IO;
using System.Management;
using System.Net.NetworkInformation;
using System.Printing;
namespace WarM
{
@@ -406,9 +408,40 @@
        #region ç”ŸæˆæŒ‰é’®
        private void bc_Click(object sender, EventArgs e)
        {
            //审核权限
            if (!DBUtility.ClsPub.Security_Log_second("Gy_BarCodeBill_automaticallyByPLC_ProduceByHand", 1, false, DBUtility.ClsPub.CurUserName))
            {
                MessageBox.Show("生成失败,无权限!");
                return;
            }
            //清除打印任务
            string msg = "";
            if (!clearPrinterTask(ref msg))
            {
                MessageBox.Show(msg);
                return;
            }
            this.Sub_SaveBill();
            Display();
            if (radioButton_IsPrint_Yes.Checked == true)
            {
                //设置打印模板,打印
                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();
        }
        //保存单据
@@ -1066,6 +1099,8 @@
                Thread thread = new Thread(ReceiveMess);
                thread.Start();
                MessageBox.Show("连接成功!");
                timer3.Enabled = true;
            }
            catch (Exception ex)
            {
@@ -1099,8 +1134,8 @@
            }
            catch (Exception ex)
            {
                MessageBox.Show("接口异常,已关闭连接:" + ex.Message);
                socket.Close();
                //MessageBox.Show("接口异常,已关闭连接:" + ex.Message);
                //socket.Close();
            }
        }
@@ -1140,9 +1175,43 @@
            return false;
        }
        //清除指定打印机任务
        private bool clearPrinterTask(ref string msg)
        {
            try
            {
                PrintServer localPrintServer = new LocalPrintServer();
                PrintQueue pq = localPrintServer.GetPrintQueue(comboBox_PrinterParams.Text.Replace("(默认)", "").ToLower());
                pq.Refresh();
                PrintJobInfoCollection allPrintJobs = pq.GetPrintJobInfoCollection();
                foreach (PrintSystemJobInfo printJob in allPrintJobs)
                {
                    printJob.Cancel();
                }
                return true;
            }
            catch(Exception ex)
            {
                msg = ex.Message;
                return false;
            }
        }
        //根据通讯信息生成条码并打印
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (produceQty > 0)
            {
                //清除打印任务
                string msg = "";
                if (!clearPrinterTask(ref msg))
                {
                    MessageBox.Show(msg);
                    produceQty = 0;
                }
            }
            for (int i = 0; i < produceQty; i++)
            {
                //生成条码
@@ -1151,18 +1220,21 @@
                //获取需要打印的数据
                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 != "")
                if (radioButton_IsPrint_Yes.Checked == true)
                {
                    Report.Printer.PrinterName = comboBox_PrinterParams.Text.Replace("(默认)", "");
                    //设置打印模板,打印
                    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);
                }
                Report.Print(false);
                if (i == produceQty - 1)
                {
@@ -1253,6 +1325,10 @@
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                timer3.Enabled = false;
            }
        }
        #endregion
@@ -2360,5 +2436,105 @@
            }
        }
        #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
        #region å»ºç«‹é€šè®¯åŽï¼Œä¸æ–­å‘服务器发送数据,用于帮助服务端确定连接状态
        private void timer4_Tick(object sender, EventArgs e)
        {
            if (getTargetInternetStatus()&& socket!=null && socket.Connected)
            {
                try
                {
                    socket.Send(Encoding.ASCII.GetBytes("1"));
                }
                catch (Exception ex)
                {
                }
            }
        }
        #endregion
    }
}