PlcUtil.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  4. public class PlcUtil {
  5. // PLC 连接同时被取码线程和信号灯线程使用,读写统一加锁,避免并发导致读写失败
  6. private static final Object PLC_LOCK = new Object();
  7. // 上料请求镭雕码内容
  8. public static Boolean getFeeding(S7PLC s7PLC){
  9. synchronized (PLC_LOCK) {
  10. try{
  11. return s7PLC.readBoolean("DB5000.0.4");
  12. }catch (Exception e){
  13. e.printStackTrace();
  14. return false;
  15. }
  16. }
  17. }
  18. // 二维码接收反馈
  19. public static Boolean getReply(S7PLC s7PLC){
  20. synchronized (PLC_LOCK) {
  21. try{
  22. return s7PLC.readBoolean("DB5000.0.5");
  23. }catch (Exception e){
  24. e.printStackTrace();
  25. return false;
  26. }
  27. }
  28. }
  29. // 获取码内容
  30. public static String getQrcode(S7PLC s7PLC){
  31. synchronized (PLC_LOCK) {
  32. try{
  33. return s7PLC.readString("DB5000.260",28).trim();
  34. }catch (Exception e){
  35. e.printStackTrace();
  36. return "";
  37. }
  38. }
  39. }
  40. // 设置码内容:写完后立即读回校验
  41. public static Boolean setQrcode(S7PLC s7PLC,String sn){
  42. synchronized (PLC_LOCK) {
  43. try{
  44. s7PLC.writeString("DB5000.260", sn);
  45. String readBack = s7PLC.readString("DB5000.260",28).trim();
  46. MesClient.log.info("写PLC码: {}, 读回: {}", sn, readBack);
  47. return true;
  48. }catch (Exception e){
  49. MesClient.log.error("写PLC码失败: " + sn, e);
  50. return false;
  51. }
  52. }
  53. }
  54. public static void monitorPlc(S7PLC s7PLC){
  55. Boolean feeding = getFeeding(s7PLC);
  56. Boolean reply = getReply(s7PLC);
  57. // 反馈复位后,允许下一轮提交
  58. if(!reply){
  59. MesClient.resultSubmitted = false;
  60. }
  61. // 二维码反馈:打码成功,结束本轮(只打码,不提交工位结果)
  62. if(reply && MesClient.qrLoaded && !MesClient.resultSubmitted){
  63. MesClient.resultSubmitted = true;
  64. MesClient.resetScanA();
  65. MesClient.successLabel.setVisible(true);
  66. MesClient.setMenuStatus("打码成功,等待下一件",0);
  67. }
  68. // 计划已完成/无计划时不再自动取码;失败后按间隔重试
  69. if (feeding && MesClient.dateLoading == 0 && !MesClient.qrLoaded && !MesClient.planStop
  70. && System.currentTimeMillis() >= MesClient.nextRetryTime){
  71. MesClient.dateLoading = 1;
  72. JSONObject retObj = DataUtil.getHmbContent(MesClient.user_menu.getText().trim());
  73. if(retObj == null){
  74. MesClient.setMenuStatus("请求失败,请重试",-1);
  75. MesClient.nextRetryTime = System.currentTimeMillis() + 10000;
  76. MesClient.dateLoading = 0;
  77. return;
  78. }
  79. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  80. String hmb = retObj.getString("data");
  81. // 显示服务端下发的产品号
  82. String pn = retObj.getString("pn");
  83. if(pn != null && !pn.isEmpty()){
  84. MesClient.currentPn = pn;
  85. MesClient.pnLabel.setText(pn);
  86. }
  87. if(!setQrcode(s7PLC,hmb)){
  88. MesClient.setMenuStatus("写入PLC失败,请检查PLC连接",-1);
  89. MesClient.nextRetryTime = System.currentTimeMillis() + 10000;
  90. MesClient.dateLoading = 0;
  91. return;
  92. }
  93. // 锁住,本件未完成前不再取新码
  94. MesClient.qrLoaded = true;
  95. MesClient.dateLoading = 0;
  96. MesClient.product_sn.setText(hmb);
  97. MesClient.mesClientFrame.repaint();
  98. }else{
  99. MesClient.nextRetryTime = System.currentTimeMillis() + 10000;
  100. if(retObj.get("result")==null){
  101. MesClient.setMenuStatus("请求失败,请重试",-1);
  102. }else{
  103. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  104. String code = retObj.getString("code");
  105. if("PLAN_FINISHED".equals(code) || "NO_PLAN".equals(code)){
  106. MesClient.planStop = true;
  107. MesClient.log.info("停止自动取码,code={}", code);
  108. }
  109. MesClient.setMenuStatus(retObj.getString("message"),-1);
  110. }
  111. }
  112. MesClient.dateLoading = 0;
  113. }
  114. }
  115. // Boolean reply = getReply(s7PLC);
  116. // if(reply){
  117. // String code = getQrcode(s7PLC);
  118. // if(!code.isEmpty()){
  119. // setQrcode(s7PLC," ");
  120. // }
  121. // }
  122. }
  123. }