| | |
| | | import HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.core.common.DateUtils; |
| | | import com.core.common.SpringUtils; |
| | | import com.zy.asrs.entity.BasCrnp; |
| | | import com.zy.asrs.service.BasCrnpService; |
| | | import com.zy.core.ThreadHandler; |
| | | import com.zy.core.cache.MessageQueue; |
| | | import com.zy.core.cache.OutputQueue; |
| | |
| | | private SiemensS7Net siemensNet; |
| | | private CrnSlave slave; |
| | | private CrnProtocol crnProtocol; |
| | | private boolean resetFlag = false; |
| | | |
| | | public CrnThread(CrnSlave slave) { |
| | | this.slave = slave; |
| | |
| | | while (true) { |
| | | try { |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Devp, slave.getId()); |
| | | Task task = MessageQueue.poll(SlaveType.Crn, slave.getId()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | } |
| | |
| | | * 读取状态 |
| | | */ |
| | | private void readStatus(){ |
| | | OperateResultExOne<byte[]> result = siemensNet.Read("DB8.18", (short) 56); |
| | | OperateResultExOne<byte[]> result = siemensNet.Read("DB8.18", (short) 62); |
| | | if (result.IsSuccess) { |
| | | OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId())); |
| | | if (null == crnProtocol) { |
| | | crnProtocol = new CrnProtocol(); |
| | | } |
| | |
| | | crnProtocol.setXDuration(siemensNet.getByteTransform().TransSingle(result.Content, 54)); |
| | | crnProtocol.setYDuration(siemensNet.getByteTransform().TransSingle(result.Content, 58)); |
| | | |
| | | /** |
| | | * 当读到status == 90.任务完成等待WCS确认 时 |
| | | * 下发任务完成指令 |
| | | */ |
| | | OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId())); |
| | | |
| | | // 复位信号 |
| | | if (crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) { |
| | | CrnCommand crnCommand = new CrnCommand(); |
| | | crnCommand.setAckFinish((short)1); |
| | | write(crnCommand); |
| | | if (resetFlag) { |
| | | CrnCommand crnCommand = new CrnCommand(); |
| | | crnCommand.setAckFinish((short)1); |
| | | if (write(crnCommand)) { |
| | | resetFlag = false; |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | // 根据实时信息更新数据库 |
| | | BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class); |
| | | BasCrnp basCrnp = new BasCrnp(); |
| | | basCrnp.setCrnNo(slave.getId()); |
| | | if (!basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){ |
| | | log.error("堆垛机plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); |
| | | } |
| | | |
| | | } else { |
| | | OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | log.error("读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); |
| | |
| | | /** |
| | | * 写入数据 |
| | | */ |
| | | private void write(CrnCommand command){ |
| | | private boolean write(CrnCommand command){ |
| | | if (null == command) { |
| | | log.error("堆垛机写入命令为空"); |
| | | return; |
| | | return false; |
| | | } |
| | | command.setCrnNo(slave.getId()); |
| | | short[] array = new short[9]; |
| | |
| | | array[8] = command.getDestinationPosZ(); |
| | | OperateResult result = siemensNet.Write("DB8.0", array); |
| | | if (result.IsSuccess) { |
| | | System.out.println(MessageFormat.format("【{0}】[id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); |
| | | log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); |
| | | OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); |
| | | // readStatus(); |
| | | return true; |
| | | } else { |
| | | System.out.println(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | log.error("写入堆垛机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | |
| | | public void close() { |
| | | siemensNet.ConnectClose(); |
| | | } |
| | | |
| | | public CrnProtocol getCrnProtocol(){ |
| | | if (crnProtocol == null) { |
| | | readStatus(); |
| | | } |
| | | return this.crnProtocol; |
| | | } |
| | | |
| | | |
| | | /******************************************************************************************/ |
| | | /**************************************** 测试专用 *****************************************/ |