package com.mes.ui; import com.alibaba.fastjson2.JSON; import com.github.xingshuangs.iot.protocol.s7.service.S7PLC; import com.mes.util.CommonUtils; import com.mes.util.DateLocalUtils; import com.mes.util.JdbcUtils; import java.util.ArrayList; public class PlcUtil { public static void getParamDyDl(S7PLC s7PLC){ Boolean starta = getAStart(s7PLC); //A启动中 Boolean startb = getBStart(s7PLC); //B启动中 if(MesClient.tjFlaga == 2 || MesClient.tjFlagb == 2){ //运行中 Float dya = s7PLC.readFloat32("DB1010.2"); Float dla = s7PLC.readFloat32("DB1010.8"); Float ssa = s7PLC.readFloat32("DB1010.14"); Short cwa = s7PLC.readInt16("DB1010.18"); Short joba = s7PLC.readInt16("DB1010.20"); MesClient.param1.setText(dya+""); MesClient.param2.setText(dla+""); MesClient.param3.setText(ssa+""); MesClient.param4.setText(cwa+""); MesClient.param5.setText(joba+""); Float dyb = s7PLC.readFloat32("DB1010.26"); Float dlb = s7PLC.readFloat32("DB1010.34"); Float ssb = s7PLC.readFloat32("DB1010.42"); Short cwb = s7PLC.readInt16("DB1010.46"); Short jobb = s7PLC.readInt16("DB1010.48"); MesClient.param21.setText(dyb+""); MesClient.param22.setText(dlb+""); MesClient.param23.setText(ssb+""); MesClient.param24.setText(cwb+""); MesClient.param25.setText(jobb+""); String record_time = DateLocalUtils.getCurrentTime(); MesClient.hjparams.add(dya+"|"+dla+"|"+ssa+"|"+cwa+"|"+joba+"|"+dyb+"|"+dlb+"|"+ssb+"|"+cwb+"|"+jobb+"|"+record_time); System.out.println("cmt:"+dya+"|"+dla+"|"+ssa+"|"+cwa+"|"+joba+"|"+dyb+"|"+dlb+"|"+ssb+"|"+cwb+"|"+jobb+"|"+record_time); if(MesClient.hjparams.size() == 60){ try{ // 根据curFlag确定工位号和工件码 String oprno = MesClient.mes_gw+"A"; String sn = MesClient.curSna; if(MesClient.curFlag.equals("B")){ oprno = MesClient.mes_gw+"B"; sn = MesClient.curSnb; } if(MesClient.mes_gwflag.equals("B")){ if(MesClient.curFlag.equals("A")){ oprno = MesClient.mes_gw+"C"; }else{ oprno = MesClient.mes_gw+"D"; } } // 检查工件码是否为空,为空则不上传 if(sn != null && !sn.trim().isEmpty()){ JdbcUtils.insertCmtData(oprno,MesClient.mes_line_sn,sn, JSON.toJSONString(MesClient.hjparams)); System.out.println("cmt:"+MesClient.curFlag+" sn:"+sn); }else{ System.out.println("工件码为空,跳过保存数据"); } MesClient.hjparams = new ArrayList<>(); }catch (Exception e){ e.printStackTrace(); } } }else{ if(MesClient.hjparams.size() != 60 && MesClient.hjparams.size() > 0){ try{ // 根据curFlag确定工位号和工件码 String oprno = MesClient.mes_gw+"A"; String sn = MesClient.curSna; if(MesClient.curFlag.equals("B")){ oprno = MesClient.mes_gw+"B"; sn = MesClient.curSnb; } if(MesClient.mes_gwflag.equals("B")){ if(MesClient.curFlag.equals("A")){ oprno = MesClient.mes_gw+"C"; }else{ oprno = MesClient.mes_gw+"D"; } } // 检查工件码是否为空,为空则不上传 if(sn != null && !sn.trim().isEmpty()){ JdbcUtils.insertCmtData(oprno,MesClient.mes_line_sn,sn, JSON.toJSONString(MesClient.hjparams)); System.out.println("cmt:"+MesClient.curFlag+" sn:"+sn); }else{ System.out.println("工件码为空,跳过保存剩余数据"); } MesClient.hjparams = new ArrayList<>(); }catch (Exception e){ e.printStackTrace(); } } } } // 获取A启动状态 public static Boolean getAStart(S7PLC s7PLC){ try{ return s7PLC.readBoolean("DB1.2.2"); }catch (Exception e){ e.printStackTrace(); return false; } } // 获取A的结束状态 public static Boolean getAFinish(S7PLC s7PLC){ try{ boolean a = s7PLC.readBoolean("DB1.2.5"); boolean b = s7PLC.readBoolean("DB1.2.3"); // Boolean ret = s7PLC.readBoolean("DB1.2.0"); return a || b; }catch (Exception e){ e.printStackTrace(); return false; } } public static void getStatusA(S7PLC s7PLC){ if(MesClient.tjFlaga == 1){ // 已扫码,设备未运行 Boolean starta = getAStart(s7PLC); //A启动中 if(starta){ MesClient.curFlag = "A"; MesClient.tjFlaga = 2; MesClient.pxstatus1.setText("A:设备运行中"); // 更新curSna为当前文本框中的工件码 if(MesClient.curSna == null || MesClient.curSna.isEmpty()){ MesClient.curSna = MesClient.product_sn.getText(); } if(MesClient.lastpage == 0){ MesClient.lastpage = 1; PlcUtil.changeEnable(s7PLC,false); } }else{ // 程序号防错功能已注释 - 直接允许启动 // if(CommonUtils.checkProgramNoSn(MesClient.product_sn.getText(),MesClient.programNoA)){ Boolean reta = getEnable(s7PLC); if(!reta){ PlcUtil.changeEnable(MesClient.s7PLC,true); } // } } }else if(MesClient.tjFlaga == 2){ Boolean afinish = getAFinish(s7PLC); //A焊接完成 if(afinish){ // B启动中,A当做完成 MesClient.tjFlaga = 3; MesClient.pxstatus1.setText("A:设备运行结束,提交结果中"); MesClient.finish_ok_bt.setEnabled(true); Boolean sendret = DataUtil.sendQuality(MesClient.nettyClient,MesClient.product_sn.getText(),"OK",MesClient.user20,"A"); // Boolean sendret = DataUtil.sendQuality(MesClient.product_sn.getText(),"OK",MesClient.user20,"A"); if(!sendret){ MesClient.pxstatus1.setText("A:结果上传MES失败"); MesClient.tjStatusa = 1; }else{ // MesClient.pxstatus1.setText("A:提交成功"); // MesClient.resetScanA(); } } } } // 获取B启动状态 public static Boolean getBStart(S7PLC s7PLC){ try{ return s7PLC.readBoolean("DB1.2.3"); }catch (Exception e){ e.printStackTrace(); return false; } } // 获取B的结束状态 public static Boolean getBFinish(S7PLC s7PLC){ try{ boolean a = s7PLC.readBoolean("DB1.2.7"); boolean b = s7PLC.readBoolean("DB1.2.2"); // Boolean ret = s7PLC.readBoolean("DB1.2.1"); return a || b; }catch (Exception e){ e.printStackTrace(); return false; } } public static void getStatusB(S7PLC s7PLC){ if(MesClient.tjFlagb == 1){ // 已扫码,设备未运行 Boolean startb = getBStart(s7PLC); //B启动中 if(startb){ MesClient.curFlag = "B"; MesClient.tjFlagb = 2; MesClient.pxstatus2.setText("B:设备运行中"); // 更新curSnb为当前文本框中的工件码 if(MesClient.curSnb == null || MesClient.curSnb.isEmpty()){ MesClient.curSnb = MesClient.product_sn2.getText(); } if(MesClient.lastpage == 0){ MesClient.lastpage = 1; PlcUtil.changeEnable(s7PLC,false); } }else{ // 程序号防错功能已注释 - 直接允许启动 // if(CommonUtils.checkProgramNoSn(MesClient.product_sn2.getText(),MesClient.programNoB)){ Boolean reta = getEnable(s7PLC); if(!reta){ PlcUtil.changeEnable(MesClient.s7PLC,true); } // } } }else if(MesClient.tjFlagb == 2){ Boolean bfinish = getBFinish(s7PLC); //B焊接完成 if(bfinish){ MesClient.tjFlagb = 3; MesClient.pxstatus2.setText("B:设备运行结束,提交结果中"); MesClient.finish_ng_bt.setEnabled(true); // Boolean sendret = DataUtil.sendQuality(MesClient.product_sn2.getText(),"OK",MesClient.user20,"B"); Boolean sendret = DataUtil.sendQuality(MesClient.nettyClient,MesClient.product_sn2.getText(),"OK",MesClient.user20,"B"); if(!sendret){ MesClient.pxstatus2.setText("B:结果上传MES失败"); MesClient.tjStatusb = 1; }else{ // MesClient.pxstatus2.setText("B:提交成功"); // MesClient.resetScanB(); } } } } // 修改设备的禁用开关 public static Boolean changeEnable(S7PLC s7PLC,Boolean result){ try{ s7PLC.writeBoolean("DB9.18.3",result); // AB // s7PLC.writeBoolean("DB9.30.4",result); // CD return true; }catch (Exception e){ e.printStackTrace(); return false; } } // 获取MES允许的值 public static Boolean getEnable(S7PLC s7PLC){ Boolean ret = false; try{ ret = s7PLC.readBoolean("DB9.18.3"); // AB // ret = s7PLC.readBoolean("DB9.30.4"); // CD }catch (Exception e){ e.printStackTrace(); } return ret; } // 获取当前是A面还是B面 public static String getCurAside(S7PLC s7PLC){ String str = "A"; try{ Boolean ret = s7PLC.readBoolean("DB1.2.0"); // 当前A面 if(!ret){ str = "B"; } }catch (Exception e){ e.printStackTrace(); } return str; } // 获取程序号A public static int getProgranNoA(S7PLC s7PLC){ int str = 0; try{ int ret = Integer.valueOf(s7PLC.readByte("DB8.3")); // AB return ret; }catch (Exception e){ e.printStackTrace(); } return str; } // 获取程序号B public static int getProgranNoB(S7PLC s7PLC){ int str = 0; try{ int ret = Integer.valueOf(s7PLC.readByte("DB8.4")); // AB return ret; }catch (Exception e){ e.printStackTrace(); } return str; } }