From 75c5becc5cbf2d00c77082bdbe9bc6a08703ffb1 Mon Sep 17 00:00:00 2001
From: duhe <226547893@qq.com>
Date: 星期二, 23 七月 2024 14:09:23 +0800
Subject: [PATCH] 条码自动生成:断线重连

---
 WarM/条码打印/Gy_BarCodeBill_automaticallyByPLC_New.cs          |   96 +++++++++++++++++++++++++++++++++++++++++++++++-
 WarM/条码打印/Gy_BarCodeBill_automaticallyByPLC_New.resx        |    3 +
 WarM/条码打印/Gy_BarCodeBill_automaticallyByPLC_New.designer.cs |   20 ++++++---
 3 files changed, 110 insertions(+), 9 deletions(-)

diff --git "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.cs" "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.cs"
index c29c99c..9c6f7e7 100644
--- "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.cs"
+++ "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/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
 {
@@ -1066,6 +1067,8 @@
                 Thread thread = new Thread(ReceiveMess);
                 thread.Start();
                 MessageBox.Show("杩炴帴鎴愬姛锛�");
+
+                timer3.Enabled = true;
             }
             catch (Exception ex)
             {
@@ -1099,8 +1102,8 @@
             }
             catch (Exception ex)
             {
-                MessageBox.Show("鎺ュ彛寮傚父锛屽凡鍏抽棴杩炴帴锛�" + ex.Message);
-                socket.Close();
+                //MessageBox.Show("鎺ュ彛寮傚父锛屽凡鍏抽棴杩炴帴锛�" + ex.Message);
+                //socket.Close();
             }
         }
 
@@ -1253,6 +1256,10 @@
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
+            }
+            finally
+            {
+                timer3.Enabled = false;
             }
         }
         #endregion
@@ -2360,5 +2367,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
+            {
+                timer1.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
+
+        
     }
 }
\ No newline at end of file
diff --git "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.designer.cs" "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.designer.cs"
index cc005aa..906b851 100644
--- "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.designer.cs"
+++ "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.designer.cs"
@@ -147,6 +147,7 @@
             this.printDialog1 = new System.Windows.Forms.PrintDialog();
             this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
             this.timer2 = new System.Windows.Forms.Timer(this.components);
+            this.timer3 = new System.Windows.Forms.Timer(this.components);
             this.Tool.SuspendLayout();
             this.P1.SuspendLayout();
             this.tabControl_MainInfo.SuspendLayout();
@@ -887,7 +888,7 @@
             this.tabPage_ParamsInfo.Location = new System.Drawing.Point(4, 28);
             this.tabPage_ParamsInfo.Name = "tabPage_ParamsInfo";
             this.tabPage_ParamsInfo.Padding = new System.Windows.Forms.Padding(3);
-            this.tabPage_ParamsInfo.Size = new System.Drawing.Size(1387, 314);
+            this.tabPage_ParamsInfo.Size = new System.Drawing.Size(1786, 314);
             this.tabPage_ParamsInfo.TabIndex = 1;
             this.tabPage_ParamsInfo.Text = "閰嶇疆淇℃伅";
             // 
@@ -1343,7 +1344,7 @@
             this.tabPage3.Location = new System.Drawing.Point(4, 5);
             this.tabPage3.Margin = new System.Windows.Forms.Padding(4);
             this.tabPage3.Name = "tabPage3";
-            this.tabPage3.Size = new System.Drawing.Size(1398, 288);
+            this.tabPage3.Size = new System.Drawing.Size(1796, 288);
             this.tabPage3.TabIndex = 2;
             this.tabPage3.Text = "妗f鍒楄〃";
             this.tabPage3.UseVisualStyleBackColor = true;
@@ -1363,7 +1364,7 @@
             this.grdList.ReadOnly = true;
             this.grdList.RowHeadersWidth = 30;
             this.grdList.RowTemplate.Height = 23;
-            this.grdList.Size = new System.Drawing.Size(1398, 288);
+            this.grdList.Size = new System.Drawing.Size(1796, 288);
             this.grdList.TabIndex = 46;
             // 
             // tabPage2
@@ -1373,7 +1374,7 @@
             this.tabPage2.Margin = new System.Windows.Forms.Padding(4);
             this.tabPage2.Name = "tabPage2";
             this.tabPage2.Padding = new System.Windows.Forms.Padding(4);
-            this.tabPage2.Size = new System.Drawing.Size(1398, 288);
+            this.tabPage2.Size = new System.Drawing.Size(1796, 288);
             this.tabPage2.TabIndex = 1;
             this.tabPage2.Text = "鏉$爜淇℃伅";
             this.tabPage2.UseVisualStyleBackColor = true;
@@ -1393,7 +1394,7 @@
             this.grdSub.ReadOnly = true;
             this.grdSub.RowHeadersWidth = 30;
             this.grdSub.RowTemplate.Height = 23;
-            this.grdSub.Size = new System.Drawing.Size(1390, 280);
+            this.grdSub.Size = new System.Drawing.Size(1788, 280);
             this.grdSub.TabIndex = 45;
             // 
             // tabPage1
@@ -1449,7 +1450,7 @@
             this.tabPage4.Location = new System.Drawing.Point(4, 5);
             this.tabPage4.Name = "tabPage4";
             this.tabPage4.Padding = new System.Windows.Forms.Padding(3);
-            this.tabPage4.Size = new System.Drawing.Size(1398, 288);
+            this.tabPage4.Size = new System.Drawing.Size(1796, 288);
             this.tabPage4.TabIndex = 3;
             this.tabPage4.Text = "宸ュ崟鏉$爜";
             this.tabPage4.UseVisualStyleBackColor = true;
@@ -1469,7 +1470,7 @@
             this.grdBillBarCodeList.ReadOnly = true;
             this.grdBillBarCodeList.RowHeadersWidth = 30;
             this.grdBillBarCodeList.RowTemplate.Height = 23;
-            this.grdBillBarCodeList.Size = new System.Drawing.Size(1392, 282);
+            this.grdBillBarCodeList.Size = new System.Drawing.Size(1790, 282);
             this.grdBillBarCodeList.TabIndex = 47;
             // 
             // pageSetupDialog1
@@ -1496,6 +1497,10 @@
             // 
             this.timer2.Enabled = true;
             this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
+            // 
+            // timer3
+            // 
+            this.timer3.Tick += new System.EventHandler(this.timer3_Tick);
             // 
             // Gy_BarCodeBill_automaticallyByPLC_New
             // 
@@ -1669,5 +1674,6 @@
         private System.Windows.Forms.PrintDialog printDialog1;
         private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
         private System.Windows.Forms.Timer timer2;
+        private System.Windows.Forms.Timer timer3;
     }
 }
\ No newline at end of file
diff --git "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.resx" "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.resx"
index e5abdc0..081423d 100644
--- "a/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.resx"
+++ "b/WarM/\346\235\241\347\240\201\346\211\223\345\215\260/Gy_BarCodeBill_automaticallyByPLC_New.resx"
@@ -760,6 +760,9 @@
   <metadata name="timer2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>1058, 17</value>
   </metadata>
+  <metadata name="timer3.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>1177, 17</value>
+  </metadata>
   <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
     <value>64</value>
   </metadata>

--
Gitblit v1.9.1