hfy 2 дней назад
Родитель
Сommit
6918ccfc35

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

@@ -21,6 +21,7 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Properties;
@@ -60,6 +61,7 @@ public class MesClient extends JFrame {
     public static  ParamConfig paramConfig;
     public static Boolean check_quality_result = false;
     public static Integer work_status = 0;
+    public static volatile boolean isWeighing = false;
 
     public static JButton heart_beat_menu;
     public static JButton status_menu;
@@ -69,7 +71,7 @@ public class MesClient extends JFrame {
     public static JButton finish_ok_bt;
     public static JTextField product_sn;
     public static JButton f_scan_data_bt_1;
-    public static String comboBoxValue = "COM1";
+    public static String comboBoxValue = "COM7";
     public static String user20 = "";
 
     public static JFrame welcomeWin;
@@ -85,6 +87,7 @@ public class MesClient extends JFrame {
     public static Object[][] rowData = null;
     public static Timer cjTimer;
     public static Timer cjTimer2;
+    public static Timer autoWeighStopTimer;
     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             @Override
@@ -93,7 +96,7 @@ public class MesClient extends JFrame {
 
                     //读文件配置
                     readProperty();
-                    checkComPort();
+                    //checkComPort();
                     // 显示界面
                     mesClientFrame = new MesClient();
                     mesClientFrame.setVisible(false);
@@ -119,6 +122,7 @@ public class MesClient extends JFrame {
             serialPortService = null;
         }
         serialPortService = new WeightSerialService();
+        serialPortService.setReadingEnabled(isWeighing);
         // 默认波特率 9600(可通过 ParamConfig.DEFAULT_BAUD_RATE 修改)
         paramConfig = new ParamConfig((String) comboBox.getSelectedItem(), ParamConfig.DEFAULT_BAUD_RATE);
         System.out.println("串口号:" + comboBox.getSelectedItem() + " 波特率:" + paramConfig.getBaudRate());
@@ -128,12 +132,17 @@ public class MesClient extends JFrame {
             @Override
             public void onWeightData(WeightData data) {
                 // 仅在"称重"状态下更新界面显示
+                if (!isWeighing) {
+                    return;
+                }
                 SwingUtilities.invokeLater(new Runnable() {
                     @Override
                     public void run() {
+                        if (!isWeighing) {
+                            return;
+                        }
                         if (weight_text != null) {
-                            // 秤端发来的小数点位数比实际多1,导致值被多除以10,此处乘以10修正
-                            weight_text.setText(String.valueOf(data.getValue() * 10));
+                            weight_text.setText(BigDecimal.valueOf(data.getValue()).multiply(new BigDecimal("10")).stripTrailingZeros().toPlainString());
                         }
                         System.out.println("收到重量数据: " + data);
                     }
@@ -166,9 +175,9 @@ public class MesClient extends JFrame {
     //定时任务重连吊秤
     public static void checkComPort() {
             if(MesClient.cjTimer2!=null) {
-                MesClient.cjTimer2.cancel();
+                return;
             }
-            MesClient.cjTimer2 = new Timer();
+            MesClient.cjTimer2 = new Timer(true);
             MesClient.cjTimer2.schedule(new TimerTask() {
                 public void run() {
                     try {
@@ -185,6 +194,46 @@ public class MesClient extends JFrame {
 
     }
 
+    public static void setWeighing(boolean weighing) {
+        if (!weighing) {
+            cancelAutoWeighStopTimer();
+        }
+        isWeighing = weighing;
+        if (serialPortService != null) {
+            serialPortService.setReadingEnabled(weighing);
+        }
+        if (finish_ok_bt_1 != null) {
+            finish_ok_bt_1.setText(weighing ? "暂停" : "称重");
+        }
+        if (weight_text != null) {
+            weight_text.setEnabled(weighing);
+        }
+    }
+
+    private static void cancelAutoWeighStopTimer() {
+        if (autoWeighStopTimer != null) {
+            autoWeighStopTimer.cancel();
+            autoWeighStopTimer = null;
+        }
+    }
+
+    public static void startAutoWeighing() {
+        cancelAutoWeighStopTimer();
+        setWeighing(true);
+        autoWeighStopTimer = new Timer(true);
+        autoWeighStopTimer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                SwingUtilities.invokeLater(new Runnable() {
+                    @Override
+                    public void run() {
+                        setWeighing(false);
+                    }
+                });
+            }
+        }, 2000);
+    }
+
     //读配置文件
     private static void readProperty() throws IOException{
         String enconding = "UTF-8";
@@ -313,7 +362,8 @@ public class MesClient extends JFrame {
         weight_text.setText("");
         weight_text.setEnabled(false);
         finish_ok_bt.setEnabled(false);
-        MesClient.finish_ok_bt_1.setText("称重");
+        MesClient.setWeighing(false);
+        finish_ok_bt_1.setEnabled(false);
 
         MesClient.f_scan_data_bt_1.setEnabled(true);
         setMenuStatus("请扫工件码",0);
@@ -396,6 +446,7 @@ public class MesClient extends JFrame {
             switch(scan_type) {
                 case 1:
                     product_sn.setText(scanBarcode);
+                    weight_text.setText("");
                     break;
             }
             //刷新界面
@@ -406,9 +457,11 @@ public class MesClient extends JFrame {
                 return;
             }
 
+
             // 查询工件质量
             Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
             if(!sendret){
+                setWeighing(false);
                 JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
                 return;
             }
@@ -569,13 +622,14 @@ public class MesClient extends JFrame {
         comboBox.setMaximumSize(new Dimension(100, 40));
         comboBox.setMinimumSize(new Dimension(100, 30));
         comboBox.setPreferredSize(new Dimension(100, 30));
-        comboBox.setModel(new DefaultComboBoxModel(new String[] {"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8"}));
+        comboBox.setModel(new DefaultComboBoxModel(new String[] {"COM7"}));
         toolBar.add(comboBox);
         // 串口发生改变
         comboBox.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 //修改串口时,先关闭资源,重新初始化
+                MesClient.setWeighing(false);
                 if (serialPortService != null) {
                     serialPortService.close();
                 }
@@ -623,11 +677,38 @@ public class MesClient extends JFrame {
         finish_ok_bt.setEnabled(false);
         finish_ok_bt.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
+                if (isWeighing) {
+                    JOptionPane.showMessageDialog(mesClientFrame, "请点击暂停键", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
+                    return;
+                }
                 if(work_status == 1 && check_quality_result) {
                     // 调用bindWarehouse绑定重量并上传:wsn=重量, sn=工件码, craft=400006
                     String sn = getBarcode(product_sn.getText());
                     getUser();
+                 /*   String qret = "OK";
+                    Boolean asendret = DataUtil.sendQuality(nettyClient,sn,qret,user20);
+                    if(!asendret){
+                        JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
+                        return;
+                    }*/
                     String wsn = weight_text.getText();
+                    // 新增重量区间校验
+                    Double weightVal;
+                    try {
+                        weightVal = Double.parseDouble(wsn);
+                    } catch (NumberFormatException a) {
+                        JOptionPane.showMessageDialog(mesClientFrame, "重量格式不正确,请确认!", "提示窗口", JOptionPane.WARNING_MESSAGE);
+                        return;
+                    }
+
+                    // 判断范围 76.76 ~ 83.22
+                    if (weightVal < 76.76 || weightVal > 83.22) {
+                        JOptionPane.showMessageDialog(mesClientFrame,
+                                String.format("重量不在合格范围!当前重量:%.2f,要求区间:76.76 ~ 83.22,请重新称重", weightVal),
+                                "提示窗口",
+                                JOptionPane.WARNING_MESSAGE);
+                        return;
+                    }
                     Boolean sendret = DataUtil.bindWarehouse(nettyClient, sn, wsn, user20, "400006");
                     if (!sendret) {
                         JOptionPane.showMessageDialog(mesClientFrame, "消息发送失败,请重试", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
@@ -657,16 +738,12 @@ public class MesClient extends JFrame {
         lblNewLabel.setBounds(238, 300, 117, 70);
         indexPanelA.add(lblNewLabel);
         
-        finish_ok_bt_1 = new JButton("确定");
+        finish_ok_bt_1 = new JButton("称重");
         finish_ok_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png")));
         finish_ok_bt_1.addActionListener(new ActionListener() {
         	public void actionPerformed(ActionEvent e) {
-                if (finish_ok_bt_1.getText().equals("称重")){
-                    finish_ok_bt_1.setText("暂停");
-                }else if (finish_ok_bt_1.getText().equals("暂停")){
-                    finish_ok_bt_1.setText("称重");
-                }
-        	}
+                setWeighing(!isWeighing);
+            }
         });
         finish_ok_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 40));
         finish_ok_bt_1.setEnabled(true);
@@ -692,7 +769,7 @@ public class MesClient extends JFrame {
 		tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
 
 
-		tabbedPane.addChangeListener(new ChangeListener() {
+        tabbedPane.addChangeListener(new ChangeListener() {
             @Override
             public void stateChanged(ChangeEvent e) {
                 JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
@@ -704,6 +781,8 @@ public class MesClient extends JFrame {
                 }
             }
         });
+        MesClient.initComPort();
+        MesClient.checkComPort();
     }
 
     public static void setMenuStatus(String msg,int error){

+ 5 - 0
src/com/mes/ui/MesRevice.java

@@ -11,6 +11,8 @@ import com.mes.util.SerialPortUtils;
 import javax.swing.*;
 import java.awt.*;
 
+import static com.mes.ui.MesClient.startAutoWeighing;
+
 public class MesRevice {
 
 //     质量查询
@@ -23,12 +25,15 @@ public class MesRevice {
                 MesClient.status_menu.setForeground(Color.GREEN);
                 MesClient.check_quality_result = true;//质量合格,可以绑定加工
                 MesClient.status_menu.setText("该工件可以加工");
+                MesClient.finish_ok_bt_1.setEnabled(true);
+                MesClient.startAutoWeighing();
                 if(MesClient.work_status == 0 ){
                     String barcode36 = MesClient.getBarcode(MesClient.product_sn.getText());//处理36为码
                     MesClient.getUser();
                     DataUtil.startWork(MesClient.nettyClient,barcode36,MesClient.user20);
                 }
             }else {
+                MesClient.finish_ok_bt_1.setEnabled(false);
                 MesClient.check_quality_result = false;
                 String lmsg = ErrorMsg.getErrorMsg(processMsgRet, ProtocolParam.getLx(mes_msg));
                 MesClient.setMenuStatus(lmsg,-1);

+ 22 - 0
src/com/mes/util/WeightSerialService.java

@@ -45,6 +45,9 @@ public class WeightSerialService {
     /** 当前连接状态 */
     private final AtomicBoolean connected = new AtomicBoolean(false);
 
+    /** 是否允许解析重量数据。串口可保持连接,暂停时直接丢弃仪表上报的数据。 */
+    private final AtomicBoolean readingEnabled = new AtomicBoolean(false);
+
     /** 接收缓存:用于跨事件拼接不完整的数据帧 */
     private final ByteArrayOutputStream rxBuffer = new ByteArrayOutputStream(64);
 
@@ -228,6 +231,12 @@ public class WeightSerialService {
             if (newData == null || newData.length == 0) {
                 return;
             }
+            if (!readingEnabled.get()) {
+                synchronized (rxBuffer) {
+                    rxBuffer.reset();
+                }
+                return;
+            }
             processData(newData);
         } catch (Throwable t) {
             // 任何异常都不能让监听线程退出
@@ -331,6 +340,19 @@ public class WeightSerialService {
         return connected.get() && serialPort != null && serialPort.isOpen();
     }
 
+    public void setReadingEnabled(boolean enabled) {
+        readingEnabled.set(enabled);
+        if (!enabled) {
+            synchronized (rxBuffer) {
+                rxBuffer.reset();
+            }
+        }
+    }
+
+    public boolean isReadingEnabled() {
+        return readingEnabled.get();
+    }
+
     /**
      * 获取当前串口配置(仅当 open 成功后有效)
      */

+ 1 - 1
src/resources/config/config.properties

@@ -1,6 +1,6 @@
 #\u5DE5\u4F4D\u53F7
 #XT OP090 OP100 OP110 OP120 OP160 OP180 OP190 OP250 OP260 OP280 OP360 OP370 OP390 OP400
-mes.gw=OP480
+mes.gw=OP460
 #\u670D\u52A1\u5668IP\u5730\u5740
 #mes.server_ip=127.0.0.1
 mes.server_ip=192.168.14.99