From 6877ca889fcc8e9963bb12fe27bff106bcc79ebd Mon Sep 17 00:00:00 2001 From: wu <731351411@qq.com> Date: 星期二, 28 五月 2024 09:06:57 +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 | 371 +++++++++++++++++++++++++++------------------------- 1 files changed, 194 insertions(+), 177 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 0b206d5..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,9 +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 com.google.common.primitives.Bytes; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -22,17 +20,32 @@ /** * 鍏抽棴瑗块棬瀛恠7閫氳杩炴帴 */ - public void CloseS7client() { - if (s7PLC == null) { + public void closeS7client() { + if (s7PLC != null) { s7PLC.close(); } - s7PLC.checkConnected(); } + + /** + * 閲嶅惎瑗块棬瀛恠7閫氳杩炴帴 + */ + public boolean reStartS7client() { + if (s7PLC != null) { + try { + s7PLC.hotRestart(); + return true; + } catch (Exception ex) { + return false; + } + } + return false; + } + /** * s7閫氳杩炴帴鐘舵�� */ - public boolean CheckConnected() { + public boolean checkConnected() { return s7PLC.checkConnected(); } @@ -42,11 +55,24 @@ * @param address 鍦板潃 * @param data word鐨勫�� */ - public void WriteWord(String address, short data) { + public boolean writeWord(String address, int data) { if (s7PLC == null) { - return; + return false; } - s7PLC.writeInt16(address, data); + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeUInt16(address, data); + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } + } + while (!result && tryCount > 0); + return result; } /** @@ -55,17 +81,31 @@ * @param address 鍦板潃 * @param datas word鐨勫�� */ - public void WriteWord(String address, List<Short> datas) { + public boolean writeWord(String address, List<Integer> datas) { if (s7PLC == null) { - return; + return false; } + boolean result = false; + int tryCount = 2; // s7PLC.write(address, data); - List<String> addresslist = GetAddressList(address, datas.size(), 16); + List<String> addresslist = getAddressList(address, datas.size(), 16); MultiAddressWrite addressWrite = new MultiAddressWrite(); for (int i = 0; i < datas.size(); i++) { - addressWrite.addInt16(addresslist.get(i), datas.get(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; + } /** @@ -82,11 +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; } - s7PLC.writeBoolean(address, data); + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeBoolean(address, data); + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } + } + while (!result && tryCount > 0); + return result; } /** @@ -95,17 +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; } - // 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; + } /** @@ -114,17 +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; } + // s7PLC.write(address, data); - List<String> addresslist = GetAddressList(address, datas.size(), 1); + 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; } /** @@ -133,12 +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; } - // s7PLC.write(address, data); - s7PLC.writeByte(address, datas); + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeByte(address, datas); + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } + } + while (!result && tryCount > 0); + return result; } /** @@ -147,31 +239,22 @@ * @param address 鍦板潃 * @return 缁撴灉 */ - public List<Short> ReadWord(List<String> address) { + public List<Integer> readWord(List<String> address) { if (s7PLC == null) { return null; } - + List<Integer> result = null; try { - return s7PLC.readInt16(address); + result = s7PLC.readUInt16(address); } catch (Exception e) { + s7PLC.hotRestart(); System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - return null; + + } finally { + return result; } } - - private int getIndexFromAddress(String address) { - - // 鍙互瑙f瀽鍑哄湴鍧�涓殑鏁板瓧閮ㄥ垎锛屽苟杞崲涓烘暣鏁� - return 0; - } - - private String getAddressFromIndex(int index) { - - // 鏁存暟杞崲涓哄湴鍧�鏍煎紡鐨勫瓧绗︿覆 - return ""; - } /** * 鎸夋寚瀹氱殑鍦板潃 璇诲彇word缁撴灉闆� @@ -180,18 +263,19 @@ * @param count 杩炵画璇诲灏戜釜word * @return 缁撴灉 */ - public List<Short> ReadWord(String address, int count) { + public List<Integer> readWord(String address, int count) { if (s7PLC == null) { return null; } - - List<String> addresslist = GetAddressList(address, count, 16); + List<Integer> result = null; + List<String> addresslist = getAddressList(address, count, 16); try { - return s7PLC.readInt16(addresslist); + result = s7PLC.readUInt16(addresslist); } catch (Exception e) { + s7PLC.hotRestart(); System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - - return null; + } finally { + return result; } } @@ -202,18 +286,20 @@ * @param count 杩炵画璇诲灏戜釜byte * @return 缁撴灉 */ - public byte[] ReadByte(String address, int count) { + public byte[] readByte(String address, int count) { if (s7PLC == null) { 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; } } @@ -224,14 +310,22 @@ * @param addresslist 鍦板潃闆� * @return Boolean缁撴灉 */ - public List<Boolean> ReadBits(List<String> addresslist) { + public List<Boolean> readBits(List<String> addresslist) { if (s7PLC == null) { return null; } - return s7PLC.readBoolean(addresslist); + List<Boolean> values = new ArrayList<>(); + try { + values = s7PLC.readBoolean(addresslist); + } catch (Exception e) { + // 澶勭悊寮傚父 + s7PLC.hotRestart(); + } finally { + return values; + } } - //璇诲彇涓嶈繛缁湴鍧�bit + /* //璇诲彇涓嶈繛缁湴鍧�bit public List<Boolean> readBits(List<String> addressList) { if (s7PLC == null || addressList.isEmpty()) { return null; @@ -249,141 +343,61 @@ } return values; - } + }*/ - - //璇诲彇StringList - public List<String> readStrings(List<String> addressList) { - if (s7PLC == null) { - return null; - } - List<String> result = new ArrayList<>(); - for (String address : addressList) { - try { - byte[] bytes = s7PLC.readByte(address, 14); - if (bytes != null) { - String str = new String(bytes, StandardCharsets.UTF_8); - result.add(str); - } - } catch (Exception e) { - System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - result.add(null); - } - } - - return result; - } //璇诲彇瀛楃涓� 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; } } - - //涓嶈繛缁湴鍧�鍐欏叆Word - public void WriteWord(List<String> address, List<Short> datas) { - if (s7PLC == null) { - return; - } - - for (int i = 0; i < address.size(); i++) { - String addr = address.get(i); - short data = datas.get(i); - - if (addr.contains("-")) { - outmesid(String.valueOf(data), addr); // 鍗曠嫭澶勭悊甯︾牬鎶樺彿鐨勫湴鍧� - } else { - s7PLC.writeInt16(addr, data); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧� - } - } - } - - - //瀛楃涓插啓鍏� - public void outmesid(String data, String addr) { -// System.out.println("outmesid: " + data); - List<Byte> glassidlist = new ArrayList<>(); - String[] parts = addr.split("-"); - if (parts.length == 2) { - addr = parts[0]; // 鍙繚鐣� "-" 鍓嶉潰鐨勯儴鍒� - } - for (char iditem : data.toCharArray()) { - glassidlist.add(Byte.valueOf(String.valueOf(iditem))); - } - byte[] bytes = Bytes.toArray(glassidlist); - WriteByte(addr, bytes); - } - - //璇诲彇涓嶈繛缁瓀ord - public List<Short> readWords(List<String> addresses) { - if (s7PLC == null) { - return null; - } - List<Short> data = new ArrayList<>(); - - for (String address : addresses) { - try { - // 鍗曚釜鍦板潃 - Short value = s7PLC.readInt16(address); - data.add(value); - } catch (Exception e) { - System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - - } - - } - return data; - } - - //璇诲彇鍗曚釜word - public Short readWord(String address) { - if (s7PLC == null) { - return null; - } - try { - // 鍗曚釜鍦板潃 - return s7PLC.readInt16(address); - } catch (Exception e) { - System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); - - } - return -1; - } //璇诲彇鏃堕棿 public Long readtime(String address) { 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) { - if (s7PLC == null) - return; - - - s7PLC.writeTime(address, datas); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧� - } - - - private int extractAddressNumber(String address) { - String numberStr = address.replaceAll("\\D+", ""); // 浣跨敤姝e垯琛ㄨ揪寮忔彁鍙栨暟瀛楅儴鍒� - return Integer.parseInt(numberStr); + public boolean writetime(String address, long datas) { + if (s7PLC == null) { + return false; + } + boolean result = false; + int tryCount = 2; + do { + try { + s7PLC.writeTime(address, datas); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧� + result = true; + } catch (Exception ex) { + reStartS7client(); + } finally { + tryCount -= 1; + } + } + while (!result && tryCount > 0); + return result; } @@ -394,23 +408,26 @@ * @param count 闀垮害 * @return Boolean缁撴灉 */ - public List<Boolean> ReadBits(String address, int count) { - if (s7PLC == null) - return null; - List<String> addresslist = GetAddressList(address, count, 1); - try { - return s7PLC.readBoolean(addresslist); - } catch (Exception e) { - System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + public List<Boolean> readBits(String address, int count) { + if (s7PLC == null) { return null; } - + List<Boolean> values = new ArrayList<>(); + List<String> addresslist = getAddressList(address, count, 1); + try { + values = s7PLC.readBoolean(addresslist); + } catch (Exception e) { + s7PLC.hotRestart(); + System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + } finally { + return values; + } } ; - private List<String> GetAddressList(String address, int count, int addedbit) { + private List<String> getAddressList(String address, int count, int addedbit) { List<String> addresslist = new ArrayList<String>(); String[] stringdatas = address.trim().split("\\."); -- Gitblit v1.8.0