From 24385ce9fee382e9600ce03108a814a66990981c Mon Sep 17 00:00:00 2001
From: ZengTao <2773468879@qq.com>
Date: 星期二, 05 十二月 2023 08:10:09 +0800
Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/CanadaMes
---
springboot-vue3/src/main/java/com/example/springboot/component/S7control.java | 220 +++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 190 insertions(+), 30 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 5942a98..226f0b0 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,24 +1,30 @@
package com.example.springboot.component;
+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;
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 org.apache.ibatis.jdbc.Null;
public class S7control {
S7PLC s7PLC; // PLC閫氳绫诲疄渚�
- private EPlcType plcType = EPlcType.S1200; // 瑗块棬瀛怭LC绫诲瀷
- private String ip = "127.0.0.1"; // plc ip鍦板潃
- private int port = 21; // plc 绔彛鍙�
+ private EPlcType plcType = EPlcType.S1500; // 瑗块棬瀛怭LC绫诲瀷
+ private String ip = "192.168.10.1"; // plc ip鍦板潃
+ private int port = 102; // plc 绔彛鍙�
private static volatile S7control instance = null;
private S7control() {
if (s7PLC == null)
- s7PLC = new S7PLC(plcType, ip, port);
+ s7PLC = new S7PLC(plcType, ip, port,0,0);
}
// 鍗曚緥妯″紡 鑾峰彇绫荤殑鍞竴瀹炰緥
@@ -47,8 +53,10 @@
* @param data word鐨勫��
*/
public void WriteWord(String address, short data) {
- if (!s7PLC.checkConnected())
+ if (s7PLC==null)
+ {
return;
+ }
s7PLC.writeInt16(address, data);
}
@@ -59,7 +67,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);
@@ -76,17 +84,11 @@
* @param address 鍦板潃
* @param datas word鐨勫��
*/
- public void WriteWord(List<String> address, List<Short> datas) {
- if (!s7PLC.checkConnected())
- 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);
- }
+
+
+
+
/**
* 鎸夋寚瀹氱殑鍦板潃 鍐欏叆涓�涓狟it
@@ -95,7 +97,7 @@
* @param data Bit鐨勫��
*/
public void WriteBit(String address, Boolean data) {
- if (!s7PLC.checkConnected())
+ if (s7PLC==null)
return;
s7PLC.writeBoolean(address, data);
}
@@ -107,7 +109,7 @@
* @param datas bit鐨勫��
*/
public void WriteBit(List<String> address, List<Boolean> datas) {
- if (!s7PLC.checkConnected())
+ if (s7PLC==null)
return;
// s7PLC.write(address, data);
@@ -125,7 +127,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);
@@ -135,6 +137,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缁撴灉闆�
@@ -143,9 +157,23 @@
* @return 缁撴灉
*/
public List<Short> ReadWord(List<String> address) {
- if (!s7PLC.checkConnected())
- return null;
+ if (s7PLC==null)
+ return null;
return s7PLC.readInt16(address);
+ }
+
+
+
+ private int getIndexFromAddress(String address) {
+
+ // 鍙互瑙f瀽鍑哄湴鍧�涓殑鏁板瓧閮ㄥ垎锛屽苟杞崲涓烘暣鏁�
+ return 0;
+ }
+
+ private String getAddressFromIndex(int index) {
+
+ // 鏁存暟杞崲涓哄湴鍧�鏍煎紡鐨勫瓧绗︿覆
+ return "";
}
/**
@@ -156,11 +184,24 @@
* @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);
}
/**
@@ -170,10 +211,123 @@
* @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 || 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);
+// 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);
+ }
+// 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;
+ }
+
+
+ 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;
+ }
+
+ public Long readtime(String address) {
+ if (s7PLC==null)
+ return null;
+ return s7PLC.readTime(address);
+ }
+
+ private int extractAddressNumber(String address) {
+ String numberStr = address.replaceAll("\\D+", ""); // 浣跨敤姝e垯琛ㄨ揪寮忔彁鍙栨暟瀛楅儴鍒�
+ return Integer.parseInt(numberStr);
+ }
+
/**
* 浠庢寚瀹氱殑鍦板潃寮�濮� 杩炵画鎸塨it浣嶈鍙�
@@ -183,16 +337,18 @@
* @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;
@@ -200,11 +356,13 @@
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;
- 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) {
@@ -212,7 +370,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