PlcUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.CommonUtils;
  5. import com.mes.util.DateLocalUtils;
  6. import com.mes.util.JdbcUtils;
  7. import com.mes.util.AppLog;
  8. import java.util.ArrayList;
  9. public class PlcUtil {
  10. public static void getParamDyDl(S7PLC s7PLC){
  11. Boolean starta = getAStart(s7PLC); //A启动中
  12. Boolean startb = getBStart(s7PLC); //B启动中
  13. if(MesClient.tjFlaga == 2 || MesClient.tjFlagb == 2){ //运行中
  14. Float dya = s7PLC.readFloat32("DB1010.2");
  15. Float dla = s7PLC.readFloat32("DB1010.8");
  16. Float ssa = s7PLC.readFloat32("DB1010.14");
  17. Short cwa = s7PLC.readInt16("DB1010.18");
  18. Short joba = s7PLC.readInt16("DB1010.20");
  19. MesClient.param1.setText(dya+"");
  20. MesClient.param2.setText(dla+"");
  21. MesClient.param3.setText(ssa+"");
  22. MesClient.param4.setText(cwa+"");
  23. MesClient.param5.setText(joba+"");
  24. Float dyb = s7PLC.readFloat32("DB1010.26");
  25. Float dlb = s7PLC.readFloat32("DB1010.34");
  26. Float ssb = s7PLC.readFloat32("DB1010.42");
  27. Short cwb = s7PLC.readInt16("DB1010.46");
  28. Short jobb = s7PLC.readInt16("DB1010.48");
  29. MesClient.param21.setText(dyb+"");
  30. MesClient.param22.setText(dlb+"");
  31. MesClient.param23.setText(ssb+"");
  32. MesClient.param24.setText(cwb+"");
  33. MesClient.param25.setText(jobb+"");
  34. String record_time = DateLocalUtils.getCurrentTime();
  35. MesClient.hjparams.add(dya+"|"+dla+"|"+ssa+"|"+cwa+"|"+joba+"|"+dyb+"|"+dlb+"|"+ssb+"|"+cwb+"|"+jobb+"|"+record_time);
  36. System.out.println("cmt:"+dya+"|"+dla+"|"+ssa+"|"+cwa+"|"+joba+"|"+dyb+"|"+dlb+"|"+ssb+"|"+cwb+"|"+jobb+"|"+record_time);
  37. if(MesClient.hjparams.size() == 60){
  38. try{
  39. // 根据curFlag确定工位号和工件码
  40. String oprno = MesClient.mes_gw+"A";
  41. String sn = MesClient.curSna;
  42. if(MesClient.curFlag.equals("B")){
  43. oprno = MesClient.mes_gw+"B";
  44. sn = MesClient.curSnb;
  45. }
  46. if(MesClient.mes_gwflag.equals("B")){
  47. if(MesClient.curFlag.equals("A")){
  48. oprno = MesClient.mes_gw+"C";
  49. }else{
  50. oprno = MesClient.mes_gw+"D";
  51. }
  52. }
  53. // 检查工件码是否为空,为空则不上传
  54. if(sn != null && !sn.trim().isEmpty()){
  55. JdbcUtils.insertCmtData(oprno,MesClient.mes_line_sn,sn, JSON.toJSONString(MesClient.hjparams));
  56. System.out.println("cmt:"+MesClient.curFlag+" sn:"+sn);
  57. }else{
  58. System.out.println("工件码为空,跳过保存数据");
  59. }
  60. MesClient.hjparams = new ArrayList<>();
  61. }catch (Exception e){
  62. e.printStackTrace();
  63. }
  64. }
  65. }else{
  66. if(MesClient.hjparams.size() != 60 && MesClient.hjparams.size() > 0){
  67. try{
  68. // 根据curFlag确定工位号和工件码
  69. String oprno = MesClient.mes_gw+"A";
  70. String sn = MesClient.curSna;
  71. if(MesClient.curFlag.equals("B")){
  72. oprno = MesClient.mes_gw+"B";
  73. sn = MesClient.curSnb;
  74. }
  75. if(MesClient.mes_gwflag.equals("B")){
  76. if(MesClient.curFlag.equals("A")){
  77. oprno = MesClient.mes_gw+"C";
  78. }else{
  79. oprno = MesClient.mes_gw+"D";
  80. }
  81. }
  82. // 检查工件码是否为空,为空则不上传
  83. if(sn != null && !sn.trim().isEmpty()){
  84. JdbcUtils.insertCmtData(oprno,MesClient.mes_line_sn,sn, JSON.toJSONString(MesClient.hjparams));
  85. System.out.println("cmt:"+MesClient.curFlag+" sn:"+sn);
  86. }else{
  87. System.out.println("工件码为空,跳过保存剩余数据");
  88. }
  89. MesClient.hjparams = new ArrayList<>();
  90. }catch (Exception e){
  91. e.printStackTrace();
  92. }
  93. }
  94. }
  95. }
  96. // 获取A启动状态
  97. public static Boolean getAStart(S7PLC s7PLC){
  98. try{
  99. return s7PLC.readBoolean("DB1.2.2");
  100. }catch (Exception e){
  101. e.printStackTrace();
  102. return false;
  103. }
  104. }
  105. // 获取A的结束状态
  106. public static Boolean getAFinish(S7PLC s7PLC){
  107. try{
  108. boolean a = s7PLC.readBoolean("DB1.2.5");
  109. boolean b = s7PLC.readBoolean("DB1.2.3");
  110. // Boolean ret = s7PLC.readBoolean("DB1.2.0");
  111. return a || b;
  112. }catch (Exception e){
  113. e.printStackTrace();
  114. return false;
  115. }
  116. }
  117. public static void getStatusA(S7PLC s7PLC){
  118. if(MesClient.tjFlaga == 1){ // 已扫码,设备未运行
  119. Boolean starta = getAStart(s7PLC); //A启动中
  120. if(starta){
  121. MesClient.curFlag = "A";
  122. MesClient.tjFlaga = 2;
  123. MesClient.pxstatus1.setText("A:设备运行中");
  124. // 更新curSna为当前文本框中的工件码
  125. if(MesClient.curSna == null || MesClient.curSna.isEmpty()){
  126. MesClient.curSna = MesClient.product_sn.getText();
  127. }
  128. if(MesClient.lastpage == 0){
  129. MesClient.lastpage = 1;
  130. PlcUtil.changeEnable(s7PLC,false);
  131. }
  132. }else{
  133. // 程序号防错功能已注释 - 直接允许启动
  134. // if(CommonUtils.checkProgramNoSn(MesClient.product_sn.getText(),MesClient.programNoA)){
  135. Boolean reta = getEnable(s7PLC);
  136. if(!reta){
  137. PlcUtil.changeEnable(MesClient.s7PLC,true);
  138. }
  139. // }
  140. }
  141. }else if(MesClient.tjFlaga == 2){
  142. Boolean afinish = getAFinish(s7PLC); //A焊接完成
  143. if(afinish){ // B启动中,A当做完成
  144. MesClient.tjFlaga = 3;
  145. MesClient.pxstatus1.setText("A:设备运行结束,提交结果中");
  146. MesClient.finish_ok_bt.setEnabled(true);
  147. Boolean sendret = DataUtil.sendQuality(MesClient.nettyClient,MesClient.product_sn.getText(),"OK",MesClient.user20,"A");
  148. // Boolean sendret = DataUtil.sendQuality(MesClient.product_sn.getText(),"OK",MesClient.user20,"A");
  149. if(!sendret){
  150. AppLog.work("quality-submit-A", MesClient.product_sn.getText(), false);
  151. MesClient.pxstatus1.setText("A:结果上传MES失败");
  152. MesClient.tjStatusa = 1;
  153. }else{
  154. // MesClient.pxstatus1.setText("A:提交成功");
  155. // MesClient.resetScanA();
  156. }
  157. }
  158. }
  159. }
  160. // 获取B启动状态
  161. public static Boolean getBStart(S7PLC s7PLC){
  162. try{
  163. return s7PLC.readBoolean("DB1.2.3");
  164. }catch (Exception e){
  165. e.printStackTrace();
  166. return false;
  167. }
  168. }
  169. // 获取B的结束状态
  170. public static Boolean getBFinish(S7PLC s7PLC){
  171. try{
  172. boolean a = s7PLC.readBoolean("DB1.2.7");
  173. boolean b = s7PLC.readBoolean("DB1.2.2");
  174. // Boolean ret = s7PLC.readBoolean("DB1.2.1");
  175. return a || b;
  176. }catch (Exception e){
  177. e.printStackTrace();
  178. return false;
  179. }
  180. }
  181. public static void getStatusB(S7PLC s7PLC){
  182. if(MesClient.tjFlagb == 1){ // 已扫码,设备未运行
  183. Boolean startb = getBStart(s7PLC); //B启动中
  184. if(startb){
  185. MesClient.curFlag = "B";
  186. MesClient.tjFlagb = 2;
  187. MesClient.pxstatus2.setText("B:设备运行中");
  188. // 更新curSnb为当前文本框中的工件码
  189. if(MesClient.curSnb == null || MesClient.curSnb.isEmpty()){
  190. MesClient.curSnb = MesClient.product_sn2.getText();
  191. }
  192. if(MesClient.lastpage == 0){
  193. MesClient.lastpage = 1;
  194. PlcUtil.changeEnable(s7PLC,false);
  195. }
  196. }else{
  197. // 程序号防错功能已注释 - 直接允许启动
  198. // if(CommonUtils.checkProgramNoSn(MesClient.product_sn2.getText(),MesClient.programNoB)){
  199. Boolean reta = getEnable(s7PLC);
  200. if(!reta){
  201. PlcUtil.changeEnable(MesClient.s7PLC,true);
  202. }
  203. // }
  204. }
  205. }else if(MesClient.tjFlagb == 2){
  206. Boolean bfinish = getBFinish(s7PLC); //B焊接完成
  207. if(bfinish){
  208. MesClient.tjFlagb = 3;
  209. MesClient.pxstatus2.setText("B:设备运行结束,提交结果中");
  210. MesClient.finish_ng_bt.setEnabled(true);
  211. // Boolean sendret = DataUtil.sendQuality(MesClient.product_sn2.getText(),"OK",MesClient.user20,"B");
  212. Boolean sendret = DataUtil.sendQuality(MesClient.nettyClient,MesClient.product_sn2.getText(),"OK",MesClient.user20,"B");
  213. if(!sendret){
  214. AppLog.work("quality-submit-B", MesClient.product_sn2.getText(), false);
  215. MesClient.pxstatus2.setText("B:结果上传MES失败");
  216. MesClient.tjStatusb = 1;
  217. }else{
  218. // MesClient.pxstatus2.setText("B:提交成功");
  219. // MesClient.resetScanB();
  220. }
  221. }
  222. }
  223. }
  224. // 修改设备的禁用开关
  225. public static Boolean changeEnable(S7PLC s7PLC,Boolean result){
  226. try{
  227. // s7PLC.writeBoolean("DB9.18.1",result); // AB
  228. s7PLC.writeBoolean("DB9.30.4",result); // 610-P2
  229. return true;
  230. }catch (Exception e){
  231. e.printStackTrace();
  232. return false;
  233. }
  234. }
  235. // 获取MES允许的值
  236. public static Boolean getEnable(S7PLC s7PLC){
  237. Boolean ret = false;
  238. try{
  239. // ret = s7PLC.readBoolean("DB9.18.1"); // AB
  240. ret = s7PLC.readBoolean("DB9.30.4"); // 610-P2
  241. }catch (Exception e){
  242. e.printStackTrace();
  243. }
  244. return ret;
  245. }
  246. // 获取当前是A面还是B面(610-P2:DB1.2.0=true 时为 B 面)
  247. public static String getCurAside(S7PLC s7PLC){
  248. String str = "B";
  249. try{
  250. Boolean ret = s7PLC.readBoolean("DB1.2.0"); // 当前A面
  251. if(!ret){
  252. str = "A";
  253. }
  254. }catch (Exception e){
  255. e.printStackTrace();
  256. }
  257. return str;
  258. }
  259. // 获取程序号A
  260. public static int getProgranNoA(S7PLC s7PLC){
  261. int str = 0;
  262. try{
  263. int ret = Integer.valueOf(s7PLC.readByte("DB8.3")); // AB
  264. return ret;
  265. }catch (Exception e){
  266. e.printStackTrace();
  267. }
  268. return str;
  269. }
  270. // 获取程序号B
  271. public static int getProgranNoB(S7PLC s7PLC){
  272. int str = 0;
  273. try{
  274. int ret = Integer.valueOf(s7PLC.readByte("DB8.4")); // AB
  275. return ret;
  276. }catch (Exception e){
  277. e.printStackTrace();
  278. }
  279. return str;
  280. }
  281. }