From 2e4f53844fc90e5b2603c83256488e73fa3a1e1a Mon Sep 17 00:00:00 2001 From: wtt <1985833171@qq.com> Date: 星期四, 01 八月 2024 11:24:23 +0800 Subject: [PATCH] 1 --- WarM/条码打印/Gy_BarCodeBill_automaticallyByPLC_New.cs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 101 insertions(+), 2 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 cd8997c..53ff25e 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 { @@ -1017,6 +1018,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("璇烽�夋嫨婧愬崟锛�"); @@ -1060,6 +1067,8 @@ Thread thread = new Thread(ReceiveMess); thread.Start(); MessageBox.Show("杩炴帴鎴愬姛锛�"); + + timer3.Enabled = true; } catch (Exception ex) { @@ -1093,8 +1102,8 @@ } catch (Exception ex) { - MessageBox.Show("鎺ュ彛寮傚父锛屽凡鍏抽棴杩炴帴锛�" + ex.Message); - socket.Close(); + //MessageBox.Show("鎺ュ彛寮傚父锛屽凡鍏抽棴杩炴帴锛�" + ex.Message); + //socket.Close(); } } @@ -1236,6 +1245,7 @@ if (socket.Connected) { socket.Close(); + MessageBox.Show("杩炴帴鍏抽棴鎴愬姛锛�"); } else { @@ -1246,6 +1256,10 @@ catch (Exception ex) { MessageBox.Show(ex.Message); + } + finally + { + timer3.Enabled = false; } } #endregion @@ -2353,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 + { + 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 + + } } \ No newline at end of file -- Gitblit v1.9.1