| | |
| | | case 2: |
| | | write((RgvCommand) task.getData()); |
| | | break; |
| | | //工位1写入数据 |
| | | case 4: |
| | | write1((RgvCommand) task.getData()); |
| | | break; |
| | | //工位2写入数据 |
| | | case 5: |
| | | write2((RgvCommand) task.getData()); |
| | | break; |
| | | // 复位 |
| | | case 3: |
| | | RgvCommand command = (RgvCommand) task.getData(); |
| | |
| | | command = new RgvCommand(); |
| | | } |
| | | command.setRgvNo(slave.getId()); // RGV编号 |
| | | command.setTaskNo1((short) 0); // 工作号 |
| | | command.setAckFinish1((short) 1); // 任务完成确认位 |
| | | command.setTaskMode1(RgvTaskModeType.NONE); // 任务模式 |
| | | command.setSourceStaNo1((short)0); // 源站 |
| | | command.setDestinationStaNo1((short)0); // 目标站 |
| | | command.setTaskNo2((short) 0); // 工作号 |
| | | command.setAckFinish2((short) 1); // 任务完成确认位 |
| | | command.setTaskMode2(RgvTaskModeType.NONE); // 任务模式 |
| | | command.setSourceStaNo2((short)0); // 源站 |
| | | command.setDestinationStaNo2((short)0); // 目标站 |
| | | command.setTaskNo((short) 0); // 工作号 |
| | | command.setAckFinish((short) 1); // 任务完成确认位 |
| | | command.setTaskMode(RgvTaskModeType.NONE); // 任务模式 |
| | | command.setDestinationStaNo(0L); // 目标站 |
| | | command.setCommand((short)0); |
| | | write(command); |
| | | break; |
| | | // 回原点 避让 |
| | | case 9: |
| | | RgvCommand commandAvoidanceXY = (RgvCommand) task.getData(); |
| | | if (null == commandAvoidanceXY) { |
| | | commandAvoidanceXY = new RgvCommand(); |
| | | } |
| | | commandAvoidanceXY.setRgvNo(slave.getId()); // RGV编号 |
| | | commandAvoidanceXY.setTaskNo1((short) 32222); // 工作号 |
| | | commandAvoidanceXY.setAckFinish1((short) 1); // 任务完成确认位 |
| | | commandAvoidanceXY.setTaskMode1(RgvTaskModeType.X_MOVE); // 任务模式 |
| | | commandAvoidanceXY.setSourceStaNo1((short)0); // 源站 |
| | | commandAvoidanceXY.setDestinationStaNo1(slave.getId()==1? (short) 101:(short)116); // 目标站 |
| | | commandAvoidanceXY.setTaskNo2((short) 0); // 工作号 |
| | | commandAvoidanceXY.setAckFinish2((short) 1); // 任务完成确认位 |
| | | commandAvoidanceXY.setTaskMode2(RgvTaskModeType.X_MOVE); // 任务模式 |
| | | commandAvoidanceXY.setSourceStaNo2((short)0); // 源站 |
| | | commandAvoidanceXY.setDestinationStaNo2(slave.getId()==1? (short) 101:(short)117); // 目标站 |
| | | commandAvoidanceXY.setCommand((short)0); |
| | | write(commandAvoidanceXY); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | Thread.sleep(500); |
| | | } catch (Exception e) { |
| | | log.error("RGV线程异常!!!"+e.getMessage()); |
| | | // e.printStackTrace(); |
| | | } |
| | | |
| | |
| | | } |
| | | rgvProtocol.setMode((short) -1); |
| | | rgvProtocol.setStatus((short)-1); |
| | | rgvProtocol.setTaskNo1((short)0); |
| | | rgvProtocol.setStatus1((short)-1); |
| | | rgvProtocol.setLoaded1((short)0); |
| | | rgvProtocol.setWalkPos((short)0); |
| | | rgvProtocol.setRgvPos((short)0); |
| | | rgvProtocol.setTaskNo2((short)0); |
| | | rgvProtocol.setStatus2((short)-1); |
| | | rgvProtocol.setLoaded2((short)0); |
| | | rgvProtocol.setRgvPos(0L); |
| | | rgvProtocol.setAlarm((short)0); |
| | | rgvProtocol.setxSpeed((short) 0); |
| | | rgvProtocol.setxDistance((short) 0); |
| | |
| | | } |
| | | rgvProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 0)); |
| | | rgvProtocol.setStatus(siemensNet.getByteTransform().TransInt16(result.Content, 2)); |
| | | rgvProtocol.setTaskNo1(siemensNet.getByteTransform().TransInt16(result.Content, 4)); |
| | | rgvProtocol.setStatus1(siemensNet.getByteTransform().TransInt16(result.Content, 6)); |
| | | rgvProtocol.setLoaded1(siemensNet.getByteTransform().TransInt16(result.Content, 8)); |
| | | rgvProtocol.setRgvPos(RouteUtils.RgvPosSta(slave.getId(),siemensNet.getByteTransform().TransInt16(result.Content, 10))); |
| | | rgvProtocol.setWalkPos(siemensNet.getByteTransform().TransInt16(result.Content, 12)); |
| | | rgvProtocol.setTaskNo2(siemensNet.getByteTransform().TransInt16(result.Content, 14)); |
| | | rgvProtocol.setStatus2(siemensNet.getByteTransform().TransInt16(result.Content, 16)); |
| | | rgvProtocol.setLoaded2(siemensNet.getByteTransform().TransInt16(result.Content, 18)); |
| | | rgvProtocol.setRgvPos(siemensNet.getByteTransform().TransUInt32(result.Content, 10)); |
| | | rgvProtocol.setAlarm(siemensNet.getByteTransform().TransInt16(result.Content, 20)); |
| | | rgvProtocol.setHeart(siemensNet.getByteTransform().TransInt16(result.Content, 22)); |
| | | rgvProtocol.setTemp1(siemensNet.getByteTransform().TransInt16(result.Content, 24)); |
| | | rgvProtocol.setTemp2(siemensNet.getByteTransform().TransInt16(result.Content, 26)); |
| | | rgvProtocol.setTemp3(siemensNet.getByteTransform().TransInt16(result.Content, 28)); |
| | | rgvProtocol.setTemp4(siemensNet.getByteTransform().TransInt16(result.Content, 30)); |
| | | rgvProtocol.setTemp5(siemensNet.getByteTransform().TransInt16(result.Content, 32)); |
| | | // rgvProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 28)); |
| | | // rgvProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 40)); |
| | | // rgvProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 48)); |
| | | |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId())); |
| | | |
| | | // 工位1复位信号 |
| | | if (rgvProtocol.getStatusType1().equals(RgvStatusType.WAITING) |
| | | || rgvProtocol.getStatusType1().equals(RgvStatusType.FETCHWAITING)) { |
| | | if (rgvProtocol.getStatusType().equals(RgvStatusType.WAITING) |
| | | || rgvProtocol.getStatusType().equals(RgvStatusType.FETCHWAITING)) { |
| | | if (resetFlag1) { |
| | | RgvCommand rgvCommand = new RgvCommand(); |
| | | rgvCommand.setAckFinish1((short)1); |
| | | if (write(rgvCommand)) { |
| | | resetFlag1 = false; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 工位2复位信号 |
| | | if (rgvProtocol.getStatusType2().equals(RgvStatusType.WAITING) |
| | | || rgvProtocol.getStatusType2().equals(RgvStatusType.FETCHWAITING)) { |
| | | if (resetFlag2) { |
| | | RgvCommand rgvCommand = new RgvCommand(); |
| | | rgvCommand.setAckFinish2((short)1); |
| | | if (write(rgvCommand)) { |
| | | resetFlag2 = false; |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 工位1、2同时写入数据 |
| | | * 写入数据 |
| | | */ |
| | | private boolean write(RgvCommand command) throws InterruptedException { |
| | | if (null == command) { |
| | |
| | | command.setRgvNo(slave.getId()); |
| | | short[] array = new short[11]; |
| | | array[0] = command.getAckFinish1(); |
| | | array[1] = command.getTaskNo1(); |
| | | array[2] = command.getTaskMode1(); |
| | | array[3] = command.getSourceStaNo1(); |
| | | array[4] = command.getDestinationStaNo1(); |
| | | array[5] = command.getAckFinish2(); |
| | | array[6] = command.getTaskNo2(); |
| | | array[7] = command.getTaskMode2(); |
| | | array[8] = command.getSourceStaNo2(); |
| | | array[9] = command.getDestinationStaNo2(); |
| | | array[1] = command.getTaskNo(); |
| | | array[2] = command.getTaskMode(); |
| | | // array[4] = command.getDestinationStaNo(); |
| | | array[10] = command.getCommand(); |
| | | OperateResult result = siemensNet.Write("DB100.0", array); |
| | | |
| | | if (command.getAckFinish1() == 0 && command.getAckFinish2() == 0) { |
| | | if (command.getAckFinish1() == 0) { |
| | | short commandFinish = 3; //工位1、2任务同时写入 |
| | | Thread.sleep(100L); |
| | | result = siemensNet.Write("DB100.20", commandFinish); |
| | |
| | | // 日志记录 |
| | | BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class); |
| | | BasRgvOpt basRgvOpt = new BasRgvOpt( |
| | | command.getTaskNo1().intValue(), |
| | | command.getTaskNo2().intValue(), |
| | | command.getTaskNo().intValue(), |
| | | command.getTaskNo().intValue(), |
| | | command.getRgvNo(), |
| | | new Date(), |
| | | command.getTaskModeType1().toString(), |
| | | command.getSourceStaNo1().intValue(), |
| | | command.getDestinationStaNo1().intValue(), |
| | | command.getSourceStaNo2().intValue(), |
| | | command.getDestinationStaNo2().intValue(), |
| | | command.getTaskModeType().toString(), |
| | | null, |
| | | null, |
| | | null, |
| | | command.getDestinationStaNo().intValue(), |
| | | null, |
| | | new Date(), |
| | | null |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 工位1写入数据 |
| | | */ |
| | | private boolean write1(RgvCommand command) throws InterruptedException { |
| | | if (null == command) { |
| | | log.error("RGV写入命令为空"); |
| | | return false; |
| | | } |
| | | |
| | | // siemensNet.Write("DB100.20", command.getCommand()); |
| | | |
| | | command.setRgvNo(slave.getId()); |
| | | short[] array = new short[5]; |
| | | array[0] = command.getAckFinish1(); |
| | | array[1] = command.getTaskNo1(); |
| | | array[2] = command.getTaskMode1();//任务模式 |
| | | array[3] = command.getSourceStaNo1(); |
| | | array[4] = command.getDestinationStaNo1(); |
| | | // siemensNet.Write("DB100.20", 0); |
| | | |
| | | OperateResult result = siemensNet.Write("DB100.0", array); |
| | | |
| | | if (command.getAckFinish1() == 0) { |
| | | short commandFinish = 1; //工位1任务写入 |
| | | Thread.sleep(100L); |
| | | result = siemensNet.Write("DB100.20", commandFinish); |
| | | } |
| | | |
| | | try { |
| | | // 日志记录 |
| | | BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class); |
| | | BasRgvOpt basRgvOpt = new BasRgvOpt( |
| | | command.getTaskNo1().intValue(), |
| | | command.getTaskNo2().intValue(), |
| | | command.getRgvNo(), |
| | | new Date(), |
| | | command.getTaskModeType1().toString(), |
| | | command.getSourceStaNo1().intValue(), |
| | | command.getDestinationStaNo1().intValue(), |
| | | command.getSourceStaNo2().intValue(), |
| | | command.getDestinationStaNo2().intValue(), |
| | | null, |
| | | new Date(), |
| | | null |
| | | ); |
| | | bean.insert(basRgvOpt); |
| | | } catch (Exception ignore) {} |
| | | |
| | | if (result != null && result.IsSuccess) { |
| | | Thread.sleep(200); |
| | | this.readStatus(); |
| | | log.info("RGV 工位1命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 工位1命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); |
| | | return true; |
| | | } else { |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】写入RGV plc工位1数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | log.error("写入RGV plc工位1数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 工位2写入数据 |
| | | */ |
| | | private boolean write2(RgvCommand command) throws InterruptedException { |
| | | if (null == command) { |
| | | log.error("RGV写入命令为空"); |
| | | return false; |
| | | } |
| | | |
| | | command.setRgvNo(slave.getId()); |
| | | short[] array = new short[6]; |
| | | array[0] = command.getAckFinish2();//任务完成确认位 |
| | | array[1] = command.getTaskNo2();//任务号 |
| | | array[2] = command.getTaskMode2();//任务模式 |
| | | array[3] = command.getSourceStaNo2();//源站 |
| | | array[4] = command.getDestinationStaNo2();//目标站 |
| | | array[5] = command.getCommand();//任务下发确认 |
| | | |
| | | OperateResult result = siemensNet.Write("DB100.10", array); |
| | | |
| | | if (command.getAckFinish2() == 0) { |
| | | short commandFinish = 2; //工位2任务写入 |
| | | Thread.sleep(100L); |
| | | result = siemensNet.Write("DB100.20", commandFinish); |
| | | } |
| | | |
| | | try { |
| | | // 日志记录 |
| | | BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class); |
| | | BasRgvOpt basRgvOpt = new BasRgvOpt( |
| | | command.getTaskNo1().intValue(), |
| | | command.getTaskNo2().intValue(), |
| | | command.getRgvNo(), |
| | | new Date(), |
| | | command.getTaskModeType1().toString(), |
| | | command.getSourceStaNo1().intValue(), |
| | | command.getDestinationStaNo1().intValue(), |
| | | command.getSourceStaNo2().intValue(), |
| | | command.getDestinationStaNo2().intValue(), |
| | | null, |
| | | new Date(), |
| | | null |
| | | ); |
| | | bean.insert(basRgvOpt); |
| | | } catch (Exception ignore) {} |
| | | |
| | | if (result != null && result.IsSuccess) { |
| | | Thread.sleep(200); |
| | | this.readStatus(); |
| | | log.info("RGV 工位2命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 工位2命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); |
| | | return true; |
| | | } else { |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】写入RGV plc工位2数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | log.error("写入RGV plc工位2数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | siemensNet.ConnectClose(); |
| | | } |
| | | |
| | | /******************************************************************************************/ |
| | | /**************************************** 测试专用 *****************************************/ |
| | | /*****************************************************************************************/ |
| | | public static void main(String[] args) throws InterruptedException { |
| | | RgvSlave slave = new RgvSlave(); |
| | | slave.setId(1); |
| | | slave.setIp("192.168.6.9"); |
| | | slave.setRack(0); |
| | | slave.setSlot(0); |
| | | RgvThread rgvThread = new RgvThread(slave); |
| | | rgvThread.connect(); |
| | | rgvThread.readStatus(); |
| | | System.out.println(JSON.toJSONString(rgvThread.rgvProtocol)); |
| | | Thread.sleep(3000L); |
| | | |
| | | } |
| | | |
| | | } |