package com.mes.ui; import com.github.s7connector.api.DaveArea; import com.github.xingshuangs.iot.protocol.s7.service.S7PLC; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.HashMap; import java.util.Map; public class S7Util { public static Integer db = 9014; // FSW=9014 CNC=9015 // 监听状态 public static void getDeviceState(){ try { Boolean deviceStatus = getDeviceStatus(); if(MesClient.tjFlag == 1) { MesClient.tjTime++; if (!deviceStatus) { MesClient.tjFlag = 2; MesClient.status_menu.setText(MesClient.tjFlagText2); } if (deviceStatus || MesClient.tjTime >= 120) { // 120秒内去除MES允许 sendStartSignal(false); } }else if(MesClient.tjFlag == 2){ MesClient.tjTime++; if(deviceStatus || MesClient.tjTime >= 380){ MesClient.tjFlag = 3; MesClient.status_menu.setText(MesClient.tjFlagText3); } }else if(MesClient.tjFlag == 3) { DataUtil.sendQuality(MesClient.nettyClient,MesClient.product_sn.getText(),"OK",MesClient.user20); if (!deviceStatus) { //禁止启动 sendStartSignal(true); } }else{ //未扫码 如果程序没结束 禁止设备 if(!deviceStatus){ sendStartSignal(true); } } }catch (Exception e){ e.printStackTrace(); } } //屏蔽mes交互 如果设备禁止启动 则启动设备 public static void getDeviceShield(){ try { Boolean deviceStart = getDeviceStart(); if (deviceStart){ sendStartSignal(false); } }catch (Exception e){ e.printStackTrace(); } } // 监听急停 public static void checkStop(){ try { MesClient.initS7(); byte[] bytes = MesClient.s7Connector.read(DaveArea.DB, 9052, 1, 0); // System.out.println("DB9052.DBX0:"+byteToBinaryString(bytes)); int jtstatus = getBit(bytes[0],7); if(jtstatus == 0){ switchEnable(0); } if(MesClient.tjFlag == 2 && jtstatus == 0){ MesClient.tjResult = "NG"; MesClient.finish_ok_bt.setEnabled(true); MesClient.finish_ng_bt.setEnabled(true); } }catch (Exception e){ e.printStackTrace(); } } public static int getBit(byte b, int n) { // 将byte转换为二进制字符串 String binaryString = Integer.toBinaryString(b & 0xFF); // 补足位数到8位 binaryString = String.format("%8s", binaryString).replace(' ', '0'); // 获取第n位的字符,然后转换为整数 return binaryString.charAt(n) == '1' ? 1 : 0; } private static String byteToBinaryString(byte[] byteArray) { StringBuilder binaryString = new StringBuilder(); for (byte b : byteArray) { binaryString.append(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0')); } return binaryString.toString(); } // 转大端排序 private static byte[] xtob( byte[] bytes){ // 创建新的byte数组并复制原始数据 byte[] reversedBytes = new byte[bytes.length]; System.arraycopy(bytes, 0, reversedBytes, 0, bytes.length); // 反转byte数组元素的位置 for (int i = 0; i < bytes.length / 2; i++) { byte temp = reversedBytes[i]; reversedBytes[i] = reversedBytes[reversedBytes.length - 1 - i]; reversedBytes[reversedBytes.length - 1 - i] = temp; } return reversedBytes; } // 格式化s7取出的值 private static String formatS7Val(byte[] val){ int intValue = new BigInteger(xtob(val)).intValue(); float floatValue = Float.intBitsToFloat(intValue); // 保留1位小数 DecimalFormat df = new DecimalFormat("#.#"); df.setMaximumFractionDigits(1); String result = df.format(floatValue); float roundedNum = Float.parseFloat(result); return roundedNum+""; } private static Integer formatS7Int(byte[] val){ return new BigInteger(xtob(val)).intValue(); } private static Float formatS7Float(byte[] val){ int intValue = new BigInteger(xtob(val)).intValue(); float floatValue = Float.intBitsToFloat(intValue); // 保留1位小数 DecimalFormat df = new DecimalFormat("#.#"); df.setMaximumFractionDigits(1); String result = df.format(floatValue); float roundedNum = Float.parseFloat(result); return roundedNum; } // 禁止/启用启动 public static Boolean switchEnable(int state){ try{ MesClient.initS7(); byte[] bytes2 = new byte[1]; if(state == 1){ //启用启动 bytes2[0] = 0x01; }else{ // 禁止启动 bytes2[0] = 0x00; } MesClient.s7Connector.write(DaveArea.DB,9052,0,bytes2); return true; }catch (Exception e){ e.printStackTrace(); return false; } } // 获取允许运行状态 public static Integer getSwitchEnable(){ try{ MesClient.initS7(); //DB9052.DBB0 Integer ss = new BigInteger(xtob(MesClient.s7Connector.read(DaveArea.DB, 9052, 1, 0))).intValue(); return ss; }catch (Exception e){ e.printStackTrace(); return 0; } } // 获取设备运行状态(是否结束) DB9015.DBX0.0 =1 运行结束,true =0 运行中,false public static Boolean getDeviceStatus() { try { MesClient.initS7(); // 读取 DB9015.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始 byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0); byte b9015 = data9015[0]; Boolean result = (b9015 & (1 << 0)) != 0; return result; } catch (Exception e) { e.printStackTrace(); } return false; } //XY数据 // 获取报警状态(是否报警) DB9015.DBX0.2 =1 报警,true =0 没有报警,false public static Boolean getDeviceAlarm() { try { MesClient.initS7(); // 读取 DB9015.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始 byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0); byte b9015 = data9015[0]; Boolean result = (b9015 & (1 << 2)) != 0; return result; } catch (Exception e) { e.printStackTrace(); } return false; } // 获取设备允许启动信号 DB9015.DBX0.1 =0 允许启动,true =1 禁止启动,false public static Boolean getDeviceStart() { try { MesClient.initS7(); // 读取 DB9015.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始 byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0); byte b9015 = data9015[0]; Boolean result = (b9015 & (1 << 1)) != 0; return result; } catch (Exception e) { e.printStackTrace(); } return false; } // 获取设备允许启动信号 DB9000.DBX3.2 =0 MES启动,true =1 禁止启动,false public static Boolean getMesStatus() { try { MesClient.initS7(); byte[] data9000 = MesClient.s7Connector.read(DaveArea.DB, 9000, 1, 3); byte b9000 = data9000[0]; Boolean result = (b9000 & (1 << 2)) != 0; return result; } catch (Exception e) { e.printStackTrace(); } return false; } // 发送设备允许启动信号DB9000.DBX3.2 =0 MES开启,true =1 MES屏蔽,false public static boolean sendMesStatus(boolean value){ try { MesClient.initS7(); byte[] data = MesClient.s7Connector.read(DaveArea.DB, 9000, 1, 3); byte b = data[0]; String binary = String.format("%8s", Integer.toBinaryString(b & 0xFF)) .replace(' ', '0'); // 输出"11110110" System.out.println("s:"+binary); int s = getBit(b,2); int ss = getBit(b,4); System.out.println("s:"+s); System.out.println("ss:"+ss); if (value) { b = (byte)(b | (1 << 2)); // 置1 } else { b = (byte)(b & ~(1 << 2)); // 清0 } // 写回去 byte[] toWrite = new byte[]{b}; byte c = toWrite[0]; String binary2 = String.format("%8s", Integer.toBinaryString(c & 0xFF)) .replace(' ', '0'); // 输出"11110110" System.out.println("s:"+binary2); MesClient.s7Connector.write(DaveArea.DB, 9000, 3, toWrite); return true; } catch (Exception e) { e.printStackTrace(); } return false; } //读取 测试 public static Map readBits() { Map result = new HashMap<>(); try { MesClient.initS7(); // 读取 DB9015.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始 byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0); byte b9015 = data9015[0]; result.put("DB9015.DBX0.0", (b9015 & (1 << 0)) != 0); result.put("DB9015.DBX0.1", (b9015 & (1 << 1)) != 0); result.put("DB9015.DBX0.2", (b9015 & (1 << 2)) != 0); // 读取 DB9000.DBB3(1字节,包含 DBX3.0~DBX3.7) // byte[] data9000 = MesClient.s7Connector.read(DaveArea.DB, 9000, 1, 3); // byte b9000 = data9000[0]; // // result.put("DB9000.DBX3.2", (b9000 & (1 << 2)) != 0); } catch (Exception e) { e.printStackTrace(); } return result; } // 发送设备允许启动信号 DB9015.DBX0.1 =0 允许启动,true =1 禁止启动,false public static boolean sendStartSignal(boolean value){ try { MesClient.initS7(); byte[] data = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 0); byte b = data[0]; System.out.println(b); if (value) { b = (byte)(b | (1 << 1)); // 置1 } else { b = (byte)(b & ~(1 << 1)); // 清0 } System.out.println("b:"+b); // b = 1; // 写回去 byte[] toWrite = new byte[]{b}; System.out.println(toWrite.toString()); MesClient.s7Connector.write(DaveArea.DB, S7Util.db, 0, toWrite); return true; } catch (Exception e) { e.printStackTrace(); } return false; } //写入 测试 public static boolean writeBit(int dbNumber, int byteIndex, int bitIndex, boolean value) { try { MesClient.initS7(); // 先读出对应字节 byte[] data = MesClient.s7Connector.read(DaveArea.DB, dbNumber, 1, byteIndex); byte b = data[0]; // 修改指定位 if (value) { b = (byte)(b | (1 << bitIndex)); // 置1 } else { b = (byte)(b & ~(1 << bitIndex)); // 清0 } // 写回去 byte[] toWrite = new byte[]{b}; MesClient.s7Connector.write(DaveArea.DB, dbNumber, byteIndex, toWrite); return true; } catch (Exception e) { e.printStackTrace(); return false; } } // 获取运行的程序名 public static String getProgramName(S7PLC s7PLC){ String programName = ""; try{ programName = s7PLC.readProgramName(); }catch (Exception e){ e.printStackTrace(); programName = ""; } 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; } } // 读取扭矩值 - DB4900.DBD364 (4字节浮点数) public static double readTorque(S7PLC s7PLC){ try { MesClient.initS7(); byte[] data = MesClient.s7Connector.read(DaveArea.DB, 4900, 4, 364); return formatS7Float(data); } catch (Exception e) { return 0; } } // DB9043.DBX120.0 public static Boolean getParamFlag() { try { MesClient.initS7(); // 读取 DB9043.DBB0(1字节,包含 DBX0.0~DBX0.7) 1个字节,从第0个字节开始 byte[] data9015 = MesClient.s7Connector.read(DaveArea.DB, S7Util.db, 1, 120); byte b9015 = data9015[0]; Boolean result = (b9015 & (1 << 0)) != 0; return result; } catch (Exception e) { e.printStackTrace(); } return false; } // 读取X位移 - DB5700.DBD0 (4字节浮点数) public static double readXPosition(){ try { MesClient.initS7(); byte[] data = MesClient.s7Connector.read(DaveArea.DB, 5700, 4, 0); return formatS7Float(data); } catch (Exception e) { return 0; } } // 读取Y位移 - DB5701.DBD0 (4字节浮点数) public static double readYPosition(){ try { MesClient.initS7(); byte[] data = MesClient.s7Connector.read(DaveArea.DB, 5701, 4, 0); return formatS7Float(data); } catch (Exception e) { return 0; } } }