| | |
| | | * 工位2复位信号 |
| | | */ |
| | | private boolean resetFlag2 = false; |
| | | private boolean connectRgv = false; |
| | | |
| | | public RgvThread(RgvSlave slave) { |
| | | this.slave = slave; |
| | |
| | | @Override |
| | | @SuppressWarnings("InfiniteLoopStatement") |
| | | public void run() { |
| | | this.connect(); |
| | | connectRgv = this.connect(); |
| | | while(!connectRgv){ |
| | | try { |
| | | connectRgv = this.connect(); |
| | | Thread.sleep(100); |
| | | } catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | |
| | | // 启动线程自动重连 |
| | | new Thread(this::rgvConnect).start(); |
| | | |
| | | // 启动读数据线程 |
| | | new Thread(this::readStatusRgv).start(); |
| | | |
| | | // 启动任务下发线程 |
| | | new Thread(this::taskIssued).start(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 任务下发 |
| | | */ |
| | | private void taskIssued() { |
| | | while (true) { |
| | | try { |
| | | int step = 1; |
| | |
| | | switch (step) { |
| | | // 读数据 |
| | | case 1: |
| | | readStatus(); |
| | | // readStatus(); |
| | | break; |
| | | //工位1写入数据 |
| | | case 2: |
| | |
| | | command.setTaskMode1(RgvTaskModeType.NONE); // 任务模式 |
| | | command.setSourceStaNo1((short)0); // 源站 |
| | | command.setDestinationStaNo1((short)0); // 目标站 |
| | | write(command); |
| | | write3(command); |
| | | break; |
| | | // 复位 |
| | | case 4: |
| | | RgvCommand command4 = (RgvCommand) task.getData(); |
| | | if (null == command4) { |
| | | command4 = new RgvCommand(); |
| | | } |
| | | command4.setRgvNo(slave.getId()); // RGV编号 |
| | | command4.setTaskNo1((short) 0); // 工作号 |
| | | command4.setAckFinish1((short) 1); // 任务完成确认位 |
| | | command4.setTaskMode1(RgvTaskModeType.X_MOVE); // 任务模式 |
| | | command4.setSourceStaNo1((short)0); // 源站 |
| | | command4.setDestinationStaNo1((short)0); // 目标站 |
| | | write3(command4); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | Thread.sleep(500); |
| | | Thread.sleep(100); |
| | | } catch (Exception e) { |
| | | log.error("RGV写线程异常"+e.getMessage()); |
| | | // e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | private void rgvConnect() { |
| | | while (true) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | if(!connectRgv){ |
| | | try { |
| | | connectRgv = this.connect(); |
| | | Thread.sleep(100); |
| | | } catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | |
| | | log.error("rgv连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | initRgv(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void readStatusRgv() { |
| | | while (true) { |
| | | try { |
| | | Thread.sleep(40); |
| | | readStatus(); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("RGV读线程异常"+e.getMessage()); |
| | | |
| | | log.error("RGV数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | initRgv(); |
| | | // e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | if (null == rgvProtocol) { |
| | | rgvProtocol = new RgvProtocol(); |
| | | } |
| | | rgvProtocol.setRgvNo(slave.getId()); |
| | | rgvProtocol.setMode((short) -1); |
| | | rgvProtocol.setStatus((short)-1); |
| | | rgvProtocol.setTaskNo1((short)0); |
| | |
| | | private void readStatus(){ |
| | | try { |
| | | OperateResultExOne<byte[]> result = siemensNet.Read("DB100.0", (short) 20); |
| | | if (result.IsSuccess) { |
| | | OperateResultExOne<byte[]> resultV = siemensNet.Read("DB20.16", (short) 2); |
| | | OperateResultExOne<byte[]> resultE = siemensNet.Read("DB20.26", (short) 2); |
| | | if (result.IsSuccess && resultV.IsSuccess && resultE.IsSuccess) { |
| | | if (null == rgvProtocol) { |
| | | rgvProtocol = new RgvProtocol(); |
| | | rgvProtocol.setRgvNo(slave.getId()); |
| | | } |
| | | rgvProtocol.setRgvNo(slave.getId()); |
| | | rgvProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 2)); |
| | | rgvProtocol.setStartSta(siemensNet.getByteTransform().TransInt16(result.Content, 4)); |
| | | rgvProtocol.setEndSta(siemensNet.getByteTransform().TransInt16(result.Content, 6)); |
| | |
| | | rgvProtocol.setStatus(siemensNet.getByteTransform().TransInt16(result.Content, 12)); |
| | | rgvProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 14)); |
| | | rgvProtocol.setRgvPos(siemensNet.getByteTransform().TransUInt32(result.Content, 16)); |
| | | rgvProtocol.setInstantaneousSpeed(Double.valueOf(siemensNet.getByteTransform().TransInt16(resultV.Content, 0))); |
| | | rgvProtocol.setEndStaM(siemensNet.getByteTransform().TransInt16(resultE.Content, 0)); |
| | | // rgvProtocol.setRgvPos((long)NumUtils.GetRandomIntInRange(1737000)); |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId())); |
| | | |
| | |
| | | array[3] = command.getTaskMode1();//任务模式 |
| | | array[4] = command.getTaskNo1(); |
| | | |
| | | |
| | | |
| | | OperateResult result = siemensNet.Write("DB24.0", array); |
| | | |
| | | if (command.getAckFinish1().equals((short)0)) { |
| | | Thread.sleep(100L); |
| | | // Thread.sleep(100L); |
| | | siemensNet.Write("DB24.10.7", command.getRgvSome() == 1); |
| | | Thread.sleep(20L); |
| | | result = siemensNet.Write("DB24.10.0", true); |
| | | } else { |
| | | siemensNet.Write("DB24.10.1", true); |
| | |
| | | } catch (Exception ignore) {} |
| | | |
| | | if (result != null && result.IsSuccess) { |
| | | Thread.sleep(200); |
| | | this.readStatus(); |
| | | // Thread.sleep(200); |
| | | // this.readStatus(); |
| | | log.info("RGV 工位1命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command)); |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 工位1命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(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; |
| | | } |
| | | } |
| | | private boolean write3(RgvCommand command) throws InterruptedException { |
| | | if (null == command) { |
| | | log.error("RGV写入命令为空"); |
| | | return false; |
| | | } |
| | | |
| | | siemensNet.Write("DB24.10.0", false); |
| | | siemensNet.Write("DB24.10.1", false); |
| | | |
| | | command.setRgvNo(slave.getId()); |
| | | short[] array = new short[5]; |
| | | array[0] = command.getRgvNo().shortValue(); |
| | | array[1] = command.getSourceStaNo1(); |
| | | array[2] = command.getDestinationStaNo1(); |
| | | array[3] = command.getTaskMode1();//任务模式 |
| | | array[4] = command.getTaskNo1(); |
| | | |
| | | // OperateResult result = siemensNet.Write("DB24.0", array); |
| | | OperateResult result = null; |
| | | if (command.getAckFinish1().equals((short)0)) { |
| | | Thread.sleep(20L); |
| | | result = siemensNet.Write("DB24.10.0", true); |
| | | } else { |
| | | siemensNet.Write("DB24.10.1", true); |
| | | } |
| | | |
| | | 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.toJSONString(command)); |
| | | OutputQueue.RGV.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 工位1命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command))); |
| | | return true; |