Forráskód Böngészése

转速进给以及扫工装二维码

jingbo 12 órája
szülő
commit
4c8c75ff1a

+ 11 - 0
.project

@@ -14,4 +14,15 @@
 	<natures>
 		<nature>org.eclipse.jdt.core.javanature</nature>
 	</natures>
+	<filteredResources>
+		<filter>
+			<id>1776748191027</id>
+			<name></name>
+			<type>30</type>
+			<matcher>
+				<id>org.eclipse.core.resources.regexFilterMatcher</id>
+				<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
+			</matcher>
+		</filter>
+	</filteredResources>
 </projectDescription>

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

@@ -304,4 +304,29 @@ public class DataUtil {
         }
         return result;
     }
+
+    // 上传设备参数到服务器
+    public static JSONObject upDeviceParams(String upparams) {
+        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 url = "http://"+mes_server_ip+":8980/js/a/mes/mesDeviceParam/batchsave";
+            String params = "__ajax=json&params="+upparams;
+            System.out.println("params="+params);
+            String result = doPost(url,params);
+            System.out.println("result="+result);
+
+            if(result.equalsIgnoreCase("false")) {
+                return null;
+            }else {
+                return JSONObject.parseObject(result);
+            }
+        }catch (Exception e){
+            return null;
+        }
+    }
 }

+ 4 - 0
src/com/mes/ui/LoginFarme.java

@@ -176,6 +176,10 @@ public class LoginFarme extends JFrame {
                     MesClient.initTcpConnection();
                     //启动timer心跳包
                     MesClient.startHeartBeatTimer();
+                    // 启动设备参数读取定时器
+                    MesClient.startDeviceParamTimer();
+                    // 启动设备参数上传定时器
+//                    MesClient.startUpDeviceParamsTimer();
 
                     //1操作工人,2管理员
                     //登录成功

+ 111 - 2
src/com/mes/ui/MesClient.java

@@ -1,5 +1,6 @@
 package com.mes.ui;
 
+import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import com.github.s7connector.api.S7Connector;
 import com.github.s7connector.api.S7Serializer;
@@ -35,6 +36,8 @@ import java.sql.SQLException;
 import java.sql.Statement;
 import java.time.LocalDateTime;
 import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
 import java.util.Properties;
 import java.util.Timer;
 import java.util.TimerTask;
@@ -100,9 +103,9 @@ public class MesClient extends JFrame {
 
     public static JLabel fxlabel;
 
-    public static String plcUrl = "192.168.88.89"; // 180AF
+//    public static String plcUrl = "192.168.88.89"; // 180AF
 
-//    public static String plcUrl = "192.168.88.99"; // 180BE  280AB
+    public static String plcUrl = "192.168.88.99"; // 180BE  280AB
 
     public static S7PLC s7PLC = new S7PLC(EPlcType.SINUMERIK_828D, plcUrl);
 
@@ -135,6 +138,18 @@ public class MesClient extends JFrame {
 
     public static String programNo = "";
 
+
+    // 设备实时参数
+    public static double actSpindleSpeed = 0;  // 主轴转速
+    public static double feedRate = 0;         // 进给速度
+
+    // 数据缓存
+    public static List<String> deviceParams = new ArrayList<>();
+
+    // 显示组件
+    public static JLabel spindleSpeedLabel;
+    public static JLabel feedRateLabel;
+
     public static void main(String[] args) {
         if (LockUtil.getInstance().isAppActive() == true){
 //            JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
@@ -280,6 +295,81 @@ public class MesClient extends JFrame {
         }, 100,mes_heart_icon_cycle*1000);
     }
 
+
+    // 设备参数读取定时器
+    public static java.util.Timer deviceParamTimer;
+
+    public static void startDeviceParamTimer() {
+        if(deviceParamTimer != null) {
+            deviceParamTimer.cancel();
+        }
+        deviceParamTimer = new java.util.Timer();
+        deviceParamTimer.schedule(new TimerTask() {
+            public void run() {
+                try {
+                    if (work_status == 1){
+                        // 读取转速和进给
+                        actSpindleSpeed = S7Util.readActSpindleSpeed(s7PLC);
+                        feedRate = S7Util.readFeedRate(s7PLC);
+                        // 更新UI
+                        final double spindle = actSpindleSpeed;
+                        final double feed = feedRate;
+                        SwingUtilities.invokeLater(() -> {
+                            spindleSpeedLabel.setText(String.format("转速: %.1f rpm", spindle));
+                            feedRateLabel.setText(String.format("进给: %.1f mm/min", feed));
+                        });
+                        // 存储数据
+                        final String currentSn = product_sn.getText();
+                        if(currentSn != null && !currentSn.isEmpty()) {
+                            String record_time = DateLocalUtils.getCurrentTime();
+                            deviceParams.add(actSpindleSpeed + "|" + feedRate + "|" + record_time);
+                            if(deviceParams.size() == 60) {
+                                try {
+                                    JdbcUtils.insertDeviceParamData(mes_gw, mes_line_sn, currentSn, JSON.toJSONString(deviceParams));
+                                    log.info("设备参数数据已存储: " + currentSn);
+                                    deviceParams = new ArrayList<>();
+                                }catch (Exception e) {
+                                    log.info("存储设备参数异常: " + e.getMessage());
+                                }
+                            }
+                        }
+                    }
+                } catch (Exception e) {
+                    log.info("读取设备参数异常: " + e.getMessage());
+                }
+            }
+        }, 100, 500);  // 首次延迟100ms,之后每500ms执行
+    }
+
+    // 设备参数上传定时器
+    public static java.util.Timer upDeviceParamsTimer;
+
+    public static void startUpDeviceParamsTimer() {
+        if(upDeviceParamsTimer != null) {
+            upDeviceParamsTimer.cancel();
+        }
+        upDeviceParamsTimer = new java.util.Timer();
+        upDeviceParamsTimer.schedule(new TimerTask() {
+            public void run() {
+                try{
+                    List<Map<String, Object>> prods = JdbcUtils.getDeviceParams();
+                    System.out.println("prods:"+ JSON.toJSONString(prods));
+                    if(prods.size() > 0){
+                        JSONObject retObj = DataUtil.upDeviceParams(JSON.toJSONString(prods));
+                        if(retObj != null && retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")) {
+                            for(Map<String, Object> prodReq : prods) {
+                                JdbcUtils.updateDeviceParamSync(Integer.valueOf(prodReq.get("id").toString()), 1);
+                            }
+                            log.info("设备参数数据上传成功,数量: " + prods.size());
+                        }
+                    }
+                }catch (Exception e){
+                    log.info("上传设备参数异常: " + e.getMessage());
+                }
+            }
+        }, 1000, 30*1000);  // 首次延迟1秒,之后每30秒执行
+    }
+
     //设置tcp连接状态显示
     public static void setTcpStatus() {
         if(tcp_connect_flag) {
@@ -404,6 +494,13 @@ public class MesClient extends JFrame {
 //                JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
                 return;
             }
+            //弹框扫码,如果和规定的工装编号一致才可以继续
+            String scanBarcode1 = JOptionPane.showInputDialog(null, "请扫工装二维码");
+            if (!scanBarcode1.equalsIgnoreCase("OP280A")){
+                //菜单栏给出提示信息
+                MesClient.setMenuStatus("请扫正确的工装二维码",-1);
+                return;
+            }
 
             // 查询工件质量
             Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
@@ -722,6 +819,18 @@ public class MesClient extends JFrame {
         searchScrollPaneDj = new JScrollPane(indexPanelC);
         indexPanelC.setLayout(null);
 
+        // 转速显示
+        spindleSpeedLabel = new JLabel("转速: 0 rpm");
+        spindleSpeedLabel.setBounds(81, 240, 400, 40);
+        spindleSpeedLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
+        indexPanelA.add(spindleSpeedLabel);
+
+        // 进给显示
+        feedRateLabel = new JLabel("进给: 0 mm/min");
+        feedRateLabel.setBounds(500, 240, 400, 40);
+        feedRateLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
+        indexPanelA.add(feedRateLabel);
+
         tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
 
 

+ 48 - 1
src/com/mes/ui/S7Util.java

@@ -10,7 +10,7 @@ import java.util.Map;
 
 public class S7Util {
 
-    public static Integer db = 9015; // FSW=9014 CNC=9015
+    public static Integer db = 9014; // FSW=9014 CNC=9015
     // 监听状态
     public static void getDeviceState(){
         try {
@@ -200,6 +200,9 @@ public class S7Util {
         return false;
     }
 
+    //XY数据
+
+
 
     // 获取报警状态(是否报警)  DB9015.DBX0.2   =1 报警,true   =0 没有报警,false
     public static Boolean getDeviceAlarm() {
@@ -374,4 +377,48 @@ public class S7Util {
         return programName;
     }
 
+
+    // 读取主轴转速 - 传入s7PLC参数
+    public static double readActSpindleSpeed(S7PLC s7PLC){
+        try {
+            return s7PLC.readActSpindleSpeed();
+        } catch (Exception e) {
+            return 0;
+        }
+    }
+
+    // 读取进给速度 - DB9050.DBD32 (4字节浮点数)
+    public static double readFeedRate(S7PLC s7PLC){
+        try {
+            MesClient.initS7();
+            byte[] data = MesClient.s7Connector.read(DaveArea.DB, 9050, 4, 32);
+            return formatS7Float(data);
+        } catch (Exception e) {
+            return 0;
+        }
+    }
+
+    // 读取X位移 - DB5700.DBD0 (4字节浮点数)
+    public static double readXPosition(S7PLC s7PLC){
+        try {
+            MesClient.initS7();
+            byte[] data = MesClient.s7Connector.read(DaveArea.DB, 5700, 4, 32);
+            return formatS7Float(data);
+        } catch (Exception e) {
+            return 0;
+        }
+    }
+
+    // 读取Y位移 - DB5701.DBD0 (4字节浮点数)
+    public static double readYPosition(S7PLC s7PLC){
+        try {
+            MesClient.initS7();
+            byte[] data = MesClient.s7Connector.read(DaveArea.DB, 5701, 4, 32);
+            return formatS7Float(data);
+        } catch (Exception e) {
+            return 0;
+        }
+    }
+
+
 }

+ 78 - 0
src/com/mes/util/JdbcUtils.java

@@ -5,8 +5,13 @@ import org.slf4j.LoggerFactory;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 public class JdbcUtils {
 	public static final Logger log =  LoggerFactory.getLogger(JdbcUtils.class);
@@ -77,6 +82,18 @@ public class JdbcUtils {
 				")";
 		statement.executeUpdate(submitRecord);
 
+		// 创建设备参数表
+		String deviceParamTable = "CREATE TABLE if not exists bw_device_param(\n" +
+				"   id INTEGER PRIMARY KEY AUTOINCREMENT, -- 自增ID\n" +
+				"   oprno VARCHAR(20),                      -- 工位号 \n" +
+				"   line_sn VARCHAR(20),                    -- 产线编号\n" +
+				"   sn VARCHAR(48),                        -- 产品序列号\n" +
+				"   content VARCHAR(5000),                 -- 存储内容(60条记录的JSON)\n" +
+				"   create_time DATETIME,                  -- 创建时间\n" +
+				"   sync INTEGER DEFAULT 0                 -- 同步状态(0=未同步,1=已同步)\n" +
+				")";
+		statement.executeUpdate(deviceParamTable);
+
         statement.close();
     }
     
@@ -127,6 +144,67 @@ public class JdbcUtils {
 		return ret;
 	}
 
+	// 向 bw_device_param表 插入设备参数数据
+	public static boolean insertDeviceParamData(String oprno, String line_sn, String sn, String content){
+		boolean ret = false;
+		String record_time = DateLocalUtils.getCurrentTime();
+		try {
+			// 确保连接已经打开
+			if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
+				JdbcUtils.openConnection();
+			}
+			Statement statement=conn.createStatement();
+			String insertSQL = "INSERT INTO bw_device_param (oprno, line_sn, sn, content, create_time, sync)" +
+					"VALUES('" + oprno + "', '" + line_sn + "', '" + sn + "', '" + content + "', '" + record_time + "', 0)";
+			statement.executeUpdate(insertSQL);
+			statement.close();
+			ret = true;
+			log.info("向bw_device_param表插入数据成功");
+		} catch (SQLException e) {
+			ret = false;
+			log.info("向bw_device_param表插入数据失败: " + e.getMessage());
+		}
+		return ret;
+	}
+
+	// 查询未同步的设备参数数据
+	public static List<Map<String, Object>> getDeviceParams() {
+		String sql = "select id,oprno,line_sn,sn,content,sync from bw_device_param where sync = 0 order by id asc limit 100";
+		List<Map<String, Object>> prods = new ArrayList<>();
+		try {
+			if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
+				JdbcUtils.openConnection();
+			}
+			Statement statement = conn.createStatement();
+			ResultSet ret = statement.executeQuery(sql);
+			while (ret.next()) {
+				Map<String, Object> map = new HashMap<>();
+				map.put("id", ret.getInt(1));
+				map.put("oprno", ret.getString(2));
+				map.put("lineSn", ret.getString(3));
+				map.put("sn", ret.getString(4));
+				map.put("content", ret.getString(5));
+				map.put("sync", ret.getInt(6));
+				prods.add(map);
+			}
+			ret.close();
+			statement.close();
+		} catch (SQLException e) {
+			log.info("查询未同步设备参数数据失败: " + e.getMessage());
+		}
+		return prods;
+	}
+
+	// 更新设备参数同步状态
+	public static void updateDeviceParamSync(Integer id, Integer sync) throws SQLException {
+		if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
+			JdbcUtils.openConnection();
+		}
+		Statement statement = conn.createStatement();
+		statement.executeUpdate("update bw_device_param set sync = " + sync + " where id = " + id);
+		statement.close();
+	}
+
     
     public static void close(){
         try {

+ 18 - 7
src/com/mes/util/Test.java

@@ -1,24 +1,35 @@
 package com.mes.util;
 
 import com.github.s7connector.api.DaveArea;
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
+import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
 import com.mes.ui.MesClient;
+import com.mes.ui.S7Util;
 
 import java.math.BigInteger;
 import java.util.HashMap;
 import java.util.Map;
 
 public class Test {
+
+    public static S7PLC s7PLC = new S7PLC(EPlcType.SINUMERIK_828D, "192.168.88.99");
     public static void main(String[] args) {
 //        Integer switchEnable = getSwitchEnable();
 //        System.out.println(switchEnable);
-        Map<String, Boolean> stringBooleanMap = readBits();
-        System.out.println(stringBooleanMap);
+//        Map<String, Boolean> stringBooleanMap = readBits();
+//        System.out.println(stringBooleanMap);
 //        writeBit(9015, 0, 1, true);
-        writeBit(9015, 0, 0, false);
-        writeBit(9015, 0, 1, false);
-        writeBit(9000, 3, 2, false);
-        Map<String, Boolean> stringBooleanMap2 = readBits();
-        System.out.println(stringBooleanMap2);
+//        writeBit(9015, 0, 0, false);
+//        writeBit(9015, 0, 1, false);
+//        writeBit(9000, 3, 2, false);
+//        Map<String, Boolean> stringBooleanMap2 = readBits();
+//        System.out.println(stringBooleanMap2);
+        System.out.println("读取主轴转速:"+S7Util.readActSpindleSpeed(s7PLC));
+        System.out.println("读取进给速度:"+S7Util.readFeedRate(s7PLC));
+        System.out.println("读取X位移:"+S7Util.readXPosition(s7PLC));
+        System.out.println("读取y位移:"+S7Util.readYPosition(s7PLC));
+
+
     }
 
     public static Map<String, Boolean> readBits() {

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

@@ -1,4 +1,4 @@
-mes.gw=OP180F
+mes.gw=OP280B
 #mes.server_ip=127.0.0.1
 mes.server_ip=192.168.18.99
 mes.tcp_port=3000