zhoushihao
2024-07-29 6facef85e36e9596a36bd85f3c822ceb3ed2ffe2
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
package com.mes.common;
 
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.mes.device.PlcParameterObject;
import com.mes.tools.InitUtil;
import com.mes.tools.S7control;
import lombok.extern.slf4j.Slf4j;
 
import java.util.Date;
 
 
/**
 * @Author : zhoush
 * @Date: 2024/4/9 15:13
 * @Description:
 */
@Slf4j
public class S7object extends Thread {
    public S7control plccontrol; // PLC通讯类实例
    private EPlcType plcType = EPlcType.S1200; // 西门子PLC类型
    private String ip = "192.168.20.2"; // plc ip地址
    private int port = 102; // plc 端口号
 
 
    public PlcParameterObject PlcMesObject;
    private static volatile S7object instance = null;
 
    private S7object() {
        if (plccontrol == null) {
            plccontrol = new S7control(plcType, ip, port, 0, 0);
 
            String PlcLoadGlass = System.getProperty("user.dir") + "/JsonFile/PlcCacheVerticalGlass.json";
            System.out.println("获取到的文件路径为:" + PlcLoadGlass);
            PlcMesObject = InitUtil.initword(PlcLoadGlass);
        }
    }
 
    // 单例模式 获取类的唯一实例
    public static S7object getinstance() {
        if (instance == null) {
            synchronized (S7object.class) {
                if (instance == null) {
                    instance = new S7object();
                }
            }
        }
        return instance;
    }
 
    @Override
    public void run() {
        while (this != null) {
            try {
                Thread.sleep(100);
 
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Date startDate = new Date();
            byte[] getplcvlues = plccontrol.readByte(PlcMesObject.getPlcAddressBegin(), PlcMesObject.getPlcAddressLength());
            Date endDate = new Date();
            log.info("大理片笼读取plc数据,开始时间:{},结束时间:{},共耗时:{}ms,结束扫码任务", startDate, endDate, endDate.getTime() - startDate.getTime());
            if (getplcvlues != null) {
                PlcMesObject.setPlcParameterList(getplcvlues);
                log.info("大理片当前进卧转立确认字为d01:{},d04:{},进笼送片任务确认字为:d03:{},d05:{},出片任务确认字为{}",
                        PlcMesObject.getPlcParameter("MESToD01").getValue(),
                        PlcMesObject.getPlcParameter("MESToD04").getValue(),
                        PlcMesObject.getPlcParameter("MESToD03").getValue(),
                        PlcMesObject.getPlcParameter("MESToD05").getValue(),
                        PlcMesObject.getPlcParameter("MESToPLC").getValue());
            }
        }
    }
}