| | |
| | | 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 = new JarProtocol(); |
| | | jarProtocol.setJarNo(slave.getId()); |
| | | } |
| | | jarProtocol.setJarNo(slave.getId()); |
| | | jarProtocol.setMode(2);//模式 |
| | | jarProtocol.setStatus((short)0);//状态 |
| | | jarProtocol.setAutoing(true);//自动 |
| | |
| | | jarProtocol.setRightDoorOpen(0);//open the right door //出料门 |
| | | jarProtocol.setRightDoorClose(0);//close the right door //出料门 |
| | | |
| | | jarProtocol.setJarTemperature(0); |
| | | jarProtocol.setJarTemperature(0.0F); |
| | | return; |
| | | } |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read("V300", (short) 28); |
| | | if (result.IsSuccess) { |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read("V300", (short) 30); |
| | | OperateResultExOne<byte[]> result338 = siemensS7Net.Read("V338", (short) 4); |
| | | if (result.IsSuccess && result338.IsSuccess) { |
| | | if (null == jarProtocol) { |
| | | jarProtocol = new JarProtocol(); |
| | | jarProtocol.setJarNo(slave.getId()); |
| | | } |
| | | jarProtocol.setJarNo(slave.getId()); |
| | | if (slave.getId() == 1 || slave.getId() == 3){ |
| | | jarProtocol.setRightDoor(siemensS7Net.getByteTransform().TransInt16(result.Content, 0) != 0);//左门状态 //出料门 |
| | | jarProtocol.setLeftDoor(siemensS7Net.getByteTransform().TransInt16(result.Content, 2) != 0);//右门状态 //进料门 |
| | |
| | | jarProtocol.setMode((int)siemensS7Net.getByteTransform().TransInt16(result.Content, 12));//模式 |
| | | jarProtocol.setJarErr((int)siemensS7Net.getByteTransform().TransInt16(result.Content, 14));//异常码 |
| | | jarProtocol.setStatus(siemensS7Net.getByteTransform().TransInt16(result.Content, 16));//状态 |
| | | jarProtocol.setJarTemperature((int)siemensS7Net.getByteTransform().TransInt16(result.Content, 26));//温度 |
| | | jarProtocol.setJarTemperature(siemensS7Net.getByteTransform().TransSingle(result.Content, 26));//温度 |
| | | |
| | | jarProtocol.setJarPressure(siemensS7Net.getByteTransform().TransSingle(result338.Content, 0));//压力 |
| | | |
| | | jarProtocol.setAutoing(siemensS7Net.getByteTransform().TransInt16(result.Content, 12) == 2);//自动 |
| | | |
| | | // 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()); |
| | | // |
| | | // jarProtocol.setMode(2);//模式 |
| | | // jarProtocol.setStatus((short)6);//状态 |
| | |
| | | } |
| | | } 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(); |
| | |
| | | String resultV2 = null; |
| | | short resultS2 = 0; |
| | | |
| | | switch (command.getTaskMode()){ |
| | | switch (command.getTaskModeType().id){ |
| | | case 1: |
| | | case 2: |
| | | case 3: |
| | |
| | | OperateResultExOne<byte[]> resultRead2 = siemensS7Net.Read(resultV2, (short) 2); |
| | | if (resultRead2.IsSuccess) { |
| | | short transInt16 = siemensS7Net.getByteTransform().TransInt16(resultRead2.Content, 0); |
| | | if (transInt16 == resultS1 || transInt16 == (short) 3){ |
| | | if (transInt16 == resultS2 || transInt16 == (short) 3){ |
| | | break; |
| | | } else { |
| | | log.error("写入硫化罐plc数据失败,重新下发任务 写入直接失败 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSONString(command),writeCount); |