| | |
| | | add(1008);add(1009);add(1010);add(1011);add(1012);add(1013);add(1014);add(1015); |
| | | add(1016);add(1017);add(1018);add(1019);add(1020);add(1021);add(1022);add(1023); |
| | | }}; |
| | | private boolean connectDev = false; |
| | | |
| | | |
| | | /** |
| | | * 条码数量 |
| | |
| | | @Override |
| | | @SuppressWarnings("InfiniteLoopStatement") |
| | | public void run() { |
| | | connect(); |
| | | while (true) { |
| | | connectDev = connect(); |
| | | while(!connectDev){ |
| | | try { |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Devp, slave.getId()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | } |
| | | switch (step) { |
| | | // 读数据 |
| | | case 1: |
| | | read(); |
| | | break; |
| | | // 写数据 ID+目标站 |
| | | case 2: |
| | | write((StaProtocol)task.getData()); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | // 心跳 |
| | | // heartbeat(); |
| | | Thread.sleep(400); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | connectDev = this.connect(); |
| | | Thread.sleep(100); |
| | | } catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | |
| | | // 启动线程自动重连 |
| | | new Thread(this::devConnect).start(); |
| | | new Thread(this::readStatusDev).start(); |
| | | new Thread(this::writeStatusDev).start(); |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | private void devConnect() { |
| | | while (true) { |
| | | try { |
| | | Thread.sleep(1000); |
| | | |
| | | if(!connectDev){ |
| | | try { |
| | | connectDev = this.connect(); |
| | | Thread.sleep(100); |
| | | } catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.info("dev连接失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | |
| | | // log.error("rgv连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | try{ |
| | | DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class); |
| | | deviceErrorService.addDeviceError("dev", slave.getId(), "rgv连接失败"+e.getMessage()); |
| | | } catch (Exception e2){ |
| | | // log.error("e2:"+e2.getMessage()); |
| | | } |
| | | initSite(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | /** |
| | | * 初始化站点状态 |
| | | */ |
| | |
| | | return result; |
| | | } |
| | | |
| | | |
| | | |
| | | private void writeStatusDev() { |
| | | while (true) { |
| | | try { |
| | | if(!connectDev){ |
| | | try { |
| | | Thread.sleep(1000L); |
| | | } catch (Exception e){ |
| | | |
| | | } |
| | | continue; |
| | | } |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Devp, slave.getId()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | } |
| | | switch (step) { |
| | | // 读数据 |
| | | case 1: |
| | | // read(); |
| | | break; |
| | | // 写数据 ID+目标站 |
| | | case 2: |
| | | write((StaProtocol)task.getData()); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | // 心跳 |
| | | // heartbeat(); |
| | | Thread.sleep(200); |
| | | } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | private void readStatusDev() { |
| | | while (true) { |
| | | try { |
| | | if(!connectDev){ |
| | | try { |
| | | Thread.sleep(1000L); |
| | | } catch (Exception e){ |
| | | |
| | | } |
| | | initSite(); |
| | | continue; |
| | | } |
| | | Thread.sleep(50); |
| | | // System.out.println("读线程"+ slave.getId()); |
| | | |
| | | read(); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("RGV数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | try{ |
| | | DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class); |
| | | deviceErrorService.addDeviceError("rgv", slave.getId(), "RGV数据读取线程异常"+e.getMessage()); |
| | | } catch (Exception e2){ |
| | | // log.error("e2:"+e2.getMessage()); |
| | | } |
| | | initSite(); |
| | | // e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 读取状态 ====> 整块plc |
| | | */ |
| | |
| | | staProtocol.setInEnable(status[2]); // 可入 |
| | | // staProtocol.setInEnable(true); // 可入 |
| | | staProtocol.setOutEnable(status[3]);// 可出 |
| | | staProtocol.setOutEnable(true);// 可出 |
| | | // staProtocol.setOutEnable(true);// 可出 |
| | | staProtocol.setEmptyMk(status[4]); // 空板信号 |
| | | staProtocol.setFullPlt(status[5]); // 满托盘 |
| | | staProtocol.setHigh(status[6]); // 高库位 |
| | |
| | | } |
| | | |
| | | //条码 |
| | | Thread.sleep(200); |
| | | OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.840.0",(short) (barcodeSize*8)); |
| | | // Thread.sleep(50); |
| | | OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB400.100.0",(short) (barcodeSize*8)); |
| | | if (result2.IsSuccess) { |
| | | for (int i = 0; i < barcodeSize; i++) { |
| | | String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,8, "UTF-8"); |
| | | String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,6, "UTF-8"); |
| | | BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1); |
| | | if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) { |
| | | barcodeThread.setBarcode(barcode); |
| | | } |
| | | } |
| | | } |
| | | |
| | | //条码 |
| | | // Thread.sleep(50); |
| | | int[] staW = new int[]{1004,1014,1020}; |
| | | |
| | | OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB400.148.0",(short) (staW.length*4)); |
| | | if (result3.IsSuccess) { |
| | | for (int i = 0; i < staW.length; i++) { |
| | | Integer siteId = staW[i]; // 站点编号 |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (null == staProtocol) { |
| | | staProtocol = new StaProtocol(); |
| | | station.put(siteId, staProtocol); |
| | | } |
| | | staProtocol.setSiteId(siteId); |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool(result3.Content, i*4 + 2, 2); |
| | | staProtocol.setFrontErr(status[0]); // 前超限 |
| | | staProtocol.setBackErr(status[1]); // 后超限 |
| | | staProtocol.setHighErr(status[2]); // 高超限 |
| | | staProtocol.setLeftErr(status[3]); // 左超限 |
| | | staProtocol.setRightErr(status[4]); // 右超限 |
| | | } |
| | | } |
| | | |
| | |
| | | throw new Exception("更新数据库数据失败"); |
| | | } |
| | | } catch (Exception e){ |
| | | log.error("e:"+e.getMessage()); |
| | | // log.error("e:"+e.getMessage()); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | |
| | | DeviceErrorService deviceErrorService = SpringUtils.getBean(DeviceErrorService.class); |
| | | deviceErrorService.deleteDeviceError("devp", slave.getId()); |
| | | } catch (Exception e){ |
| | | log.error("e:"+e.getMessage()); |
| | | // log.error("e:"+e.getMessage()); |
| | | } |
| | | } else { |
| | | OutputQueue.DEVP.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())); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置入库标记 |
| | | */ |
| | | @Override |
| | | public void setErrorDev(Integer siteId, String msgErr) { |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (null != staProtocol) { |
| | | staProtocol.setErrorDev(msgErr); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | siemensS7Net.ConnectClose(); |