Przeglądaj źródła

扫码枪传二维码给PLC

wangxichen 13 godzin temu
rodzic
commit
5d350ef589
1 zmienionych plików z 88 dodań i 2 usunięć
  1. 88 2
      src/com/mes/component/WorkJPanel70Xh.java

+ 88 - 2
src/com/mes/component/WorkJPanel70Xh.java

@@ -130,6 +130,16 @@ public class WorkJPanel70Xh extends JPanel {
     public JButton manualSubmitBtn;
     public volatile String pendingManualSn = ""; // 待使用的手动码,下次扫码请求时消费
 
+    // OP080扫码结果推送PLC:扫到码后留存15s写入DB1120.516(S7String),到期清空
+    public static final String ADDR_PUSH_SN = "DB1120.516";
+    public static final String ADDR_PUSH_LEN = "DB1120.517"; // S7String实际长度字节
+    public static final long PUSH_HOLD_MS = 15000;
+    public volatile String holdSn = "";      // 留存的码,空=无
+    public volatile long holdUntil = 0;      // 留存截止时间
+    public String writtenSn = "";            // 已写入PLC的码,仅推送定时器线程使用
+    public JTextField pushEchoField;         // PLC回显(读回DB1120.516)
+    public Timer pushTimer;
+
     public WorkJPanel70Xh(String position){
         this("OP080", position); // 默认反面换型台(原OP070配置)
     }
@@ -220,7 +230,7 @@ public class WorkJPanel70Xh extends JPanel {
         productSnFeeding.setForeground(Color.BLACK);
         productSnFeeding.setFont(new Font("微软雅黑", Font.PLAIN, 16));
         productSnFeeding.setEditable(false);
-        productSnFeeding.setBounds(workWidth/5, 50, workWidth*4/5, 40);
+        productSnFeeding.setBounds(workWidth/5, 50, workWidth*3/5, 40); // 右侧留给刷新按钮
         productSnFeeding.addMouseListener(new MouseAdapter() {
             public void mouseClicked(MouseEvent e) {
                 String sn = productSnFeeding.getText().trim();
@@ -265,7 +275,7 @@ public class WorkJPanel70Xh extends JPanel {
         });
         resetFeedingBtn.setFont(new Font("微软雅黑", Font.PLAIN, 22));
         resetFeedingBtn.setBounds(workWidth*4/5 + 10, 50, workWidth/5 - 10, 40);
-//        this.add(resetFeedingBtn);
+        this.add(resetFeedingBtn);
 
         resetBtn = new JButton("刷新");
         resetBtn.addActionListener(new ActionListener() {
@@ -334,8 +344,31 @@ public class WorkJPanel70Xh extends JPanel {
 
         setManualMode(MesClient.manualScanEnabled);
 
+        // PLC回显行(仅OP080):显示从DB1120.516读回的值
+        if("OP080".equals(oprno)){
+            JLabel pushEchoLabel = new JLabel("回显");
+            pushEchoLabel.setHorizontalAlignment(SwingConstants.CENTER);
+            pushEchoLabel.setForeground(Color.BLACK);
+            pushEchoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
+            pushEchoLabel.setToolTipText(ADDR_PUSH_SN);
+            pushEchoLabel.setBounds(0, 180, workWidth/5, 40);
+            this.add(pushEchoLabel);
+
+            pushEchoField = new JTextField("");
+            pushEchoField.setHorizontalAlignment(SwingConstants.CENTER);
+            pushEchoField.setForeground(Color.BLACK);
+            pushEchoField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
+            pushEchoField.setEditable(false);
+            pushEchoField.setToolTipText(ADDR_PUSH_SN);
+            pushEchoField.setBounds(workWidth/5, 180, workWidth*3/5, 40);
+            this.add(pushEchoField);
+        }
+
         formatElementUI();
         startOprnoTimer();
+        if("OP080".equals(oprno)){
+            startPushTimer();
+        }
 
 //        resetScanA();
 
@@ -466,6 +499,53 @@ public class WorkJPanel70Xh extends JPanel {
         }, 100,1500);
     }
 
+    // 扫码结果推送定时器(独立于扫码主流程):留存期内写码,到期写空,并读回显
+    public void startPushTimer() {
+        if(pushTimer!=null) {
+            pushTimer.cancel();
+        }
+        pushTimer = new Timer();
+        pushTimer.schedule(new TimerTask() {
+            public void run() {
+                if(MesClient.isLogin != 1 || isLocal == 1){
+                    return;
+                }
+                try{
+                    String target = System.currentTimeMillis() <= holdUntil ? holdSn : "";
+                    if(!target.equals(writtenSn)){
+                        if(target.isEmpty()){
+                            // writeString不支持空串:S7String第2字节为实际长度,连同16位字符一起写0清空
+                            s7PLC.writeByte(ADDR_PUSH_LEN, new byte[1 + SN_LENGTH]);
+                        }else{
+                            s7PLC.writeString(ADDR_PUSH_SN, target);
+                        }
+                        writtenSn = target;
+                        log.info(oprno+pos+"写入"+ADDR_PUSH_SN+":"+(target.isEmpty() ? "清空" : target));
+                    }
+                    pushEchoField.setText(readPushSn());
+                }catch (Exception e) {
+                    log.error(oprno+pos+"推送"+ADDR_PUSH_SN+"失败:"+e.getMessage());
+                }
+            }
+        }, 1000,500);
+    }
+
+    // 按原始字节读S7String:[最大长度][实际长度][字符...],实际长度为0时返回空串
+    public String readPushSn(){
+        byte[] bs = s7PLC.readByte(ADDR_PUSH_SN, 2 + SN_LENGTH);
+        int len = Math.min(bs[1] & 0xFF, SN_LENGTH);
+        return new String(bs, 2, len, java.nio.charset.StandardCharsets.US_ASCII).trim();
+    }
+
+    // 留存扫到的码,只改内存不碰PLC;同一个码重复触发不刷新截止时间
+    public void holdPushSn(String sn){
+        if(sn.equals(holdSn)){
+            return;
+        }
+        holdUntil = System.currentTimeMillis() + PUSH_HOLD_MS;
+        holdSn = sn;
+    }
+
     // 显示/隐藏手动输入行;关闭时丢弃未使用的手动码
     public void setManualMode(boolean enabled){
         manualLabel.setVisible(enabled);
@@ -512,6 +592,7 @@ public class WorkJPanel70Xh extends JPanel {
         }else{
             productSnFeeding.setText(sn);
             writeScanFinish(true);   //允许扫码反馈
+            holdPushSn(sn);          //留存15s推送DB1120.516(仅OP080启动推送定时器)
         }
     }
     public void barCode(){
@@ -561,8 +642,13 @@ public class WorkJPanel70Xh extends JPanel {
 //        writeAllowBlanking(false);
     }
 
+    // 刷新扫码:清空扫码框、待用手动码、全局码,setMenuStatus 同步写空缓存
     public void resetFeeding() {
+        log.info(oprno+pos+"刷新扫码,清除工件码:"+productSnFeeding.getText()+",操作人:"+MesClient.user_menu.getText());
         productSnFeeding.setText("");
+        pendingManualSn = "";
+        manualSnField.setText("");
+        MesClient.preSn = "";
         tjFeedingFlag = 0;
         setMenuStatus("等待请求扫码",0);
         resetFeedingBtn.setEnabled(true);