| | |
| | | add(2022);add(2024); |
| | | add(2028);add(2030); |
| | | add(2025);add(2027); |
| | | add(2031);add(2033); |
| | | add(2031);add(2032);add(2033); |
| | | add(2034);add(2035);add(2037); |
| | | // add(2041);add(2042);add(2043);add(2044); |
| | | // add(2045);add(2046);add(2047);add(2048); |
| | |
| | | } else if(siteId<4000){ |
| | | if (siteId == 2110) { |
| | | if (resultArm5.IsSuccess) { |
| | | // 转换为字符串 |
| | | String resultString = siemensS7Net.getByteTransform().TransString(resultArm5.Content, 2, 254,"UTF-8"); |
| | | // 去除可能的空字符 |
| | | // resultString = resultString.TrimEnd('\0'); |
| | | // System.out.println("2110Barcode:"+resultString); |
| | | staProtocol.setBarcode(resultString); |
| | | staProtocol.setBarcode(parseS7String(resultArm5)); |
| | | } else { |
| | | staProtocol.setBarcode(""); |
| | | } |
| | | } else if (siteId == 2120){ |
| | | if (resultArm6.IsSuccess) { |
| | | // 转换为字符串 |
| | | String resultString = siemensS7Net.getByteTransform().TransString(resultArm6.Content, 2, 254,"UTF-8"); |
| | | // 去除可能的空字符 |
| | | // resultString = resultString.TrimEnd('\0'); |
| | | // System.out.println("2120Barcode:"+resultString); |
| | | staProtocol.setBarcode(resultString); |
| | | staProtocol.setBarcode(parseS7String(resultArm6)); |
| | | } else { |
| | | staProtocol.setBarcode(""); |
| | | } |
| | |
| | | boolean[] status1 = siemensS7Net.getByteTransform().TransBool(result.Content, offset + offset3 - 4, 3); |
| | | boolean[] statusErr = siemensS7Net.getByteTransform().TransBool(result.Content, offset + offset3 - 8, 3); |
| | | boolean[] statusSign = siemensS7Net.getByteTransform().TransBool(resultErr.Content, offsetSign, 3); |
| | | |
| | | boolean loading = false; |
| | | if(!status1[0]) |
| | | { |
| | | loading = true; |
| | | } |
| | | staProtocol.setLoading(loading); // 有物 |
| | | if(siteId == 2032){ |
| | | staProtocol.setLoading(loading); // 有物 |
| | | |
| | | } |
| | | staProtocol.setInEnable(status1[2]); // 可入 |
| | | staProtocol.setOutEnable(status1[2]);// 可出 |
| | | staProtocol.setErr(status1[6]); |
| | |
| | | News.info("SiemensDevp"+" - 5"+" - 输送线命令下发 [id:{}] >>>>> 命令下发: {}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | } |
| | | } |
| | | // /** |
| | | // * 写入 ID + 目标站 =====> 单站点写入(带读回校验 + 重试机制) |
| | | // */ |
| | | // private void write(StaProtocol staProtocol) throws InterruptedException { |
| | | // if (staProtocol == null) { |
| | | // return; |
| | | // } |
| | | // |
| | | // Integer siteId = staProtocol.getSiteId(); |
| | | // Integer offset = siteOffsetMap.get(siteId); |
| | | // if (offset == null) { |
| | | // log.warn("站点 {} 没有定义偏移量,跳过写入", siteId); |
| | | // return; |
| | | // } |
| | | // |
| | | // Integer offset2 = getOffsetBySiteId(siteId); |
| | | // Integer offset3 = getOffsetBySiteId2(siteId); // 虽然本方法主要用不到,但保留以防后续扩展 |
| | | // |
| | | // // 准备期望写入的值(用于后续比对) |
| | | // final long expectWorkNo = staProtocol.getWorkNo(); |
| | | // final long expectStaNo = staProtocol.getStaNo() != null ? staProtocol.getStaNo().intValue() : 0; |
| | | // |
| | | // boolean isSpecial2 = specialSites2.contains(siteId); |
| | | // final long expectStaNoOffset = isSpecial2 ? (offset + offset2 + 48) : (offset + offset2 + 12); |
| | | // |
| | | // // 确定要操作的 DB 块 |
| | | // String dbName; |
| | | // if (siteId < 1029) { |
| | | // dbName = "DB100."; |
| | | // } else if (siteId < 1042) { |
| | | // dbName = "DB101."; |
| | | // } else if (siteId < 1054) { |
| | | // dbName = "DB104."; |
| | | // } else if (siteId < 1080) { |
| | | // dbName = "DB103."; |
| | | // } else if (siteId < 1111) { |
| | | // dbName = "DB102."; |
| | | // } else if (siteId < 2031) { |
| | | // dbName = "DB200."; |
| | | // } else if (siteId < 2121) { |
| | | // dbName = "DB201."; |
| | | // } else if (siteId < 4000) { |
| | | // // 2楼PDA条码站点特殊处理(目前不做读回重试) |
| | | // Integer offsetBarcode = site2lBarcodeOffsetMap.get(siteId); |
| | | // if (offsetBarcode == null) { |
| | | // log.warn("站点 {} 没有定义2楼条码偏移量,跳过条码写入", siteId); |
| | | // return; |
| | | // } |
| | | // OperateResult writeBarcode = siemensS7Net.Write("DB202." + offsetBarcode, staProtocol.getBarcode()); |
| | | // if (writeBarcode.IsSuccess) { |
| | | // log.info("2楼PDA条码写入成功 site={}, barcode={}", siteId, staProtocol.getBarcode()); |
| | | // } else { |
| | | // log.error("2楼PDA条码写入失败 site={}, barcode={}", siteId, staProtocol.getBarcode()); |
| | | // } |
| | | // return; |
| | | // } else { |
| | | // dbName = "DB400."; |
| | | // } |
| | | // |
| | | // String addrWorkNo = dbName + (offset + offset2); |
| | | // String addrStaNo = dbName + expectStaNoOffset; |
| | | // |
| | | // int maxRetry = 5; |
| | | // int tryCount = 0; |
| | | // boolean success = false; |
| | | // |
| | | // while (tryCount < maxRetry && !success) { |
| | | // tryCount++; |
| | | // |
| | | // // 1. 尝试写入 |
| | | // OperateResult writeWork = siemensS7Net.Write(addrWorkNo, expectWorkNo); |
| | | // writeWork = siemensS7Net.Write("DB100." + (offset + offset2), staProtocol.getWorkNo()); // 工作号 |
| | | // |
| | | // Thread.sleep(80); |
| | | // |
| | | // OperateResult writeSta = siemensS7Net.Write(addrStaNo, expectStaNo); |
| | | // Thread.sleep(120); |
| | | // |
| | | // if (!writeWork.IsSuccess || !writeSta.IsSuccess) { |
| | | // log.warn("第{}次写入失败 site={}, workNo={}, targetSta={}", |
| | | // tryCount, siteId, expectWorkNo, expectStaNo); |
| | | // Thread.sleep(300); |
| | | // continue; |
| | | // } |
| | | // |
| | | // // 2. 读回校验(至少读64字节,确保覆盖所需字段) |
| | | // OperateResultExOne<byte[]> readResult = siemensS7Net.Read(dbName + (offset + offset2), (short) 64); |
| | | // if (!readResult.IsSuccess || readResult.Content == null || readResult.Content.length < 64) { |
| | | // log.warn("第{}次读回失败,无法校验 site={}", tryCount, siteId); |
| | | // Thread.sleep(300); |
| | | // continue; |
| | | // } |
| | | // |
| | | // byte[] data = readResult.Content; |
| | | // |
| | | // // 计算相对偏移进行读取 |
| | | // int relativeWorkNoOffset = 0; |
| | | // int relativeStaNoOffset = (int) (expectStaNoOffset - (offset + offset2)); |
| | | // |
| | | // int actualWorkNo = siemensS7Net.getByteTransform().TransInt32(data, relativeWorkNoOffset); |
| | | // int actualStaNo = siemensS7Net.getByteTransform().TransInt32(data, relativeStaNoOffset); |
| | | // |
| | | // boolean match = (actualWorkNo == expectWorkNo) && (actualStaNo == expectStaNo); |
| | | // |
| | | // if (match) { |
| | | // success = true; |
| | | // log.info("写入并校验成功 site={}, workNo={}, targetSta={}, 第{}次尝试", |
| | | // siteId, expectWorkNo, expectStaNo, tryCount); |
| | | // } else { |
| | | // log.warn("第{}次校验失败 site={}, 期望[workNo={},staNo={}], 实际[workNo={},staNo={}]", |
| | | // tryCount, siteId, expectWorkNo, expectStaNo, actualWorkNo, actualStaNo); |
| | | // Thread.sleep(300); |
| | | // } |
| | | // } |
| | | // |
| | | // // 最终结果处理 |
| | | // if (success) { |
| | | // log.info("输送线命令下发并校验成功 [plc:{}] site:{} workNo:{} target:{}", |
| | | // slave.getId(), siteId, expectWorkNo, expectStaNo); |
| | | // |
| | | // OutputQueue.DEVP.offer(MessageFormat.format( |
| | | // "【{0}】输送线命令下发并校验成功 [id:{1}] >>>>> site:{2} workNo:{3} target:{4}", |
| | | // DateUtils.convert(new Date()), slave.getId(), siteId, expectWorkNo, expectStaNo)); |
| | | // |
| | | // News.info("SiemensDevp - 5 - 输送线命令下发并校验成功 [id:{}] >>>>> {}", |
| | | // slave.getId(), JSON.toJSON(staProtocol)); |
| | | // } else { |
| | | // log.error("经过{}次尝试,写入并校验仍然失败!plc={} site={} workNo预期={} target预期={}", |
| | | // maxRetry, slave.getId(), siteId, expectWorkNo, expectStaNo); |
| | | // |
| | | // OutputQueue.DEVP.offer(MessageFormat.format( |
| | | // "【{0}】经过{1}次尝试,输送线写入校验失败! [id:{2}] site:{3} workNo:{4} target:{5}", |
| | | // DateUtils.convert(new Date()), maxRetry, slave.getId(), siteId, expectWorkNo, expectStaNo)); |
| | | // |
| | | // News.error("SiemensDevp - 4 - 多次尝试后写入校验失败!site={} workNo={} target={}", |
| | | // siteId, expectWorkNo, expectStaNo); |
| | | // } |
| | | // |
| | | // // 操作日志记录(无论成功失败都记录) |
| | | // try { |
| | | // BasDevpOptService bean = SpringUtils.getBean(BasDevpOptService.class); |
| | | // BasDevpOpt basDevpOpt = new BasDevpOpt( |
| | | // staProtocol.getWorkNo(), |
| | | // new Date(), |
| | | // staProtocol.getSiteId(), |
| | | // (int) staProtocol.getStaNo(), |
| | | // new Date(), |
| | | // String.valueOf(staProtocol.isLoading()), |
| | | // staProtocol.getBarcode(), |
| | | // staProtocol.isFrontErr() ? 1L : 0L, |
| | | // staProtocol.isBackErr() ? 1L : 0L, |
| | | // staProtocol.isHighErr() ? 1L : 0L, |
| | | // staProtocol.isLeftErr() ? 1L : 0L, |
| | | // staProtocol.isRightErr() ? 1L : 0L, |
| | | // staProtocol.isBarcodeErr() ? 1L : 0L, |
| | | // staProtocol.getErrCode() |
| | | // ); |
| | | // bean.insert(basDevpOpt); |
| | | // } catch (Exception e) { |
| | | // log.warn("写入操作日志失败 site={}", siteId, e); |
| | | // } |
| | | // } |
| | | |
| | | // 更新入出库模式 |
| | | private void updateIoMode() throws InterruptedException { |
| | |
| | | } |
| | | } |
| | | |
| | | private String parseS7String(OperateResultExOne<byte[]> result) { |
| | | |
| | | if (result == null || !result.IsSuccess || result.Content == null) { |
| | | return ""; |
| | | } |
| | | |
| | | byte[] buf = result.Content; |
| | | |
| | | if (buf.length < 2) { |
| | | return ""; |
| | | } |
| | | |
| | | // S7 字符串格式: |
| | | // buf[0] = 最大长度 |
| | | // buf[1] = 当前真实长度 |
| | | int realLen = buf[1] & 0xFF; |
| | | |
| | | if (realLen <= 0) { |
| | | return ""; |
| | | } |
| | | |
| | | // 防止 PLC 写错长度导致越界 |
| | | if (realLen > buf.length - 2) { |
| | | realLen = buf.length - 2; |
| | | } |
| | | |
| | | String value = siemensS7Net.getByteTransform() |
| | | .TransString(buf, 2, realLen, "UTF-8"); |
| | | |
| | | return value == null ? "" : value.trim(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 设置入库标记 |
| | | */ |