| | |
| | | plcAddressStaNo = "DB73." + (4 * 6 + 4); |
| | | break; |
| | | } |
| | | // String plcAddressWorkNo = "DB100." + index * 6; |
| | | // String plcAddressStaNo = "DB100." + (index * 6 + 4); |
| | | |
| | | while (writeCount < 5) { |
| | | // **读取当前PLC状态,避免不必要的写入** |
| | | OperateResultExOne<byte[]> readResult = siemensS7Net.Read(plcAddressWorkNo, (short) 6); |
| | | if (readResult.IsSuccess) { |
| | | int currentWorkNo = siemensS7Net.getByteTransform().TransInt32(readResult.Content, 0); |
| | | short currentStaNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 4); |
| | | |
| | | if (currentWorkNo == staProtocol.getWorkNo().intValue() && currentStaNo == staProtocol.getStaNo()) { |
| | | log.info("站点 {} 当前状态已匹配,无需重复写入", staProtocol.getSiteId()); |
| | | return; |
| | | } |
| | | } |
| | | |
| | | // **写入新任务** |
| | | if (writeTaskToPLC(plcAddressWorkNo, plcAddressStaNo, staProtocol)) { |
| | | writeFlag = true; |
| | | log.info("输送线命令写入成功,PLC编号={},站点数据={},尝试次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount); |
| | | break; |
| | | } |
| | | |
| | | log.warn("输送线命令写入失败,PLC编号={},站点数据={},尝试次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount); |
| | | writeCount++; |
| | | // **写入新任务** |
| | | if (writeTaskToPLC(plcAddressWorkNo, plcAddressStaNo, staProtocol)) { |
| | | writeFlag = true; |
| | | log.info("输送线命令写入成功,PLC编号={},站点数据={},尝试次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount); |
| | | } |
| | | |
| | | // **写入失败处理** |
| | |
| | | * 写入新任务到 PLC 并验证是否成功 |
| | | */ |
| | | private boolean writeTaskToPLC(String plcAddressWorkNo, String plcAddressStaNo, StaProtocol staProtocol) throws InterruptedException { |
| | | OperateResult writeResult1 = siemensS7Net.Write(plcAddressWorkNo, staProtocol.getWorkNo().intValue()); |
| | | OperateResult writeResult2 = siemensS7Net.Write(plcAddressStaNo, staProtocol.getStaNo()); |
| | | |
| | | OperateResult writeResult1 = siemensS7Net.Write(plcAddressWorkNo, staProtocol.getWorkNo().intValue()); |
| | | |
| | | if (writeResult1.IsSuccess && writeResult2.IsSuccess) { |
| | | Thread.sleep(200); // 等待 PLC 识别新值 |
| | | OperateResultExOne<byte[]> readResult = siemensS7Net.Read(plcAddressWorkNo, (short) 6); |