*
L
2025-07-17 9e61d8c647482905e46c7e4ac04a9054bde411f6
src/main/java/com/zy/core/thread/RgvThread.java
@@ -47,6 +47,7 @@
     * 工位2复位信号
     */
    private boolean resetFlag2 = false;
    private boolean connectRgv = false;
    public RgvThread(RgvSlave slave) {
        this.slave = slave;
@@ -55,9 +56,41 @@
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        this.connect();
        connectRgv = this.connect();
        while(!connectRgv){
            try {
                connectRgv = this.connect();
                Thread.sleep(100);
            } catch (Exception e){
            }
        }
        // 启动线程自动重连
        new Thread(this::rgvConnect).start();
        // 启动读数据线程
        new Thread(this::readStatusRgv).start();
        // 启动任务下发线程
        new Thread(this::taskIssued).start();
    }
    /**
     * 任务下发
     */
    private void taskIssued() {
        while (true) {
            try {
                if(!connectRgv){
                    try {
                        Thread.sleep(1000L);
                    } catch (Exception e){
                    }
                    continue;
                }
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Rgv, slave.getId());
                if (task != null) {
@@ -66,7 +99,7 @@
                switch (step) {
                    // 读数据
                    case 1:
                        readStatus();
//                        readStatus();
                        break;
                    // 工位1、2写入数据
                    case 2:
@@ -100,6 +133,26 @@
                        command.setCommand((short)0);
                        write(command);
                        break;
                    // 复位
                    case 8:
                        RgvCommand command4 = (RgvCommand) task.getData();
                        if (null == command4) {
                            command4 = new RgvCommand();
                        }
                        command4.setRgvNo(slave.getId()); // RGV编号
//                        command4.setTaskNo1((short) command4); // 工作号
                        command4.setAckFinish1((short) 1);  // 任务完成确认位
                        command4.setTaskMode1(RgvTaskModeType.NONE); // 任务模式
                        command4.setSourceStaNo1((short)0);     // 源站
                        command4.setDestinationStaNo1((short)0);     // 目标站
                        command4.setTaskNo2((short) 0); // 工作号
                        command4.setAckFinish2((short) 1);  // 任务完成确认位
                        command4.setTaskMode2(RgvTaskModeType.NONE); // 任务模式
                        command4.setSourceStaNo2((short)0);     // 源站
                        command4.setDestinationStaNo2((short)0);     // 目标站
                        command4.setCommand((short)0);
                        write(command4);
                        break;
                    // 回原点  避让
                    case 9:
                        RgvCommand commandAvoidanceXY = (RgvCommand) task.getData();
@@ -107,11 +160,11 @@
                            commandAvoidanceXY = new RgvCommand();
                        }
                        commandAvoidanceXY.setRgvNo(slave.getId()); // RGV编号
                        commandAvoidanceXY.setTaskNo1((short) 9999); // 工作号
                        commandAvoidanceXY.setTaskNo1((short) 32222); // 工作号
                        commandAvoidanceXY.setAckFinish1((short) 1);  // 任务完成确认位
                        commandAvoidanceXY.setTaskMode1(RgvTaskModeType.X_MOVE); // 任务模式
                        commandAvoidanceXY.setSourceStaNo1((short)0);     // 源站
                        commandAvoidanceXY.setDestinationStaNo1(slave.getId()==1? (short) 101:(short)117);     // 目标站
                        commandAvoidanceXY.setDestinationStaNo1(slave.getId()==1? (short) 101:(short)116);     // 目标站
                        commandAvoidanceXY.setTaskNo2((short) 0); // 工作号
                        commandAvoidanceXY.setAckFinish2((short) 1);  // 任务完成确认位
                        commandAvoidanceXY.setTaskMode2(RgvTaskModeType.X_MOVE); // 任务模式
@@ -120,15 +173,67 @@
                        commandAvoidanceXY.setCommand((short)0);
                        write(commandAvoidanceXY);
                        break;
                    // 工位1完成、2写入数据
                    case 10:
                        write10((RgvCommand) task.getData());
                        break;
                    default:
                        break;
                }
                Thread.sleep(500);
                Thread.sleep(100);
            } catch (Exception e) {
                log.error("RGV写线程异常"+e.getMessage());
//                e.printStackTrace();
            }
        }
    }
    private void rgvConnect() {
        while (true) {
            try {
                Thread.sleep(1000L);
                if(!connectRgv){
                    try {
                        connectRgv = this.connect();
                        Thread.sleep(100L);
                    } catch (Exception e){
                    }
                }
            } catch (Exception e) {
                log.error("rgv连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                initRgv();
//                e.printStackTrace();
            }
        }
    }
    private void readStatusRgv() {
        while (true) {
            try {
                if(!connectRgv){
                    try {
                        Thread.sleep(1000L);
                    } catch (Exception e){
                    }
                    continue;
                }
                Thread.sleep(40);
                readStatus();
            } catch (Exception e) {
                log.error("RGV读线程异常"+e.getMessage());
                log.error("RGV数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                initRgv();
//                e.printStackTrace();
            }
        }
    }
    /**
@@ -152,6 +257,17 @@
        rgvProtocol.setxSpeed((short) 0);
        rgvProtocol.setxDistance((short) 0);
        rgvProtocol.setxDuration((short) 0);
        try {
            // 根据实时信息更新数据库
            BasRgvService basRgvService = SpringUtils.getBean(BasRgvService.class);
            BasRgv basRgv = new BasRgv();
            basRgv.setRgvNo(slave.getId());
            basRgv.setRgvSts((int)rgvProtocol.getMode());
            basRgv.setLoaded2(rgvProtocol.getLoaded2().intValue());
            if (!basRgvService.updateById(rgvProtocol.toSqlModel(basRgv))){
                log.error("RGV plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            }
        } catch (Exception ignore){}
    }
    @Override
@@ -238,6 +354,8 @@
                    BasRgv basRgv = new BasRgv();
                    basRgv.setRgvNo(slave.getId());
                    basRgv.setRgvSts((int)rgvProtocol.getMode());
                    basRgv.setLoaded2(rgvProtocol.getLoaded2().intValue());
                    basRgv.setNowStaNo(rgvProtocol.getRgvPosI());
                    if (!basRgvService.updateById(rgvProtocol.toSqlModel(basRgv))){
                        log.error("RGV plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
                    }
@@ -319,6 +437,69 @@
        }
    }
    /**
     * 工位1完成、2同时写入数据
     */
    private boolean write10(RgvCommand command) throws InterruptedException {
        if (null == command) {
            log.error("RGV写入命令为空");
            return false;
        }
//        convertRow(command);
        command.setRgvNo(slave.getId());
        short[] array = new short[11];
        array[0] = command.getAckFinish1();
        array[1] = command.getTaskNo1();
        array[2] = command.getTaskMode1();
        array[3] = command.getSourceStaNo1();
        array[4] = command.getDestinationStaNo1();
        array[5] = command.getAckFinish2();
        array[6] = command.getTaskNo2();
        array[7] = command.getTaskMode2();
        array[8] = command.getSourceStaNo2();
        array[9] = command.getDestinationStaNo2();
        array[10] = command.getCommand();
        OperateResult result = siemensNet.Write("DB100.0", array);
        if (command.getAckFinish2() == 0) {
            short commandFinish = 2;  //工位2任务同时写入
            Thread.sleep(100L);
            result = siemensNet.Write("DB100.20", commandFinish);
        }
        try {
            // 日志记录
            BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class);
            BasRgvOpt basRgvOpt = new BasRgvOpt(
                    command.getTaskNo1().intValue(),
                    command.getTaskNo2().intValue(),
                    command.getRgvNo(),
                    new Date(),
                    command.getTaskModeType1().toString(),
                    command.getSourceStaNo1().intValue(),
                    command.getDestinationStaNo1().intValue(),
                    command.getSourceStaNo2().intValue(),
                    command.getDestinationStaNo2().intValue(),
                    null,
                    new Date(),
                    null
            );
            bean.insert(basRgvOpt);
        } catch (Exception ignore) {}
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            this.readStatus();
            log.info("RGV 命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
            return true;
        } else {
            OutputQueue.RGV.offer(MessageFormat.format("【{0}】写入RGV plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("写入RGV plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
    }
    /**
     * 工位1写入数据
     */
@@ -328,7 +509,7 @@
            return false;
        }
        siemensNet.Write("DB100.20", command.getCommand());
//        siemensNet.Write("DB100.20", command.getCommand());
        command.setRgvNo(slave.getId());
        short[] array = new short[5];