| | |
| | | private JarSlave slave; |
| | | private JarProtocol jarProtocol; |
| | | private short heartBeatVal = 1; |
| | | private boolean connectJar = false; |
| | | |
| | | |
| | | public JarThread(JarSlave slave) { |
| | | this.slave = slave; |
| | |
| | | @Override |
| | | @SuppressWarnings("InfiniteLoopStatement") |
| | | public void run() { |
| | | this.connect(); |
| | | |
| | | connectJar = this.connect(); |
| | | while(!connectJar){ |
| | | try { |
| | | connectJar = this.connect(); |
| | | Thread.sleep(100); |
| | | } catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | |
| | | // 启动线程自动重连 |
| | | new Thread(this::jarConnect).start(); |
| | | |
| | | // 启动读数据线程 |
| | | new Thread(this::readStatusJar).start(); |
| | | |
| | | // 启动任务下发线程 |
| | | new Thread(this::taskIssued).start(); |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 任务下发 |
| | | */ |
| | | private void taskIssued() { |
| | | while (true) { |
| | | try { |
| | | int step = 1; |
| | |
| | | switch (step) { |
| | | // 读数据 |
| | | case 1: |
| | | readStatus(); |
| | | // readStatus(); |
| | | break; |
| | | // 写数据 ID+目标站 |
| | | case 2: |
| | |
| | | } |
| | | // 心跳 |
| | | // heartbeat(); |
| | | Thread.sleep(500); |
| | | Thread.sleep(200); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("JAR写线程异常"+e.getMessage()); |
| | | |
| | | // e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | private void jarConnect() { |
| | | while (true) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | if(!connectJar){ |
| | | try { |
| | | connectJar = this.connect(); |
| | | Thread.sleep(100); |
| | | } catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("jar连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | initSte(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void readStatusJar() { |
| | | while (true) { |
| | | try { |
| | | Thread.sleep(50); |
| | | // System.out.println("读线程"+ slave.getId()); |
| | | |
| | | readStatus(); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("JAR数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | |
| | | initSte(); |
| | | // e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 初始化堆垛机状态 |
| | | */ |
| | | private void initSte() { |
| | | if (null == jarProtocol) { |
| | | jarProtocol = new JarProtocol(); |
| | | try{ |
| | | if (null == jarProtocol) { |
| | | jarProtocol = new JarProtocol(); |
| | | } |
| | | jarProtocol.setJarNo(slave.getId()); |
| | | jarProtocol.setMode( 0); |
| | | jarProtocol.setStatus(JarStatusType.OFF_LINE); |
| | | jarProtocol.setJarErr(0); |
| | | } catch (Exception e){ |
| | | log.error("JAR初始化异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } |
| | | jarProtocol.setJarNo(slave.getId()); |
| | | jarProtocol.setMode( 0); |
| | | jarProtocol.setStatus(JarStatusType.OFF_LINE); |
| | | jarProtocol.setJarErr(0); |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | boolean result = false; |
| | | // 不能有 槽号 和 机架号 |
| | | siemensS7Net = new SiemensS7Net(SiemensPLCS.S200Smart, slave.getIp()); |
| | | |
| | | try{ |
| | | // 不能有 槽号 和 机架号 |
| | | siemensS7Net = new SiemensS7Net(SiemensPLCS.S200Smart, slave.getIp()); |
| | | // siemensS7Net.setRack(slave.getRack().byteValue()); |
| | | // siemensS7Net.setSlot(slave.getSlot().byteValue()); |
| | | OperateResult connect = siemensS7Net.ConnectServer(); |
| | | if(connect.IsSuccess){ |
| | | result = true; |
| | | OutputQueue.STE.offer(MessageFormat.format( "【{0}】硫化罐plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | News.info("硫化罐plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } else { |
| | | OutputQueue.STE.offer(MessageFormat.format("【{0}】硫化罐plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] ", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | News.error("硫化罐plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | initSte(); |
| | | OperateResult connect = siemensS7Net.ConnectServer(); |
| | | if(connect.IsSuccess){ |
| | | result = true; |
| | | OutputQueue.STE.offer(MessageFormat.format( "【{0}】硫化罐plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | News.info("硫化罐plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } else { |
| | | OutputQueue.STE.offer(MessageFormat.format("【{0}】硫化罐plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] ", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | News.error("硫化罐plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | initSte(); |
| | | } |
| | | } catch (Exception e){ |
| | | log.error("硫化罐plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } |
| | | // melsecMcNet.ConnectClose(); |
| | | return result; |
| | | // melsecMcNet.ConnectClose(); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | jarProtocol.setAutoing(siemensS7Net.getByteTransform().TransInt16(result.Content, 12) == 2);//自动 |
| | | |
| | | if (jarProtocol.statusType .equals(JarStatusType.SOS) || jarProtocol.statusType .equals(JarStatusType.SOS2)){ |
| | | // if (jarProtocol.statusType .equals(JarStatusType.SOS)){ |
| | | // jarProtocol.setHoldingSign(!jarProtocol.getStatus().equals(jarProtocol.getUpStatus())); |
| | | // } else |
| | | if (jarProtocol.statusType .equals(JarStatusType.SOS2)){ |
| | | jarProtocol.setHoldingSign(!jarProtocol.getStatus().equals(jarProtocol.getUpStatus())); |
| | | if (!jarProtocol.rightDoorClose.equals(jarProtocol.leftDoorClose)) { |
| | | if (jarProtocol.rightDoorClose == 0 || jarProtocol.leftDoorClose == 0){ |
| | | if (jarProtocol.rightDoorClose == 2 || jarProtocol.leftDoorClose == 2){ |
| | | jarProtocol.setLeftDoorClose(0);//close the left door //进料门 |
| | | jarProtocol.setRightDoorClose(0);//close the right door //出料门 |
| | | } |
| | | } |
| | | if (jarProtocol.rightDoorOpen == 0 || jarProtocol.leftDoorOpen == 0){ |
| | | if (jarProtocol.rightDoorOpen == 2 || jarProtocol.leftDoorOpen == 2){ |
| | | jarProtocol.setLeftDoorOpen(0);//open the left door //进料门 |
| | | jarProtocol.setRightDoorOpen(0);//open the right door //出料门 |
| | | } |
| | | } |
| | | } |
| | | } else if (jarProtocol.statusType .equals(JarStatusType.WAITING3) || jarProtocol.statusType .equals(JarStatusType.WAITING4)){ |
| | | jarProtocol.setOpenDoorSign(!jarProtocol.getStatus().equals(jarProtocol.getUpStatus())); |
| | | } else if (jarProtocol.statusType .equals(JarStatusType.WAITING5) || jarProtocol.statusType .equals(JarStatusType.OFF_LINE)){ |
| | | jarProtocol.setCloseDoorSign(!jarProtocol.getStatus().equals(jarProtocol.getUpStatus())); |
| | | } else if (jarProtocol.leftDoorOpen==1 || jarProtocol.leftDoorClose==1 || |
| | | jarProtocol.rightDoorOpen==1 || jarProtocol.rightDoorClose==1){ |
| | | |
| | | } else { |
| | | if (jarProtocol.statusType .equals(JarStatusType.WAITING2)){ |
| | | |
| | | if (jarProtocol.rightDoorOpen == 2 || jarProtocol.leftDoorOpen == 2){ |
| | | jarProtocol.setLeftDoorOpen(0);//open the left door //进料门 |
| | | jarProtocol.setRightDoorOpen(0);//open the right door //出料门 |
| | | } |
| | | if (jarProtocol.rightDoorClose == 2 || jarProtocol.leftDoorClose == 2){ |
| | | jarProtocol.setLeftDoorClose(0);//close the left door //进料门 |
| | | jarProtocol.setRightDoorClose(0);//close the right door //出料门 |
| | | } |
| | | } else if (jarProtocol.statusType .equals(JarStatusType.WAITING1) || jarProtocol.statusType .equals(JarStatusType.MOVING)){ |
| | | if (!jarProtocol.rightDoorOpen.equals(jarProtocol.leftDoorOpen)) { |
| | | if (jarProtocol.rightDoorOpen == 0 || jarProtocol.leftDoorOpen == 0){ |
| | | if (jarProtocol.rightDoorOpen == 2 || jarProtocol.leftDoorOpen == 2){ |
| | | jarProtocol.setLeftDoorOpen(0);//open the left door //进料门 |
| | | jarProtocol.setRightDoorOpen(0);//open the right door //出料门 |
| | | } |
| | | } |
| | | } |
| | | if (jarProtocol.rightDoorClose == 0 || jarProtocol.leftDoorClose == 0){ |
| | | if (jarProtocol.rightDoorClose == 2 || jarProtocol.leftDoorClose == 2){ |
| | | jarProtocol.setLeftDoorClose(0);//close the left door //进料门 |
| | | jarProtocol.setRightDoorClose(0);//close the right door //出料门 |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | jarProtocol.setUpStatus(jarProtocol.getStatus()); |
| | |
| | | } |
| | | } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | log.error(e.getMessage()); |
| | | log.error("硫化罐plc状态信息失败"+e.getMessage()); |
| | | OutputQueue.STE.offer(MessageFormat.format("【{0}】读取硫化罐plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | // News.error("读取穿梭车plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | initSte(); |