sunxianglong hai 5 días
pai
achega
c8183a15e6

+ 1 - 0
.classpath

@@ -10,6 +10,7 @@
 	<classpathentry kind="src" path="src/resources"/>
 	<classpathentry kind="output" path="bin"/>
 	<classpathentry kind="lib" path="lib/s7connector-2.1.jar"/>
+	<classpathentry kind="lib" path="lib/iot-communication-1.4.4.jar"/>
 	<classpathentry kind="lib" path="lib/slf4j-api-2.0.6.jar"/>
 	<classpathentry kind="lib" path="lib/okhttp-4.10.0.jar"/>
 	<classpathentry kind="lib" path="lib/kotlin-stdlib-1.8.10.jar"/>

+ 33 - 0
src/com/mes/ui/DataUtil.java

@@ -67,6 +67,39 @@ public class DataUtil {
         }
     }
 
+    /**
+     * HTTP 质量校验:/mesProductRecord/pccheck
+     * @return 接口 JSON;result=true 表示通过;失败返回 null 或 result!=true
+     */
+    public static JSONObject checkQualityHttp(String sn, String userCode) {
+        try {
+            String enconding = "UTF-8";
+            InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
+            Properties pro = new Properties();
+            BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
+            pro.load(br);
+            String mes_server_ip = pro.getProperty("mes.server_ip");
+            String oprno = pro.getProperty("mes.gw").trim();
+            String lineSn = pro.getProperty("mes.line_sn").trim();
+            String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/pccheck";
+            String params = "__ajax=json&oprno=" + oprno
+                    + "&lineSn=" + lineSn
+                    + "&sn=" + sn.trim()
+                    + "&userCode=" + (userCode == null ? "" : userCode.trim())
+                    + "&__sid=" + MesClient.sessionid;
+            System.out.println("checkQualityHttp params=" + params);
+            String result = doPost(url, params);
+            System.out.println("checkQualityHttp result=" + result);
+            if (result == null || result.equalsIgnoreCase("false")) {
+                return null;
+            }
+            return JSONObject.parseObject(result);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
     public static Boolean startWork(NettyClient nettyClient,String sn,String user){
         try{
             String msgType = "MKSW";

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

@@ -71,6 +71,13 @@ public class MesClient extends JFrame {
     public static JTextField product_sn;
     public static JButton f_scan_data_bt_1;
 
+    public static JLabel wire_speed_label;
+    public static JLabel current_label;
+    public static JLabel voltage_label;
+    public static JLabel wire_speed_value;
+    public static JLabel current_value;
+    public static JLabel voltage_value;
+
     public static String user20 = "";
 
     public static JFrame welcomeWin;
@@ -89,6 +96,10 @@ public class MesClient extends JFrame {
 
     public static SerialPort serialPort; // 串口对象
 
+    public static String mes_plc_ip = "192.168.6.40";
+    public static Timer plcMonitorTimer;
+    public static boolean plc_finish_handled = false;
+
     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             @Override
@@ -110,6 +121,8 @@ public class MesClient extends JFrame {
 
                     serialPort = ModbusRtu.connect();
 
+                    S7Util.init(mes_plc_ip);
+
                     startJsTimer();
 
                 }catch (Exception e){
@@ -133,10 +146,13 @@ public class MesClient extends JFrame {
         mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port"));
         mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
         mes_line_sn = pro.getProperty("mes.line_sn");
+        if (pro.getProperty("mes.plc_ip") != null) {
+            mes_plc_ip = pro.getProperty("mes.plc_ip").trim();
+        }
 
         mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
 
-        System.out.println(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
+        System.out.println(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle + ";" + mes_plc_ip);
     }
 
     // 初始化TCP
@@ -296,26 +312,23 @@ public class MesClient extends JFrame {
     }
 
     public static void resetScanA() {
+        stopPlcMonitor();
+        S7Util.setQualityOk(false);
+        plc_finish_handled = false;
+
         work_status = 0;
         check_quality_result = false;
-//		MesClient.start_work_bt.setVisible(false);
-//        MesClient.finish_ng_bt.setVisible(false);
-//        MesClient.finish_ok_bt.setVisible(false);
         MesClient.finish_ok_bt.setEnabled(false);
         MesClient.finish_ng_bt.setEnabled(false);
         product_sn.setText("");
+        clearProcessDataUI();
 
         MesClient.f_scan_data_bt_1.setEnabled(true);
-//		DataUtil.stopWork(sessionid);
         MesClient.status_menu.setText("请扫工件码");
-//		product_result_text.setText("");
-//		work_status_text.setText("");
-//        MesClient.setMenuStatus("上件牙套数据个数不符合要求",-1);
 
         MesClient.checkState = false;
         MesClient.jsFlag = 0;
         MesClient.jsCount = 0;
-//        MesClient.formatDeviceState();
         ModbusRtu.closeDevice(serialPort);
         updateMaterailData();
     }
@@ -366,40 +379,134 @@ public class MesClient extends JFrame {
                 break;
         }
 
-        //弹窗扫工件码
-        String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
+        final String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
         if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
-            //获取用户
             getUser();
-            //获取扫码内容36位
-            String barcode36 = getBarcode(scanBarcode);//处理36为码
-            //工位号
-            String gw = "";
             switch(scan_type) {
                 case 1:
                     product_sn.setText(scanBarcode);
                     break;
             }
-            //刷新界面
             mesClientFrame.repaint();
 
-            if(!tcp_connect_flag) {
-                JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
-                return;
-            }
-
-            // 查询工件质量
-            Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
-            if(!sendret){
-                JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
-                return;
-            }
+            setMenuStatus("正在质量校验...", 0);
+            f_scan_data_bt_1.setEnabled(false);
+
+            new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    final JSONObject retObj = DataUtil.checkQualityHttp(scanBarcode, user_menu.getText());
+                    SwingUtilities.invokeLater(new Runnable() {
+                        @Override
+                        public void run() {
+                            boolean ok = false;
+                            String message = "质量校验失败";
+                            if (retObj != null) {
+                                Object result = retObj.get("result");
+                                if (result != null && ("true".equalsIgnoreCase(String.valueOf(result)) || Boolean.TRUE.equals(result))) {
+                                    ok = true;
+                                }
+                                if (retObj.getString("message") != null && !retObj.getString("message").isEmpty()) {
+                                    message = retObj.getString("message");
+                                }
+                            }
+                            if (ok) {
+                                onQualityCheckPassed();
+                            } else {
+                                f_scan_data_bt_1.setEnabled(true);
+                                setMenuStatus(message, -1);
+                                JOptionPane.showMessageDialog(mesClientFrame, message, "提示窗口", JOptionPane.INFORMATION_MESSAGE);
+                            }
+                        }
+                    });
+                }
+            }).start();
         }else {
             JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE);
             return;
         }
     }
 
+    /** 质量校验通过:置位 DB17.8.0,启动 PLC 监控 */
+    public static void onQualityCheckPassed() {
+        check_quality_result = true;
+        work_status = 1;
+        plc_finish_handled = false;
+        finish_ok_bt.setEnabled(false);
+        finish_ng_bt.setEnabled(false);
+        f_scan_data_bt_1.setEnabled(false);
+        clearProcessDataUI();
+
+        S7Util.setQualityOk(true);
+        setMenuStatus("质量校验通过,等待设备焊接", 0);
+        startPlcMonitor();
+    }
+
+    public static void startPlcMonitor() {
+        stopPlcMonitor();
+        plcMonitorTimer = new Timer();
+        plcMonitorTimer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                try {
+                    if (work_status != 1) {
+                        return;
+                    }
+                    // 焊接中:每秒读取送丝速度/电流/电压
+                    if (S7Util.isWelding()) {
+                        final int wireSpeed = S7Util.readWireSpeed();
+                        final int current = S7Util.readCurrent();
+                        final int voltage = S7Util.readVoltage();
+                        SwingUtilities.invokeLater(new Runnable() {
+                            @Override
+                            public void run() {
+                                updateProcessDataUI(wireSpeed, current, voltage);
+                                setMenuStatus("设备焊接中...", 0);
+                            }
+                        });
+                    }
+                    // 完成信号:点亮 OK/NG
+                    if (!plc_finish_handled && S7Util.isFinish()) {
+                        plc_finish_handled = true;
+                        SwingUtilities.invokeLater(new Runnable() {
+                            @Override
+                            public void run() {
+                                finish_ok_bt.setEnabled(true);
+                                finish_ng_bt.setEnabled(true);
+                                setMenuStatus("设备加工完成,请提交结果", 0);
+                            }
+                        });
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }, 1000, 1000);
+    }
+
+    public static void stopPlcMonitor() {
+        if (plcMonitorTimer != null) {
+            plcMonitorTimer.cancel();
+            plcMonitorTimer = null;
+        }
+    }
+
+    public static void updateProcessDataUI(int wireSpeed, int current, int voltage) {
+        if (wire_speed_value != null) {
+            wire_speed_value.setText(String.valueOf(wireSpeed));
+        }
+        if (current_value != null) {
+            current_value.setText(String.valueOf(current));
+        }
+        if (voltage_value != null) {
+            voltage_value.setText(String.valueOf(voltage));
+        }
+    }
+
+    public static void clearProcessDataUI() {
+        updateProcessDataUI(0, 0, 0);
+    }
+
     public static void logoff() {
 //        welcomeWin.setVisible(true);
         mesClientFrame.setVisible(false);
@@ -574,12 +681,32 @@ public class MesClient extends JFrame {
         f_scan_data_bt_1.setBounds(693, 70, 198, 70);
         indexPanelA.add(f_scan_data_bt_1);
 
-//        String[] hjtitles = new String[]{"焊机1","焊机2","焊机3"};
-//        String[] hjvals = new String[]{"HJ001","HJ002","HJ003"};
-//        mesRadioHj = new MesRadio(hjtitles,hjvals);
-//        mesRadioHj.setSize(500,50);
-//        mesRadioHj.setBounds(190,170,500,50);
-//        indexPanelA.add(mesRadioHj);
+        wire_speed_label = new JLabel("送丝速度:");
+        wire_speed_label.setFont(new Font("微软雅黑", Font.PLAIN, 22));
+        wire_speed_label.setBounds(81, 160, 130, 40);
+        indexPanelA.add(wire_speed_label);
+        wire_speed_value = new JLabel("0");
+        wire_speed_value.setFont(new Font("微软雅黑", Font.BOLD, 22));
+        wire_speed_value.setBounds(210, 160, 120, 40);
+        indexPanelA.add(wire_speed_value);
+
+        current_label = new JLabel("电流:");
+        current_label.setFont(new Font("微软雅黑", Font.PLAIN, 22));
+        current_label.setBounds(360, 160, 80, 40);
+        indexPanelA.add(current_label);
+        current_value = new JLabel("0");
+        current_value.setFont(new Font("微软雅黑", Font.BOLD, 22));
+        current_value.setBounds(440, 160, 120, 40);
+        indexPanelA.add(current_value);
+
+        voltage_label = new JLabel("电压:");
+        voltage_label.setFont(new Font("微软雅黑", Font.PLAIN, 22));
+        voltage_label.setBounds(590, 160, 80, 40);
+        indexPanelA.add(voltage_label);
+        voltage_value = new JLabel("0");
+        voltage_value.setFont(new Font("微软雅黑", Font.BOLD, 22));
+        voltage_value.setBounds(670, 160, 120, 40);
+        indexPanelA.add(voltage_value);
 
         finish_ok_bt = new JButton("OK");
         finish_ok_bt.setEnabled(false);

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

@@ -59,15 +59,9 @@ public class MesRevice {
 
                 MesClient.f_scan_data_bt_1.setEnabled(false);
 
-//                String sn = MesClient.getBarcode(MesClient.product_sn.getText());
-//                MesClient.getUser();
-//                DataUtil.bindUser(MesClient.nettyClient,sn,MesClient.user20);
-
-                MesClient.finish_ok_bt.setEnabled(true);
-                MesClient.finish_ng_bt.setEnabled(true);
-
-//                MesClient.checkState = true;
-//                MesClient.formatDeviceState();
+                // OK/NG 由 DB17.0.0 完成信号点亮,此处保持灰色
+                MesClient.finish_ok_bt.setEnabled(false);
+                MesClient.finish_ng_bt.setEnabled(false);
 
                 MesClient.jsFlag = 0;
                 ModbusRtu.openDevice(MesClient.serialPort);

+ 80 - 0
src/com/mes/ui/S7Util.java

@@ -0,0 +1,80 @@
+package com.mes.ui;
+
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
+import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
+
+/**
+ * CMT 焊机 DB17 点位读写
+ */
+public class S7Util {
+
+    /** 完成信号 */
+    public static final String ADDR_FINISH = "DB17.0.0";
+    /** 送丝速度 uint */
+    public static final String ADDR_WIRE_SPEED = "DB17.2.0";
+    /** 电流 uint */
+    public static final String ADDR_CURRENT = "DB17.4.0";
+    /** 电压 uint */
+    public static final String ADDR_VOLTAGE = "DB17.6.0";
+    /** 质量校验通过后置位,允许设备加工 */
+    public static final String ADDR_QUALITY_OK = "DB17.8.0";
+    /** 焊接中信号,为 true 时周期性读取过程数据 */
+    public static final String ADDR_WELDING = "DB17.10.0";
+
+    public static S7PLC s7PLC;
+
+    public static void init(String plcIp) {
+        s7PLC = new S7PLC(EPlcType.S200_SMART, plcIp);
+    }
+
+    public static boolean readBoolean(String address) {
+        try {
+            return s7PLC.readBoolean(address);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    public static void writeBoolean(String address, boolean value) {
+        try {
+            s7PLC.writeBoolean(address, value);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /** 读取无符号 16 位整数(uint / Word) */
+    public static int readUInt(String address) {
+        try {
+            return Short.toUnsignedInt(s7PLC.readInt16(address));
+        } catch (Exception e) {
+            e.printStackTrace();
+            return 0;
+        }
+    }
+
+    public static void setQualityOk(boolean value) {
+        writeBoolean(ADDR_QUALITY_OK, value);
+    }
+
+    public static boolean isWelding() {
+        return readBoolean(ADDR_WELDING);
+    }
+
+    public static boolean isFinish() {
+        return readBoolean(ADDR_FINISH);
+    }
+
+    public static int readWireSpeed() {
+        return readUInt(ADDR_WIRE_SPEED);
+    }
+
+    public static int readCurrent() {
+        return readUInt(ADDR_CURRENT);
+    }
+
+    public static int readVoltage() {
+        return readUInt(ADDR_VOLTAGE);
+    }
+}

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

@@ -1,6 +1,7 @@
-mes.gw=OP240
+mes.gw=OP120
 #mes.server_ip=127.0.0.1
 mes.server_ip=10.0.20.10
 mes.tcp_port=3000
 mes.heart_beat_cycle=60
 mes.line_sn=XT
+mes.plc_ip=192.168.0.1