zwl
2025-07-19 93ff20c63a5e22e83d08a9e326fe12391eb82abc
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -44,14 +44,14 @@
    private short heartBeatVal = 1;
    public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{
        add(101);add(102);add(103);add(104);
        add(201);add(202);add(203);add(204);
        add(301);add(302);add(303);add(304);add(305);add(306);
        add(101);
        add(102);
        add(103);
        add(104);
        add(105);
        add(106);
    }};
    public static final ArrayList<Integer> staNos2 = new ArrayList<Integer>() {{
        add(401);add(402);
    }};
    private Integer count = 0;
    /**
     * 条码数量
@@ -66,8 +66,10 @@
     * 3.出库启动中 (不能生成入库工作档)
     * 4.出库模式
     */
    public IoModeType ioModeOf1F = IoModeType.NONE;
    public IoModeType ioModeOf2F = IoModeType.NONE;
//    public IoModeType ioMode = IoModeType.NONE;
    //    public IoModeType ioMode = IoModeType.NONE;
    public IoModeType ioModeOf3F = IoModeType.NONE;
    public IoModeType ioModeOf4F = IoModeType.NONE;
//    public IoModeType ioMode = IoModeType.NONE;
@@ -79,8 +81,6 @@
        switch (slave.getId()) {
            case 1:
                return staNos1;
            case 2:
                return staNos2;
            default:
                throw new CoolException("服务器异常");
        }
@@ -104,7 +104,7 @@
                        break;
                    // 写数据 ID+目标站
                    case 2:
                        write((StaProtocol)task.getData());
                        write((StaProtocol) task.getData());
                        break;
                    default:
                        break;
@@ -123,26 +123,30 @@
     * 初始化站点状态
     */
    private void initSite() {
        count++;
        ArrayList<Integer> staNos = getStaNo();
        // 站点编号
        for (Integer siteId : staNos) {
            StaProtocol staProtocol = station.get(siteId);
            if (null == staProtocol) {
                staProtocol = new StaProtocol();
                staProtocol.setSiteId(siteId);
                station.put(siteId, staProtocol);
            }
            staProtocol.setWorkNo((short) 0);    // ID
            staProtocol.setAutoing(false);      // 自动
            staProtocol.setLoading(false);      // 有物
            staProtocol.setInEnable(false);     // 可入
            staProtocol.setOutEnable(false);    // 可出
            staProtocol.setEmptyMk(false);      // 空板信号
            staProtocol.setStaNo((short) 0);     // 目标站
        if (count > 10) {
            // 站点编号
            for (Integer siteId : staNos) {
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                staProtocol.setWorkNo((short) 0);    // ID
                staProtocol.setAutoing(false);      // 自动
                staProtocol.setLoading(false);      // 有物
                staProtocol.setInEnable(false);     // 可入
                staProtocol.setOutEnable(false);    // 可出
                staProtocol.setEmptyMk(false);      // 空板信号
                staProtocol.setStaNo((short) 0);     // 目标站
            if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                staProtocol.setPakMk(true);
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
            }
            count = 0;
        }
    }
@@ -153,12 +157,12 @@
        siemensS7Net.setRack(slave.getRack().byteValue());
        siemensS7Net.setSlot(slave.getSlot().byteValue());
        OperateResult connect = siemensS7Net.ConnectServer();
        if(connect.IsSuccess){
        if (connect.IsSuccess) {
            result = true;
            OutputQueue.DEVP.offer(MessageFormat.format( "【{0}】输送线plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】输送线plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            log.info("输送线plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format( "【{0}】输送线plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]  [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】输送线plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]  [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            log.error("输送线plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            initSite();
        }
@@ -174,7 +178,7 @@
        updateIoMode();
        ArrayList<Integer> staNos = getStaNo();
        int staNoSize = staNos.size();
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB100.0", (short) (staNoSize*4));
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (staNoSize * 8));
        if (result.IsSuccess) {
            for (int i = 0; i < staNoSize; i++) {
                Integer siteId = staNos.get(i); // 站点编号
@@ -184,33 +188,12 @@
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt16(result.Content, i*4));     // 工作号
                staProtocol.setWorkNo((short) siemensS7Net.getByteTransform().TransInt32(result.Content, i * 8));     // 工作号
                staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, i*4 + 2));   // 目标站
            }
        }
//        Thread.sleep(200);
//        OperateResultExOne<byte[]> result0 = siemensS7Net.Read("DB101.0", (short) 186);
//        if (result0.IsSuccess) {
//            for (int i = 0; i < 93; i++) {
//                Integer siteId = staNos.get(i); // 站点编号
//                StaProtocol staProtocol = station.get(siteId);
//                if (null == staProtocol) {
//                    staProtocol = new StaProtocol();
//                    staProtocol.setSiteId(siteId);
//                    station.put(siteId, staProtocol);
//                }
//                staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result0.Content, i*2));   // 目标站
//            }
//        }
        Thread.sleep(200);
        OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB100.100", (short) (staNoSize));
        if (result1.IsSuccess) {
            for (int i = 0; i < staNoSize; i++) {
                Integer siteId = staNos.get(i); // 站点编号
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result1.Content, i, 1);
                StaProtocol staProtocol = station.get(siteId);
                staProtocol.setAutoing(status[0]);  // 自动
                staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, i * 8 + 4));   // 目标站
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, i * 8 + 6, 2);
                staProtocol.setAutoing(status[0]);  //
                staProtocol.setLoading(status[1]);  // 有物
                staProtocol.setInEnable(status[2]); // 可入
                staProtocol.setOutEnable(status[3]);// 可出
@@ -218,34 +201,57 @@
                staProtocol.setFullPlt(status[5]);  // 满托盘
                staProtocol.setHigh(status[6]);     // 高库位
                staProtocol.setLow(status[7]);      // 低库位
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
            }
        }
        Thread.sleep(200);
        OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB100.150",(short)(barcodeSize*8));
        //扫码器
        OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.600.0", (short) (barcodeSize * 10));
        if (result2.IsSuccess) {
            for (int i = 0; i < barcodeSize; i++) {
                String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,8, "UTF-8");
                String barcode = siemensS7Net.getByteTransform().TransString(result2.Content, i * 10+2, 8, "UTF-8");
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1);
                if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                if (!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                    barcodeThread.setBarcode(barcode);
                }
            }
        }
//        OperateResultExOne<Short> result2 = siemensS7Net.ReadInt16("DB200.0");
//        if (result2.IsSuccess) {
//            this.ioMode = IoModeType.get(result2.Content);
//        }
        //外形检测
        OperateResultExOne<byte[]> resultErr1 = siemensS7Net.Read("DB101.702.0", (short) (barcodeSize*6));
        if (resultErr1.IsSuccess) {
            for (int i = 0; i < barcodeSize; i++) {
                int sta = 0;
                switch (i) {
                    case 0:
                        sta = 102;
                        break;
                    case 1:
                        sta = 104;
                        break;
                    case 2:
                        sta = 106;
                        break;
                }
                StaProtocol staProtocol1 = station.get(sta);
                boolean[] status1 = siemensS7Net.getByteTransform().TransBool(resultErr1.Content, i*6, 1);
                staProtocol1.setFrontErr(status1[0]);
                staProtocol1.setBackErr(status1[1]);
                staProtocol1.setHighErr(status1[2]);
                staProtocol1.setLeftErr(status1[3]);
                staProtocol1.setRightErr(status1[4]);
                staProtocol1.setWeightErr(status1[5]);
                staProtocol1.setBarcodeErr(status1[6]);
            }
        }
        if (result.IsSuccess && result1.IsSuccess) {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
        if (!Cools.isEmpty(result) && result.IsSuccess) {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), slave.getId()));
            // 根据实时信息更新数据库
            try {
@@ -276,39 +282,75 @@
     * 写入 ID+目标站 =====> 单站点写入
     */
    private void write(StaProtocol staProtocol) throws InterruptedException {
        if (null == staProtocol) {
        if (staProtocol == null) {
            return;
        }
        ArrayList<Integer> staNos = getStaNo();
        int index = staNos.indexOf(staProtocol.getSiteId());
        short[] array = new short[2];
        array[0] = staProtocol.getWorkNo();
        array[1] = staProtocol.getStaNo();
        OperateResult write = siemensS7Net.Write("DB100." + index*4, array);
//        OperateResult write = siemensS7Net.Write("DB100." + index*2, staProtocol.getWorkNo());    // 工作号
//        Thread.sleep(500);
//        OperateResult write1 = siemensS7Net.Write("DB101." + index*2, staProtocol.getStaNo());    // 目标站
        if (!write.IsSuccess) {
            staProtocol = station.get(staProtocol.getSiteId());
            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
                staProtocol.setPakMk(true);
            }
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol)));
            log.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol));
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol)));
            log.info("输送线命令下发 [id:{}] >>>>> 命令下发: {}",  slave.getId(), JSON.toJSON(staProtocol));
        if (index == -1) {
            log.error("站点编号 {} 不在已知列表中,无法写入任务!", staProtocol.getSiteId());
            return;
        }
        int writeCount = 0; // 任务下发尝试次数
        boolean writeFlag = false; // 任务下发成功标记
        String plcAddressWorkNo = "DB100." + index * 6;
        String plcAddressStaNo = "DB100." + (index * 6 + 4);
        Thread.sleep(100);
        while (writeCount < 5) {
            // **读取当前PLC状态,避免不必要的写入**
            OperateResultExOne<byte[]> readResult = siemensS7Net.Read(plcAddressWorkNo, (short) 6);
            if (readResult.IsSuccess) {
                int currentWorkNo = siemensS7Net.getByteTransform().TransInt32(readResult.Content, 0);
                short currentStaNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 4);
                if (currentWorkNo == staProtocol.getWorkNo().intValue() && currentStaNo == staProtocol.getStaNo()) {
                    log.info("站点 {} 当前状态已匹配,无需重复写入", staProtocol.getSiteId());
                    return;
                }
            }
            // **清零并确认**
            if (!clearPLCData(plcAddressWorkNo, plcAddressStaNo, staProtocol.getSiteId())) {
                writeCount++;
                continue; // 重新尝试清零
            }
            // **写入新任务**
            if (writeTaskToPLC(plcAddressWorkNo, plcAddressStaNo, staProtocol)) {
                writeFlag = true;
                log.info("输送线命令写入成功,PLC编号={},站点数据={},尝试次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                break;
            }
            log.warn("输送线命令写入失败,PLC编号={},站点数据={},尝试次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
            writeCount++;
        }
        // **写入失败处理**
        handleWriteFailure(staProtocol, writeFlag);
    }
    // 更新入出库模式
    private void updateIoMode() throws InterruptedException {
        if (this.ioModeOf1F != IoModeType.NONE) {
            if (!siemensS7Net.Write("DB100.80", this.ioModeOf1F.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线F1入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线1F入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (this.ioModeOf2F != IoModeType.NONE) {
            if (!siemensS7Net.Write("DB100.180", this.ioModeOf2F.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线2F入出库模式失败。输送线plc编号={1}", slave.getId()));
            if (!siemensS7Net.Write("DB100.82", this.ioModeOf2F.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线F2入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线2F入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (this.ioModeOf3F != IoModeType.NONE) {
            if (!siemensS7Net.Write("DB100.84", this.ioModeOf3F.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线F3入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线3F入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
    }
@@ -316,7 +358,7 @@
    /**
     * 心跳
     */
    private void heartbeat(){
    private void heartbeat() {
        if (heartBeatVal == 1) {
            heartBeatVal = 2;
        } else {
@@ -347,14 +389,14 @@
    public static void main(String[] args) {
        System.out.println(staNos1.indexOf(129));
        System.out.println(staNos1.size());
        for (int i = 0; i< staNos1.size(); i++) {
        for (int i = 0; i < staNos1.size(); i++) {
//            System.out.println(i*2);
//            System.out.println(i*2 + 200);
//            System.out.println(i);
        }
        int index = staNos1.indexOf(128);
        System.out.println(index*2);
        System.out.println(index*2 + 200);
        System.out.println(index * 2);
        System.out.println(index * 2 + 200);
    }
//    public static void main(String[] args) throws Exception {
@@ -377,5 +419,64 @@
//        System.out.println(JSON.toJSONString(devpThread.station));
//
//    }
    /**
     * 清零 PLC 数据并验证清零是否成功
     */
    private boolean clearPLCData(String plcAddressWorkNo, String plcAddressStaNo, int siteId) throws InterruptedException {
        siemensS7Net.Write(plcAddressWorkNo, 0);
        siemensS7Net.Write(plcAddressStaNo, (short) 0);
        Thread.sleep(100); // 等待PLC识别
        OperateResultExOne<byte[]> readResult = siemensS7Net.Read(plcAddressWorkNo, (short) 6);
        if (readResult.IsSuccess) {
            int readWorkNo = siemensS7Net.getByteTransform().TransInt32(readResult.Content, 0);
            short readStaNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 4);
            if (readWorkNo == 0 && readStaNo == 0) {
                return true; // 清零成功
            }
        }
        log.warn("站点 {} 清零失败,尝试重新清零...", siteId);
        return false;
    }
    /**
     * 写入新任务到 PLC 并验证是否成功
     */
    private boolean writeTaskToPLC(String plcAddressWorkNo, String plcAddressStaNo, StaProtocol staProtocol) throws InterruptedException {
        OperateResult writeResult2 = siemensS7Net.Write(plcAddressStaNo, staProtocol.getStaNo());
        OperateResult writeResult1 = siemensS7Net.Write(plcAddressWorkNo, staProtocol.getWorkNo().intValue());
        if (writeResult1.IsSuccess && writeResult2.IsSuccess) {
            Thread.sleep(200); // 等待 PLC 识别新值
            OperateResultExOne<byte[]> readResult = siemensS7Net.Read(plcAddressWorkNo, (short) 6);
            if (readResult.IsSuccess) {
                int workNo = siemensS7Net.getByteTransform().TransInt32(readResult.Content, 0);
                short staNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 4);
                return workNo == staProtocol.getWorkNo().intValue() && staNo == staProtocol.getStaNo();
            }
        }
        return false;
    }
    /**
     * 处理写入失败的情况
     */
    private void handleWriteFailure(StaProtocol staProtocol, boolean writeFlag) {
        if (!writeFlag) {
            StaProtocol currentStaProtocol = station.get(staProtocol.getSiteId());
            if (currentStaProtocol.getWorkNo() == 0 && currentStaProtocol.getStaNo() == 0) {
                currentStaProtocol.setPakMk(true);
            }
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】输送线命令尝试5次失败。PLC编号={1},站点数据={2}",
                    slave.getId(), JSON.toJSON(currentStaProtocol)));
            log.error("输送线命令尝试5次失败,PLC编号={},站点数据={}", slave.getId(), JSON.toJSON(currentStaProtocol));
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】输送线命令成功 [id:{1}] >>>>> {2}",
                    DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol)));
            log.info("输送线命令成功 [id:{}] >>>>> {}", slave.getId(), JSON.toJSON(staProtocol));
        }
    }
}