wu
2024-01-15 733aaaac3ed6a41b78f74c0bcb4d5de7725d788c
springboot-vue3/src/main/java/com/example/springboot/component/S7control.java
@@ -37,13 +37,21 @@
        }
        return instance;
    }
    /**
     * 关闭西门子s7通讯连接
     */
    public void CloseS7client() {
        if (s7PLC == null)
            s7PLC.close();
            s7PLC.checkConnected();
    }
    /**
     * s7通讯连接状态
     */
    public boolean CheckConnected() {
        return s7PLC.checkConnected();
    }
    /**
@@ -157,10 +165,17 @@
     * @return 结果
     */
    public List<Short> ReadWord(List<String> address) {
        if (s7PLC==null)
              return null;
        return s7PLC.readInt16(address);
        if (s7PLC == null)
            return null;
        try {
            return s7PLC.readInt16(address);
        } catch (Exception e) {
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        }
    }
@@ -184,12 +199,19 @@
     * @return 结果
     */
    public List<Short> ReadWord(String address, int count) {
        if (s7PLC==null)
             return null;
        if (s7PLC == null)
            return null;
        List<String> addresslist = GetAddressList(address, count, 16);
        return s7PLC.readInt16(addresslist);
        try {
            return s7PLC.readInt16(addresslist);
        } catch (Exception e) {
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        }
    }
    /**
     * 按指定的地址 读取byte结果集
     * 
@@ -201,7 +223,15 @@
        if (s7PLC==null)
             return null;
       // List<String> addresslist = GetAddressList(address, count, 16);
        return s7PLC.readByte(address,count);
        try {
            return s7PLC.readByte(address, count);
        }catch (Exception e) {
            // 处理异常
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        }
    }
    /**
@@ -237,38 +267,27 @@
//读取String
    public List<String> readStrings(List<String> addressList) {
        if (s7PLC == null) {
            return null;
        }
        List<String> result = new ArrayList<>();
        for (String address : addressList) {
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);
//            System.out.println(bytes.toString());
          if (bytes != null) {
            String str = new String(bytes, StandardCharsets.UTF_8);
//          System.out.println(str);
//              if(str == null ){
//                   str = "";
//              }
            if (bytes != null) {
                String str = new String(bytes, StandardCharsets.UTF_8);
                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());
        } catch (Exception e) {
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            result.add(null);
        }
        return result;
    }
    return result;
}
//不连续地址写入Word
    public void WriteWord(List<String> address, List<Short> datas) {
@@ -304,7 +323,7 @@
        S7control.getinstance().WriteByte(addr, bytes);
    }
//读取不连续word
    public List<Short> readWords(List<String> addresses) {
        if (s7PLC == null) {
            return null;
@@ -328,11 +347,29 @@
    }
//读取时间
    public Long readtime(String address) {
        if (s7PLC==null)
            return null;
public Long readtime(String address) {
    if (s7PLC == null)
        return null;
    try {
        return s7PLC.readTime(address);
    } catch (Exception e) {
        System.out.println("读取 " + address + " 失败:" + e.getMessage());
        return null;
    }
}
    public void writetime(String address, long datas) {
        if (s7PLC == null)
            return;
        s7PLC.writeTime(address, datas); // 将数据写入单个地址
    }
    private int extractAddressNumber(String address) {
        String numberStr = address.replaceAll("\\D+", ""); // 使用正则表达式提取数字部分
@@ -348,11 +385,17 @@
     * @return Boolean结果
     */
    public List<Boolean> ReadBits(String address, int count) {
        if (s7PLC==null)
        if (s7PLC == null)
            return null;
        List<String> addresslist = GetAddressList(address, count, 1);
        return s7PLC.readBoolean(addresslist);
    }
        try {
            return s7PLC.readBoolean(addresslist);
        } catch (Exception e) {
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        }
    };