PlcUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  4. import com.mes.util.DateLocalUtils;
  5. import com.mes.util.JdbcUtils;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class PlcUtil {
  9. private static final String DB = "DB200";
  10. private static final String A_ENABLE = DB + ".0.1";
  11. private static final String B_ENABLE = DB + ".38.0";
  12. private static final String A_STATUS = DB + ".34";
  13. private static final String B_STATUS = DB + ".72";
  14. private static final String A_POSITION = DB + ".156.3";
  15. private static final String B_POSITION = DB + ".156.4";
  16. private static final WeldPoint[] WELD_POINTS = new WeldPoint[]{
  17. new WeldPoint("R1", 76.1, 78, 80, 84, 88, 92),
  18. new WeldPoint("R2", 96.2, 98, 100, 104, 108, 112),
  19. new WeldPoint("R3", 116.2, 118, 120, 124, 128, 132),
  20. new WeldPoint("R4", 136.2, 138, 140, 144, 148, 152)
  21. };
  22. // 缓存上一次的参数值,用于检测变化
  23. private static String lastParamValues = null;
  24. public static void getParamDyDl(S7PLC s7PLC) {
  25. if (MesClient.tjFlaga == 2 || MesClient.tjFlagb == 2) {
  26. List<String> values = readWeldParams(s7PLC);
  27. String recordTime = DateLocalUtils.getCurrentTime();
  28. values.add("time=" + recordTime);
  29. MesClient.hjparams.add(String.join("|", values));
  30. refreshParamText(values);
  31. // 只在参数变化时打印(排除 time 字段)
  32. String currentParams = String.join("|", values.subList(0, values.size() - 1));
  33. if (!currentParams.equals(lastParamValues)) {
  34. System.out.println("cmt:" + String.join("|", values));
  35. lastParamValues = currentParams;
  36. }
  37. if (MesClient.hjparams.size() == 60) {
  38. saveCurrentParams();
  39. }
  40. } else if (MesClient.hjparams.size() > 0) {
  41. saveCurrentParams();
  42. lastParamValues = null; // 重置缓存
  43. }
  44. }
  45. private static List<String> readWeldParams(S7PLC s7PLC) {
  46. List<String> values = new ArrayList<>();
  47. for (WeldPoint point : WELD_POINTS) {
  48. boolean finished = readBoolean(s7PLC, point.finishAddress());
  49. int seq = readInt16(s7PLC, point.seqAddress());
  50. float current = readFloat32(s7PLC, point.currentAddress());
  51. float voltage = readFloat32(s7PLC, point.voltageAddress());
  52. float wireSpeed = readFloat32(s7PLC, point.wireSpeedAddress());
  53. float weldSpeed = readFloat32(s7PLC, point.weldSpeedAddress());
  54. values.add(point.name + ":finish=" + finished);
  55. values.add(point.name + ":seq=" + seq);
  56. values.add(point.name + ":current=" + current);
  57. values.add(point.name + ":voltage=" + voltage);
  58. values.add(point.name + ":wireSpeed=" + wireSpeed);
  59. values.add(point.name + ":weldSpeed=" + weldSpeed);
  60. }
  61. return values;
  62. }
  63. private static void refreshParamText(List<String> values) {
  64. float r1Current = valueOf(values, "R1:current=");
  65. float r1Voltage = valueOf(values, "R1:voltage=");
  66. float r1WireSpeed = valueOf(values, "R1:wireSpeed=");
  67. float r1WeldSpeed = valueOf(values, "R1:weldSpeed=");
  68. int r1Seq = (int) valueOf(values, "R1:seq=");
  69. if ("B".equals(MesClient.curFlag)) {
  70. MesClient.param21.setText(String.valueOf(r1Voltage));
  71. MesClient.param22.setText(String.valueOf(r1Current));
  72. MesClient.param23.setText(String.valueOf(r1WireSpeed));
  73. MesClient.param24.setText(String.valueOf(r1Seq));
  74. MesClient.param25.setText(String.valueOf(r1WeldSpeed));
  75. } else {
  76. MesClient.param1.setText(String.valueOf(r1Voltage));
  77. MesClient.param2.setText(String.valueOf(r1Current));
  78. MesClient.param3.setText(String.valueOf(r1WireSpeed));
  79. MesClient.param4.setText(String.valueOf(r1Seq));
  80. MesClient.param5.setText(String.valueOf(r1WeldSpeed));
  81. }
  82. }
  83. private static float valueOf(List<String> values, String prefix) {
  84. for (String value : values) {
  85. if (value.startsWith(prefix)) {
  86. try {
  87. return Float.parseFloat(value.substring(prefix.length()));
  88. } catch (Exception ignored) {
  89. return 0;
  90. }
  91. }
  92. }
  93. return 0;
  94. }
  95. private static void saveCurrentParams() {
  96. try {
  97. String oprno = MesClient.mes_gw + "A";
  98. String sn = MesClient.curSna;
  99. if ("B".equals(MesClient.curFlag)) {
  100. oprno = MesClient.mes_gw + "B";
  101. sn = MesClient.curSnb;
  102. }
  103. // 检查工件码和参数列表都不为空
  104. if (sn != null && !sn.trim().isEmpty()) {
  105. if (MesClient.hjparams != null && !MesClient.hjparams.isEmpty()) {
  106. JdbcUtils.insertCmtData(oprno, MesClient.mes_line_sn, sn, JSON.toJSONString(MesClient.hjparams));
  107. System.out.println("cmt:" + MesClient.curFlag + " sn:" + sn + " params_count:" + MesClient.hjparams.size());
  108. } else {
  109. System.out.println("weld params is empty, skip save (sn:" + sn + ")");
  110. }
  111. } else {
  112. System.out.println("workpiece sn is empty, skip cmt params save");
  113. }
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. } finally {
  117. MesClient.hjparams = new ArrayList<>();
  118. }
  119. }
  120. public static Boolean getAStart(S7PLC s7PLC) {
  121. return getWeldStatus(s7PLC, "A") == 1;
  122. }
  123. public static Boolean getAFinish(S7PLC s7PLC) {
  124. return getWeldStatus(s7PLC, "A") == 2;
  125. }
  126. public static void getStatusA(S7PLC s7PLC) {
  127. int status = getWeldStatus(s7PLC, "A");
  128. if (MesClient.tjFlaga == 1) {
  129. if (status == 1) {
  130. MesClient.curFlag = "A";
  131. MesClient.tjFlaga = 2;
  132. MesClient.pxstatus1.setText("A:\u8bbe\u5907\u8fd0\u884c\u4e2d");
  133. if (MesClient.curSna == null || MesClient.curSna.isEmpty()) {
  134. MesClient.curSna = MesClient.product_sn.getText();
  135. }
  136. changeEnable(s7PLC, false, "A");
  137. } else if (!getEnable(s7PLC, "A")) {
  138. changeEnable(s7PLC, true, "A");
  139. }
  140. } else if (MesClient.tjFlaga == 2 && status == 2) {
  141. finishA(s7PLC);
  142. }
  143. }
  144. private static void finishA(S7PLC s7PLC) {
  145. MesClient.tjFlaga = 3;
  146. MesClient.pxstatus1.setText("A:\u8bbe\u5907\u8fd0\u884c\u7ed3\u675f,\u63d0\u4ea4\u7ed3\u679c\u4e2d");
  147. MesClient.finish_ok_bt.setEnabled(true);
  148. changeEnable(s7PLC, false, "A");
  149. ackWeldStatus(s7PLC, "A");
  150. saveCurrentParams();
  151. Boolean sendret = DataUtil.sendQuality(MesClient.nettyClient, MesClient.product_sn.getText(), "OK", MesClient.user20, "A");
  152. if (!sendret) {
  153. MesClient.pxstatus1.setText("A:\u7ed3\u679c\u4e0a\u4f20MES\u5931\u8d25");
  154. MesClient.tjStatusa = 1;
  155. }
  156. }
  157. public static Boolean getBStart(S7PLC s7PLC) {
  158. return getWeldStatus(s7PLC, "B") == 1;
  159. }
  160. public static Boolean getBFinish(S7PLC s7PLC) {
  161. return getWeldStatus(s7PLC, "B") == 2;
  162. }
  163. public static void getStatusB(S7PLC s7PLC) {
  164. int status = getWeldStatus(s7PLC, "B");
  165. if (MesClient.tjFlagb == 1) {
  166. if (status == 1) {
  167. MesClient.curFlag = "B";
  168. MesClient.tjFlagb = 2;
  169. MesClient.pxstatus2.setText("B:\u8bbe\u5907\u8fd0\u884c\u4e2d");
  170. if (MesClient.curSnb == null || MesClient.curSnb.isEmpty()) {
  171. MesClient.curSnb = MesClient.product_sn2.getText();
  172. }
  173. changeEnable(s7PLC, false, "B");
  174. } else if (!getEnable(s7PLC, "B")) {
  175. changeEnable(s7PLC, true, "B");
  176. }
  177. } else if (MesClient.tjFlagb == 2 && status == 2) {
  178. finishB(s7PLC);
  179. }
  180. }
  181. private static void finishB(S7PLC s7PLC) {
  182. MesClient.tjFlagb = 3;
  183. MesClient.pxstatus2.setText("B:\u8bbe\u5907\u8fd0\u884c\u7ed3\u675f,\u63d0\u4ea4\u7ed3\u679c\u4e2d");
  184. MesClient.finish_ng_bt.setEnabled(true);
  185. changeEnable(s7PLC, false, "B");
  186. ackWeldStatus(s7PLC, "B");
  187. saveCurrentParams();
  188. Boolean sendret = DataUtil.sendQuality(MesClient.nettyClient, MesClient.product_sn2.getText(), "OK", MesClient.user20, "B");
  189. if (!sendret) {
  190. MesClient.pxstatus2.setText("B:\u7ed3\u679c\u4e0a\u4f20MES\u5931\u8d25");
  191. MesClient.tjStatusb = 1;
  192. }
  193. }
  194. public static Boolean changeEnable(S7PLC s7PLC, Boolean result) {
  195. return changeEnable(s7PLC, result, getCurAside(s7PLC));
  196. }
  197. public static Boolean changeEnable(S7PLC s7PLC, Boolean result, String side) {
  198. try {
  199. s7PLC.writeBoolean(enableAddress(side), result);
  200. return true;
  201. } catch (Exception e) {
  202. e.printStackTrace();
  203. return false;
  204. }
  205. }
  206. public static Boolean getEnable(S7PLC s7PLC) {
  207. return getEnable(s7PLC, getCurAside(s7PLC));
  208. }
  209. public static Boolean getEnable(S7PLC s7PLC, String side) {
  210. return readBoolean(s7PLC, enableAddress(side));
  211. }
  212. public static String getReserveSide(S7PLC s7PLC) {
  213. int aStatus = getWeldStatus(s7PLC, "A");
  214. int bStatus = getWeldStatus(s7PLC, "B");
  215. if (aStatus == 1 || MesClient.tjFlaga == 2) {
  216. return "B";
  217. }
  218. if (bStatus == 1 || MesClient.tjFlagb == 2) {
  219. return "A";
  220. }
  221. return getCurAside(s7PLC);
  222. }
  223. public static String getCurAside(S7PLC s7PLC) {
  224. try {
  225. boolean aPosition = s7PLC.readBoolean(A_POSITION);
  226. boolean bPosition = s7PLC.readBoolean(B_POSITION);
  227. if (aPosition) {
  228. return "A";
  229. }
  230. if (bPosition) {
  231. return "B";
  232. }
  233. } catch (Exception e) {
  234. e.printStackTrace();
  235. }
  236. if (MesClient.lastDetectedPage != null && !MesClient.lastDetectedPage.isEmpty()) {
  237. return MesClient.lastDetectedPage;
  238. }
  239. return "A";
  240. }
  241. public static int getProgranNoA(S7PLC s7PLC) {
  242. return 0;
  243. }
  244. public static int getProgranNoB(S7PLC s7PLC) {
  245. return 0;
  246. }
  247. private static int getWeldStatus(S7PLC s7PLC, String side) {
  248. return readInt16(s7PLC, statusAddress(side));
  249. }
  250. private static void ackWeldStatus(S7PLC s7PLC, String side) {
  251. try {
  252. s7PLC.writeInt16(statusAddress(side), (short) 0);
  253. } catch (Exception e) {
  254. e.printStackTrace();
  255. }
  256. }
  257. private static String enableAddress(String side) {
  258. return "B".equals(side) ? B_ENABLE : A_ENABLE;
  259. }
  260. private static String statusAddress(String side) {
  261. return "B".equals(side) ? B_STATUS : A_STATUS;
  262. }
  263. private static boolean readBoolean(S7PLC s7PLC, String address) {
  264. try {
  265. return s7PLC.readBoolean(address);
  266. } catch (Exception e) {
  267. e.printStackTrace();
  268. return false;
  269. }
  270. }
  271. private static int readInt16(S7PLC s7PLC, String address) {
  272. try {
  273. return s7PLC.readInt16(address);
  274. } catch (Exception e) {
  275. e.printStackTrace();
  276. return 0;
  277. }
  278. }
  279. private static float readFloat32(S7PLC s7PLC, String address) {
  280. try {
  281. return s7PLC.readFloat32(address);
  282. } catch (Exception e) {
  283. e.printStackTrace();
  284. return 0;
  285. }
  286. }
  287. private static class WeldPoint {
  288. private final String name;
  289. private final double finishOffset;
  290. private final int seqOffset;
  291. private final int currentOffset;
  292. private final int voltageOffset;
  293. private final int wireSpeedOffset;
  294. private final int weldSpeedOffset;
  295. private WeldPoint(String name, double finishOffset, int seqOffset, int currentOffset, int voltageOffset,
  296. int wireSpeedOffset, int weldSpeedOffset) {
  297. this.name = name;
  298. this.finishOffset = finishOffset;
  299. this.seqOffset = seqOffset;
  300. this.currentOffset = currentOffset;
  301. this.voltageOffset = voltageOffset;
  302. this.wireSpeedOffset = wireSpeedOffset;
  303. this.weldSpeedOffset = weldSpeedOffset;
  304. }
  305. private String finishAddress() {
  306. return DB + "." + String.valueOf(finishOffset);
  307. }
  308. private String seqAddress() {
  309. return DB + "." + seqOffset;
  310. }
  311. private String currentAddress() {
  312. return DB + "." + currentOffset;
  313. }
  314. private String voltageAddress() {
  315. return DB + "." + voltageOffset;
  316. }
  317. private String wireSpeedAddress() {
  318. return DB + "." + wireSpeedOffset;
  319. }
  320. private String weldSpeedAddress() {
  321. return DB + "." + weldSpeedOffset;
  322. }
  323. }
  324. }