pang.jiabao
1 天以前 1ba71b9ee89817b1931e950c966a320c75ef02c3
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -38,23 +38,61 @@
public class SiemensDevpThread implements Runnable, DevpThread {
    public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{
        add(101);add(102);add(103);add(104);add(105);add(106);add(107);add(108);add(109);add(110);
        add(111);add(112);add(113);add(114);add(115);add(116); // 侧面从 index 16开始 长度12
        add(201);add(202);add(203);add(204);add(205);add(206);add(207);add(208);add(209);add(210);add(211);add(212);
        add(101);
        add(102);
        add(103);
        add(104);
        add(105);
        add(106);
        add(107);
        add(108);
        add(109);
        add(110);
        add(111);
        add(112);
        add(113);
        add(114);
        add(115);
        add(116); // 侧面从 index 16开始 长度12
        add(201);
        add(202);
        add(203);
        add(204);
        add(205);
        add(206);
        add(207);
        add(208);
        add(209);
        add(210);
        add(211);
        add(212);
    }};
    public static final ArrayList<Integer> staNos2 = new ArrayList<Integer>() {{
        add(301);add(302);add(303);add(304);add(305);add(306);add(307);add(308);
        add(301);
        add(302);
        add(303);
        add(304);
        add(305);
        add(306);
        add(307);
        add(308);
    }};
    // 正面5个条码扫描站
    public static final ArrayList<Integer> barcodeSite2 = new ArrayList<Integer>() {{
        add(102);add(105);add(108);add(111);add(114);
        add(102);
        add(105);
        add(108);
        add(111);
        add(114);
    }};
    // 侧面三个条码扫描站
    public static final ArrayList<Integer> barcodeSite = new ArrayList<Integer>() {{
        add(202);add(206);add(211);
        add(202);
        add(206);
        add(211);
    }};
    /**
@@ -159,6 +197,7 @@
        }
    }
    @Override
    public boolean connect() {
        boolean result = false;
@@ -185,6 +224,8 @@
     * 读取状态 ====> 整块plc
     */
    private void read() throws InterruptedException {
        // 更新入出库模式
        updateIoMode();
        ArrayList<Integer> staNos = getStaNo();
        int staNoSize = staNos.size();
@@ -256,26 +297,32 @@
            if (result3.IsSuccess) {
                for (int i = 0; i < 5; i++) {
                    String barcode = siemensS7Net.getByteTransform().TransString(result3.Content, i * 40, 40, "UTF-8");
                    if(!Cools.isEmpty()){
                    if (!Cools.isEmpty()) {
                        barcode = barcode.trim();
                    }
                    List<String> barcodeList = Arrays.stream(barcode.split(";")).collect(Collectors.toList());
                    Iterator<String> iterator = barcodeList.iterator();
                    while (iterator.hasNext()){
                    while (iterator.hasNext()) {
                        String next = iterator.next();
                        if(next.length() == 6) {
                        if (next.length() == 6) {
                            barcode = next;
                            iterator.remove();
                            break;
                        }
                    }
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i+1);
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1);
                    if (!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                        barcodeThread.setBarcode(barcode);
                    }
                    StaProtocol staProtocol = station.get(barcodeSite2.get(i));
                    if (staProtocol != null) {
                        while (iterator.hasNext()) {
                            String next = iterator.next();
                            if (next.length() < 7 || next.length() > 10 || !next.matches("[0-9]*")) {
                                iterator.remove();
                            }
                        }
                        staProtocol.setMatIdList(barcodeList);
                    }
                }
@@ -368,11 +415,11 @@
            }
            // 读正面故障反馈
            OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.922.0", (short) (16 * 4));
            OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.2340.0", (short) (16 * 2));
            if (resultErr.IsSuccess) {
                for (int i = 0; i < 16; i++) {
                    Integer siteId = staNos.get(i); // 站点编号
                    boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i * 4, 1);
                    boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i * 2, 2);
                    StaProtocol staProtocol = station.get(siteId);
                    staProtocol.setBreakerErr(status[0]);
                    staProtocol.setInfraredErr(status[1]);
@@ -382,16 +429,20 @@
                    staProtocol.setInverterErr(status[5]);
                    staProtocol.setContactErr(status[6]);
                    staProtocol.setUpcontactErr(status[7]);
                    staProtocol.setPushTheEnvelopeErr(status[8]);
                    staProtocol.setRaiseTheLowerLimitErr(status[9]);
                    staProtocol.setLeftProtectionTriggersErr(status[10]);
                    staProtocol.setRightProtectionTriggersErr(status[11]);
                    staProtocol.setGratingBlockingErr(status[12]);
                }
            }
            // 读侧面故障反馈
            OperateResultExOne<byte[]> resultErrCM = siemensS7Net.Read("DB101.2540.0", (short) (12 * 4));
            OperateResultExOne<byte[]> resultErrCM = siemensS7Net.Read("DB101.2540.0", (short) (12 * 2));
            if (resultErrCM.IsSuccess) {
                for (int i = 16; i < staNoSize; i++) {
                    Integer siteId = staNos.get(i); // 站点编号
                    boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErrCM.Content, (i - 16) * 4, 1);
                    boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErrCM.Content, (i - 16) * 2, 1);
                    StaProtocol staProtocol = station.get(siteId);
                    staProtocol.setBreakerErr(status[0]);
                    staProtocol.setInfraredErr(status[1]);
@@ -481,15 +532,18 @@
        String workNoAddress;
        // 目标站地址
        String staNoAddress;
        // 高度类型地址
        String heightAddress;
        // 目标站地址
        String heightAddress = null;
        if (index < 16) {
            workNoAddress = "DB100." + index * 4;
            staNoAddress = "DB100." + (index * 4 + 2);
            heightAddress = "DB104." + (index * 4 + 2);
        } else {
            workNoAddress = "DB100." + (400 + (index-16) * 4);
            staNoAddress = "DB100." + (400 + (index-16) * 4 + 2);
            workNoAddress = "DB100." + (400 + (index - 16) * 4);
            staNoAddress = "DB100." + (400 + (index - 16) * 4 + 2);
        }
        int i = barcodeSite2.indexOf(staProtocol.getSiteId());
        if (i != -1) {
            heightAddress = "DB104." + (i * 2);
        }
        OperateResult writeResult;
@@ -498,6 +552,47 @@
        //任务下发成功标识
        boolean writeFlag = false;
        while (writeCount < 5) {
            if (!Cools.isEmpty(staProtocol.getHeight()) && heightAddress != null) {
                OperateResult writeResult3 = siemensS7Net.Write(heightAddress, Short.parseShort(staProtocol.getHeight()));
                writeResult3 = siemensS7Net.Write(heightAddress, Short.parseShort(staProtocol.getHeight()));
                writeResult3 = siemensS7Net.Write(heightAddress, Short.parseShort(staProtocol.getHeight()));
                writeResult3 = siemensS7Net.Write(heightAddress, Short.parseShort(staProtocol.getHeight()));
                log.info("写入高度地址:{},{}", heightAddress, writeResult3);
                if (!writeResult3.IsSuccess) {
                    //fangzhi xierukushibai,jiu duoxiejici,meixunhuan
                    writeResult3 = siemensS7Net.Write(heightAddress, Short.parseShort(staProtocol.getHeight()));
                    if (!writeResult3.IsSuccess) {
                        writeResult3 = siemensS7Net.Write(heightAddress, Short.parseShort(staProtocol.getHeight()));    // 目标站
                    }
                }
            }
            // 侧面出库,给输送线写条码
            Short staNo = staProtocol.getStaNo();
            String barcode = staProtocol.getBarcode();
            if (staNo == 203 || staNo == 207 || staNo == 212) {
                if (Cools.isEmpty(barcode)) {
                    log.error("侧面出库到:{}站点,条码值为空,任务号:{}", staNo, staProtocol.getWorkNo());
                } else {
                    String[] split = barcode.split("#");
                    int offset = 48 + (staNo - 201) * 24;
                    byte[] bytes = new byte[(split.length + 1) * 6];
                    for (int j = 0, k = 0; j < split.length; j++) {
                        String s = split[j];
                        for (int m = 0; m < s.length(); m++) {
                            bytes[k++] = (byte) s.charAt(m);
                        }
                    }
                    OperateResult writeResult4 = siemensS7Net.Write("DB79" + offset, bytes);
                    if (writeResult4.IsSuccess) {
                        log.info("侧面出库到:{}站点,给输送线写条码:{}成功", staNo, barcode);
                    } else {
                        log.error("侧面出库到:{}站点,给输送线写条码:{}失败", staNo, barcode);
                    }
                }
            }
            OperateResult writeResult1 = siemensS7Net.Write(workNoAddress, staProtocol.getWorkNo());    // 工作号
            OperateResult writeResult2 = siemensS7Net.Write(staNoAddress, staProtocol.getStaNo());    // 目标站
            if (writeResult1.IsSuccess && writeResult2.IsSuccess) {
@@ -508,9 +603,9 @@
//                    short staNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 2);
//                    if (staProtocol.getWorkNo().equals(workNo) && staProtocol.getStaNo().equals(staNo)) {
//                        //任务命令写入成功
                        writeFlag = true;
                        log.info("写入输送线命令后返回成功,并且回读成功。输送线plc编号={},{},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                        break;
                    writeFlag = true;
                    log.info("写入输送线命令后返回成功,并且回读成功。输送线plc编号={},{},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                    break;
//                    } else {//返回结果是成功了,但是真实值不相同
//                        writeCount++;
//                        OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线命令后返回成功,但是读取任务值不一致。输送线plc编号={1},站点数据={2},写入次数={3}",
@@ -571,9 +666,10 @@
//        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());    // 目标站
    /// /        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());
@@ -593,7 +689,6 @@
//            }
//        }
//    }
    private void write2(StaProtocol staProtocol) throws InterruptedException {
        if (null == staProtocol) {
            return;
@@ -681,27 +776,53 @@
    // 更新入出库模式
    private void updateIoMode() throws InterruptedException {
        if (slave.getId() == 1 && this.ioModeOf1F1 != null) {
            if (!siemensS7Net.Write("DB4.2", this.ioModeOf1F1.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线21F1入出库模式失败。输送线plc编号={2}", slave.getId()));
            if (!siemensS7Net.Write("DB105.0", this.ioModeOf1F1.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线21F1入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线1F1入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (slave.getId() == 1 && this.ioModeOf1F2 != null) {
            if (!siemensS7Net.Write("DB105.2", this.ioModeOf1F2.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线2F2入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线1F2入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (slave.getId() == 1 && this.ioModeOf1F3 != null) {
            if (!siemensS7Net.Write("DB105.4", this.ioModeOf1F3.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线3F1入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线1F3入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (slave.getId() == 1 && this.ioModeOf1F4 != null) {
            if (!siemensS7Net.Write("DB105.6", this.ioModeOf1F4.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线3F2入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线1F4入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (slave.getId() == 1 && this.ioModeOf1F5 != null) {
            if (!siemensS7Net.Write("DB105.8", this.ioModeOf1F5.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线3F2入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线1F5入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (slave.getId() == 1 && this.ioModeOf2F1 != null) {
            if (!siemensS7Net.Write("DB105.10", this.ioModeOf2F1.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线2F1入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线2F1入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (slave.getId() == 1 && this.ioModeOf1F2 != null) {
            if (!siemensS7Net.Write("DB4.4", this.ioModeOf1F2.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线2F2入出库模式失败。输送线plc编号={3}", slave.getId()));
        if (slave.getId() == 1 && this.ioModeOf2F2 != null) {
            if (!siemensS7Net.Write("DB105.12", this.ioModeOf2F2.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线2F2入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线2F2入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (slave.getId() == 1 && this.ioModeOf1F2 != null) {
            if (!siemensS7Net.Write("DB4.2", this.ioModeOf1F2.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线3F1入出库模式失败。输送线plc编号={2}", slave.getId()));
                log.error("写入输送线3F1入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
        if (slave.getId() == 1 && this.ioModeOf1F2 != null) {
            if (!siemensS7Net.Write("DB4.4", this.ioModeOf1F2.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线3F2入出库模式失败。输送线plc编号={3}", slave.getId()));
                log.error("写入输送线3F2入出库模式失败。输送线plc编号={}", slave.getId());
        if (slave.getId() == 1 && this.ioModeOf2F3 != null) {
            if (!siemensS7Net.Write("DB105.14", this.ioModeOf2F3.id).IsSuccess) {
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线2F3入出库模式失败。输送线plc编号={1}", slave.getId()));
                log.error("写入输送线2F3入出库模式失败。输送线plc编号={}", slave.getId());
            }
        }
    }
@@ -722,6 +843,52 @@
    public void close() {
        siemensS7Net.ConnectClose();
    }
    public static void main(String[] args) throws Exception {
        DevpSlave slave = new DevpSlave();
        slave.setIp("10.49.210.41");
//        SiemensDevpThread devpThread = new SiemensDevpThread(slave);
//        devpThread.connect();
        SiemensS7Net siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp());
        siemensS7Net.setRack(slave.getRack().byteValue());
        siemensS7Net.setSlot(slave.getSlot().byteValue());
        OperateResult connect = siemensS7Net.ConnectServer();
        // 侧面出库,给输送线写条码
        Short staNo = 212;
        String barcode = "800001#123456#811112#811113";
        String[] split = barcode.split("#");
        int offset = 48 + (staNo - 201) * 24;
        byte[] bytes = new byte[(split.length + 1) * 6];
        for (int j = 0, k = 0; j < split.length; j++) {
            String s = split[j];
            for (int m = 0; m < s.length(); m++) {
                bytes[k++] = (byte) s.charAt(m);
            }
        }
        OperateResult writeResult4 = siemensS7Net.Write("DB79" + offset, bytes);
        if (writeResult4.IsSuccess) {
            log.info("侧面出库到:{}站点,给输送线写条码:{}成功", staNo, barcode);
        } else {
            log.error("侧面出库到:{}站点,给输送线写条码:{}失败", staNo, barcode);
        }
//        devpThread.write2(0);
//        devpThread.read();
//        // 写
//        StaProtocol staProtocol = devpThread.getStation().get(1);
//        staProtocol.setWorkNo((short) 232);
//        staProtocol.setStaNo((short) 6);
//        staProtocol.setAutoing(true);
//        staProtocol.setEmptyMk(true);
//        staProtocol.setInEnable(true);
//        devpThread.write(staProtocol);
//        System.out.println("----------------------------------------");
//        // 读
//        devpThread.read();
//        System.out.println(JSON.toJSONString(devpThread.station));
    }
}