From e19d95da199d0d9e30d14b0a23c0470e37fd1883 Mon Sep 17 00:00:00 2001
From: 严智鑫 <test>
Date: 星期一, 19 八月 2024 08:51:32 +0800
Subject: [PATCH] 工程实体类移到 公共里
---
hangzhoumesParent/common/servicebase/src/main/java/com/mes/tools/S7control.java | 356 +++++++++++++++++++++++++++++++++--------------------------
1 files changed, 200 insertions(+), 156 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 fafbd45..45bf01e 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,25 @@
* @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) {
+ System.out.println("鍚憄lc鍐欏懡浠よ繃绋嬩腑鍙戠敓寮傚父锛屽師鍥犱负锛�" + ex.getMessage());
+ reStartS7client();
+ } finally {
+ tryCount -= 1;
+ }
+ }
+ while (!result && tryCount > 0);
+ return result;
}
/**
@@ -55,17 +82,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 +123,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 +149,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 +181,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 +214,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 +240,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 +264,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 +287,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 +311,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,115 +344,61 @@
}
return values;
- }
+ }*/
- //璇诲彇String
- public List<String> readStrings(List<String> addressList) {
+ //璇诲彇瀛楃涓�
+ public String readString(String address) {
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;
- }
-
-
- //涓嶈繛缁湴鍧�鍐欏叆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); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧�
- }
+ String result = null;
+ try {
+ result = s7PLC.readString(address);
+ } catch (Exception e) {
+ s7PLC.hotRestart();
+ System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+ } finally {
+ return result;
}
}
-
- //瀛楃涓插啓鍏�
- 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;
- }
//璇诲彇鏃堕棿
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;
}
@@ -368,23 +409,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