#
yxFwq
8 天以前 b8e32099c3d0542a966f9bf0d24bfec73cc13882
src/main/java/com/zy/core/thread/SteThread.java
@@ -44,6 +44,7 @@
    private SteProtocol steProtocol;
    private short heartBeatVal = 1;
    private boolean resetFlag = false;
    private boolean connectSte = false;
    public SteThread(SteSlave slave) {
        this.slave = slave;
@@ -52,7 +53,31 @@
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        this.connect();
        connectSte = this.connect();
        while(!connectSte){
            try {
                connectSte = this.connect();
                Thread.sleep(100);
            } catch (Exception e){
            }
        }
        // 启动线程自动重连
        new Thread(this::steConnect).start();
        // 启动读数据线程
        new Thread(this::readStatusSte).start();
        // 启动任务下发线程
        new Thread(this::taskIssued).start();
    }
    /**
     * 任务下发
     */
    private void taskIssued() {
        while (true) {
            try {
                int step = 1;
@@ -63,7 +88,7 @@
                switch (step) {
                    // 读数据
                    case 1:
                        readStatus();
//                        readStatus();
                        break;
                    // 写入数据
                    case 2:
@@ -74,12 +99,53 @@
                }
                // 心跳
//                heartbeat();
                Thread.sleep(500);
                Thread.sleep(50);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("STE写线程异常"+e.getMessage());
//                e.printStackTrace();
            }
        }
    }
    private void steConnect() {
        while (true) {
            try {
                Thread.sleep(1000);
                if(!connectSte){
                    try {
                        connectSte = this.connect();
                        Thread.sleep(100);
                    } catch (Exception e){
                    }
                }
            } catch (Exception e) {
                log.error("ste连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                initSte();
//                e.printStackTrace();
            }
        }
    }
    private void readStatusSte() {
        while (true) {
            try {
                Thread.sleep(50);
//                System.out.println("读线程"+ slave.getId());
                readStatus();
            } catch (Exception e) {
                log.error("RGV数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                initSte();
//                e.printStackTrace();
            }
        }
    }
    /**
@@ -123,7 +189,7 @@
     */
    private void readStatus(){
        try {
            OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) 27);
            OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) 28);
            if (result.IsSuccess) {
                if (null == steProtocol) {
                    steProtocol = new SteProtocol();
@@ -155,8 +221,8 @@
                boolean[] status2 = siemensS7Net.getByteTransform().TransBool(result.Content, 27, 1);
                steProtocol.setChargeStatus(status[0]?(short)1:(short)0);
                steProtocol.setTrack(status[1]?(short)1:(short)0);
                steProtocol.setChargeStatus(status2[0]?(short)1:(short)0);
                steProtocol.setTrack(status2[1]?(short)1:(short)0);
//                steProtocol.setLoad(status2[6]?(short)1:(short)0);
                steProtocol.setLoad(status2[7]?(short)1:(short)0);
@@ -218,6 +284,8 @@
        }
        command.setSteNo(slave.getId());
        OperateResult result = null;
        News.error("穿梭车写入命令"+JSON.toJSONString(command));
        // 开始任务
        if (!command.getComplete()) {
            //组织任务前,先清空写任务确认位,以及任务完成确认位
@@ -226,6 +294,7 @@
            siemensS7Net.Write("DB100.12", (short) 0);//起始点位
            siemensS7Net.Write("DB100.14", (short) 0);//目的点位
            siemensS7Net.Write("DB100.16.0", false);//任务开始确认位
            siemensS7Net.Write("DB100.16.1", false);//任务完成确认位
//            siemensS7Net.Write("DB100.18", (short) 0);//硫化罐号
            // 1.任务号
            OperateResult result0 = siemensS7Net.Write("DB100.2", command.getTaskNo().shortValue());