From a9bcb83a7942e3da175ab6da9ed16eda38f49180 Mon Sep 17 00:00:00 2001 From: wuyouming666 <2265557248@qq.com> Date: 星期一, 27 五月 2024 14:38:19 +0800 Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/HangZhouMes --- hangzhoumesParent/common/servicebase/src/main/java/com/mes/tools/S7control.java | 219 +++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 150 insertions(+), 69 deletions(-) diff --git a/hangzhoumesParent/common/servicebase/src/main/java/com/mes/tools/S7control.java b/hangzhoumesParent/common/servicebase/src/main/java/com/mes/tools/S7control.java index 10e349a..32812b3 100644 --- a/hangzhoumesParent/common/servicebase/src/main/java/com/mes/tools/S7control.java +++ b/hangzhoumesParent/common/servicebase/src/main/java/com/mes/tools/S7control.java @@ -3,6 +3,7 @@ import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType; import com.github.xingshuangs.iot.protocol.s7.service.MultiAddressWrite; import com.github.xingshuangs.iot.protocol.s7.service.S7PLC; + import java.util.ArrayList; import java.util.List; @@ -24,13 +25,20 @@ s7PLC.close(); } } + /** * 閲嶅惎瑗块棬瀛恠7閫氳杩炴帴 */ - public void reStartS7client() { + public boolean reStartS7client() { if (s7PLC != null) { - s7PLC.hotRestart(); + try { + s7PLC.hotRestart(); + return true; + } catch (Exception ex) { + return false; + } } + return false; } @@ -47,14 +55,24 @@ * @param address 鍦板潃 * @param data word鐨勫�� */ - public void writeWord(String address, int data) { + public boolean writeWord(String address, int data) { if (s7PLC == null) { - return; + return false; } - else if(!s7PLC.checkConnected()) { - s7PLC.hotRestart(); + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeUInt16(address, data); + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } } - s7PLC.writeUInt16(address, data); + while (!result && tryCount > 0); + return result; } /** @@ -63,20 +81,31 @@ * @param address 鍦板潃 * @param datas word鐨勫�� */ - public void writeWord(String address, List<Integer> datas) { + public boolean writeWord(String address, List<Integer> datas) { if (s7PLC == null) { - return; + return false; } - else if(!s7PLC.checkConnected()) { - s7PLC.hotRestart(); - } + boolean result = false; + int tryCount = 2; // s7PLC.write(address, data); List<String> addresslist = getAddressList(address, datas.size(), 16); MultiAddressWrite addressWrite = new MultiAddressWrite(); for (int i = 0; i < datas.size(); i++) { addressWrite.addUInt16(addresslist.get(i), datas.get(i)); } - s7PLC.writeMultiData(addressWrite); + do { + try { + s7PLC.writeMultiData(addressWrite); + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } + } + while (!result && tryCount > 0); + return result; + } /** @@ -93,14 +122,24 @@ * @param address 鍦板潃 * @param data Bit鐨勫�� */ - public void writeBit(String address, Boolean data) { + public boolean writeBit(String address, Boolean data) { if (s7PLC == null) { - return; + return false; } - else if(!s7PLC.checkConnected()) { - s7PLC.hotRestart(); + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeBoolean(address, data); + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } } - s7PLC.writeBoolean(address, data); + while (!result && tryCount > 0); + return result; } /** @@ -109,20 +148,30 @@ * @param address 鍦板潃 * @param datas bit鐨勫�� */ - public void writeBit(List<String> address, List<Boolean> datas) { + public boolean writeBit(List<String> address, List<Boolean> datas) { if (s7PLC == null) { - return; + return false; } - else if(!s7PLC.checkConnected()) { - s7PLC.hotRestart(); - } - // s7PLC.write(address, data); MultiAddressWrite addressWrite = new MultiAddressWrite(); for (int i = 0; i < address.size(); i++) { addressWrite.addBoolean(address.get(i), datas.get(i)); } - s7PLC.writeMultiData(addressWrite); + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeMultiData(addressWrite); + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } + } + while (!result && tryCount > 0); + return result; + } /** @@ -131,20 +180,31 @@ * @param address 鍦板潃 * @param datas word鐨勫�� */ - public void writeBit(String address, List<Boolean> datas) { + public boolean writeBit(String address, List<Boolean> datas) { if (s7PLC == null) { - return; + return false; } - else if(!s7PLC.checkConnected()) { - s7PLC.hotRestart(); - } + // s7PLC.write(address, data); List<String> addresslist = getAddressList(address, datas.size(), 1); MultiAddressWrite addressWrite = new MultiAddressWrite(); for (int i = 0; i < datas.size(); i++) { addressWrite.addBoolean(addresslist.get(i), datas.get(i)); } - s7PLC.writeMultiData(addressWrite); + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeMultiData(addressWrite); + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } + } + while (!result && tryCount > 0); + return result; } /** @@ -153,15 +213,24 @@ * @param address 鍦板潃 * @param datas byte鐨勫�� */ - public void writeByte(String address, byte[] datas) { + public boolean writeByte(String address, byte[] datas) { if (s7PLC == null) { - return; + return false; } - else if(!s7PLC.checkConnected()) { - s7PLC.hotRestart(); + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeByte(address, datas); + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } } - // s7PLC.write(address, data); - s7PLC.writeByte(address, datas); + while (!result && tryCount > 0); + return result; } /** @@ -174,16 +243,17 @@ if (s7PLC == null) { return null; } - + List<Integer> result = null; try { - return s7PLC.readUInt16(address); + result = s7PLC.readUInt16(address); } catch (Exception e) { s7PLC.hotRestart(); System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - return null; + + } finally { + return result; } } - /** @@ -197,15 +267,15 @@ if (s7PLC == null) { return null; } - + List<Integer> result = null; List<String> addresslist = getAddressList(address, count, 16); try { - return s7PLC.readUInt16(addresslist); + result = s7PLC.readUInt16(addresslist); } catch (Exception e) { s7PLC.hotRestart(); System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - - return null; + } finally { + return result; } } @@ -221,14 +291,15 @@ return null; } // List<String> addresslist = GetAddressList(address, count, 16); - + byte[] bytes = null; try { - return s7PLC.readByte(address, count); + bytes = s7PLC.readByte(address, count); } catch (Exception e) { // 澶勭悊寮傚父 s7PLC.hotRestart(); System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - return null; + } finally { + return bytes; } } @@ -243,14 +314,15 @@ if (s7PLC == null) { return null; } + List<Boolean> values = new ArrayList<>(); try { - return s7PLC.readBoolean(addresslist); + values = s7PLC.readBoolean(addresslist); } catch (Exception e) { // 澶勭悊寮傚父 s7PLC.hotRestart(); - return null; + } finally { + return values; } - } /* //璇诲彇涓嶈繛缁湴鍧�bit @@ -274,22 +346,21 @@ }*/ - - //璇诲彇瀛楃涓� public String readString(String address) { if (s7PLC == null) { return null; } + String result = null; try { - return s7PLC.readString(address); + result = s7PLC.readString(address); } catch (Exception e) { s7PLC.hotRestart(); System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - return null; + } finally { + return result; } } - //璇诲彇鏃堕棿 @@ -297,27 +368,36 @@ if (s7PLC == null) { return null; } + Long result = null; try { - return s7PLC.readTime(address); + result = s7PLC.readTime(address); } catch (Exception e) { s7PLC.hotRestart(); System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - return null; + } finally { + return result; } } - public void writetime(String address, long datas) { + public boolean writetime(String address, long datas) { if (s7PLC == null) { - return; + return false; } - else if(!s7PLC.checkConnected()) - { - s7PLC.hotRestart(); + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeTime(address, datas); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧� + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } } - - - s7PLC.writeTime(address, datas); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧� + while (!result && tryCount > 0); + return result; } @@ -332,15 +412,16 @@ if (s7PLC == null) { return null; } + List<Boolean> values = new ArrayList<>(); List<String> addresslist = getAddressList(address, count, 1); try { - return s7PLC.readBoolean(addresslist); + values = s7PLC.readBoolean(addresslist); } catch (Exception e) { s7PLC.hotRestart(); System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - return null; + } finally { + return values; } - } ; -- Gitblit v1.8.0