From 536ba9c36bc53201cfbafb7a1063f119a2ee3a1d Mon Sep 17 00:00:00 2001
From: ZengTao <2773468879@qq.com>
Date: 星期四, 30 十一月 2023 09:40:44 +0800
Subject: [PATCH] 更新主界面
---
springboot-vue3/src/main/java/com/example/springboot/component/S7control.java | 186 ++++++++++++++++++++++++++++++++++++++++------
1 files changed, 161 insertions(+), 25 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 dca9e9c..e207db3 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
@@ -18,9 +18,7 @@
private S7control() {
if (s7PLC == null)
- s7PLC = new S7PLC(plcType, ip, port);
- s7PLC.connect();
- System.out.println(ip);
+ s7PLC = new S7PLC(plcType, ip, port,0,0);
}
// 鍗曚緥妯″紡 鑾峰彇绫荤殑鍞竴瀹炰緥
@@ -49,8 +47,10 @@
* @param data word鐨勫��
*/
public void WriteWord(String address, short data) {
- if (!s7PLC.checkConnected())
+ if (s7PLC==null)
+ {
return;
+ }
s7PLC.writeInt16(address, data);
}
@@ -61,7 +61,7 @@
* @param datas word鐨勫��
*/
public void WriteWord(String address, List<Short> datas) {
- if (!s7PLC.checkConnected())
+ if (s7PLC==null)
return;
// s7PLC.write(address, data);
List<String> addresslist = GetAddressList(address, datas.size(), 16);
@@ -79,16 +79,38 @@
* @param datas word鐨勫��
*/
public void WriteWord(List<String> address, List<Short> datas) {
- if (!s7PLC.checkConnected())
+ if (s7PLC == null)
return;
- // s7PLC.write(address, data);
- MultiAddressWrite addressWrite = new MultiAddressWrite();
for (int i = 0; i < address.size(); i++) {
- addressWrite.addInt16(address.get(i), datas.get(i));
+ String addr = address.get(i);
+ short data = datas.get(i);
+
+ if (addr.contains("-")) {
+ // 澶勭悊鑼冨洿鍦板潃
+ String[] range = addr.split("-");
+ if (range.length == 2) {
+ String startAddr = range[0].trim();
+ String endAddr = range[1].trim();
+
+ int startIndex = Integer.parseInt(startAddr.substring(startAddr.indexOf('.') + 1));
+ int endIndex = Integer.parseInt(endAddr.substring(endAddr.indexOf('.') + 1));
+
+ for (int j = startIndex; j <= endIndex; j++) {
+ String currentAddress = startAddr.substring(0, startAddr.indexOf('.') + 1) + j;
+ s7PLC.writeInt16(currentAddress, data); // 灏嗘暟鎹啓鍏ュ綋鍓嶅湴鍧�
+ }
+ }
+ } else {
+ // 澶勭悊鍗曚釜鍦板潃
+ s7PLC.writeInt16(addr, data); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧�
+ }
}
- s7PLC.writeMultiData(addressWrite);
}
+
+
+
+
/**
* 鎸夋寚瀹氱殑鍦板潃 鍐欏叆涓�涓狟it
@@ -97,7 +119,7 @@
* @param data Bit鐨勫��
*/
public void WriteBit(String address, Boolean data) {
- if (!s7PLC.checkConnected())
+ if (s7PLC==null)
return;
s7PLC.writeBoolean(address, data);
}
@@ -109,7 +131,7 @@
* @param datas bit鐨勫��
*/
public void WriteBit(List<String> address, List<Boolean> datas) {
- if (!s7PLC.checkConnected())
+ if (s7PLC==null)
return;
// s7PLC.write(address, data);
@@ -127,7 +149,7 @@
* @param datas word鐨勫��
*/
public void WriteBit(String address, List<Boolean> datas) {
- if (!s7PLC.checkConnected())
+ if (s7PLC==null)
return;
// s7PLC.write(address, data);
List<String> addresslist = GetAddressList(address, datas.size(), 1);
@@ -137,6 +159,18 @@
}
s7PLC.writeMultiData(addressWrite);
}
+ /**
+ * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆澶氫釜byte
+ *
+ * @param address 鍦板潃
+ * @param datas byte鐨勫��
+ */
+ public void WriteByte(String address, byte[] datas) {
+ if (s7PLC==null)
+ return;
+ // s7PLC.write(address, data);
+ s7PLC.writeByte(address, datas);
+ }
/**
* 鎸夋寚瀹氱殑鍦板潃 璇诲彇word缁撴灉闆�
@@ -145,10 +179,59 @@
* @return 缁撴灉
*/
public List<Short> ReadWord(List<String> address) {
- if (!s7PLC.checkConnected())
- return null;
-
+ if (s7PLC==null)
+ return null;
return s7PLC.readInt16(address);
+ }
+
+ public List<Short> readWords(List<String> addresses) {
+ if (s7PLC == null) {
+ return null;
+ }
+
+ List<Short> data = new ArrayList<>();
+
+ for (String address : addresses) {
+ if (address.contains("-")) {
+ String[] range = address.split("-");
+ String startAddress = range[0];
+ String endAddress = range[1];
+
+ if (startAddress.equals(endAddress)) {
+ // 鍗曚釜鍦板潃
+ Short value = s7PLC.readInt16(startAddress);
+ data.add(value);
+ } else {
+ // 鑼冨洿鍦板潃
+ int startIndex = getIndexFromAddress(startAddress);
+ int endIndex = getIndexFromAddress(endAddress);
+
+ for (int i = startIndex; i <= endIndex; i++) {
+ String currentAddress = getAddressFromIndex(i);
+ Short value = s7PLC.readInt16(currentAddress);
+ data.add(value);
+ }
+ }
+ } else {
+ // 鍗曚釜鍦板潃
+ Short value = s7PLC.readInt16(address);
+ data.add(value);
+ }
+ }
+
+ return data;
+ }
+
+ private int getIndexFromAddress(String address) {
+
+ // 鍙互瑙f瀽鍑哄湴鍧�涓殑鏁板瓧閮ㄥ垎锛屽苟杞崲涓烘暣鏁�
+ return 0;
+ }
+
+ private String getAddressFromIndex(int index) {
+
+ // 鏁存暟杞崲涓哄湴鍧�鏍煎紡鐨勫瓧绗︿覆
+ return "";
}
/**
@@ -159,11 +242,25 @@
* @return 缁撴灉
*/
public List<Short> ReadWord(String address, int count) {
- if (!s7PLC.checkConnected())
- return null;
+ if (s7PLC==null)
+ return null;
List<String> addresslist = GetAddressList(address, count, 16);
return s7PLC.readInt16(addresslist);
+ }
+ /**
+ * 鎸夋寚瀹氱殑鍦板潃 璇诲彇byte缁撴灉闆�
+ *
+ * @param address 鍦板潃
+ * @param count 杩炵画璇诲灏戜釜byte
+ * @return 缁撴灉
+ */
+ public byte[] ReadByte(String address, int count) {
+ if (s7PLC==null)
+ return null;
+
+ // List<String> addresslist = GetAddressList(address, count, 16);
+ return s7PLC.readByte(address,count);
}
/**
@@ -173,10 +270,46 @@
* @return Boolean缁撴灉
*/
public List<Boolean> ReadBits(List<String> addresslist) {
- if (!s7PLC.checkConnected())
- return null;
+ if (s7PLC==null)
+ return null;
return s7PLC.readBoolean(addresslist);
}
+
+ public List<Boolean> readBits(List<String> addressList) {
+ if (s7PLC == null)
+ return null;
+
+ List<Boolean> result = new ArrayList<>();
+
+ for (String address : addressList) {
+ if (address.contains("~")) {
+ String[] range = address.split("~");
+ String startAddress = range[0];
+ String endAddress = range[1];
+
+ int startIndex = extractAddressNumber(startAddress);
+ int endIndex = extractAddressNumber(endAddress);
+
+ String prefix = startAddress.substring(0, startAddress.indexOf(".") + 1);
+
+ for (int i = startIndex; i <= endIndex; i++) {
+ String newAddress = prefix + i;
+ result.add(s7PLC.readBoolean(newAddress));
+ }
+ } else {
+ result.add(s7PLC.readBoolean(address));
+ }
+ }
+
+ return result;
+ }
+
+
+ private int extractAddressNumber(String address) {
+ String numberStr = address.replaceAll("\\D+", ""); // 浣跨敤姝e垯琛ㄨ揪寮忔彁鍙栨暟瀛楅儴鍒�
+ return Integer.parseInt(numberStr);
+ }
+
/**
* 浠庢寚瀹氱殑鍦板潃寮�濮� 杩炵画鎸塨it浣嶈鍙�
@@ -186,16 +319,16 @@
* @return Boolean缁撴灉
*/
public List<Boolean> ReadBits(String address, int count) {
- if (!s7PLC.checkConnected())
+ if (s7PLC==null)
return null;
List<String> addresslist = GetAddressList(address, count, 1);
return s7PLC.readBoolean(addresslist);
}
private List<String> GetAddressList(String address, int count, int addedbit) {
- List<String> addresslist = new ArrayList<>();
+ List<String> addresslist = new ArrayList<String>();
- String[] stringdatas = address.split(".");
+ String[] stringdatas = address.trim().split("\\.");
if (stringdatas.length < 2 || !address.startsWith("DB"))
return null;
int dbwindex = 0;
@@ -207,7 +340,8 @@
} else
return null;
- for (int i = 0; i < count; i++) {
+ addresslist.add(address);
+ for (int i = 0; i < count-1; i++) {
int bitcurrent = bitindex + addedbit;
if (bitcurrent > 7) {
@@ -215,7 +349,9 @@
bitindex = 0;
} else
bitindex = bitcurrent;
- addresslist.add(stringdatas[0] + "." + dbwindex + "." + bitindex);
+
+ String endstr=stringdatas.length==3?"." + bitindex:"";
+ addresslist.add(stringdatas[0] + "." + dbwindex + endstr);
}
return addresslist;
}
--
Gitblit v1.8.0