From b40eab1e582d72ca2e71afb2f5604671f5193f22 Mon Sep 17 00:00:00 2001 From: Junjie <540245094@qq.com> Date: 星期三, 13 八月 2025 20:09:00 +0800 Subject: [PATCH] # --- src/main/java/com/zy/core/thread/SiemensCrnThread.java | 140 ++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 120 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/zy/core/thread/SiemensCrnThread.java b/src/main/java/com/zy/core/thread/SiemensCrnThread.java index 31b9ff5..f9ecce5 100644 --- a/src/main/java/com/zy/core/thread/SiemensCrnThread.java +++ b/src/main/java/com/zy/core/thread/SiemensCrnThread.java @@ -5,12 +5,16 @@ import HslCommunication.Profinet.Siemens.SiemensPLCS; import HslCommunication.Profinet.Siemens.SiemensS7Net; import com.alibaba.fastjson.JSON; +import com.core.common.Cools; import com.core.common.DateUtils; import com.core.common.SpringUtils; import com.zy.asrs.entity.BasCrnOpt; import com.zy.asrs.entity.BasCrnp; +import com.zy.asrs.entity.DeviceDataLog; import com.zy.asrs.service.BasCrnOptService; import com.zy.asrs.service.BasCrnpService; +import com.zy.asrs.service.DeviceDataLogService; +import com.zy.common.utils.News; import com.zy.core.CrnThread; import com.zy.core.cache.MessageQueue; import com.zy.core.cache.OutputQueue; @@ -23,6 +27,7 @@ import lombok.extern.slf4j.Slf4j; import java.text.MessageFormat; +import java.util.Base64; import java.util.Date; /** @@ -38,6 +43,11 @@ private CrnProtocol crnProtocol; private boolean resetFlag = false; + public Long sign = System.currentTimeMillis(); + + public boolean isRunning = true; + + /** * 鍫嗗灈鏈烘槸鍚﹀湪鍥炲師鐐硅繍鍔ㄤ腑鏍囪 */ @@ -51,7 +61,7 @@ @SuppressWarnings("InfiniteLoopStatement") public void run() { this.connect(); - while (true) { + while (isRunning) { try { int step = 1; Task task = MessageQueue.poll(SlaveType.Crn, slave.getId()); @@ -92,7 +102,7 @@ } catch (Exception e) { // e.printStackTrace(); } - + sign = System.currentTimeMillis(); } } @@ -132,10 +142,10 @@ if(connect.IsSuccess){ result = true; OutputQueue.CRN.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())); - log.info("鍫嗗灈鏈簆lc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); + News.info("SiemensCrn"+" - 1"+" - 鍫嗗灈鏈簆lc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); } else { OutputQueue.CRN.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())); - log.error("鍫嗗灈鏈簆lc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); + News.error("SiemensCrn"+" - 2"+" - 鍫嗗灈鏈簆lc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); initCrn(); } // siemensNet.ConnectClose(); @@ -147,10 +157,15 @@ */ private void readStatus(){ try { - OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 56); + short len = 56; + if (slave.getId() == 1) { + len = 58; + } + OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", len); if (result.IsSuccess) { if (null == crnProtocol) { crnProtocol = new CrnProtocol(); + crnProtocol.setCrnNo(slave.getId()); } crnProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 0)); crnProtocol.setTaskNo(siemensNet.getByteTransform().TransInt16(result.Content, 2)); @@ -173,11 +188,14 @@ crnProtocol.setyDistance(siemensNet.getByteTransform().TransInt16(result.Content, 44)); crnProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 48)); crnProtocol.setyDuration(siemensNet.getByteTransform().TransInt16(result.Content, 52)); + if (slave.getId() == 1) { + crnProtocol.setCrnLane((int) siemensNet.getByteTransform().TransInt16(result.Content, 56)); + } OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId())); // 澶嶄綅淇″彿 - if (crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) { + if (!Cools.isEmpty(crnProtocol.getStatusType()) && crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) { if (resetFlag) { if(crnProtocol.getTaskNo()==9999){ backHpFlag = false; @@ -190,6 +208,22 @@ } } + if (System.currentTimeMillis() - crnProtocol.getDeviceDataLog() > 1000 * 5) { + //閲囬泦鏃堕棿瓒呰繃5s锛屼繚瀛樹竴娆℃暟鎹褰� + //淇濆瓨鏁版嵁璁板綍 + DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class); + DeviceDataLog deviceDataLog = new DeviceDataLog(); + deviceDataLog.setOriginData(Base64.getEncoder().encodeToString(result.Content)); + deviceDataLog.setWcsData(JSON.toJSONString(crnProtocol)); + deviceDataLog.setType("crn"); + deviceDataLog.setDeviceNo(crnProtocol.getCrnNo()); + deviceDataLog.setCreateTime(new Date()); + deviceDataLogService.insert(deviceDataLog); + + //鏇存柊閲囬泦鏃堕棿 + crnProtocol.setDeviceDataLog(System.currentTimeMillis()); + } + try { // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴� BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class); @@ -197,19 +231,19 @@ basCrnp.setCrnNo(slave.getId()); basCrnp.setCrnSts((int)crnProtocol.getMode()); if (!basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){ - log.error("鍫嗗灈鏈簆lc鏁版嵁搴撴洿鏂板け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); + News.error("SiemensCrn"+" - 3"+" - 鍫嗗灈鏈簆lc鏁版嵁搴撴洿鏂板け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); } } catch (Exception ignore){} } else { initCrn(); OutputQueue.CRN.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())); - log.error("璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); + News.error("SiemensCrn"+" - 4"+" - 璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); } } catch (Exception e) { e.printStackTrace(); OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆戣鍙栧爢鍨涙満plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); - log.error("璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); + News.error("SiemensCrn"+" - 5"+" - 璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); initCrn(); } } @@ -228,12 +262,29 @@ */ private boolean write(CrnCommand command) throws InterruptedException { if (null == command) { - log.error("鍫嗗灈鏈哄啓鍏ュ懡浠や负绌�"); + News.error("SiemensCrn"+" - 6"+" - 鍫嗗灈鏈哄啓鍏ュ懡浠や负绌�"); return false; } - convertRow(command); + + int writeAck = 0; + do { + OperateResult resultAck = siemensNet.Write("DB100.0", (short) 0); + if (resultAck.IsSuccess){ + Thread.sleep(200); + OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 2); + short ack = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0); + if (ack != 0) { + writeAck++; + }else { + News.info("鍫嗗灈鏈哄懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), "ack澶嶄綅瀹屾垚"); + break; + } + } + }while (writeAck <5); + +// convertRow(command); command.setCrnNo(slave.getId()); - short[] array = new short[12]; + short[] array = new short[10]; array[0] = command.getAckFinish(); array[1] = command.getTaskNo(); array[2] = command.getTaskMode(); @@ -243,16 +294,61 @@ array[6] = command.getDestinationPosX(); array[7] = command.getDestinationPosY(); array[8] = command.getDestinationPosZ(); - array[9] = command.getSourceStaNo(); - array[10] = command.getDestinationStaNo(); - array[11] = command.getCommand(); - OperateResult result = siemensNet.Write("DB100.0", array); +// array[9] = command.getSourceStaNo(); +// array[10] = command.getDestinationStaNo(); + array[9] = command.getCommand(); + + OperateResult result = null; + int idx = 0; + do { + OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 20); + if (resultRead.IsSuccess) { + if (command.getAckFinish() == 0) { + short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2); + short taskMode = siemensNet.getByteTransform().TransInt16(resultRead.Content, 4); + short sourcePosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 6); + short sourcePosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 8); + short sourcePosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 10); + short destinationPosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 12); + short destinationPosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 14); + short destinationPosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 16); + if(taskNo == 0 || taskMode == 0 || sourcePosX == 0 || sourcePosY == 0 || sourcePosZ == 0 || destinationPosX == 0 || destinationPosY == 0 || destinationPosZ == 0) { + result = siemensNet.Write("DB100.0", array); + }else { + break; + } + }else { + short ackFinish = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0); + if(ackFinish != command.getAckFinish()) { + result = siemensNet.Write("DB100.0", array); + }else { + break; + } + } + } + idx++; + Thread.sleep(500L); + } while (idx < 5); if (command.getAckFinish() == 0) { short commandFinish = 1; - result = siemensNet.Write("DB100.22", commandFinish); + int i = 0; + do { + OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 4); + OperateResultExOne<byte[]> resultReadConfirm = siemensNet.Read("DB100.18", (short) 2); + if (resultRead.IsSuccess && resultReadConfirm.IsSuccess) { + short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2); + short confirm = siemensNet.getByteTransform().TransInt16(resultReadConfirm.Content, 0); + if(taskNo != 0 && confirm == 0) { + result = siemensNet.Write("DB100.18", commandFinish); + } + } + i++; + Thread.sleep(500L); + } while (i < 5); } + this.crnProtocol.setLastCommandTime(System.currentTimeMillis()); try { // 鏃ュ織璁板綍 BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class); @@ -279,16 +375,15 @@ if (result != null && result.IsSuccess) { Thread.sleep(200); this.readStatus(); - log.info("鍫嗗灈鏈哄懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); + News.info("SiemensCrn"+" - 7"+" - 鍫嗗灈鏈哄懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); return true; } else { OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆戝啓鍏ュ爢鍨涙満plc鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); - log.error("鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); + News.error("SiemensCrn"+" - 8"+" - 鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); return false; } } - @Override public void close() { @@ -410,4 +505,9 @@ } + // 鎻愪緵涓�涓柟娉曟潵鍋滄绾跨▼ + public void requestStop() { + isRunning = false; + } + } -- Gitblit v1.9.1