严智鑫
2025-09-22 fa095d4c1a440cf28a9ac82aaf9fe26984010670
JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/common/S7objectMachine.java
@@ -3,7 +3,6 @@
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.mes.device.PlcParameterInfo;
import com.mes.device.PlcParameterObject;
import com.mes.service.ModbusTcp;
import com.mes.tools.InitUtil;
import com.mes.tools.S7control;
import lombok.extern.slf4j.Slf4j;
@@ -17,14 +16,15 @@
 * @Description:
 */
@Slf4j
public class S7objectMachine extends Thread {
public class S7objectMachine{
    public S7control plccontrol; // PLC通讯类实例
    private EPlcType plcType = EPlcType.S1200; // 西门子PLC类型
    private String ip = ""; // plc ip地址
    private int port = 102; // plc 端口号
    private String plcFileName=""; // json名称
    public PlcParameterObject PlcMesObject;
    public S7objectMachine(String ip,int port,String plcFileName,EPlcType plcType) {
    public S7objectMachine(String ip, int port, String plcFileName, EPlcType plcType) {
        initialize(ip,port,plcFileName,plcType);
        if (plccontrol == null) {
            plccontrol = new S7control(this.plcType, this.ip, this.port, 0, 0);
@@ -51,34 +51,28 @@
        }
        log.info(logInfo);
    }
    @Override
    public void run() {
        while (this != null) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                byte[] resultValues=new byte[PlcMesObject.getPlcAddressLength()];
                int maxRead=1092;
                int size=PlcMesObject.getPlcAddressLength()%maxRead==0?
                        (PlcMesObject.getPlcAddressLength()/maxRead):
                        (PlcMesObject.getPlcAddressLength()/maxRead+1);
                for (int i = 0; i <size ; i++) {
                    int begin=i*maxRead;
                    int length=(i==size-1?PlcMesObject.getPlcAddressLength()-begin:maxRead);
                    String beginAddress=PlcMesObject.getPlcAddressBegin().substring(0, PlcMesObject.getPlcAddressBegin().indexOf("."))+"."+begin;
                    byte[] getplcvlues = plccontrol.readByte(beginAddress, length);
                    System.arraycopy(getplcvlues,0,resultValues,begin,length);
                }
                if (resultValues != null) {
                    PlcMesObject.setPlcParameterList(resultValues);
                }
            } catch (Exception e) {
                //log.info("异常:ip:{},port:{}",this.ip,this.port);
            }
    public void readData() {
        try {
            byte[] resultValues=plccontrol.readByte(PlcMesObject.getPlcAddressBegin(), PlcMesObject.getPlcAddressLength());
//            byte[] resultValues=new byte[PlcMesObject.getPlcAddressLength()];
//            int maxRead=1092;
//            int size=PlcMesObject.getPlcAddressLength()%maxRead==0?
//                    (PlcMesObject.getPlcAddressLength()/maxRead):
//                    (PlcMesObject.getPlcAddressLength()/maxRead+1);
//            for (int i = 0; i <size ; i++) {
//                int begin=i*maxRead;
//                int length=(i==size-1?PlcMesObject.getPlcAddressLength()-begin:maxRead);
//                String beginAddress=PlcMesObject.getPlcAddressBegin().substring(0, PlcMesObject.getPlcAddressBegin().indexOf("."))+"."+begin;
//                byte[] getplcvlues = plccontrol.readByte(beginAddress, length);
//                System.arraycopy(getplcvlues,0,resultValues,begin,length);
//            }
            if (resultValues.length>0) {
                PlcMesObject.setPlcParameterList(resultValues);
            }
        } catch (Exception e) {
            PlcMesObject=null;
            log.info("异常:ip:{},port:{},错误{},{}",this.ip,this.port,e.getMessage(),e.toString());
        }
    }
}