liuwei 1 день назад
Родитель
Сommit
2f64ec671e

+ 95 - 34
src/com/mes/ui/MesClient.java

@@ -74,6 +74,7 @@ public class MesClient extends JFrame {
     public static JButton finish_ng_bt;
     public static JTextField product_sn;
     public static JButton f_scan_data_bt_1;
+    public static JButton f_start_bt;
 
     public static String user20 = "";
     public static boolean mes_enable = true; // true=MES开启
@@ -341,7 +342,7 @@ public class MesClient extends JFrame {
         MesClient.finish_ok_bt.setEnabled(false);
         MesClient.finish_ng_bt.setEnabled(false);
         product_sn.setText("");
-        product_sn.setEditable(true);
+        product_sn.setEditable(false);
         tjFlag = 0;
         mesStartFlag = 0;
         curTaskName.setText("");
@@ -353,14 +354,64 @@ public class MesClient extends JFrame {
         if (param4 != null) {
             param4.setText("");
         }
-
-//        f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
-//        f_scan_data_bt_1.setText("扫码");
-//        MesClient.f_scan_data_bt_1.setEnabled(true);
+        setStartButtonEnabled(false);
+        if (f_scan_data_bt_1 != null) {
+            f_scan_data_bt_1.setEnabled(true);
+        }
+        // 测完恢复常态:继续每 5s 发停止/复位
+        if (serialPortUtils != null && serialPortUtils.isOpen) {
+            WorkTimer.startStopTimer();
+        }
         MesClient.setMenuStatus("请扫工件码",0);
+    }
+
+    /** 启动按钮可用状态(校验成功后点亮;检测中强制不可用) */
+    public static void setStartButtonEnabled(boolean enabled) {
+        if (f_start_bt == null) {
+            return;
+        }
+        // 设备检测中(已进入测试)不允许再点启动
+        if (enabled && mesStartFlag != null && mesStartFlag == 1) {
+            f_start_bt.setEnabled(false);
+            return;
+        }
+        f_start_bt.setEnabled(enabled);
+    }
 
-//        MesClient.result.setText("等待结果");
-//        MesClient.result.setForeground(Color.GRAY);
+    /**
+     * 扫码校验成功后:停止复位定时器,并按启动模式点亮启动或自动启动。
+     */
+    public static void onScanValidated() {
+        WorkTimer.stopStopTimer();
+        if (configParam != null && "自动启动".equals(configParam.getStartMode())) {
+            doStartTest();
+        } else {
+            setStartButtonEnabled(true);
+        }
+    }
+
+    /**
+     * 点击启动:发启动报文,然后熄灭启动按钮。
+     * 复位定时器已在扫码成功时停止,此处不再发停止信号。
+     */
+    public static void doStartTest() {
+        if (work_status != 1) {
+            setMenuStatus("请先扫工件码", -1);
+            return;
+        }
+        WorkTimer.stopStopTimer();
+        loadAteqProgramTimes();
+        boolean ok = serialPortUtils != null && serialPortUtils.start();
+        setStartButtonEnabled(false);
+        if (ok) {
+            mesStartFlag = 1; // 已发启动,视为进入检测,启动按钮保持不可用
+            setMenuStatus("已启动,测试中", 0);
+            result.setText("等待结果");
+            result.setForeground(Color.GRAY);
+        } else {
+            setMenuStatus("启动失败,请检查设备后重试", -1);
+            setStartButtonEnabled(true);
+        }
     }
 
     public static int userLoginHours;//用户登录所处小时
@@ -628,12 +679,23 @@ public class MesClient extends JFrame {
     }
 
     public static void scanBarcode() {
+        doStartTest();
+    }
+
+    /** 扫码按钮:弹窗输入工件码后走与输入框回车相同的校验流程 */
+    public static void scanBarcodeByButton() {
         if (work_status == 1) {
-            loadAteqProgramTimes();
-            MesClient.serialPortUtils.start();
+            setMenuStatus("已扫码,请点击启动", 0);
+            return;
+        }
+        String scanBarcode = JOptionPane.showInputDialog(mesClientFrame, "请扫工件码");
+        if (scanBarcode == null || scanBarcode.trim().isEmpty()) {
+            setMenuStatus("请扫工件码", -1);
             return;
         }
-        setMenuStatus("请先扫工件码", -1);
+        product_sn.setText(scanBarcode.trim());
+        scan_type = 1;
+        scanBarcode2();
     }
 
     /**
@@ -750,14 +812,14 @@ public class MesClient extends JFrame {
                     MesClient.setMenuStatus("消息发送失败,请重试",-1);
                     return;
                 }
+                // 扫码已通过本地校验并已发质量查询:此后不再发停止/复位
+                WorkTimer.stopStopTimer();
             }else{
                 work_status = 1;
                 MesClient.tjFlag = 1;
                 MesClient.setMenuStatus("扫码成功,等待测试",0);
                 product_sn.setEditable(false);
-                if(MesClient.configParam.getStartMode().equals("自动启动")){
-                    MesClient.serialPortUtils.start();
-                }
+                MesClient.onScanValidated();
                 MesClient.result.setText("等待结果");
                 MesClient.result.setForeground(Color.GRAY);
             }
@@ -821,15 +883,9 @@ public class MesClient extends JFrame {
             mesClientFrame.repaint();
 
             work_status = 1;
-//            f_scan_data_bt_1.setEnabled(false);
             MesClient.tjFlag = 1;
             MesClient.setMenuStatus("扫码成功,等待测试",0);
-
-            if(MesClient.configParam.getStartMode().equals("自动启动")){
-                MesClient.serialPortUtils.start();
-            }
-            f_scan_data_bt_1.setIcon(null);
-            f_scan_data_bt_1.setText("启动");
+            MesClient.onScanValidated();
 
         }else {
             JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
@@ -1000,30 +1056,35 @@ public class MesClient extends JFrame {
 
         product_sn = new JTextField();
         product_sn.setHorizontalAlignment(SwingConstants.LEFT);
-        product_sn.setEditable(true);
-        product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28));
-        product_sn.setBounds(81, 70, 602, 70);
+        product_sn.setEditable(false);
+        product_sn.setFocusable(false);
+        product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 24));
+        // 尺寸不变:宽550;与按钮统一间距 18px
+        product_sn.setBounds(50, 70, 550, 70);
         indexPanelA.add(product_sn);
         product_sn.setColumns(10);
-        product_sn.addActionListener(new ActionListener() {
+
+        f_scan_data_bt_1 = new JButton("扫码");
+        f_scan_data_bt_1.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 scan_type = 1;
-                scanBarcode2();
+                scanBarcodeByButton();
             }
         });
+        f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 28));
+        f_scan_data_bt_1.setBounds(618, 70, 120, 70);
+        indexPanelA.add(f_scan_data_bt_1);
 
-//        f_scan_data_bt_1 = new JButton("扫码");
-        f_scan_data_bt_1 = new JButton("启动");
-        f_scan_data_bt_1.addActionListener(new ActionListener() {
+        f_start_bt = new JButton("启动");
+        f_start_bt.setEnabled(false);
+        f_start_bt.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
-                scan_type = 1;
-                scanBarcode();
+                doStartTest();
             }
         });
-//        f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
-        f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32));
-        f_scan_data_bt_1.setBounds(693, 70, 198, 70);
-        indexPanelA.add(f_scan_data_bt_1);
+        f_start_bt.setFont(new Font("微软雅黑", Font.PLAIN, 28));
+        f_start_bt.setBounds(756, 70, 120, 70);
+        indexPanelA.add(f_start_bt);
         
         JLabel lblNewLabel = new JLabel("测试压");
         lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));

+ 6 - 3
src/com/mes/ui/MesRevice.java

@@ -3,6 +3,7 @@ package com.mes.ui;
 import com.mes.netty.ProtocolParam;
 import com.mes.component.MyDialog;
 import com.mes.util.ErrorMsg;
+import com.mes.util.WorkTimer;
 
 import javax.swing.*;
 import java.awt.*;
@@ -19,13 +20,15 @@ public class MesRevice {
                 MesClient.setMenuStatus("该工件可以测试,等待测试",0);
                 MesClient.product_sn.setEditable(false);
                 MesClient.loadAteqProgramTimes();
-                if(MesClient.configParam.getStartMode().equals("自动启动")){
-                    MesClient.serialPortUtils.start();
-                }
+                MesClient.onScanValidated();
                 MesClient.result.setText("等待结果");
                 MesClient.result.setForeground(Color.GRAY);
             }else {
                 MesClient.check_quality_result = false;
+                MesClient.setStartButtonEnabled(false);
+                if (MesClient.serialPortUtils != null && MesClient.serialPortUtils.isOpen) {
+                    WorkTimer.startStopTimer();
+                }
                 String lmsg = ErrorMsg.getErrorMsg(processMsgRet, ProtocolParam.getLx(mes_msg));
                 MesClient.setMenuStatus(lmsg,-1);
             }

+ 2 - 0
src/com/mes/util/SerialPortUtils.java

@@ -237,6 +237,7 @@ public class SerialPortUtils {
                     });
 
                     WorkTimer.start();
+                    WorkTimer.startStopTimer();
                 }
             }
         }catch (Exception e) {
@@ -266,6 +267,7 @@ public class SerialPortUtils {
     }
 
     public boolean closePort() {
+        WorkTimer.stopStopTimer();
         if (serialPort != null && serialPort.isOpen()) {
             if (serialPort.closePort()) {
                 log.info("串口已关闭.");

+ 23 - 3
src/com/mes/util/WorkTimer.java

@@ -23,6 +23,7 @@ public class WorkTimer {
 
     public static void start() {
         if(workTimer != null){
+            workTimer.cancel();
             workTimer = null;
         }
         workTimer = new Timer();
@@ -36,16 +37,34 @@ public class WorkTimer {
         }
     }
 
+    /** 常态每 5 秒发送停止/复位报文(站号来自配置,默认 01) */
+    public static void startStopTimer() {
+        stopStopTimer();
+        stopTimer = new Timer("ateq-stop-timer", true);
+        stopTimer.schedule(new StopTimerTask(), 0, 5000);
+        log.info("已启动气密仪停止/复位定时器(5s)");
+    }
+
+    /** 点击启动前停止复位定时器,避免与测程抢串口 */
+    public static void stopStopTimer() {
+        if (stopTimer != null) {
+            stopTimer.cancel();
+            stopTimer = null;
+            log.info("已停止气密仪停止/复位定时器");
+        }
+    }
+
     // 发送禁止运行的定时任务
     private static class StopTimerTask extends TimerTask {
         @Override
         public void run() {
             try {
-                if (MesClient.work_status == 0 || !MesClient.check_quality_result) {
-                    ATEQ.stop(); // 发送重置命令, 禁止设备启动
+                if (MesClient.serialPortUtils == null || !MesClient.serialPortUtils.isOpen) {
+                    return;
                 }
+                ATEQ.stop(); // 发送重置命令, 禁止设备启动
             } catch (Exception e) {
-                log.error("向设备发送禁止运行的定时任务报错");
+                log.error("向设备发送禁止运行的定时任务报错: {}", e.getMessage());
             }
         }
     }
@@ -78,6 +97,7 @@ public class WorkTimer {
                                 boolean isEnd = false;
                                 if (measureResult.isTesting) {
                                     MesClient.mesStartFlag = 1;
+                                    MesClient.setStartButtonEnabled(false);
                                     MesClient.setMenuStatus("测试中", 0);
                                 } else if (MesClient.mesStartFlag == 1) {
                                     // 曾进入测试中,状态变为非00,表示测试结束