严智鑫
2025-04-03 dedf9da2dc20825fbd3dee51f0a0546782ec0f9d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.mes.common;
 
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;
 
import java.util.LinkedHashMap;
 
 
/**
 * @Author : yanzhixin
 * @Date: 2024/4/9 15:13
 * @Description:
 */
@Slf4j
public class S7objectMachine extends Thread {
    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) {
        initialize(ip,port,plcFileName,plcType);
        if (plccontrol == null) {
            plccontrol = new S7control(this.plcType, this.ip, this.port, 0, 0);
            //String plcFileUrl = System.getProperty("user.dir") + "D:/HangZhouMes/JsonFile/"+this.plcFileName+".json";
            String plcFileUrl = "D:/mes/JsonFile/"+this.plcFileName+".json";
            PlcMesObject = InitUtil.initword(plcFileUrl);
        }
    }
    public void initialize(String ip,int port,String plcFileName,EPlcType plcType){
        this.ip = ip;
        this.port = port;
        this.plcFileName = plcFileName;
        this.plcType=  plcType;
    }
 
    /**
     * 打印参数值
     */
    public void consoleLogInfo(){
        String logInfo=this.plcFileName+" ";
        LinkedHashMap<String, PlcParameterInfo> thisPlcParameterInfo=PlcMesObject.getPlcParameterMap();
        for (String key:thisPlcParameterInfo.keySet()) {
            logInfo+=key+":"+thisPlcParameterInfo.get(key).getValue()+",";
        }
        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);
            }
 
        }
    }
}