#
zwl
2026-02-09 ad79ba405b2d1ac96423f88f4e8a76c584b9d38a
src/main/java/com/zy/core/network/real/ZyCrnRealConnect.java
@@ -5,12 +5,9 @@
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasCrnpOpt;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.BasCrnpOptService;
import com.zy.asrs.utils.Utils;
import com.zy.core.News;
import com.zy.core.cache.OutputQueue;
import com.zy.core.model.CommandResponse;
@@ -34,13 +31,13 @@
        boolean connected = false;
        siemensNet = new SiemensS7Net(SiemensPLCS.S1200, deviceConfig.getIp());
        OperateResult connect = siemensNet.ConnectServer();
        if(connect.IsSuccess){
        if (connect.IsSuccess) {
            connected = true;
            OutputQueue.CRN.offer(MessageFormat.format( "【{0}】堆垛机plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
            News.info("SiemensCrn"+" - 1"+" - 堆垛机plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】堆垛机plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
            News.info("堆垛机plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】堆垛机plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
            News.error("SiemensCrn"+" - 2"+" - 堆垛机plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
            News.error("堆垛机plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        }
//        siemensNet.ConnectClose();
        return connected;
@@ -55,7 +52,7 @@
    @Override
    public ZyCrnStatusEntity getStatus() {
        try {
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 56);
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 60);
            if (result.IsSuccess) {
                ZyCrnStatusEntity crnStatus = new ZyCrnStatusEntity();
                crnStatus.setCrnNo(deviceConfig.getDeviceNo());
@@ -69,27 +66,33 @@
                crnStatus.setWalkPos((int) siemensNet.getByteTransform().TransInt16(result.Content, 14));
                crnStatus.setLoaded((int) siemensNet.getByteTransform().TransInt16(result.Content, 16));
                crnStatus.setAlarm((int) siemensNet.getByteTransform().TransInt16(result.Content, 18));
                crnStatus.setTemp1((int) siemensNet.getByteTransform().TransInt16(result.Content, 20));
                crnStatus.setTemp2((int) siemensNet.getByteTransform().TransInt16(result.Content, 22));
                crnStatus.setTemp3((int) siemensNet.getByteTransform().TransInt16(result.Content, 24));
                crnStatus.setTemp4((int) siemensNet.getByteTransform().TransInt16(result.Content, 26));
                crnStatus.setXSpeed((int) siemensNet.getByteTransform().TransInt16(result.Content, 28));
                crnStatus.setYSpeed((int) siemensNet.getByteTransform().TransInt16(result.Content, 32));
                crnStatus.setZSpeed((int) siemensNet.getByteTransform().TransInt16(result.Content, 36));
                crnStatus.setXDistance((int) siemensNet.getByteTransform().TransInt16(result.Content, 40));
                crnStatus.setYDistance((int) siemensNet.getByteTransform().TransInt16(result.Content, 44));
                crnStatus.setXDuration((int) siemensNet.getByteTransform().TransInt16(result.Content, 48));
                crnStatus.setYDuration((int) siemensNet.getByteTransform().TransInt16(result.Content, 52));
                crnStatus.setTemp1((int) siemensNet.getByteTransform().TransInt16(result.Content, 24));
                crnStatus.setTemp2((int) siemensNet.getByteTransform().TransInt16(result.Content, 26));
                crnStatus.setXSpeed((double) siemensNet.getByteTransform().TransSingle(result.Content, 28));
                crnStatus.setYSpeed((double) siemensNet.getByteTransform().TransSingle(result.Content, 32));
                crnStatus.setZSpeed((double) siemensNet.getByteTransform().TransSingle(result.Content, 36));
                crnStatus.setXDistance((double) siemensNet.getByteTransform().TransSingle(result.Content, 40));
                crnStatus.setYDistance((double) siemensNet.getByteTransform().TransSingle(result.Content, 44));
                crnStatus.setXDuration((double) siemensNet.getByteTransform().TransSingle(result.Content, 48));
                crnStatus.setYDuration((double) siemensNet.getByteTransform().TransSingle(result.Content, 52));
                crnStatus.setWeight((double) siemensNet.getByteTransform().TransSingle(result.Content, 56));
                // 读取DB110.200 - DB110.398
                OperateResultExOne<byte[]> resultDb110 = siemensNet.Read("DB110.200", (short) 200);
                if (resultDb110.IsSuccess) {
                    short[] db110Data = siemensNet.getByteTransform().TransInt16(resultDb110.Content, 0, 100);
                    crnStatus.setDb110Data(db110Data);
                }
                return crnStatus;
            } else {
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
                News.error("SiemensCrn"+" - 4"+" - 读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
                News.error("读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
            News.error("SiemensCrn"+" - 5"+" - 读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
            News.error("读取垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        }
        return null;
    }
@@ -105,7 +108,6 @@
            }
            int writeAck = 0;
            boolean ackResult = false;
            do {
                OperateResult resultAck = siemensNet.Write("DB100.0", (short) 0);
                if (resultAck.IsSuccess) {
@@ -116,116 +118,229 @@
                        writeAck++;
                    } else {
                        News.info("堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), "ack复位完成");
                        ackResult = true;
                        break;
                    }
                }
            } while (writeAck < 5);
            if (!ackResult) {
                response.setMessage("堆垛机命令下发[id:{}] >>>>> {}" + command.getCrnNo() + "ack复位失败");
            short[] array = new short[9];
            array[0] = (short) 0;
            array[1] = command.getTaskNo().shortValue();
            array[2] = command.getTaskMode().shortValue();
            array[3] = command.getSourcePosX().shortValue();
            array[4] = command.getSourcePosY().shortValue();
            array[5] = command.getSourcePosZ().shortValue();
            array[6] = command.getDestinationPosX().shortValue();
            array[7] = command.getDestinationPosY().shortValue();
            array[8] = command.getDestinationPosZ().shortValue();
            OperateResult result = siemensNet.Write("DB100.0", array);
            if (!result.IsSuccess) {
                response.setMessage("堆垛机命令下发[id:{}] >>>>> {}" + command.getCrnNo() + "任务状态位异常");
                return response;
            }
            short[] array = new short[10];
            array[0] = command.getAckFinish();
            array[1] = command.getTaskNo();
            array[2] = command.getTaskMode();
            array[3] = command.getSourcePosX();
            array[4] = command.getSourcePosY();
            array[5] = command.getSourcePosZ();
            array[6] = command.getDestinationPosX();
            array[7] = command.getDestinationPosY();
            array[8] = command.getDestinationPosZ();
            array[9] = command.getCommand();
            OperateResult result = null;
            int idx = 0;
            do {
                OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 20);
                if (resultRead.IsSuccess) {
                    if (command.getAckFinish() == 0) {
                        short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                        short taskMode = siemensNet.getByteTransform().TransInt16(resultRead.Content, 4);
                        short sourcePosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 6);
                        short sourcePosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 8);
                        short sourcePosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 10);
                        short destinationPosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 12);
                        short destinationPosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 14);
                        short destinationPosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 16);
                        if (taskNo == 0 || taskMode == 0 || sourcePosX == 0 || sourcePosY == 0 || sourcePosZ == 0 || destinationPosX == 0 || destinationPosY == 0 || destinationPosZ == 0) {
                            result = siemensNet.Write("DB100.0", array);
                        } else {
                            break;
                        }
                    short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                    short taskMode = siemensNet.getByteTransform().TransInt16(resultRead.Content, 4);
                    short sourcePosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 6);
                    short sourcePosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 8);
                    short sourcePosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 10);
                    short destinationPosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 12);
                    short destinationPosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 14);
                    short destinationPosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 16);
                    if (taskNo == 0 || taskMode == 0 || sourcePosX == 0 || sourcePosY == 0 || sourcePosZ == 0 || destinationPosX == 0 || destinationPosY == 0 || destinationPosZ == 0) {
                        result = siemensNet.Write("DB100.0", array);
                    } else {
                        short ackFinish = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
                        if (ackFinish != command.getAckFinish()) {
                            result = siemensNet.Write("DB100.0", array);
                        } else {
                            break;
                        }
                        break;
                    }
                }
                idx++;
                Thread.sleep(500);
            } while (idx < 5);
            if (command.getAckFinish() == 0) {
                short commandFinish = 1;
                int i = 0;
                do {
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 4);
                    OperateResultExOne<byte[]> resultReadConfirm = siemensNet.Read("DB100.18", (short) 2);
                    if (resultRead.IsSuccess && resultReadConfirm.IsSuccess) {
                        short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                        short confirm = siemensNet.getByteTransform().TransInt16(resultReadConfirm.Content, 0);
                        if (taskNo != 0 && confirm == 0) {
                            result = siemensNet.Write("DB100.18", commandFinish);
                        }
                    }
                    i++;
                    Thread.sleep(500);
                } while (i < 5);
            OperateResult confirmResult = siemensNet.Write("DB100.18", (short) 1);
            if (!confirmResult.IsSuccess) {
                News.error("堆垛机写入确认数据失败 ===>> [id:{}]", command.getCrnNo());
                response.setMessage("堆垛机写入确认数据失败");
                return response;
            }
            if (result != null && result.IsSuccess) {
                News.info("SiemensCrn" + " - 7" + " - 堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), JSON.toJSON(command));
                News.info("堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), JSON.toJSON(command));
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), command.getCrnNo(), JSON.toJSON(command)));
                response.setResult(true);
                response.setMessage("命令下发成功");
            } else {
                News.error("SiemensCrn" + " - 8" + " - 写入堆垛机plc数据失败 ===>> [id:{}]", command.getCrnNo());
                News.error("写入堆垛机plc数据失败 ===>> [id:{}]", command.getCrnNo());
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}]", DateUtils.convert(new Date()), command.getCrnNo()));
                response.setResult(false);
                response.setMessage("命令下发失败");
            }
            if (!Cools.isEmpty(command.getLocNo())){
                OperateResult resultAck = siemensNet.Write("DB110." + command.getIndex() * 2, command.getValue());
                if (resultAck.IsSuccess) {
                    News.info("库位白色灯命令下发[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
                } else {
                    News.error("库位白色灯命令下发失败[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
                }
            }
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            String sourceLocNo = Utils.getLocNo(command.getSourcePosX(), command.getSourcePosY(), command.getSourcePosZ());
            String targetLocNo = Utils.getLocNo(command.getDestinationPosX(), command.getDestinationPosY(), command.getDestinationPosZ());
        }
        return response;
    }
            // 日志记录
            BasCrnpOptService bean = SpringUtils.getBean(BasCrnpOptService.class);
            BasCrnpOpt basCrnpOpt = new BasCrnpOpt(
                    command.getTaskNo().intValue(),    // 任务号
                    command.getCrnNo(),    // 堆垛机[非空]
                    new Date(),    // 下发时间
                    String.valueOf(command.getTaskMode()),    // 模式
                    sourceLocNo, //源库位
                    targetLocNo, //目标库位
                    null,    // 修改时间
                    null,    // 修改人员
                    null,    // 备注
                    JSON.toJSONString(command),    // 指令
                    JSON.toJSONString(getStatus()),    // 系统状态
                    1,    // 下发状态{0:未下发,1:已下发}
                    JSON.toJSONString(response)    // 响应
            );
            bean.insert(basCrnpOpt);
    @Override
    public CommandResponse sendCommand1(CrnCommand command) {
        CommandResponse response = new CommandResponse(false);
        try {
            if (null == command) {
                News.error("堆垛机写入命令为空");
                response.setMessage("堆垛机写入命令为空");
                return response;
            }
            int writeAck = 0;
            do {
                OperateResult resultAck = siemensNet.Write("DB100.0", (short) 0);
                if (resultAck.IsSuccess) {
                    Thread.sleep(200);
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 2);
                    short ack = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
                    if (ack != 0) {
                        writeAck++;
                    } else {
                        News.info("堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), "ack复位完成");
                        break;
                    }
                }
            } while (writeAck < 5);
            short[] array = new short[10];
            array[0] = (short) 1;
            array[1] = command.getTaskNo().shortValue();
            array[2] = command.getTaskMode().shortValue();
            array[3] = command.getSourcePosX().shortValue();
            array[4] = command.getSourcePosY().shortValue();
            array[5] = command.getSourcePosZ().shortValue();
            array[6] = command.getDestinationPosX().shortValue();
            array[7] = command.getDestinationPosY().shortValue();
            array[8] = command.getDestinationPosZ().shortValue();
            array[9] = (short) 0;
            OperateResult result = siemensNet.Write("DB100.0", array);
            if (!result.IsSuccess) {
                response.setMessage("堆垛机命令下发[id:{}] >>>>> {}" + command.getCrnNo() + "任务状态位异常");
                return response;
            }
            int idx = 0;
            do {
                OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 20);
                if (resultRead.IsSuccess) {
                    short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                    short taskMode = siemensNet.getByteTransform().TransInt16(resultRead.Content, 4);
                    short sourcePosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 6);
                    short sourcePosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 8);
                    short sourcePosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 10);
                    short destinationPosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 12);
                    short destinationPosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 14);
                    short destinationPosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 16);
                    if (taskNo == 0 || taskMode == 0 || sourcePosX == 0 || sourcePosY == 0 || sourcePosZ == 0 || destinationPosX == 0 || destinationPosY == 0 || destinationPosZ == 0) {
                        result = siemensNet.Write("DB100.0", array);
                    } else {
                        break;
                    }
                }
                idx++;
                Thread.sleep(500);
            } while (idx < 5);
            if (result != null && result.IsSuccess) {
                News.info("堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), JSON.toJSON(command));
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), command.getCrnNo(), JSON.toJSON(command)));
                response.setResult(true);
                response.setMessage("命令下发成功");
            } else {
                News.error("写入堆垛机plc数据失败 ===>> [id:{}]", command.getCrnNo());
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}]", DateUtils.convert(new Date()), command.getCrnNo()));
                response.setResult(false);
                response.setMessage("命令下发失败");
            }
            if (!Cools.isEmpty(command.getLocNo())){
                OperateResult resultAck = siemensNet.Write("DB110." + command.getIndex() * 2, command.getValue());
                if (resultAck.IsSuccess) {
                    Integer index = command.getIndex()*2 + 200;
                    OperateResult resultAck1 = siemensNet.Write("DB110." + index, command.getValue());
                    if (resultAck1.IsSuccess) {
                        News.info("库位橙色按钮命令下发[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
                    } else {
                        News.error("库位白色灯命令下发失败[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
                    }
                    News.info("库位白色灯命令下发[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
                } else {
                    News.error("库位白色灯命令下发失败[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
                }
            }
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
    @Override
    public CommandResponse sendCommand2(CrnCommand command) {
        CommandResponse response = new CommandResponse(false);
        try {
            if (null == command) {
                News.error("堆垛机写入命令为空");
                response.setMessage("堆垛机写入命令为空");
                return response;
            }
            OperateResult resultAck = siemensNet.Write("DB110." + command.getIndex() * 2, command.getValue());
            if (resultAck.IsSuccess) {
                News.info("库位白色灯命令下发[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
            } else {
                News.error("库位白色灯命令下发失败[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
            }
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
    @Override
    public CommandResponse sendCommand3(CrnCommand command) {
        CommandResponse response = new CommandResponse(false);
        try {
            if (null == command) {
                News.error("堆垛机写入命令为空");
                response.setMessage("堆垛机写入命令为空");
                return response;
            }
            Integer index = command.getIndex()*2 + 200;
            OperateResult resultAck = siemensNet.Write("DB110." + index, command.getValue());
            if (resultAck.IsSuccess) {
                News.info("库位橙色按钮命令下发[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
            } else {
                News.error("库位白色灯命令下发失败[locNo:{}] >>>>> {}", command.getLocNo(), command.getValue());
            }
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }