wuyouming666
2024-05-27 a9bcb83a7942e3da175ab6da9ed16eda38f49180
hangzhoumesParent/common/servicebase/src/main/java/com/mes/tools/S7control.java
@@ -3,6 +3,7 @@
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 java.util.ArrayList;
import java.util.List;
@@ -24,13 +25,20 @@
            s7PLC.close();
        }
    }
    /**
     * 重启西门子s7通讯连接
     */
    public void reStartS7client() {
    public boolean reStartS7client() {
        if (s7PLC != null) {
            try {
            s7PLC.hotRestart();
                return true;
            } catch (Exception ex) {
                return false;
        }
        }
        return false;
    }
@@ -47,14 +55,24 @@
     * @param address 地址
     * @param data    word的值
     */
    public void writeWord(String address, int data) {
    public boolean writeWord(String address, int data) {
        if (s7PLC == null) {
            return;
            return false;
        }
        else if(!s7PLC.checkConnected()) {
            s7PLC.hotRestart();
        }
        boolean result = false;
        int tryCount = 2;
        do {
            try {
        s7PLC.writeUInt16(address, data);
                result = true;
            } catch (Exception ex) {
                reStartS7client();
            } finally {
                tryCount -= 1;
            }
        }
        while (!result && tryCount > 0);
        return result;
    }
    /**
@@ -63,20 +81,31 @@
     * @param address 地址
     * @param datas   word的值
     */
    public void writeWord(String address, List<Integer> datas) {
    public boolean writeWord(String address, List<Integer> datas) {
        if (s7PLC == null) {
            return;
            return false;
        }
        else if(!s7PLC.checkConnected()) {
            s7PLC.hotRestart();
        }
        boolean result = false;
        int tryCount = 2;
        // s7PLC.write(address, data);
        List<String> addresslist = getAddressList(address, datas.size(), 16);
        MultiAddressWrite addressWrite = new MultiAddressWrite();
        for (int i = 0; i < datas.size(); i++) {
            addressWrite.addUInt16(addresslist.get(i), datas.get(i));
        }
        do {
            try {
            s7PLC.writeMultiData(addressWrite);
                result = true;
            } catch (Exception ex) {
                reStartS7client();
            } finally {
                tryCount -= 1;
            }
        }
        while (!result && tryCount > 0);
        return result;
    }
    /**
@@ -93,14 +122,24 @@
     * @param address 地址
     * @param data    Bit的值
     */
    public void writeBit(String address, Boolean data) {
    public boolean writeBit(String address, Boolean data) {
        if (s7PLC == null) {
            return;
            return false;
        }
        else if(!s7PLC.checkConnected()) {
            s7PLC.hotRestart();
        }
        boolean result = false;
        int tryCount = 2;
        do {
            try {
            s7PLC.writeBoolean(address, data);
                result = true;
            } catch (Exception ex) {
                reStartS7client();
            } finally {
                tryCount -= 1;
            }
        }
        while (!result && tryCount > 0);
        return result;
    }
    /**
@@ -109,20 +148,30 @@
     * @param address 地址
     * @param datas   bit的值
     */
    public void writeBit(List<String> address, List<Boolean> datas) {
    public boolean writeBit(List<String> address, List<Boolean> datas) {
        if (s7PLC == null) {
            return;
            return false;
        }
        else if(!s7PLC.checkConnected()) {
            s7PLC.hotRestart();
        }
        // s7PLC.write(address, data);
        MultiAddressWrite addressWrite = new MultiAddressWrite();
        for (int i = 0; i < address.size(); i++) {
            addressWrite.addBoolean(address.get(i), datas.get(i));
        }
        boolean result = false;
        int tryCount = 2;
        do {
            try {
            s7PLC.writeMultiData(addressWrite);
                result = true;
            } catch (Exception ex) {
                reStartS7client();
            } finally {
                tryCount -= 1;
            }
        }
        while (!result && tryCount > 0);
        return result;
    }
    /**
@@ -131,20 +180,31 @@
     * @param address 地址
     * @param datas   word的值
     */
    public void writeBit(String address, List<Boolean> datas) {
    public boolean writeBit(String address, List<Boolean> datas) {
        if (s7PLC == null) {
            return;
            return false;
        }
        else if(!s7PLC.checkConnected()) {
            s7PLC.hotRestart();
        }
        // s7PLC.write(address, data);
        List<String> addresslist = getAddressList(address, datas.size(), 1);
        MultiAddressWrite addressWrite = new MultiAddressWrite();
        for (int i = 0; i < datas.size(); i++) {
            addressWrite.addBoolean(addresslist.get(i), datas.get(i));
        }
        boolean result = false;
        int tryCount = 2;
        do {
            try {
            s7PLC.writeMultiData(addressWrite);
                result = true;
            } catch (Exception ex) {
                reStartS7client();
            } finally {
                tryCount -= 1;
            }
        }
        while (!result && tryCount > 0);
        return result;
    }
    /**
@@ -153,15 +213,24 @@
     * @param address 地址
     * @param datas   byte的值
     */
    public void writeByte(String address, byte[] datas) {
    public boolean writeByte(String address, byte[] datas) {
        if (s7PLC == null) {
            return;
            return false;
        }
        else if(!s7PLC.checkConnected()) {
            s7PLC.hotRestart();
        }
        // s7PLC.write(address, data);
        boolean result = false;
        int tryCount = 2;
        do {
            try {
        s7PLC.writeByte(address, datas);
                result = true;
            } catch (Exception ex) {
                reStartS7client();
            } finally {
                tryCount -= 1;
            }
        }
        while (!result && tryCount > 0);
        return result;
    }
    /**
@@ -174,16 +243,17 @@
        if (s7PLC == null) {
            return null;
        }
        List<Integer> result = null;
        try {
            return s7PLC.readUInt16(address);
            result = s7PLC.readUInt16(address);
        } catch (Exception e) {
            s7PLC.hotRestart();
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        }
    }
        } finally {
            return result;
        }
    }
    /**
@@ -197,15 +267,15 @@
        if (s7PLC == null) {
            return null;
        }
        List<Integer> result = null;
        List<String> addresslist = getAddressList(address, count, 16);
        try {
            return s7PLC.readUInt16(addresslist);
            result = s7PLC.readUInt16(addresslist);
        } catch (Exception e) {
            s7PLC.hotRestart();
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        } finally {
            return result;
        }
    }
@@ -221,14 +291,15 @@
            return null;
        }
        // List<String> addresslist = GetAddressList(address, count, 16);
        byte[] bytes = null;
        try {
            return s7PLC.readByte(address, count);
            bytes = s7PLC.readByte(address, count);
        } catch (Exception e) {
            // 处理异常
            s7PLC.hotRestart();
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        } finally {
            return bytes;
        }
    }
@@ -243,14 +314,15 @@
        if (s7PLC == null) {
            return null;
        }
        List<Boolean> values = new ArrayList<>();
        try {
            return s7PLC.readBoolean(addresslist);
            values = s7PLC.readBoolean(addresslist);
        } catch (Exception e) {
            // 处理异常
            s7PLC.hotRestart();
            return null;
        } finally {
            return values;
        }
    }
   /* //读取不连续地址bit
@@ -274,22 +346,21 @@
    }*/
    //读取字符串
    public String readString(String address) {
        if (s7PLC == null) {
            return null;
        }
        String result = null;
        try {
            return s7PLC.readString(address);
            result = s7PLC.readString(address);
        } catch (Exception e) {
            s7PLC.hotRestart();
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        } finally {
            return result;
        }
    }
    //读取时间
@@ -297,27 +368,36 @@
        if (s7PLC == null) {
            return null;
        }
        Long result = null;
        try {
            return s7PLC.readTime(address);
            result = s7PLC.readTime(address);
        } catch (Exception e) {
            s7PLC.hotRestart();
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        } finally {
            return result;
        }
    }
    public void writetime(String address, long datas) {
    public boolean writetime(String address, long datas) {
        if (s7PLC == null) {
            return;
            return false;
        }
        else if(!s7PLC.checkConnected())
        {
            s7PLC.hotRestart();
        }
        boolean result = false;
        int tryCount = 2;
        do {
            try {
        s7PLC.writeTime(address, datas); // 将数据写入单个地址
                result = true;
            } catch (Exception ex) {
                reStartS7client();
            } finally {
                tryCount -= 1;
            }
        }
        while (!result && tryCount > 0);
        return result;
    }
@@ -332,15 +412,16 @@
        if (s7PLC == null) {
            return null;
        }
        List<Boolean> values = new ArrayList<>();
        List<String> addresslist = getAddressList(address, count, 1);
        try {
            return s7PLC.readBoolean(addresslist);
            values = s7PLC.readBoolean(addresslist);
        } catch (Exception e) {
            s7PLC.hotRestart();
            System.out.println("读取 " + address + " 失败:" + e.getMessage());
            return null;
        } finally {
            return values;
        }
    }
    ;