| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package com.mes.ui;
- import com.alibaba.fastjson2.JSONObject;
- import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
- public class PlcUtil {
- // PLC 连接同时被取码线程和信号灯线程使用,读写统一加锁,避免并发导致读写失败
- private static final Object PLC_LOCK = new Object();
- // 上料请求镭雕码内容
- public static Boolean getFeeding(S7PLC s7PLC){
- synchronized (PLC_LOCK) {
- try{
- return s7PLC.readBoolean("DB5000.0.4");
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- }
- // 二维码接收反馈
- public static Boolean getReply(S7PLC s7PLC){
- synchronized (PLC_LOCK) {
- try{
- return s7PLC.readBoolean("DB5000.0.5");
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- }
- // 获取码内容
- public static String getQrcode(S7PLC s7PLC){
- synchronized (PLC_LOCK) {
- try{
- return s7PLC.readString("DB5000.260",28).trim();
- }catch (Exception e){
- e.printStackTrace();
- return "";
- }
- }
- }
- // 设置码内容:写完后立即读回校验
- public static Boolean setQrcode(S7PLC s7PLC,String sn){
- synchronized (PLC_LOCK) {
- try{
- s7PLC.writeString("DB5000.260", sn);
- String readBack = s7PLC.readString("DB5000.260",28).trim();
- MesClient.log.info("写PLC码: {}, 读回: {}", sn, readBack);
- return true;
- }catch (Exception e){
- MesClient.log.error("写PLC码失败: " + sn, e);
- return false;
- }
- }
- }
- public static void monitorPlc(S7PLC s7PLC){
- Boolean feeding = getFeeding(s7PLC);
- Boolean reply = getReply(s7PLC);
- // 反馈复位后,允许下一轮提交
- if(!reply){
- MesClient.resultSubmitted = false;
- }
- // 二维码反馈:打码成功,结束本轮(只打码,不提交工位结果)
- if(reply && MesClient.qrLoaded && !MesClient.resultSubmitted){
- MesClient.resultSubmitted = true;
- MesClient.resetScanA();
- MesClient.successLabel.setVisible(true);
- MesClient.setMenuStatus("打码成功,等待下一件",0);
- }
- // 计划已完成/无计划时不再自动取码;失败后按间隔重试
- if (feeding && MesClient.dateLoading == 0 && !MesClient.qrLoaded && !MesClient.planStop
- && System.currentTimeMillis() >= MesClient.nextRetryTime){
- MesClient.dateLoading = 1;
- JSONObject retObj = DataUtil.getHmbContent(MesClient.user_menu.getText().trim());
- if(retObj == null){
- MesClient.setMenuStatus("请求失败,请重试",-1);
- MesClient.nextRetryTime = System.currentTimeMillis() + 10000;
- MesClient.dateLoading = 0;
- return;
- }
- if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
- String hmb = retObj.getString("data");
- // 显示服务端下发的产品号
- String pn = retObj.getString("pn");
- if(pn != null && !pn.isEmpty()){
- MesClient.currentPn = pn;
- MesClient.pnLabel.setText(pn);
- }
- if(!setQrcode(s7PLC,hmb)){
- MesClient.setMenuStatus("写入PLC失败,请检查PLC连接",-1);
- MesClient.nextRetryTime = System.currentTimeMillis() + 10000;
- MesClient.dateLoading = 0;
- return;
- }
- // 锁住,本件未完成前不再取新码
- MesClient.qrLoaded = true;
- MesClient.dateLoading = 0;
- MesClient.product_sn.setText(hmb);
- MesClient.mesClientFrame.repaint();
- }else{
- MesClient.nextRetryTime = System.currentTimeMillis() + 10000;
- if(retObj.get("result")==null){
- MesClient.setMenuStatus("请求失败,请重试",-1);
- }else{
- if(retObj.get("result").toString().equalsIgnoreCase("false")){
- String code = retObj.getString("code");
- if("PLAN_FINISHED".equals(code) || "NO_PLAN".equals(code)){
- MesClient.planStop = true;
- MesClient.log.info("停止自动取码,code={}", code);
- }
- MesClient.setMenuStatus(retObj.getString("message"),-1);
- }
- }
- MesClient.dateLoading = 0;
- }
- }
- // Boolean reply = getReply(s7PLC);
- // if(reply){
- // String code = getQrcode(s7PLC);
- // if(!code.isEmpty()){
- // setQrcode(s7PLC," ");
- // }
- // }
- }
- }
|