From 9fd40b787bdd4e0caaed213d1931d9c7fb8f846c Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期三, 30 十二月 2020 09:46:33 +0800 Subject: [PATCH] ## --- src/main/java/com/zy/core/thread/MelsecDevpThread.java | 50 +++++++++++++++++++++++++++++--------------------- 1 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/zy/core/thread/MelsecDevpThread.java b/src/main/java/com/zy/core/thread/MelsecDevpThread.java index 254a8b1..1cfa839 100644 --- a/src/main/java/com/zy/core/thread/MelsecDevpThread.java +++ b/src/main/java/com/zy/core/thread/MelsecDevpThread.java @@ -8,6 +8,7 @@ import com.core.common.SpringUtils; import com.zy.asrs.entity.BasDevp; import com.zy.asrs.service.BasDevpService; +import com.zy.asrs.utils.Utils; import com.zy.core.DevpThread; import com.zy.core.cache.MessageQueue; import com.zy.core.cache.OutputQueue; @@ -71,7 +72,7 @@ } // 蹇冭烦 - heartbeat(); +// heartbeat(); Thread.sleep(400); } catch (Exception e) { e.printStackTrace(); @@ -94,6 +95,9 @@ log.error("杈撻�佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); } melsecMcNet.ConnectClose(); + try { + Thread.sleep(1000); + } catch (InterruptedException ignore) {} return result; } @@ -103,27 +107,31 @@ private void read() throws InterruptedException { OperateResultExOne<byte[]> result = melsecMcNet.Read("D101", (short) 30); if (result.IsSuccess) { - for (int i = 1; i <= 7; i++) { - StaProtocol staProtocol = station.get(i); + // 绔欑偣鏁版嵁 + for (int i = 0; i < staNos.size(); i++) { + Integer siteId = staNos.get(i); // 绔欑偣缂栧彿 + StaProtocol staProtocol = station.get(siteId); if (null == staProtocol) { staProtocol = new StaProtocol(); - staProtocol.setSiteId(i); - station.put(i, staProtocol); + staProtocol.setSiteId(siteId); + station.put(siteId, staProtocol); } - staProtocol.setWorkNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2)); // 宸ヤ綔鍙� - staProtocol.setStaNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2+40)); // 鐩爣绔� + short workNo = melsecMcNet.getByteTransform().TransInt16(result.Content, (i) * 2); + staProtocol.setWorkNo(workNo==-1?0:workNo); // 宸ヤ綔鍙� + staProtocol.setStaNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i)*2+40)); // 鐩爣绔� } } - Thread.sleep(5000); + Thread.sleep(200); OperateResultExOne<boolean[]> result1 = melsecMcNet.ReadBool("M800", (short) 64); if (result1.IsSuccess) { - for (int i = 1; i <= 7; i++) { - StaProtocol staProtocol = station.get(i); - staProtocol.setAutoing(result1.Content[(i-1)*8]); // 鑷姩 - staProtocol.setLoading(result1.Content[(i-1)*8+1]); // 鏈夌墿 - staProtocol.setInEnable(result1.Content[(i-1)*8+2]); // 鍙叆 - staProtocol.setOutEnable(result1.Content[(i-1)*8+3]);// 鍙嚭 - staProtocol.setEmptyMk(result1.Content[(i-1)*8+4]); // 绌烘澘淇″彿 + for (int i = 0; i < staNos.size(); i++) { + Integer siteId = staNos.get(i); // 绔欑偣缂栧彿 + StaProtocol staProtocol = station.get(siteId); + staProtocol.setAutoing(result1.Content[(i)*8]); // 鑷姩 + staProtocol.setLoading(result1.Content[(i)*8+1]); // 鏈夌墿 + staProtocol.setInEnable(result1.Content[(i)*8+2]); // 鍙叆 + staProtocol.setOutEnable(result1.Content[(i)*8+3]);// 鍙嚭 + staProtocol.setEmptyMk(result1.Content[(i)*8+4]); // 绌烘澘淇″彿 if (!staProtocol.isPakMk() && !staProtocol.isLoading()) { staProtocol.setPakMk(true); @@ -146,8 +154,8 @@ // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴� try { List<BasDevp> basDevps = new ArrayList<>(); - for (int i = 1; i <= 7; i++) { - StaProtocol staProtocol = station.get(i); + for (Integer siteId : staNos) { + StaProtocol staProtocol = station.get(siteId); basDevps.add(staProtocol.toSqlModel()); } BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class); @@ -172,9 +180,10 @@ if (null == staProtocol) { return; } - OperateResult write = melsecMcNet.Write("D10" + staProtocol.getSiteId(), staProtocol.getWorkNo()); // 宸ヤ綔鍙� - Thread.sleep(400); - OperateResult write1 = melsecMcNet.Write("D12" + staProtocol.getSiteId(), staProtocol.getStaNo()); // 鐩爣绔� + System.out.println(); + OperateResult write1 = melsecMcNet.Write("D1" + Utils.zerofill(String.valueOf(staNos.indexOf(staProtocol.getSiteId())+21), 2), staProtocol.getStaNo()); // 鐩爣绔� + Thread.sleep(200); + OperateResult write = melsecMcNet.Write("D1" + Utils.zerofill(String.valueOf(staNos.indexOf(staProtocol.getSiteId())+1), 2), staProtocol.getWorkNo()); // 宸ヤ綔鍙� if (!write.IsSuccess || !write1.IsSuccess) { staProtocol = station.get(staProtocol.getSiteId()); if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) { @@ -183,7 +192,6 @@ OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol))); log.error("鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol)); } else { - log.error("-------------------------------------------绗笁姝ャ�乕绔欑偣鍙凤細{}][宸ヤ綔鍙凤細{}]==>> 涓嬪彂杈撻�佺嚎鍚姩淇″彿鎴愬姛锛侊紒",staProtocol.getSiteId(),staProtocol.getWorkNo()); OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆� 杈撻�佺嚎鍛戒护涓嬪彂 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); log.info("杈撻�佺嚎鍛戒护涓嬪彂 [id:{}] >>>>> 鍛戒护涓嬪彂锛� {}", slave.getId(), JSON.toJSON(staProtocol)); } -- Gitblit v1.9.1