From 04855df566ae02edda46ccb49b3dceabc39cf0d5 Mon Sep 17 00:00:00 2001 From: zhangchao <zc857179121@qq.com> Date: 星期五, 30 八月 2024 00:03:40 +0800 Subject: [PATCH] 1 --- src/main/java/com/zy/core/thread/SiemensDevpThread.java | 261 ++++++++++++++++++++++++++-------------------------- 1 files changed, 131 insertions(+), 130 deletions(-) diff --git a/src/main/java/com/zy/core/thread/SiemensDevpThread.java b/src/main/java/com/zy/core/thread/SiemensDevpThread.java index 96b194f..95b8243 100644 --- a/src/main/java/com/zy/core/thread/SiemensDevpThread.java +++ b/src/main/java/com/zy/core/thread/SiemensDevpThread.java @@ -43,26 +43,29 @@ private SiemensS7Net siemensS7Net; private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>(); private short heartBeatVal = 1; -// public static final ArrayList<Integer> staNos = new ArrayList<Integer>() {{ + // public static final ArrayList<Integer> staNos = new ArrayList<Integer>() {{ // // }}; public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{ - add(100);add(101);add(102);add(103); - add(104);add(105);add(106);add(107); + add(101); + add(102); + add(103); + add(104); }}; public static final ArrayList<Integer> staNos2 = new ArrayList<Integer>() {{ - add(200);add(201);add(202);add(203); - add(204);add(205);add(206);add(207); + add(200); + add(201); + add(202); }}; public static final ArrayList<Integer> staNos3 = new ArrayList<Integer>() {{ - add(300);add(301);add(302);add(303); - add(304);add(305);add(306);add(307); + add(210); + add(211); + add(212); }}; - - private Integer count=0; + private Integer count = 0; /** * 鏉$爜鏁伴噺 @@ -78,7 +81,7 @@ * 4.鍑哄簱妯″紡 */ public IoModeType ioModeOf2F = IoModeType.NONE; -// public IoModeType ioMode = IoModeType.NONE; + // public IoModeType ioMode = IoModeType.NONE; public IoModeType ioModeOf4F = IoModeType.NONE; // public IoModeType ioMode = IoModeType.NONE; @@ -117,7 +120,7 @@ break; // 鍐欐暟鎹� ID+鐩爣绔� case 2: - write((StaProtocol)task.getData()); + write((StaProtocol) task.getData()); break; default: break; @@ -131,13 +134,14 @@ } } + /** * 鍒濆鍖栫珯鐐圭姸鎬� */ private void initSite() { - count ++; + count++; ArrayList<Integer> staNos = getStaNo(); - if(count > 77) { + if (count > 77) { // 绔欑偣缂栧彿 for (Integer siteId : staNos) { StaProtocol staProtocol = station.get(siteId); @@ -169,13 +173,13 @@ siemensS7Net.setRack(slave.getRack().byteValue()); siemensS7Net.setSlot(slave.getSlot().byteValue()); OperateResult connect = siemensS7Net.ConnectServer(); - if(connect.IsSuccess){ + if (connect.IsSuccess) { result = true; - 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())); - News.info("SiemensDevp"+" - 1"+" - 杈撻�佺嚎plc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); + 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())); + News.info("SiemensDevp" + " - 1" + " - 杈撻�佺嚎plc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); } 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())); - News.error("SiemensDevp"+" - 2"+" - 杈撻�佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); + 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())); + News.error("SiemensDevp" + " - 2" + " - 杈撻�佺嚎plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); } siemensS7Net.ConnectClose(); initSite(); @@ -190,9 +194,9 @@ // updateIoMode(); ArrayList<Integer> staNos = getStaNo(); int staNoSize = staNos.size(); - OperateResultExOne<byte[]> result = siemensS7Net.Read("DB100.0", (short) (staNoSize*2)); - OperateResultExOne<byte[]> result4 = siemensS7Net.Read("DB101.0", (short) (staNoSize*2)); - if (result.IsSuccess && result4.IsSuccess) { + OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (staNoSize*4)); +// OperateResultExOne<byte[]> result4 = siemensS7Net.Read("DB100.2", (short) (staNoSize*2)); + if (result.IsSuccess) { for (int i = 0; i < staNoSize; i++) { Integer siteId = staNos.get(i); // 绔欑偣缂栧彿 StaProtocol staProtocol = station.get(siteId); @@ -201,56 +205,18 @@ staProtocol.setSiteId(siteId); station.put(siteId, staProtocol); } - staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt16(result.Content, i*2)); // 宸ヤ綔鍙� + staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt16(result.Content, i*4)); // 宸ヤ綔鍙� - staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result4.Content, i*2)); // 鐩爣绔� - } - } -// Thread.sleep(200); -// OperateResultExOne<byte[]> result0 = siemensS7Net.Read("DB101.0", (short) 186); -// if (result0.IsSuccess) { -// for (int i = 0; i < 93; i++) { -// Integer siteId = staNos.get(i); // 绔欑偣缂栧彿 -// StaProtocol staProtocol = station.get(siteId); -// if (null == staProtocol) { -// staProtocol = new StaProtocol(); -// staProtocol.setSiteId(siteId); -// station.put(siteId, staProtocol); -// } -// staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result0.Content, i*2)); // 鐩爣绔� -// } -// } - //鏉$爜鎵弿鍣� - Thread.sleep(200); - OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB100.200",(short)(barcodeSize/2*8)); - if (result2.IsSuccess) { - if (slave.getId()==1){ - for (int i = 0; i < barcodeSize/2; i++) { - String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,8, "UTF-8"); - BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1); - if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)){ -// && !Cools.isEmpty(barcode) && Cools.isEmpty(barcodeThread.getBarcode())) { - barcodeThread.setBarcode(barcode); - } - } - }else if (slave.getId()==2){ - for (int i = barcodeSize/2; i < barcodeSize; i++) { - String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,(i-barcodeSize/2)*8,8, "UTF-8"); - BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1); - if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)){ -// && !Cools.isEmpty(barcode) && Cools.isEmpty(barcodeThread.getBarcode())) { - barcodeThread.setBarcode(barcode); - } - } + staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, i*4 + 2)); // 鐩爣绔� } } Thread.sleep(200); - OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB102.100", (short) (staNoSize * 2)); + OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB101.6", (short) (staNoSize * 4)); if (result1.IsSuccess) { for (int i = 0; i < staNoSize; i++) { Integer siteId = staNos.get(i); // 绔欑偣缂栧彿 - boolean[] status = siemensS7Net.getByteTransform().TransBool(result1.Content, i*2, 1); + boolean[] status = siemensS7Net.getByteTransform().TransBool(result1.Content, i*4, 1); StaProtocol staProtocol = station.get(siteId); staProtocol.setAutoing(status[0]); // 鑷姩 staProtocol.setLoading(status[1]); // 鏈夌墿 @@ -271,58 +237,94 @@ //lfd鍏ュ簱鍗拌 褰搒tamp>=2鏃舵墠鍏ュ簱 staProtocol.setStamp(staProtocol.getStamp()+1); } - -// //20230110锛屾棩蹇楁墦鍗帮紝纭鏀跺埌鍏ュ簱绔欏彲鍏ヤ俊鍙锋椂锛屽搴旀潯鐮佹暟鎹槸鍚﹀凡鏈� -// if ((siteId==101 || siteId==109 || siteId==117 || siteId==201 || siteId==207 || siteId==213) -// && staProtocol.isInEnable()){ -// Integer barcodeId = 0; -// switch (siteId){ -// case 101: -// barcodeId=1; -// break; -// case 109: -// barcodeId=2; -// break; -// case 117: -// barcodeId=3; -// break; -// case 201: -// barcodeId=4; -// break; -// case 207: -// barcodeId=5; -// break; -// case 213: -// barcodeId=6; -// break; -// } -// BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId); -// if(barcodeThread!=null){ -// //lfd鍏ュ簱鍗拌 褰搒tamp>=2鏃舵墠鍏ュ簱 -// staProtocol.setStamp(staProtocol.getStamp()+1); -// News.info("{}绔欐湁鍙叆淇″彿鏃讹紝鏉$爜鍊间负{}", siteId, barcodeThread.getBarcode()); -// if(Cools.isEmpty(barcodeThread.getBarcode())){ -// staProtocol.setInEnable(false);//鏉$爜鏁版嵁涓虹┖鏃讹紝鍏堟竻涓�娆″唴瀛樼殑绔欑偣鍙叆淇″彿锛岀瓑寰呬笅涓�娆¤幏鍙栧彲鍏ヤ俊鍙� -// } -// } -// } } } -// //RGV灏忚溅 // Thread.sleep(200); -// OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB100.160",(short)2); -// if (result3.IsSuccess) { -// Integer siteId = 16; -// StaProtocol staProtocol = station.get(siteId); -// if (null == staProtocol) { -// staProtocol = new StaProtocol(); -// staProtocol.setSiteId(siteId); -// station.put(siteId, staProtocol); +// OperateResultExOne<byte[]> result0 = siemensS7Net.Read("DB101.0", (short) 186); +// if (result0.IsSuccess) { +// for (int i = 0; i < 93; i++) { +// Integer siteId = staNos.get(i); // 绔欑偣缂栧彿 +// StaProtocol staProtocol = station.get(siteId); +// if (null == staProtocol) { +// staProtocol = new StaProtocol(); +// staProtocol.setSiteId(siteId); +// station.put(siteId, staProtocol); +// } +// staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result0.Content, i*2)); // 鐩爣绔� // } -// staProtocol.setNearbySta(String.valueOf(siemensS7Net.getByteTransform().TransInt16(result3.Content, 0))); // } + //鏉$爜鎵弿鍣� + Thread.sleep(200); + OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB101.2",(short)(barcodeSize*8)); + if (result2.IsSuccess && 1!=1) { + if (slave.getId()==1){ + for (int i = 0; i < barcodeSize/3; i++) { + String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,i*8,8, "UTF-8"); + BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1); + if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)){ +// && !Cools.isEmpty(barcode) && Cools.isEmpty(barcodeThread.getBarcode())) { + barcodeThread.setBarcode(barcode); + } + } + }else if (slave.getId()==2){ + for (int i = barcodeSize/3; i < barcodeSize; i++) { + String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,(i-barcodeSize/3)*8,8, "UTF-8"); + BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1); + if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)){ +// && !Cools.isEmpty(barcode) && Cools.isEmpty(barcodeThread.getBarcode())) { + barcodeThread.setBarcode(barcode); + } + } + }else if (slave.getId()==3){ + for (int i = barcodeSize/3+1; i < barcodeSize; i++) { + String barcode = siemensS7Net.getByteTransform().TransString(result2.Content,(i-(barcodeSize/3)*2)*8,8, "UTF-8"); + BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1); + if(!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)){ +// && !Cools.isEmpty(barcode) && Cools.isEmpty(barcodeThread.getBarcode())) { + barcodeThread.setBarcode(barcode); + } + } + } + } + + // 澶栧舰妫�娴� - 1妤� + Thread.sleep(50); + OperateResultExOne<byte[]> result4 = siemensS7Net.Read("DB101.1", (short)48); + if (result4.IsSuccess) { + StaProtocol staProtocol100 = station.get(100); + StaProtocol staProtocol110 = station.get(110); + for (int i=0 ;i<6;i++){ + boolean[] status = siemensS7Net.getByteTransform().TransBool(result4.Content, i, 1); + if (i==0){ + }else if (i==1){ + staProtocol100.setFrontErr(status[3]);// 鍓嶈秴闄� + staProtocol100.setBackErr(status[4]);// 鍚庤秴闄� + staProtocol100.setHighErr(status[5]); // 楂樿秴闄� + staProtocol100.setLeftErr(status[6]);// 宸﹁秴闄� + }else if (i==2){ + staProtocol100.setRightErr(status[0]);// 鍙宠秴闄� + + staProtocol110.setFrontErr(status[1]); + staProtocol110.setBackErr(status[2]); + staProtocol110.setHighErr(status[3]); + staProtocol110.setLeftErr(status[4]); + staProtocol110.setRightErr(status[5]); + }else if (i==3){ + }else if (i==4){ + staProtocol100.setBarcodeErr(status[2]); + staProtocol110.setBarcodeErr(status[3]); + }else if (i==5){ + staProtocol100.setWeightErr(status[0]); + staProtocol110.setWeightErr(status[1]); + staProtocol100.setBarcodeErr(status[2]); + staProtocol110.setBarcodeErr(status[3]); + } + + } + + } if (result.IsSuccess && result1.IsSuccess) { @@ -369,21 +371,21 @@ // OperateResult write = siemensS7Net.Write("DB100." + index*4, array); OperateResult write = null; - OperateResult write1 = null; //浠诲姟涓嬪彂娆℃暟 int writeCount = 0; do { - write = siemensS7Net.Write("DB100." + index*2, staProtocol.getWorkNo()); // 宸ヤ綔鍙� - Thread.sleep(500); - write1 = siemensS7Net.Write("DB101." + index*2, staProtocol.getStaNo()); // 鐩爣绔� - if(write.IsSuccess || write1.IsSuccess){ +// write = siemensS7Net.Write("DB100.0" + index*2, staProtocol.getWorkNo()); // 宸ヤ綔鍙� +// Thread.sleep(500); +// write1 = siemensS7Net.Write("DB100.0" + index*2+2, staProtocol.getStaNo()); // 鐩爣绔� + write = siemensS7Net.Write("DB100." + index * 4, array); + if (write.IsSuccess) { Thread.sleep(200); - OperateResultExOne<byte[]> readResult = siemensS7Net.Read("DB100." + index*2, (short) 2); - OperateResultExOne<byte[]> readResult1 = siemensS7Net.Read("DB101." + index*2, (short) 2); - if(readResult.IsSuccess && readResult1.IsSuccess){ + OperateResultExOne<byte[]> readResult = siemensS7Net.Read("DB100.0" + index * 2, (short) 2); + OperateResultExOne<byte[]> readResult1 = siemensS7Net.Read("DB100.0" + index * 2 + 2, (short) 2); + if (readResult.IsSuccess && readResult1.IsSuccess) { short workNo = siemensS7Net.getByteTransform().TransInt16(readResult.Content, 0); short staNo = siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 0); - if(staProtocol.getWorkNo().equals(workNo) && staProtocol.getStaNo().equals(staNo)){ + if (staProtocol.getWorkNo().equals(workNo) && staProtocol.getStaNo().equals(staNo)) { //浠诲姟鍛戒护鍐欏叆鎴愬姛 log.info("鍐欏叆鍫嗗灈鏈哄懡浠ゅ悗杩斿洖鎴愬姛锛屽苟涓斿洖璇绘垚鍔熴�傚爢鍨涙満plc缂栧彿={}锛寋}锛屽啓鍏ユ鏁�={}", slave.getId(), JSON.toJSON(staProtocol), writeCount); break; @@ -395,23 +397,22 @@ writeCount++; log.error("鍐欏叆鍫嗗灈鏈哄懡浠ゅ悗璇诲彇澶辫触銆傚爢鍨涙満plc缂栧彿={}锛岀珯鐐规暟鎹�={},鍐欏叆娆℃暟={}", slave.getId(), JSON.toJSON(staProtocol), writeCount); } - } - else { + } else { writeCount++; log.error("鍐欏叆鍫嗗灈鏈哄懡浠ゅ悗璇诲彇澶辫触銆傚爢鍨涙満plc缂栧彿={}锛岀珯鐐规暟鎹�={},鍐欏叆娆℃暟={}", slave.getId(), JSON.toJSON(staProtocol), writeCount); } - }while (writeCount<5); + } while (writeCount < 5); - if (!write.IsSuccess || !write1.IsSuccess) { + if (!write.IsSuccess) { staProtocol = station.get(staProtocol.getSiteId()); - if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) { + if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() == 0) { staProtocol.setPakMk(true); } OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={1}锛岀珯鐐规暟鎹�={2}", slave.getId(), JSON.toJSON(staProtocol))); - News.error("SiemensDevp"+" - 4"+" - 鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol)); + News.error("SiemensDevp" + " - 4" + " - 鍐欏叆杈撻�佺嚎绔欑偣鏁版嵁澶辫触銆傝緭閫佺嚎plc缂栧彿={}锛岀珯鐐规暟鎹�={}", slave.getId(), JSON.toJSON(staProtocol)); } else { OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆� 杈撻�佺嚎鍛戒护涓嬪彂 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(staProtocol))); - News.info("SiemensDevp"+" - 5"+" - 杈撻�佺嚎鍛戒护涓嬪彂 [id:{}] >>>>> 鍛戒护涓嬪彂锛� {}", slave.getId(), JSON.toJSON(staProtocol)); + News.info("SiemensDevp" + " - 5" + " - 杈撻�佺嚎鍛戒护涓嬪彂 [id:{}] >>>>> 鍛戒护涓嬪彂锛� {}", slave.getId(), JSON.toJSON(staProtocol)); } } @@ -420,7 +421,7 @@ if (this.ioModeOf2F != IoModeType.NONE) { if (!siemensS7Net.Write("DB100.180", this.ioModeOf2F.id).IsSuccess) { OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆戝啓鍏ヨ緭閫佺嚎2F鍏ュ嚭搴撴ā寮忓け璐ャ�傝緭閫佺嚎plc缂栧彿={1}", slave.getId())); - News.error("SiemensDevp"+" - 6"+" - 鍐欏叆杈撻�佺嚎2F鍏ュ嚭搴撴ā寮忓け璐ャ�傝緭閫佺嚎plc缂栧彿={}", slave.getId()); + News.error("SiemensDevp" + " - 6" + " - 鍐欏叆杈撻�佺嚎2F鍏ュ嚭搴撴ā寮忓け璐ャ�傝緭閫佺嚎plc缂栧彿={}", slave.getId()); } } } @@ -428,7 +429,7 @@ /** * 蹇冭烦 */ - private void heartbeat(){ + private void heartbeat() { if (heartBeatVal == 1) { heartBeatVal = 2; } else { @@ -436,7 +437,7 @@ } OperateResult write = siemensS7Net.Write("DB100.50", heartBeatVal); if (!write.IsSuccess) { - News.error("SiemensDevp"+" - 7"+" - 杈撻�佺嚎plc缂栧彿={} 蹇冭烦澶辫触", slave.getId()); + News.error("SiemensDevp" + " - 7" + " - 杈撻�佺嚎plc缂栧彿={} 蹇冭烦澶辫触", slave.getId()); } } @@ -459,14 +460,14 @@ public static void main(String[] args) { System.out.println(staNos1.indexOf(129)); System.out.println(staNos1.size()); - for (int i = 0; i<staNos1.size(); i++) { + for (int i = 0; i < staNos1.size(); i++) { // System.out.println(i*2); // System.out.println(i*2 + 200); // System.out.println(i); } int index = staNos1.indexOf(128); - System.out.println(index*2); - System.out.println(index*2 + 200); + System.out.println(index * 2); + System.out.println(index * 2 + 200); } // public static void main(String[] args) throws Exception { -- Gitblit v1.9.1