严智鑫
2024-03-31 895f490f4223d5b15ec948ba2155b005018aa91d
CacheGlassModule/src/main/java/com/mes/common/PlcTools/S7control.java
@@ -1,7 +1,9 @@
package com.mes.common.PlcTools;
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;
@@ -9,15 +11,37 @@
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通讯类实例
    public S7control(EPlcType plcType, String ip, int port, int rack, int slot) {
    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,0,0);
    }
    }
    public S7control(EPlcType plcType,String ip,int port,int a,int b) {
        if (s7PLC == null)
            s7PLC = new S7PLC(plcType, ip, port,a,b);
    }
    // 单例模式 获取类的唯一实例
    public static S7control getinstance() {
        if (instance == null) {
            synchronized (S7control.class) {
                if (instance == null)
                    instance = new S7control();
            }
        }
        return instance;
    }
    /**
     * 关闭西门子s7通讯连接
     */
@@ -33,7 +57,7 @@
    public boolean CheckConnected() {
        return s7PLC.checkConnected();
    }
    /**
     * 按指定的地址 写入一个word
     * 
@@ -156,7 +180,7 @@
        }
    }
    private int getIndexFromAddress(String address) {
@@ -181,7 +205,6 @@
    public List<Short> ReadWord(String address, int count) {
        if (s7PLC == null)
            return null;
        List<String> addresslist = GetAddressList(address, count, 16);
        try {
            return s7PLC.readInt16(addresslist);
@@ -191,7 +214,13 @@
            return null;
        }
    }
    public byte[] Readbyte(String address, int count) {
        byte[] byt=new byte[count];
        int wordcount=((count%2==0)?count/2:count+1);
        List<Short> word=ReadWord(address,wordcount);
        return byt;
    }
    /**
     * 按指定的地址 读取byte结果集
     * 
@@ -269,6 +298,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 +364,7 @@
            glassidlist.add(Byte.valueOf(String.valueOf(iditem)));
        }
        byte[] bytes = Bytes.toArray(glassidlist);
        WriteByte(addr, bytes);
        S7control.getinstance().WriteByte(addr, bytes);
    }
//读取不连续word
@@ -410,4 +474,11 @@
        }
        return addresslist;
    }
    public void writeString(String addr,String data) {
        s7PLC.writeString(addr,data);
    }
    public String readStrings(String addr) {
        return s7PLC.readString(addr);
    }
}