springboot-vue3/src/main/java/com/example/springboot/component/S7control.java
@@ -10,15 +10,15 @@
public class S7control {
    S7PLC s7PLC; // PLC通讯类实例
    private EPlcType plcType = EPlcType.S1200; // 西门子PLC类型
    private String ip = "127.0.0.1"; // plc ip地址
    private int port = 21; // plc 端口号
    private EPlcType plcType = EPlcType.S1500; // 西门子PLC类型
    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 +47,10 @@
     * @param data    word的值
     */
    public void WriteWord(String address, short data) {
        if (!s7PLC.checkConnected())
        if (s7PLC==null)
        {
            return;
        }
        s7PLC.writeInt16(address, data);
    }
@@ -59,7 +61,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);
@@ -77,7 +79,7 @@
     * @param datas   word的值
     */
    public void WriteWord(List<String> address, List<Short> datas) {
        if (!s7PLC.checkConnected())
        if (s7PLC==null)
            return;
        // s7PLC.write(address, data);
@@ -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,8 +157,8 @@
     * @return 结果
     */
    public List<Short> ReadWord(List<String> address) {
        if (!s7PLC.checkConnected())
            return null;
        if (s7PLC==null)
              return null;
        return s7PLC.readInt16(address);
    }
@@ -156,11 +170,25 @@
     * @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,8 +198,8 @@
     * @return Boolean结果
     */
    public List<Boolean> ReadBits(List<String> addresslist) {
        if (!s7PLC.checkConnected())
            return null;
        if (s7PLC==null)
             return null;
        return s7PLC.readBoolean(addresslist);
    }
@@ -183,16 +211,16 @@
     * @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;
@@ -204,7 +232,8 @@
        } 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 +241,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;
    }