| | |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.model.DevpSlave; |
| | | import com.zy.core.model.Task; |
| | | import com.zy.core.model.protocol.Cycle; |
| | | import com.zy.core.model.protocol.StaProtocol; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | break; |
| | | // 写数据 ID+目标站 |
| | | case 4: |
| | | writeAgvOk((StaProtocol)task.getData()); |
| | | writeCycle((StaProtocol)task.getData()); |
| | | read(); |
| | | break; |
| | | /* case 3: |
| | |
| | | handleWriteFailure(staProtocol, writeFlag); |
| | | } |
| | | |
| | | private void writeCycle(StaProtocol staProtocol) throws InterruptedException { |
| | | if (staProtocol == null) { |
| | | return; |
| | | } |
| | | |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | int index = staNos.indexOf(staProtocol.getSiteId()); |
| | | |
| | | if (index == -1) { |
| | | log.error("站点编号 {} 不在已知列表中,无法写入任务!", staProtocol.getSiteId()); |
| | | return; |
| | | } |
| | | |
| | | int writeCount = 0; // 任务下发尝试次数 |
| | | boolean writeFlag = false; // 任务下发成功标记 |
| | | String plcAddressWorkNo = ""; |
| | | String plcAddressStaNo = "" ; |
| | | switch (staProtocol.getSiteId()){ |
| | | case 105: |
| | | plcAddressWorkNo = "DB73." + 0; |
| | | plcAddressStaNo = "DB73." + 4; |
| | | break; |
| | | case 106: |
| | | plcAddressWorkNo = "DB73." + 6; |
| | | plcAddressStaNo = "DB73." + (6 + 4); |
| | | break; |
| | | case 108: |
| | | plcAddressWorkNo = "DB73." + 2 * 6; |
| | | plcAddressStaNo = "DB73." + (2 * 6 + 4); |
| | | break; |
| | | case 110: |
| | | plcAddressWorkNo = "DB73." + 3 * 6; |
| | | plcAddressStaNo = "DB73." + (3 * 6 + 4); |
| | | break; |
| | | case 112: |
| | | plcAddressWorkNo = "DB73." + 4 * 6; |
| | | 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++; |
| | | } |
| | | |
| | | // **写入失败处理** |
| | | handleWriteFailure(staProtocol, writeFlag); |
| | | } |
| | | |
| | | /** |
| | | * 清零 PLC 数据并验证清零是否成功 |
| | | */ |
| | |
| | | return false; |
| | | } |
| | | |
| | | private boolean writeTaskToPLC(String plcAddressWorkNo, String plcAddressStaNo, Cycle cycle) throws InterruptedException { |
| | | OperateResult writeResult1 = siemensS7Net.Write(plcAddressWorkNo, cycle.getWorkNo().intValue()); |
| | | OperateResult writeResult2 = siemensS7Net.Write(plcAddressStaNo, cycle.getStaNo()); |
| | | |
| | | if (writeResult1.IsSuccess && writeResult2.IsSuccess) { |
| | | Thread.sleep(200); // 等待 PLC 识别新值 |
| | | OperateResultExOne<byte[]> readResult = siemensS7Net.Read(plcAddressWorkNo, (short) 6); |
| | | if (readResult.IsSuccess) { |
| | | int workNo = siemensS7Net.getByteTransform().TransInt32(readResult.Content, 0); |
| | | short staNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 4); |
| | | return workNo == cycle.getWorkNo().intValue() && staNo == cycle.getStaNo(); |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 处理写入失败的情况 |
| | | */ |