From 6e7575427ba0aec46d377719bc58ec0355bb335a Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期日, 09 十月 2022 14:24:59 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/SteThread.java | 159 +++++++++++++++++++++++++++++++---------------------
1 files changed, 95 insertions(+), 64 deletions(-)
diff --git a/src/main/java/com/zy/core/thread/SteThread.java b/src/main/java/com/zy/core/thread/SteThread.java
index 787be85..a278c53 100644
--- a/src/main/java/com/zy/core/thread/SteThread.java
+++ b/src/main/java/com/zy/core/thread/SteThread.java
@@ -5,6 +5,7 @@
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.core.exception.CoolException;
@@ -17,7 +18,6 @@
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.SteStatusType;
-import com.zy.core.enums.SteTaskModeType;
import com.zy.core.model.SteSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.SteCommand;
@@ -27,7 +27,6 @@
import java.text.MessageFormat;
import java.util.Date;
-import java.util.Random;
/**
* shuttle 绌挎杞︾嚎绋�
@@ -43,10 +42,6 @@
private SteProtocol steProtocol;
private short heartBeatVal = 1;
private boolean resetFlag = false;
-
- private Integer lastRow;
- private Integer lastBay;
- private Integer lastLev;
public SteThread(SteSlave slave) {
this.slave = slave;
@@ -95,7 +90,7 @@
steProtocol.setSteNo(slave.getId().shortValue());
steProtocol.setMode((short) 0);
steProtocol.setStatus(SteStatusType.OFF_LINE);
- steProtocol.setTaskNo(0);
+ steProtocol.setTaskNo((short) 0);
steProtocol.setExecute(false);
steProtocol.setWaiting(false);
}
@@ -110,10 +105,10 @@
OperateResult connect = siemensS7Net.ConnectServer();
if(connect.IsSuccess){
result = true;
- OutputQueue.CRN.offer(MessageFormat.format( "銆恵0}銆戠┛姊溅plc杩炴帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+ OutputQueue.STE.offer(MessageFormat.format( "銆恵0}銆戠┛姊溅plc杩炴帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
log.info("绌挎杞lc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
} else {
- OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆戠┛姊溅plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{1}] [ip:{2}] [port:{3}] ", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+ OutputQueue.STE.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());
initSte();
}
@@ -130,8 +125,9 @@
if (result.IsSuccess) {
if (null == steProtocol) {
steProtocol = new SteProtocol();
+ steProtocol.setSteNo(slave.getId().shortValue());
}
- steProtocol.setSteNo(siemensS7Net.getByteTransform().TransInt16(result.Content, 0));
+// steProtocol.setSteNo(siemensS7Net.getByteTransform().TransInt16(result.Content, 0));
steProtocol.setMode(siemensS7Net.getByteTransform().TransInt16(result.Content, 2));
steProtocol.setStatus(siemensS7Net.getByteTransform().TransInt16(result.Content, 4));
OperateResultExOne<Boolean> executeRes = siemensS7Net.ReadBool("V2001.0");
@@ -142,9 +138,9 @@
if (waitingRes.IsSuccess) {
steProtocol.setWaiting(waitingRes.Content);
}
- steProtocol.setAlarm(siemensS7Net.getByteTransform().TransInt32(result.Content, 6));
- steProtocol.setAlarm0(siemensS7Net.getByteTransform().TransInt32(result.Content, 8));
- steProtocol.setCharge(siemensS7Net.getByteTransform().TransInt16(result.Content, 10));
+ steProtocol.setAlarm(siemensS7Net.getByteTransform().TransInt16(result.Content, 6));
+ steProtocol.setAlarm0(siemensS7Net.getByteTransform().TransInt16(result.Content, 8));
+ steProtocol.setCharge(siemensS7Net.getByteTransform().TransSingle(result.Content, 10));
// steProtocol.setFullCharge();
// steProtocol.setLowCharge();
steProtocol.setFeed(siemensS7Net.getByteTransform().TransInt16(result.Content, 14));
@@ -154,7 +150,7 @@
steProtocol.setPos(siemensS7Net.getByteTransform().TransInt16(result.Content, 18));
steProtocol.setLoad(siemensS7Net.getByteTransform().TransInt16(result.Content, 20));
steProtocol.setTrack(siemensS7Net.getByteTransform().TransInt16(result.Content, 22));
- steProtocol.setTaskNo(siemensS7Net.getByteTransform().TransInt32(result.Content, 24));
+ steProtocol.setTaskNo(siemensS7Net.getByteTransform().TransInt16(result.Content, 24));
steProtocol.setTaskType(siemensS7Net.getByteTransform().TransInt16(result.Content, 28));
steProtocol.setRow(siemensS7Net.getByteTransform().TransInt16(result.Content, 30));
steProtocol.setBay(siemensS7Net.getByteTransform().TransInt16(result.Content, 32));
@@ -170,8 +166,9 @@
steProtocol.setCrnStopRun(siemensS7Net.getByteTransform().TransInt16(result.Content, 38));
steProtocol.setCrnStopFork(siemensS7Net.getByteTransform().TransInt16(result.Content, 40));
steProtocol.setCrnAllowRun(siemensS7Net.getByteTransform().TransInt16(result.Content, 42));
+ steProtocol.setChargeStatus(siemensS7Net.getByteTransform().TransInt16(result.Content, 44));
- OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId()));
+ OutputQueue.STE.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId()));
// 澶嶄綅淇″彿
if (steProtocol.getWaiting()) {
@@ -188,6 +185,12 @@
// 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴�
BasSteService service = SpringUtils.getBean(BasSteService.class);
if (null != service) {
+ // 鍚屾pakMk
+ BasSte one = service.selectById(slave.getId());
+ if (one != null) {
+ steProtocol.setPakMk(one.getPakMk());
+ }
+
BasSte basSte = new BasSte();
basSte.setSteNo(slave.getId());
if (!service.updateById(steProtocol.toSqlModel(basSte))){
@@ -196,13 +199,13 @@
}
} else {
- OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆憑1}绌挎杞lc鐘舵�佷俊鎭け璐�",DateUtils.convert(new Date()), slave.getId()));
+ OutputQueue.STE.offer(MessageFormat.format("銆恵0}銆憑1}绌挎杞lc鐘舵�佷俊鎭け璐�",DateUtils.convert(new Date()), slave.getId()));
throw new CoolException(MessageFormat.format( "绌挎杞lc鐘舵�佷俊鎭け璐� ===>> [id:{0}] [ip:{1}] [port:{2}]", slave.getId(), slave.getIp(), slave.getPort()));
}
} 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());
+// e.printStackTrace();
+ OutputQueue.STE.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());
initSte();
}
@@ -220,13 +223,17 @@
OperateResult result = null;
// 寮�濮嬩换鍔�
if (!command.getComplete()) {
-
+ // 1.浠诲姟鍙�
+ OperateResult result0 = siemensS7Net.Write("V998", command.getTaskNo().shortValue());
+ try {
+ Thread.sleep(200);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
// 浣滀笟
if (command.getTaskMode() != 0) {
- // 1.浠诲姟鍙�
- OperateResult result0 = siemensS7Net.Write("V1000", command.getTaskNo());
// 2.浣滀笟
- OperateResult result1 = siemensS7Net.Write("V1000", command.getTaskMode()); // todo
+ OperateResult result1 = siemensS7Net.Write("V1000", command.getTaskMode());
// 3.纭寮�濮嬩换鍔�
if (result0.IsSuccess && result1.IsSuccess) {
result = siemensS7Net.Write("V2000.0", true);
@@ -244,14 +251,19 @@
} else if (command.getDelete() != null) {
result = siemensS7Net.Write("V2000.3", command.getDelete());
// 鏃犳晥鎸囦护
- } else {
+ } else if (command.getRun() != null) {
+ result = siemensS7Net.Write("V1016", command.getRun());
+ // 鏃犳晥鎸囦护
+ }else {
return false;
}
}
// 浠诲姟瀹屾垚
} else {
- result = siemensS7Net.Write("D0", true);
+ siemensS7Net.Write("V998", (short) 0);
+ siemensS7Net.Write("V1000", (short) 0);
+ result = siemensS7Net.Write("V2000.1", true);
}
try {
@@ -286,54 +298,60 @@
if (result != null && result.IsSuccess) {
// 缁存姢鏁版嵁搴撴帓鍒楀眰
if (!steProtocol.getWaiting()) {
- this.lastRow = command.getRow().intValue();
- this.lastBay = command.getBay().intValue();
- this.lastLev = command.getLev().intValue();
+ if (!Cools.isEmpty(command.getRow(), command.getBay(), command.getLev())) {
+ this.modifyPos(command.getRow().intValue(), command.getBay().intValue(), command.getLev().intValue());
+ }
}
log.info("绌挎杞﹀懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
- OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
+ OutputQueue.STE.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()));
+ OutputQueue.STE.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());
return false;
}
}
public void modifyPos(Integer row, Integer bay, Integer lev) {
- this.lastRow = row;
- this.lastBay = bay;
- this.lastLev = lev;
+ BasSteService service = SpringUtils.getBean(BasSteService.class);
+ if (!service.updatePos(this.slave.getId(), row, bay, lev)) {
+ log.error("鏇存柊{}鍙风┛姊溅瀹氫綅澶辫触 ===>> 鎺掞細銆恵}銆�, 鍒楋細銆恵}銆戯紝灞傦細銆恵}銆�", this.slave.getId(), row, bay, lev);
+ }
}
public boolean confirmPos() {
- if (this.lastRow != null && this.lastRow != 0) {
- if (this.lastBay != null && this.lastBay != 0) {
- if (this.lastLev != null && this.lastLev != 0) {
- BasSteService service = SpringUtils.getBean(BasSteService.class);
- BasSte basSte = service.selectById(slave.getId());
- if (basSte != null) {
- // 鏇存柊plc鏁版嵁鍧�
- short[] arr = new short[] {this.lastRow.shortValue(), this.lastBay.shortValue(), this.lastLev.shortValue()};
- OperateResult result = siemensS7Net.Write("V1002", arr);
- if (result.IsSuccess) {
- // 鏇存柊鏁版嵁搴�
- if (service.updatePos(this.lastRow, this.lastBay, this.lastLev) > 0) {
- this.lastRow = null;
- this.lastBay = null;
- this.lastLev = null;
- return true;
- } else {
- log.error("{}鍙风┛姊溅淇敼鏁版嵁搴撳畾浣嶅け璐ワ紒锛侊紒", slave.getId());
- }
- }
- }
-
+ BasSteService service = SpringUtils.getBean(BasSteService.class);
+ BasSte basSte = service.selectById(slave.getId());
+ if (basSte != null) {
+ // 鏇存柊plc鏁版嵁鍧�
+ short[] arr = new short[] {basSte.getRow().shortValue(), basSte.getBay().shortValue(), basSte.getLev().shortValue()};
+ OperateResult result = siemensS7Net.Write("V1002", arr);
+ if (result.IsSuccess) {
+ // 鏇存柊鏁版嵁搴�
+ if (service.updatePakMk(this.slave.getId(), "N")) {
+ return true;
+ } else {
+ log.error("{}鍙风┛姊溅淇敼鏁版嵁搴撳畾浣嶅け璐ワ紒锛侊紒", slave.getId());
}
}
}
return false;
+ }
+
+ public boolean modifyPosHandle(Integer row, Integer bay, Integer lev) {
+ short[] arr = new short[] {row.shortValue(), bay.shortValue(), lev.shortValue()};
+ OperateResult result = siemensS7Net.Write("V1002", arr);
+ if (!result.IsSuccess) {
+ log.error("鏇存柊{}鍙风┛姊溅瀹氫綅澶辫触 ===>> 鎺掞細銆恵}銆�, 鍒楋細銆恵}銆戯紝灞傦細銆恵}銆�", this.slave.getId(), row, bay, lev);
+ return false;
+ }
+ BasSteService service = SpringUtils.getBean(BasSteService.class);
+ if (!service.updatePos(this.slave.getId(), row, bay, lev)) {
+ log.error("鏇存柊{}鍙风┛姊溅瀹氫綅澶辫触 ===>> 鎺掞細銆恵}銆�, 鍒楋細銆恵}銆戯紝灞傦細銆恵}銆�", this.slave.getId(), row, bay, lev);
+ return false;
+ }
+ return true;
}
@Override
@@ -356,6 +374,12 @@
}
}
+// public void modifyPos(int wrkNo, int row, int bay, int lev) {
+// BasSteService service = SpringUtils.getBean(BasSteService.class);
+// if (!service.updatePos(wrkNo,this.slave.getId(), row, bay, lev)) {
+// log.error("鏇存柊{}鍙风┛姊溅瀹氫綅澶辫触 ===>> 鎺掞細銆恵}銆�, 鍒楋細銆恵}銆戯紝灞傦細銆恵}銆�", this.slave.getId(), row, bay, lev);
+// }
+// }
/******************************************************************************************/
/**************************************** 娴嬭瘯涓撶敤 *****************************************/
@@ -363,19 +387,21 @@
public static void main(String[] args) throws InterruptedException {
SteSlave slave = new SteSlave();
slave.setId(1);
- slave.setIp("192.168.3.39");
- slave.setPort(5015);
+ slave.setIp("192.168.2.1");
+ slave.setPort(502);
SteThread thread = new SteThread(slave);
thread.connect();
thread.readStatus();
System.out.println(JSON.toJSONString(thread.steProtocol));
// 浠诲姟浣滀笟
- SteCommand command = new SteCommand();
- command.setSteNo(1); // 鍫嗗灈鏈虹紪鍙�
- command.setTaskNo(new Random().nextInt(9000)); // 宸ヤ綔鍙�
- command.setTaskMode(SteTaskModeType.IN_LEFT); // 浠诲姟妯″紡
- thread.write(command);
+// SteCommand command = new SteCommand();
+// command.setSteNo(1); // 鍫嗗灈鏈虹紪鍙�
+// Random random = new Random();
+// int taskNo = random.nextInt(9090);
+// command.setTaskNo(taskNo); // 宸ヤ綔鍙�
+// command.setTaskMode(SteTaskModeType.MOVE_LEFT); // 浠诲姟妯″紡
+// thread.write(command);
// 浠诲姟瀹屾垚
// SteCommand command = new SteCommand();
@@ -390,14 +416,19 @@
// 澶嶄綅淇″彿
// SteCommand command = new SteCommand();
-// command.setReset((short) 1);
+// command.setReset(Boolean.TRUE);
// thread.write(command);
// 鍒犻櫎鎸囦护
// SteCommand command = new SteCommand();
-// command.setDelete((short) 1);
+// command.setDelete(Boolean.TRUE);
// thread.write(command);
+ // 绌挎杞﹁繍琛岀姝�
+ SteCommand command = new SteCommand();
+ command.setRun((short)0);
+ thread.write(command);
+
}
}
--
Gitblit v1.9.1