From f7ecb62a4fcfc86893458f1298995692315ff79f Mon Sep 17 00:00:00 2001 From: 严智鑫 <test> Date: 星期三, 06 三月 2024 10:31:45 +0800 Subject: [PATCH] 更新 --- UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java | 71 ++++++++++++++++++++++++++++++----- 1 files changed, 61 insertions(+), 10 deletions(-) diff --git a/UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java b/UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java index 9cc9e37..d62cad3 100644 --- a/UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java +++ b/UnLoadGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java @@ -1,23 +1,39 @@ package com.mes.common.PlcTools; - -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; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + public class S7control { S7PLC s7PLC; // PLC閫氳绫诲疄渚� - - public S7control(EPlcType plcType, String ip, int port, int rack, int slot) { + 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,0,0); - } + } + + // 鍗曚緥妯″紡 鑾峰彇绫荤殑鍞竴瀹炰緥 + public static S7control getinstance() { + if (instance == null) { + synchronized (S7control.class) { + if (instance == null) + instance = new S7control(); + } + } + return instance; + } + /** * 鍏抽棴瑗块棬瀛恠7閫氳杩炴帴 */ @@ -33,7 +49,7 @@ public boolean CheckConnected() { return s7PLC.checkConnected(); } - + /** * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆涓�涓獁ord * @@ -269,6 +285,41 @@ } + + + + public List<String> readStringsandword(List<String> addressList) { + if (s7PLC == null) { + return null; + } + List<String> result = new ArrayList<>(); + for (String address : addressList) { + try { + if (address.contains("-")) { + address = address.substring(0, address.indexOf("-")); + + byte[] bytes = s7PLC.readByte(address, 14); + if (bytes != null) { + String str = new String(bytes, StandardCharsets.UTF_8); + result.add(str); + } + + } else { + Short value = s7PLC.readInt16(address); + result.add(value.toString()); + + } + } 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) @@ -300,7 +351,7 @@ glassidlist.add(Byte.valueOf(String.valueOf(iditem))); } byte[] bytes = Bytes.toArray(glassidlist); - WriteByte(addr, bytes); + S7control.getinstance().WriteByte(addr, bytes); } //璇诲彇涓嶈繛缁瓀ord -- Gitblit v1.8.0