From d1ac4de7a592863f653433bef1707f121441a1ac Mon Sep 17 00:00:00 2001
From: zhoushihao <zsh19950802@163.com>
Date: 星期五, 24 五月 2024 12:01:49 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'
---
hangzhoumesParent/common/servicebase/src/main/java/com/mes/tools/S7control.java | 222 +++++++++++++++++++-----------------------------------
1 files changed, 79 insertions(+), 143 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..10e349a 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,6 @@
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 +19,25 @@
/**
* 鍏抽棴瑗块棬瀛恠7閫氳杩炴帴
*/
- public void CloseS7client() {
- if (s7PLC == null) {
+ public void closeS7client() {
+ if (s7PLC != null) {
s7PLC.close();
}
- s7PLC.checkConnected();
}
+ /**
+ * 閲嶅惎瑗块棬瀛恠7閫氳杩炴帴
+ */
+ public void reStartS7client() {
+ if (s7PLC != null) {
+ s7PLC.hotRestart();
+ }
+ }
+
/**
* s7閫氳杩炴帴鐘舵��
*/
- public boolean CheckConnected() {
+ public boolean checkConnected() {
return s7PLC.checkConnected();
}
@@ -42,11 +47,14 @@
* @param address 鍦板潃
* @param data word鐨勫��
*/
- public void WriteWord(String address, short data) {
+ public void writeWord(String address, int data) {
if (s7PLC == null) {
return;
}
- s7PLC.writeInt16(address, data);
+ else if(!s7PLC.checkConnected()) {
+ s7PLC.hotRestart();
+ }
+ s7PLC.writeUInt16(address, data);
}
/**
@@ -55,17 +63,20 @@
* @param address 鍦板潃
* @param datas word鐨勫��
*/
- public void WriteWord(String address, List<Short> datas) {
+ public void writeWord(String address, List<Integer> datas) {
if (s7PLC == null) {
return;
}
+ else if(!s7PLC.checkConnected()) {
+ s7PLC.hotRestart();
+ }
// 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);
+ s7PLC.writeMultiData(addressWrite);
}
/**
@@ -82,11 +93,14 @@
* @param address 鍦板潃
* @param data Bit鐨勫��
*/
- public void WriteBit(String address, Boolean data) {
+ public void writeBit(String address, Boolean data) {
if (s7PLC == null) {
return;
}
- s7PLC.writeBoolean(address, data);
+ else if(!s7PLC.checkConnected()) {
+ s7PLC.hotRestart();
+ }
+ s7PLC.writeBoolean(address, data);
}
/**
@@ -95,9 +109,12 @@
* @param address 鍦板潃
* @param datas bit鐨勫��
*/
- public void WriteBit(List<String> address, List<Boolean> datas) {
+ public void writeBit(List<String> address, List<Boolean> datas) {
if (s7PLC == null) {
return;
+ }
+ else if(!s7PLC.checkConnected()) {
+ s7PLC.hotRestart();
}
// s7PLC.write(address, data);
@@ -105,7 +122,7 @@
for (int i = 0; i < address.size(); i++) {
addressWrite.addBoolean(address.get(i), datas.get(i));
}
- s7PLC.writeMultiData(addressWrite);
+ s7PLC.writeMultiData(addressWrite);
}
/**
@@ -114,17 +131,20 @@
* @param address 鍦板潃
* @param datas word鐨勫��
*/
- public void WriteBit(String address, List<Boolean> datas) {
+ public void writeBit(String address, List<Boolean> datas) {
if (s7PLC == null) {
return;
}
+ else if(!s7PLC.checkConnected()) {
+ s7PLC.hotRestart();
+ }
// 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);
+ s7PLC.writeMultiData(addressWrite);
}
/**
@@ -133,11 +153,14 @@
* @param address 鍦板潃
* @param datas byte鐨勫��
*/
- public void WriteByte(String address, byte[] datas) {
+ public void writeByte(String address, byte[] datas) {
if (s7PLC == null) {
return;
}
- // s7PLC.write(address, data);
+ else if(!s7PLC.checkConnected()) {
+ s7PLC.hotRestart();
+ }
+ // s7PLC.write(address, data);
s7PLC.writeByte(address, datas);
}
@@ -147,31 +170,21 @@
* @param address 鍦板潃
* @return 缁撴灉
*/
- public List<Short> ReadWord(List<String> address) {
+ public List<Integer> readWord(List<String> address) {
if (s7PLC == null) {
return null;
}
try {
- return s7PLC.readInt16(address);
+ return s7PLC.readUInt16(address);
} catch (Exception e) {
+ s7PLC.hotRestart();
System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
return null;
}
}
- private int getIndexFromAddress(String address) {
-
- // 鍙互瑙f瀽鍑哄湴鍧�涓殑鏁板瓧閮ㄥ垎锛屽苟杞崲涓烘暣鏁�
- return 0;
- }
-
- private String getAddressFromIndex(int index) {
-
- // 鏁存暟杞崲涓哄湴鍧�鏍煎紡鐨勫瓧绗︿覆
- return "";
- }
/**
* 鎸夋寚瀹氱殑鍦板潃 璇诲彇word缁撴灉闆�
@@ -180,15 +193,16 @@
* @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<String> addresslist = getAddressList(address, count, 16);
try {
- return s7PLC.readInt16(addresslist);
+ return s7PLC.readUInt16(addresslist);
} catch (Exception e) {
+ s7PLC.hotRestart();
System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
return null;
@@ -202,7 +216,7 @@
* @param count 杩炵画璇诲灏戜釜byte
* @return 缁撴灉
*/
- public byte[] ReadByte(String address, int count) {
+ public byte[] readByte(String address, int count) {
if (s7PLC == null) {
return null;
}
@@ -212,6 +226,7 @@
return s7PLC.readByte(address, count);
} catch (Exception e) {
// 澶勭悊寮傚父
+ s7PLC.hotRestart();
System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
return null;
}
@@ -224,14 +239,21 @@
* @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);
+ try {
+ return s7PLC.readBoolean(addresslist);
+ } catch (Exception e) {
+ // 澶勭悊寮傚父
+ s7PLC.hotRestart();
+ return null;
+ }
+
}
- //璇诲彇涓嶈繛缁湴鍧�bit
+ /* //璇诲彇涓嶈繛缁湴鍧�bit
public List<Boolean> readBits(List<String> addressList) {
if (s7PLC == null || addressList.isEmpty()) {
return null;
@@ -249,30 +271,10 @@
}
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) {
@@ -282,81 +284,13 @@
try {
return s7PLC.readString(address);
} catch (Exception e) {
+ s7PLC.hotRestart();
System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
return null;
}
}
- //涓嶈繛缁湴鍧�鍐欏叆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) {
@@ -366,6 +300,7 @@
try {
return s7PLC.readTime(address);
} catch (Exception e) {
+ s7PLC.hotRestart();
System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
return null;
}
@@ -373,17 +308,16 @@
public void writetime(String address, long datas) {
- if (s7PLC == null)
+ if (s7PLC == null) {
return;
+ }
+ else if(!s7PLC.checkConnected())
+ {
+ s7PLC.hotRestart();
+ }
s7PLC.writeTime(address, datas); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧�
- }
-
-
- private int extractAddressNumber(String address) {
- String numberStr = address.replaceAll("\\D+", ""); // 浣跨敤姝e垯琛ㄨ揪寮忔彁鍙栨暟瀛楅儴鍒�
- return Integer.parseInt(numberStr);
}
@@ -394,13 +328,15 @@
* @param count 闀垮害
* @return Boolean缁撴灉
*/
- public List<Boolean> ReadBits(String address, int count) {
- if (s7PLC == null)
+ public List<Boolean> readBits(String address, int count) {
+ if (s7PLC == null) {
return null;
- List<String> addresslist = GetAddressList(address, count, 1);
+ }
+ List<String> addresslist = getAddressList(address, count, 1);
try {
return s7PLC.readBoolean(addresslist);
} catch (Exception e) {
+ s7PLC.hotRestart();
System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
return null;
}
@@ -410,7 +346,7 @@
;
- 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