From a93a3f1532d33849187dcd1a372c315ec4511e04 Mon Sep 17 00:00:00 2001
From: wu <731351411@qq.com>
Date: 星期一, 04 十二月 2023 09:04:01 +0800
Subject: [PATCH] 理片笼逻辑和界面更改
---
springboot-vue3/src/main/java/com/example/springboot/component/S7control.java | 143 ++++++++++++++++++++++++++++++++---------------
1 files changed, 96 insertions(+), 47 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 5caea97..b6afa91 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
@@ -1,11 +1,15 @@
package com.example.springboot.component;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
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;
public class S7control {
@@ -78,17 +82,9 @@
* @param address 鍦板潃
* @param datas word鐨勫��
*/
- public void WriteWord(List<String> address, List<Short> datas) {
- 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));
- }
- s7PLC.writeMultiData(addressWrite);
- }
+
+
@@ -164,43 +160,7 @@
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) {
@@ -238,7 +198,6 @@
public byte[] ReadByte(String address, int count) {
if (s7PLC==null)
return null;
-
// List<String> addresslist = GetAddressList(address, count, 16);
return s7PLC.readByte(address,count);
}
@@ -255,6 +214,96 @@
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;
+ }
+
+
+ public List<String> readStrings(List<String> addressList) {
+ if (s7PLC == null) {
+ return null;
+ }
+
+ List<String> result = new ArrayList<>();
+
+ for (String address : addressList) {
+ byte[] bytes = s7PLC.readByte(address, 14);
+
+ if (bytes != null) {
+ String str = new String(bytes, StandardCharsets.US_ASCII);
+ result.add(str);
+ }
+ }
+
+ return result;
+ }
+
+
+ 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);
+ S7control.getinstance().WriteByte(addr, bytes);
+ }
+
+
+ public List<Short> readWords(List<String> addresses) {
+ if (s7PLC == null) {
+ return null;
+ }
+
+ List<Short> data = new ArrayList<>();
+
+ for (String address : addresses) {
+
+ // 鍗曚釜鍦板潃
+ Short value = s7PLC.readInt16(address);
+ data.add(value);
+
+ }
+
+ return data;
+ }
+
+ private int extractAddressNumber(String address) {
+ String numberStr = address.replaceAll("\\D+", ""); // 浣跨敤姝e垯琛ㄨ揪寮忔彁鍙栨暟瀛楅儴鍒�
+ return Integer.parseInt(numberStr);
+ }
+
+
/**
* 浠庢寚瀹氱殑鍦板潃寮�濮� 杩炵画鎸塨it浣嶈鍙�
*
--
Gitblit v1.8.0