pang.jiabao
2025-09-17 1c80f6410a4bd8d2d7cfece250d32b9bbac98ef8
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -5,7 +5,6 @@
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
@@ -21,15 +20,16 @@
import com.zy.core.model.protocol.StaError1;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.entity.BasDevp;
import com.zy.entity.WrkMast;
import com.zy.service.BasDevpService;
import com.zy.service.WrkMastService;
import com.zy.utils.News;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.text.MessageFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
@@ -46,10 +46,7 @@
    private short heartBeatVal = 1;
    private StaError1 staError1;
    public static final ArrayList<Integer> staNos = new ArrayList<Integer>() {{
        add(100);add(101);add(102);add(103);
        add(104);add(105);add(106);add(107);
        add(200);add(201);add(202);add(203);add(210);
        add(300);add(301);add(302);add(303);add(304);add(305);
        add(101);add(102);add(103);add(104);add(105);add(106);add(107);add(108);
    }};
    public static final ArrayList<Integer> BarcodeList = new ArrayList<Integer>() {{
@@ -73,11 +70,6 @@
     * 4.出库模式
     */
    public IoModeType ioModeOf101 = IoModeType.NONE;
    public IoModeType ioModeOf103 = IoModeType.NONE;
    public IoModeType ioModeOf305 = IoModeType.NONE;
//    public IoModeType ioMode = IoModeType.NONE;
//    public IoModeType ioModeOf4F = IoModeType.NONE;
//    public IoModeType ioMode = IoModeType.NONE;
    public SiemensDevpThread(DevpSlave slave) {
        this.slave = slave;
@@ -106,7 +98,7 @@
                    // 写数据 ID+目标站
                    case 2:
                        write((StaProtocol)task.getData());
                        log.error("输送线下发命令:"+((StaProtocol) task.getData()).getWorkNo()+","+((StaProtocol) task.getData()).getStaNo());
//                        log.error("输送线下发命令:"+((StaProtocol) task.getData()).getWorkNo()+","+((StaProtocol) task.getData()).getStaNo());
                        break;
                    default:
                        break;
@@ -120,6 +112,7 @@
        }
    }
    /**
     * 初始化站点状态
     */
@@ -170,11 +163,8 @@
    /**
     * 读取状态 ====> 整块plc
     */
    private void read() throws InterruptedException {
//        // 更新入出库模式
//        updateIoMode();
//        ArrayList<Integer> staNos = getStaNo();
//        int staNoSize = staNos.size();
    private void read() {
        // 读一楼连续8个站点
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (8*8));
        if (result.IsSuccess) {
@@ -205,83 +195,6 @@
                }
            }
        }
        // 读二楼连续4个站点
        OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.800", (short) (4*8));
        if (result2.IsSuccess) {
            for (int i = 8; i < 12; 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.setWorkNo(siemensS7Net.getByteTransform().TransInt32(result2.Content, (i-8)*8));     // 工作号
                staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result2.Content, (i-8)*8 + 4));   // 目标站
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result2.Content, (i-8)*8 + 6, 2);
                staProtocol.setAutoing(status[0]);  // 自动
                staProtocol.setLoading(status[1]);  // 有物
                staProtocol.setInEnable(status[2]); // 可入
                staProtocol.setOutEnable(status[3]);// 可出
                staProtocol.setEmptyMk(status[4]);  // 空板信号
                staProtocol.setFullPlt(status[5]);  // 满托盘
                staProtocol.setHigh(status[6]);     // 高库位
                staProtocol.setLow(status[7]);      // 低库位
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
            }
        }
        // 读二楼210
        OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB101.880", (short) (8));
        if (result3.IsSuccess) {
                Integer siteId = staNos.get(12); // 站点编号210
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(result3.Content, 0));     // 工作号
                staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result3.Content, 4));   // 目标站
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result3.Content, 6, 2);
                staProtocol.setAutoing(status[0]);  // 自动
                staProtocol.setLoading(status[1]);  // 有物
                staProtocol.setInEnable(status[2]); // 可入
                staProtocol.setOutEnable(status[3]);// 可出
                staProtocol.setEmptyMk(status[4]);  // 空板信号
                staProtocol.setFullPlt(status[5]);  // 满托盘
                staProtocol.setHigh(status[6]);     // 高库位
                staProtocol.setLow(status[7]);      // 低库位
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
        }
//        BasRgvMapService basRgvMapService = SpringUtils.getBean(BasRgvMapService.class);
        //RGV小车1
//        Thread.sleep(100);
//        OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB50.200",(short)10);
//        if (result3.IsSuccess) {
//            BasRgvMap basRgvMap = basRgvMapService.selectByRgvNo((int) siemensS7Net.getByteTransform().TransInt16(result3.Content, 0));
//            if (!Cools.isEmpty(basRgvMap)){
//                Integer siteId = 1;
//                StaProtocol staProtocol = station.get(siteId);
//                if (null == staProtocol) {
//                    staProtocol = new StaProtocol();
//                    staProtocol.setSiteId(siteId);
//                    station.put(siteId, staProtocol);
//                }
//                staProtocol.setAutoing(true);
//                staProtocol.setNearbySta(String.valueOf(siemensS7Net.getByteTransform().TransInt16(result3.Content, 8)));
//            }
//        }
        // 条码扫描器
        OperateResultExOne<byte[]> barcodeResult = siemensS7Net.Read("DB101.1672", (short) ((BarcodeList.size()+1) * 8)); // 不连续
@@ -299,7 +212,7 @@
        OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.1652", (short) ((staNosErrList.size()+1)*4)); // 不连续,多读中间一个
        if (resultErr.IsSuccess){
            for (int i = 0;i<staNosErrList.size();i++){
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i*9+2, 1);
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, i*8+2, 1);
                StaProtocol staProtocol = station.get(staNosErrList.get(i));
                staProtocol.setFrontErr(status[0]);
                staProtocol.setBackErr(status[1]);
@@ -310,6 +223,7 @@
                staProtocol.setBarcodeErr(status[6]);
            }
        }
        // 读一楼连续8个站点,故障信息反馈
        OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read("DB101.3312", (short) (8*2));
        if (resultErr2.IsSuccess) {
@@ -327,112 +241,6 @@
            }
        }
        // 读二楼连续4个站点,故障信息反馈
        OperateResultExOne<byte[]> resultErr3 = siemensS7Net.Read("DB101.3512", (short) (4*2));
        if (resultErr3.IsSuccess) {
            for (int i = 8; i < 12; i++) {
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr3.Content, (i-8) * 2 + 1, 1);
                StaProtocol staProtocol = station.get(staNos.get(i)); // 站点编号
                staProtocol.setBreakerErr(status[0]);
                staProtocol.setInfraredErr(status[1]);
                staProtocol.setOutTimeErr(status[2]);
                staProtocol.setSeizeSeatErr(status[3]);
                staProtocol.setWrkYgoodsN(status[4]);
                staProtocol.setInverterErr(status[5]);
                staProtocol.setContactErr(status[6]);
                staProtocol.setUpcontactErr(status[7]);
            }
        }
        // 一楼出入库模式切换
        OperateResultExOne<byte[]> ioModeResult = siemensS7Net.Read("DB101.3724", (short) 1);
        if (ioModeResult.IsSuccess) {
            boolean[] ioModelBooleans = siemensS7Net.getByteTransform().TransBool(ioModeResult.Content, 0, 1);
//            log.info("读取出入库模式:" + Arrays.toString(ioModelBooleans));
            WrkMastService wrkMastService = SpringUtils.getBean(WrkMastService.class);
            if (ioModelBooleans[0]) { // 100切入库
                if (ioModeOf101 == IoModeType.PAKOUT_MODE) {
                    int outCount = wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 101)
                            .eq("sta_no", 100).in("wrk_sts", 12, 13, 14));
                    if (outCount == 0) {
                        boolean[] booleans = new boolean[8];
                        booleans[0]=true;
                        ioModeOf101 = IoModeType.PAKIN_MODE;
                        siemensS7Net.Write("DB100.824",booleans);
                        log.info("101切换入库模式成功");
                    }
                }
            }
            if (ioModelBooleans[1]) { // 100切出库
                if (ioModeOf101 == IoModeType.PAKIN_MODE) {
                    int inCount = wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 1)
                            .eq("sta_no", 101).in("wrk_sts", 2, 3, 4));
                    if (inCount == 0) {
                        boolean[] booleans = new boolean[8];
                        booleans[0]=true;
                        ioModeOf101 = IoModeType.PAKOUT_MODE;
                        siemensS7Net.Write("DB100.824",booleans);
                        log.info("101切换出库模式成功");
                    }
                }
            }
            if (ioModelBooleans[2]) { // 102切入库
                if (ioModeOf103 == IoModeType.PAKOUT_MODE) {
                    int outCount = wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 101)
                            .eq("sta_no", 102).in("wrk_sts", 12, 13, 14));
                    if (outCount == 0) {
                        boolean[] booleans = new boolean[8];
                        booleans[1]=true;
                        ioModeOf103 = IoModeType.PAKIN_MODE;
                        siemensS7Net.Write("DB100.824",booleans);
                        log.info("103切换入库模式成功");
                    }
                }
            }
            if (ioModelBooleans[3]) { // 102切出库
                if (ioModeOf103 == IoModeType.PAKIN_MODE) {
                    int inCount = wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 1)
                            .eq("sta_no", 103).in("wrk_sts", 2, 3, 4));
                    if (inCount == 0) {
                        boolean[] booleans = new boolean[8];
                        booleans[1]=true;
                        ioModeOf103 = IoModeType.PAKOUT_MODE;
                        siemensS7Net.Write("DB100.824",booleans);
                        log.info("103切换出库模式成功");
                    }
                }
            }
        }
//        OperateResultExOne<byte[]> resultErr3 = siemensS7Net.Read("DB13.400", (short) 3);
//        if (resultErr3.IsSuccess) {
//            if (staError1 == null){
//                staError1 = new StaError1();
//            }
//            boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr3.Content, 0, 3);
//            staError1.setEMERGENCY_Stop_Core(status[0]);
//            staError1.setEMERGENCY_STOP1(status[1]);
//            staError1.setEMERGENCY_STOP2(status[2]);
//            staError1.setEMERGENCY_STOP3(status[3]);
//            staError1.setEMERGENCY_STOP4(status[4]);
//
//            staError1.setDoor_EMERGENCY_STOP1(status[5]);
//            staError1.setDoor_EMERGENCY_STOP2(status[6]);
//            staError1.setDoor_EMERGENCY_STOP3(status[7]);
//            staError1.setDoor_EMERGENCY_STOP4(status[8]);
//            staError1.setDoor_EMERGENCY_STOP5(status[9]);
//            staError1.setDoor_EMERGENCY_STOP6(status[10]);
//
//            staError1.setDoor_Open_Err1(status[11]);
//            staError1.setDoor_Open_Err1(status[12]);
//            staError1.setDoor_Open_Err1(status[13]);
//            staError1.setDoor_Open_Err1(status[14]);
//            staError1.setDoor_Open_Err1(status[15]);
//            staError1.setDoor_Open_Err1(status[16]);
//
//        }
        if (result.IsSuccess) {
@@ -481,11 +289,6 @@
            case 105: address = 20; break;
            case 106: address = 24; break;
            case 107: address = 28; break;
            case 200: address = 400; break;
            case 201: address = 404; break;
            case 202: address = 408; break;
            case 203: address = 412; break;
            case 210: address = 440; break;
            default:
        }
        if(address == -1) {
@@ -521,16 +324,6 @@
        }
    }
    // 更新入出库模式
    private void updateIoMode() throws InterruptedException {
//        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()));
//                News.error("SiemensDevp"+" - 6"+" - 写入输送线2F入出库模式失败。输送线plc编号={}", slave.getId());
//            }
//        }
    }
    /**
     * 心跳
     */
@@ -562,12 +355,13 @@
        siemensS7Net.ConnectClose();
    }
//    public static void main(String[] args) throws Exception {
//        DevpSlave slave = new DevpSlave();
//        slave.setIp("192.168.2.125");
//        SiemensDevpThread devpThread = new SiemensDevpThread(slave);
//        devpThread.connect();
//        devpThread.read();
    public static void main(String[] args) throws Exception {
        DevpSlave slave = new DevpSlave();
        slave.setIp("10.10.10.18");
        SiemensDevpThread devpThread = new SiemensDevpThread(slave);
        devpThread.connect();
        devpThread.read();
//        // 写
//        StaProtocol staProtocol = devpThread.getStation().get(1);
//        staProtocol.setWorkNo((short) 232);
@@ -580,7 +374,7 @@
//        // 读
//        devpThread.read();
//        System.out.println(JSON.toJSONString(devpThread.station));
//
//    }
    }
}