#
luxiaotao1123
2020-08-31 1dce0bc1ce1640e944b31bbe7f6699ab4dad23bc
src/main/java/com/zy/core/thread/DevpThread.java
@@ -75,7 +75,7 @@
                }
                // 心跳
                heartbeat();
//                heartbeat();
                Thread.sleep(400);
            } catch (Exception e) {
                e.printStackTrace();
@@ -104,8 +104,8 @@
    /**
     * 读取状态 ====> 整块plc
     */
    private void read(){
        OperateResultExOne<byte[]> result = melsecMcNet.Read("DB100.0", (short) 40);
    private void read() throws InterruptedException {
        OperateResultExOne<byte[]> result = melsecMcNet.Read("D101", (short) 30);
        if (result.IsSuccess) {
            for (int i = 1; i <= 8; i++) {
                StaProtocol staProtocol = station.get(i);
@@ -115,19 +115,23 @@
                    station.put(i, staProtocol);
                }
                staProtocol.setWorkNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2));     // 工作号
                staProtocol.setStaNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2+20));   // 目标站
                staProtocol.setStaNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2+40));   // 目标站
            }
        }
        OperateResultExOne<byte[]> result1 = melsecMcNet.Read("DB100.40", (short) 8);
        Thread.sleep(100);
        OperateResultExOne<boolean[]> result1 = melsecMcNet.ReadBool("M800", (short) 64);
        if (result1.IsSuccess) {
            for (int i = 1; i <= 8; i++) {
                boolean[] status = melsecMcNet.getByteTransform().TransBool(result1.Content, i-1, 1);
                StaProtocol staProtocol = station.get(i);
                staProtocol.setAutoing(status[0]);  // 自动
                staProtocol.setLoading(status[1]);  // 有物
                staProtocol.setInEnable(status[2]); // 可入
                staProtocol.setOutEnable(status[3]);// 可出
                staProtocol.setEmptyMk(status[4]);  // 空板信号
                staProtocol.setAutoing(result1.Content[(i-1)*8]);  // 自动
                staProtocol.setLoading(result1.Content[(i-1)*8+1]);  // 有物
                staProtocol.setInEnable(result1.Content[(i-1)*8+2]); // 可入
                staProtocol.setOutEnable(result1.Content[(i-1)*8+3]);// 可出
                staProtocol.setEmptyMk(result1.Content[(i-1)*8+4]);  // 空板信号
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
            }
        }
        if (result.IsSuccess && result1.IsSuccess) {
@@ -158,12 +162,13 @@
    /**
     * 写入 ID+目标站 =====> 单站点写入
     */
    private void write(StaProtocol staProtocol){
    private void write(StaProtocol staProtocol) throws InterruptedException {
        if (null == staProtocol) {
            return;
        }
        OperateResult write = melsecMcNet.Write("DB100." + (staProtocol.getSiteId() - 1) * 2, staProtocol.getWorkNo());    // 工作号
        OperateResult write1 = melsecMcNet.Write("DB100." + ((staProtocol.getSiteId() - 1) * 2 + 20), staProtocol.getStaNo());    // 目标站
        OperateResult write = melsecMcNet.Write("D10" + staProtocol.getSiteId(), staProtocol.getWorkNo());    // 工作号
        Thread.sleep(100);
        OperateResult write1 = melsecMcNet.Write("D12" + staProtocol.getSiteId(), staProtocol.getStaNo());    // 目标站
//        boolean[] status = new boolean[8];
//        status[0] = staProtocol.isAutoing();
//        status[1] = staProtocol.isLoading();
@@ -172,10 +177,14 @@
//        status[4] = staProtocol.isEmptyMk();
//        OperateResult write2 = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) + 40), status);     //  状态
        if (!write.IsSuccess || !write1.IsSuccess) {
            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)));
            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));
        }
    }
@@ -186,7 +195,7 @@
        if (null == staProtocol) {
            return;
        }
        OperateResult write = melsecMcNet.Write("DB100." + (staProtocol.getSiteId() - 1) * 2, staProtocol.getWorkNo());    // 工作号
        OperateResult write = melsecMcNet.Write("D10" + staProtocol.getSiteId(), staProtocol.getWorkNo());    // 工作号
        if (!write.IsSuccess ) {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol)));
            log.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol));
@@ -202,7 +211,7 @@
        if (null == staProtocol) {
            return;
        }
        OperateResult write = melsecMcNet.Write("DB100." + ((staProtocol.getSiteId() - 1) * 2 + 20), staProtocol.getStaNo());    // 目标站
        OperateResult write = melsecMcNet.Write("D12" + staProtocol.getSiteId(), staProtocol.getStaNo());    // 目标站
        if (!write.IsSuccess ) {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol)));
            log.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol));
@@ -231,23 +240,33 @@
        melsecMcNet.ConnectClose();
    }
    public static void main(String[] args) {
    public static void main(String[] args) throws Exception {
        DevpSlave slave = new DevpSlave();
        slave.setIp("192.168.2.125");
        slave.setIp("192.168.3.65");
        slave.setPort(6000);
        DevpThread devpThread = new DevpThread(slave);
        devpThread.connect();
        devpThread.read();
        OperateResult d1021 = devpThread.melsecMcNet.Write("D102", (short) 234);
        if (d1021.IsSuccess) {
            System.out.println("success");
        }
        OperateResultExOne<Short> d102 = devpThread.melsecMcNet.ReadInt16("D102");
        System.out.println(d102.Content);
//        devpThread.read();
        System.out.println("第一次读");
        // 写
        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("----------------------------------------");
//        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("----------------------------------------");
        Thread.sleep(400);
        // 读
        devpThread.read();
//        devpThread.read();
        System.out.println("第二次读");
        System.out.println(JSON.toJSONString(devpThread.station));
    }