From e36b74525f9c7400da2d3438c5e4164622da059a Mon Sep 17 00:00:00 2001 From: wuyouming666 <2265557248@qq.com> Date: 星期三, 24 一月 2024 13:14:21 +0800 Subject: [PATCH] 封装初始化方法为工具类,方便调用, getPlcParameterValues getPlcBitValues 方法codeid 按照传入参数的顺序遍历 来获取值 --- springboot-vue3/src/main/java/com/example/springboot/component/S7control.java | 206 ++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 154 insertions(+), 52 deletions(-) diff --git a/springboot-vue3/src/main/java/com/example/springboot/component/S7control.java b/springboot-vue3/src/main/java/com/example/springboot/component/S7control.java index 8db324e..ce4f674 100644 --- a/springboot-vue3/src/main/java/com/example/springboot/component/S7control.java +++ b/springboot-vue3/src/main/java/com/example/springboot/component/S7control.java @@ -3,6 +3,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; +import java.sql.Time; import java.util.ArrayList; import java.util.List; @@ -36,13 +37,21 @@ } return instance; } - + /** * 鍏抽棴瑗块棬瀛恠7閫氳杩炴帴 */ public void CloseS7client() { if (s7PLC == null) s7PLC.close(); + s7PLC.checkConnected(); + } + + /** + * s7閫氳杩炴帴鐘舵�� + */ + public boolean CheckConnected() { + return s7PLC.checkConnected(); } /** @@ -156,10 +165,17 @@ * @return 缁撴灉 */ public List<Short> ReadWord(List<String> address) { - if (s7PLC==null) - return null; - return s7PLC.readInt16(address); + if (s7PLC == null) + return null; + + try { + return s7PLC.readInt16(address); + } catch (Exception e) { + System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + return null; + } } + @@ -183,12 +199,19 @@ * @return 缁撴灉 */ public List<Short> ReadWord(String address, int count) { - if (s7PLC==null) - return null; + if (s7PLC == null) + return null; List<String> addresslist = GetAddressList(address, count, 16); - return s7PLC.readInt16(addresslist); + try { + return s7PLC.readInt16(addresslist); + } catch (Exception e) { + System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + + return null; + } } + /** * 鎸夋寚瀹氱殑鍦板潃 璇诲彇byte缁撴灉闆� * @@ -200,7 +223,15 @@ if (s7PLC==null) return null; // List<String> addresslist = GetAddressList(address, count, 16); - return s7PLC.readByte(address,count); + + try { + return s7PLC.readByte(address, count); + }catch (Exception e) { + // 澶勭悊寮傚父 + System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + return null; + } + } /** @@ -214,56 +245,86 @@ return null; return s7PLC.readBoolean(addresslist); } - - public List<Boolean> readBits(List<String> addressList) { - if (s7PLC == null || addressList.isEmpty()) { - return null; - } - - List<Boolean> values = new ArrayList<>(); - for (String address : addressList) { - boolean value = s7PLC.readBoolean(address); - values.add(value); - } - - return values; +//璇诲彇涓嶈繛缁湴鍧�bit +public List<Boolean> readBits(List<String> addressList) { + if (s7PLC == null || addressList.isEmpty()) { + return null; } + List<Boolean> values = new ArrayList<>(); + for (String address : addressList) { + try { + boolean value = s7PLC.readBoolean(address); + values.add(value); + } catch (Exception e) { + // 澶勭悊寮傚父 + System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + } + } - public List<String> readStrings(List<String> addressList) { + return values; +} + + +//璇诲彇String +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 List<String> readStringsandword(List<String> addressList) { if (s7PLC == null) { return null; } - List<String> result = new ArrayList<>(); - for (String address : addressList) { - byte[] bytes = s7PLC.readByte(address, 14); -// System.out.println(bytes.toString()); - if (bytes != null) { - String str = new String(bytes, StandardCharsets.UTF_8); -// System.out.println(str); -// if(str == null ){ -// str = ""; -// } - result.add(str); + try { + if (address.contains("-")) { + address = address.substring(0, address.indexOf("-")); + + byte[] bytes = s7PLC.readByte(address, 14); + if (bytes != null) { + String str = new String(bytes, StandardCharsets.UTF_8); + result.add(str); + } + + } else { + Short value = s7PLC.readInt16(address); + result.add(value.toString()); + + } + } catch (Exception e) { + System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + result.add(null); } -// if ( bytes.toString().contains("@")) { -// String str = ""; -// -// result.add(str); -// }else{ -// -// String str = new String(bytes, StandardCharsets.US_ASCII); -// result.add(str); -// } -// System.out.println(bytes.toString()); } return result; } + +//涓嶈繛缁湴鍧�鍐欏叆Word public void WriteWord(List<String> address, List<Short> datas) { if (s7PLC == null) return; @@ -279,7 +340,10 @@ } } } - + + + +//瀛楃涓插啓鍏� public void outmesid(String data, String addr) { // System.out.println("outmesid: " + data); List<Byte> glassidlist = new ArrayList<>(); @@ -294,7 +358,7 @@ S7control.getinstance().WriteByte(addr, bytes); } - +//璇诲彇涓嶈繛缁瓀ord public List<Short> readWords(List<String> addresses) { if (s7PLC == null) { return null; @@ -303,15 +367,44 @@ List<Short> data = new ArrayList<>(); for (String address : addresses) { + try { - // 鍗曚釜鍦板潃 - Short value = s7PLC.readInt16(address); - data.add(value); + // 鍗曚釜鍦板潃 + Short value = s7PLC.readInt16(address); + data.add(value); + } catch (Exception e) { + System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + + } } - return data; } + +//璇诲彇鏃堕棿 +public Long readtime(String address) { + if (s7PLC == null) + return null; + try { + return s7PLC.readTime(address); + } catch (Exception e) { + System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + return null; + } +} + + + 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垯琛ㄨ揪寮忔彁鍙栨暟瀛楅儴鍒� @@ -327,11 +420,19 @@ * @return Boolean缁撴灉 */ public List<Boolean> ReadBits(String address, int count) { - if (s7PLC==null) + if (s7PLC == null) return null; List<String> addresslist = GetAddressList(address, count, 1); - return s7PLC.readBoolean(addresslist); - } + try { + return s7PLC.readBoolean(addresslist); + } catch (Exception e) { + System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage()); + return null; + } + + }; + + private List<String> GetAddressList(String address, int count, int addedbit) { List<String> addresslist = new ArrayList<String>(); @@ -344,6 +445,7 @@ if (stringdatas.length == 2) { dbwindex = Integer.parseInt(stringdatas[1]); } else if (stringdatas.length == 3) { + dbwindex = Integer.parseInt(stringdatas[1]); bitindex = Integer.parseInt(stringdatas[2]); } else return null; -- Gitblit v1.8.0