1
pjb
2025-08-11 38add77dd27643bc83c916aeceac5dcde76bac02
src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -134,6 +134,8 @@
                    // 读数据
                    case 1:
                        read();
                        Thread.sleep(100);
                        readsanqi();
                        break;
                    // 写数据 ID+目标站
                    case 2:
@@ -144,7 +146,7 @@
                }
                // 心跳
//                heartbeat();
                Thread.sleep(300);
                Thread.sleep(200);
            } catch (Exception e) {
                e.printStackTrace();
            }
@@ -189,8 +191,6 @@
                return barcode1;
            case 2:
                return barcode2;
            case 3:
                return barcode3;
            default:
                throw new CoolException("获取条码扫描器失败!");
        }
@@ -302,6 +302,66 @@
//                log.error("读取输送线plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] ", slave.getId(), slave.getIp(), slave.getPort());
            }
            //外形检测
            OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.42", (short) 8);
            if (resultErr.IsSuccess) {
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, 0, 1);
                StaProtocol staProtocol = station.get(304);
                staProtocol.setFrontErr(status[0]);
                staProtocol.setBackErr(status[1]);
                staProtocol.setHighErr(status[2]);
                staProtocol.setLeftErr(status[3]);
                staProtocol.setRightErr(status[4]);
                staProtocol.setWeightErr(status[5]);
                staProtocol.setBarcodeErr(status[6]);
            }
            //条码扫描器
            Thread.sleep(200);
            ArrayList<Integer> barcodeList = getBarcodeList();
            OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.60", (short) (barcodeList.size() * 8));
            if (result2.IsSuccess) {
                for (int i = 0; i < barcodeList.size(); i++) {
                    Integer barcodeId = barcodeList.get(i);
                    String barcode = siemensS7Net.getByteTransform().TransString(result2.Content, i * 8, 8, "UTF-8");
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId);
                    if (!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                        barcodeThread.setBarcode(barcode);
                    }
                }
            }
        }
        // 根据实时信息更新数据库
        try {
            if (!station.isEmpty()) {
                List<BasDevp> basDevps = new ArrayList<>();
                for (Integer siteId : staNos) {
                    StaProtocol staProtocol = station.get(siteId);
                    basDevps.add(staProtocol.toSqlModel());
                }
                BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                    throw new Exception("更新数据库数据失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
        }
    }
    /**
     * 读取状态 ====> 整块plc
     */
    private void readsanqi() throws InterruptedException {
        ArrayList<Integer> staNos = staNos3;
        int staNoSize = staNos3.size();
        if (slave.getId() == 2) {
            //
            OperateResultExOne<byte[]> resultsanqi = siemensS7Net.Read("DB101.120", (short) (staNoSize * 8));
            if (resultsanqi.IsSuccess) {
@@ -335,57 +395,30 @@
            }
            //外形检测
            OperateResultExOne<byte[]> resultErr = siemensS7Net.Read("DB101.42", (short) 8);
            if (resultErr.IsSuccess) {
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr.Content, 0, 1);
                StaProtocol staProtocol = station.get(304);
                staProtocol.setFrontErr(status[0]);
                staProtocol.setBackErr(status[1]);
                staProtocol.setHighErr(status[2]);
                staProtocol.setLeftErr(status[3]);
                staProtocol.setRightErr(status[4]);
                staProtocol.setWeightErr(status[5]);
                staProtocol.setBarcodeErr(status[6]);
            }
            //外形检测 三期
            OperateResultExOne<byte[]> resultErrsanqi = siemensS7Net.Read("DB101.160", (short) 8);
            OperateResultExOne<byte[]> resultErrsanqi = siemensS7Net.Read("DB101.162", (short) (staNoSize * 8));
            if (resultErrsanqi.IsSuccess) {
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErrsanqi.Content, 0, 1);
                StaProtocol staProtocol = station.get(304);
                staProtocol.setFrontErr(status[0]);
                staProtocol.setBackErr(status[1]);
                staProtocol.setHighErr(status[2]);
                staProtocol.setLeftErr(status[3]);
                staProtocol.setRightErr(status[4]);
                staProtocol.setWeightErr(status[5]);
                staProtocol.setBarcodeErr(status[6]);
            }
            //条码扫描器
            Thread.sleep(200);
            ArrayList<Integer> barcodeList = getBarcodeList();
            OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.60", (short) (barcodeList.size() * 8));
            if (result2.IsSuccess) {
                for (int i = 0; i < barcodeList.size(); i++) {
                    Integer barcodeId = barcodeList.get(i);
                    String barcode = siemensS7Net.getByteTransform().TransString(result2.Content, i * 8, 8, "UTF-8");
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId);
                    if (!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
                        barcodeThread.setBarcode(barcode);
                    }
                for (int i = 0; i < staNoSize; i++) {
                    Integer siteId = staNos.get(i); // 站点编号
                    boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErrsanqi.Content, i * 4, 1);
                    StaProtocol staProtocol = station.get(siteId);
                    staProtocol.setFrontErr(status[0]);
                    staProtocol.setBackErr(status[1]);
                    staProtocol.setHighErr(status[2]);
                    staProtocol.setLeftErr(status[3]);
                    staProtocol.setRightErr(status[4]);
                    staProtocol.setWeightErr(status[5]);
                    staProtocol.setBarcodeErr(status[6]);
                }
            }
            //条码扫描器 三期
            Thread.sleep(200);
            ArrayList<Integer> barcodeListsanqi = getBarcodeList();
            OperateResultExOne<byte[]> result2siqi = siemensS7Net.Read("DB101.60", (short) (barcodeListsanqi.size() * 8));
            ArrayList<Integer> barcodeListsanqi = barcode3;
            OperateResultExOne<byte[]> result2siqi = siemensS7Net.Read("DB101.180", (short) (barcodeListsanqi.size() * 8));
            if (result2siqi.IsSuccess) {
                for (int i = 0; i < barcodeList.size(); i++) {
                for (int i = 0; i < barcodeListsanqi.size(); i++) {
                    Integer barcodeId = barcodeListsanqi.get(i);
                    String barcode = siemensS7Net.getByteTransform().TransString(result2siqi.Content, i * 8, 8, "UTF-8");
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId);
@@ -395,24 +428,43 @@
                }
            }
        }
        // 根据实时信息更新数据库
        try {
            if (!station.isEmpty()) {
                List<BasDevp> basDevps = new ArrayList<>();
                for (Integer siteId : staNos) {
            //故障 三期
            OperateResultExOne<byte[]> resultErrs = siemensS7Net.Read("DB101.220", (short) (staNoSize * 4));
            if (resultErrs.IsSuccess) {
                for (int i = 0; i < staNoSize; i++) {
                    Integer siteId = staNos.get(i); // 站点编号
                    boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErrs.Content, i * 4, 1);
                    StaProtocol staProtocol = station.get(siteId);
                    basDevps.add(staProtocol.toSqlModel());
                    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]);
                }
                BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                    throw new Exception("更新数据库数据失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            // 根据实时信息更新数据库
            try {
                if (!station.isEmpty()) {
                    List<BasDevp> basDevps = new ArrayList<>();
                    for (Integer siteId : staNos) {
                        StaProtocol staProtocol = station.get(siteId);
                        basDevps.add(staProtocol.toSqlModel());
                    }
                    BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                    if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
                        throw new Exception("更新数据库数据失败");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            }
        }
    }
@@ -427,9 +479,10 @@
        OperateResult write = null;
        OperateResult write1 = null;
        int index;
        int index2 = 0;
        if (staProtocol.getSiteId() >= 310) {
            index = staNos3.indexOf(staProtocol.getSiteId());
            index = index + 54;
            index2 = 66;
        } else {
            index = staNos.indexOf(staProtocol.getSiteId());
        }
@@ -458,10 +511,10 @@
            boolean writeFlag = false;
            while (writeCount < 5) {
                // 写2号输送线
                write = siemensS7Net.Write("DB100." + index * 4, array);    // 工作号、目标
                write = siemensS7Net.Write("DB100." + (index * 4 + index2), array);    // 工作号、目标
                if (write.IsSuccess) {
                    Thread.sleep(200);
                    OperateResultExOne<byte[]> readResult = siemensS7Net.Read("DB100." + index * 4, (short) 4);
                    OperateResultExOne<byte[]> readResult = siemensS7Net.Read("DB100." + (index * 4 + index2), (short) 4);
                    //OperateResultExOne<byte[]> readResult = siemensS7Net.Read("DB1000." + index*4, (short)4);
                    if (readResult.IsSuccess) {
                        short workNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 0);
@@ -524,14 +577,14 @@
            boolean writeFlag = false;
            while (writeCount < 5) {
                // 写2号输送线
                write = siemensS7Net.Write("DB100." + index * 6, staProtocol.getWorkNo().intValue());    // 工作号
                write = siemensS7Net.Write("DB100." + (index * 6 + index2), staProtocol.getWorkNo().intValue());    // 工作号
                Thread.sleep(200);
                write1 = siemensS7Net.Write("DB100." + (index * 6 + 4), staProtocol.getStaNo());    // 目标站
                write1 = siemensS7Net.Write("DB100." + (index * 6 + 4 + index2), staProtocol.getStaNo());    // 目标站
                if (write.IsSuccess) {
                    Thread.sleep(200);
                    OperateResultExOne<byte[]> readResult = siemensS7Net.Read("DB100." + index * 6, (short) 4);
                    OperateResultExOne<byte[]> readResult = siemensS7Net.Read("DB100." + (index * 6 + index2), (short) 4);
                    Thread.sleep(200);
                    OperateResultExOne<byte[]> readResult1 = siemensS7Net.Read("DB100." + (index * 6 + 4), (short) 2);
                    OperateResultExOne<byte[]> readResult1 = siemensS7Net.Read("DB100." + (index * 6 + 4 + index2), (short) 2);
                    //OperateResultExOne<byte[]> readResult = siemensS7Net.Read("DB1000." + index*4, (short)4);
                    if (readResult.IsSuccess && readResult1.IsSuccess) {
                        int workNo = siemensS7Net.getByteTransform().TransInt32(readResult.Content, 0);