PlcUtil.java 11 KB

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