#
zwl
2025-09-12 ac0d8b029f0b51615be54b28d66519594949c3d9
src/main/java/com/zy/core/thread/SiemensCrnThread.java
@@ -38,6 +38,11 @@
    private CrnProtocol crnProtocol;
    private boolean resetFlag = false;
    /**
     * 堆垛机是否在回原点运动中标记
     */
    private boolean backHpFlag = false;
    public SiemensCrnThread(CrnSlave slave) {
        this.slave = slave;
    }
@@ -142,10 +147,11 @@
     */
    private void readStatus(){
        try {
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 70);
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 56);
            if (result.IsSuccess) {
                if (null == crnProtocol) {
                    crnProtocol = new CrnProtocol();
                    crnProtocol.setCrnNo(slave.getId());
                }
                crnProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 0));
                crnProtocol.setTaskNo(siemensNet.getByteTransform().TransInt16(result.Content, 2));
@@ -162,18 +168,21 @@
                crnProtocol.setTemp3(siemensNet.getByteTransform().TransInt16(result.Content, 24));
                crnProtocol.setTemp4(siemensNet.getByteTransform().TransInt16(result.Content, 26));
                crnProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 28));
                crnProtocol.setySpeed(siemensNet.getByteTransform().TransInt16(result.Content, 30));
                crnProtocol.setzSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 32));
                crnProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 34));
                crnProtocol.setyDistance(siemensNet.getByteTransform().TransInt16(result.Content, 36));
                crnProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 38));
                crnProtocol.setyDuration(siemensNet.getByteTransform().TransInt16(result.Content, 40));
                crnProtocol.setySpeed(siemensNet.getByteTransform().TransInt16(result.Content, 32));
                crnProtocol.setzSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 36));
                crnProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 40));
                crnProtocol.setyDistance(siemensNet.getByteTransform().TransInt16(result.Content, 44));
                crnProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 48));
                crnProtocol.setyDuration(siemensNet.getByteTransform().TransInt16(result.Content, 52));
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                // 复位信号
                if (crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) {
                    if (resetFlag) {
                        if(crnProtocol.getTaskNo()==9999){
                            backHpFlag = false;
                        }
                        CrnCommand crnCommand = new CrnCommand();
                        crnCommand.setAckFinish((short)1);
                        if (write(crnCommand)) {
@@ -218,14 +227,16 @@
    /**
     * 写入数据
     */
    private boolean write(CrnCommand command) throws InterruptedException {
    private synchronized boolean write(CrnCommand command) throws InterruptedException {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        if (null == command) {
            log.error("堆垛机写入命令为空");
            return false;
        }
        convertRow(command);
//        convertRow(command);
        command.setCrnNo(slave.getId());
        short[] array = new short[12];
        short[] array = new short[10];
        array[0] = command.getAckFinish();
        array[1] = command.getTaskNo();
        array[2] = command.getTaskMode();
@@ -235,14 +246,62 @@
        array[6] = command.getDestinationPosX();
        array[7] = command.getDestinationPosY();
        array[8] = command.getDestinationPosZ();
        array[9] = command.getSourceStaNo();
        array[10] = command.getDestinationStaNo();
        array[11] = command.getCommand();
//        array[9] = command.getSourceStaNo();
//        array[10] = command.getDestinationStaNo();
        array[9] = command.getCommand();
        OperateResult result18 = siemensNet.Write("DB100.18", (short)0);
        Thread.sleep(200L);
        OperateResult result = siemensNet.Write("DB100.0", array);
        Thread.sleep(100L);
        OperateResult result1 = siemensNet.Write("DB100.10", 1);
        int i=0;
        do{
            //堆垛机任务写入后,回读一次,看是否成功
            Thread.sleep(100);
            OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 20);
            if (resultRead.IsSuccess) {
                CrnCommand one = new CrnCommand();
                one.setTaskNo(siemensNet.getByteTransform().TransInt16(resultRead.Content, 2));
                one.setTaskMode(siemensNet.getByteTransform().TransInt16(resultRead.Content, 4));
                one.setSourcePosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 6));
                one.setSourcePosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 8));
                one.setSourcePosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 10));
                one.setDestinationPosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 12));
                one.setDestinationPosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 14));
                one.setDestinationPosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 16));
                if (!command.getTaskNo().equals(one.getTaskNo()) || !command.getTaskMode().equals(one.getTaskMode())
                        || !command.getSourcePosX().equals(one.getSourcePosX()) || !command.getSourcePosY().equals(one.getSourcePosY())
                        || !command.getSourcePosZ().equals(one.getSourcePosZ()) || !command.getDestinationPosX().equals(one.getDestinationPosX())
                        || !command.getDestinationPosY().equals(one.getDestinationPosY()) || !command.getDestinationPosZ().equals(one.getDestinationPosZ())
                ){
                    i++;
                    log.error("堆垛机命令地址写入后回读失败[id:{}]>>>>重写[{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), i,JSON.toJSON(command),JSON.toJSON(one));
                    result = siemensNet.Write("DB100.0", array);
                }else{
                    break;
                }
            }
        }while(i<5);
        if (command.getAckFinish() == 0) {
            short commandFinish = 1;
            result = siemensNet.Write("DB100.22", commandFinish);
            Thread.sleep(100L);
            result = siemensNet.Write("DB100.18", commandFinish);
            int signFinish = 1;
            while (signFinish<5){
                OperateResultExOne<byte[]> result10018 = siemensNet.Read("DB100.18", (short) 2);
                short transInt16 = siemensNet.getByteTransform().TransInt16(result10018.Content, 0);
                if (transInt16 != commandFinish){
                    log.info("下发DB100.18  回读失败" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.18  回读失败" + "array:"+ JSON.toJSONString(array));
                    result = siemensNet.Write("DB100.18", commandFinish);
                    signFinish++;
                }else {
                    log.info("下发DB100.18" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.18" + "array:"+ JSON.toJSONString(array));
                    break;
                }
            }
        }
        try {
@@ -269,14 +328,14 @@
        } catch (Exception ignore) {}
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            Thread.sleep(300);
            this.readStatus();
            log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            log.info(methodName + ":堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
            return true;
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("写入堆垛机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            log.error(methodName + ":写入堆垛机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
    }