|
|
@@ -0,0 +1,447 @@
|
|
|
+package com.mes.ui;
|
|
|
+
|
|
|
+import com.ghgande.j2mod.modbus.io.ModbusTCPTransaction;
|
|
|
+import com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersRequest;
|
|
|
+import com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersResponse;
|
|
|
+import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
|
|
|
+import com.ghgande.j2mod.modbus.procimg.Register;
|
|
|
+import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
|
|
|
+import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
|
|
|
+import com.mes.util.HcPlcPointUtilA;
|
|
|
+//import com.mes.util.HcPlcPointUtilB; // B枪已停用
|
|
|
+import com.mes.util.JdbcUtils;
|
|
|
+
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+
|
|
|
+public class PlcUtil {
|
|
|
+
|
|
|
+ public static void getDataA(TCPMasterConnection connection){
|
|
|
+ if (!MesClient.bindLbStatus) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 1. 构造读取请求(功能码 03,从 D100 开始读 120 个寄存器,覆盖 D100~D219)
|
|
|
+ ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(100, 120);
|
|
|
+ request.setUnitID(1);
|
|
|
+
|
|
|
+ // 2. 发送请求并获取响应
|
|
|
+ ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection);
|
|
|
+ transaction.setRequest(request);
|
|
|
+ transaction.execute();
|
|
|
+ ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse();
|
|
|
+
|
|
|
+ // 3. 提取数据并映射到 dRegs 数组
|
|
|
+ Register[] registers = response.getRegisters();
|
|
|
+ short[] dRegs = new short[300];
|
|
|
+ for (int i = 0; i < registers.length; i++) {
|
|
|
+ dRegs[100 + i] = registers[i].toShort();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 解析数据
|
|
|
+ HcPlcPointUtilA.PlcMesData mesData = HcPlcPointUtilA.readAllPoint(dRegs);
|
|
|
+
|
|
|
+ MesClient.maxa = mesData.setQty;
|
|
|
+ MesClient.param1.setText(String.valueOf(mesData.setQty));
|
|
|
+ String name = mesData.modelName;
|
|
|
+ MesClient.lblA.setText("拉铆枪A("+name+")");
|
|
|
+
|
|
|
+ if (MesClient.maxa <= 0){
|
|
|
+ MesClient.maxa = mesData.setQty;
|
|
|
+ MesClient.param1.setText(String.valueOf(mesData.setQty));
|
|
|
+ }
|
|
|
+
|
|
|
+ MesClient.param2.setText(String.valueOf(MesClient.sortA));
|
|
|
+ int currentOk = mesData.okQty;
|
|
|
+ if (currentOk == MesClient.sortA + 1){
|
|
|
+ // 更新计数
|
|
|
+ MesClient.sortA = currentOk;
|
|
|
+ MesClient.aFinish = currentOk;
|
|
|
+ MesClient.param2.setText(String.valueOf(currentOk));
|
|
|
+
|
|
|
+ // 采集实时结果(对应旧逻辑的 F-out, S-out, F-min/max, S-min/max)
|
|
|
+ String fout = String.valueOf(mesData.tensionResult); // 拉力
|
|
|
+ String sout = String.valueOf(mesData.dispResult); // 位移
|
|
|
+ String fmin = String.valueOf(mesData.tensionMin);
|
|
|
+ String smin = String.valueOf(mesData.dispMin);
|
|
|
+ String fmax = String.valueOf(mesData.tensionMax);
|
|
|
+ String smax = String.valueOf(mesData.dispMax);
|
|
|
+ name = mesData.modelName;
|
|
|
+ MesClient.lblA.setText("拉铆枪A("+name+")");
|
|
|
+
|
|
|
+ System.out.println("cur:" + currentOk);
|
|
|
+ // 只有条码存在时才写入数据库
|
|
|
+ if (!MesClient.product_sn.getText().isEmpty()) {
|
|
|
+ JdbcUtils.insertProdData(
|
|
|
+ MesClient.mes_gw,
|
|
|
+ MesClient.mes_line_sn,
|
|
|
+ MesClient.product_sn.getText(),
|
|
|
+ "A",
|
|
|
+ fout, sout, fmin, smin, fmax, smax,
|
|
|
+ "OK".equals(mesData.procDesc)?"1":"2", // 质量结果 OK
|
|
|
+ String.valueOf(currentOk),
|
|
|
+ MesClient.user_menu.getText()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 更新产品条码
|
|
|
+ if (mesData.barcode != null && !mesData.barcode.isEmpty()) {
|
|
|
+ MesClient.product_sn.setText(mesData.barcode);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 7. 上传结果(旧逻辑中有 upResult())
|
|
|
+ upResult();
|
|
|
+
|
|
|
+
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+// int cur = plc.readInt32("DB41.32.0");
|
|
|
+//// int cur = 3;
|
|
|
+//// if(MesClient.aMax <= 0){
|
|
|
+//// MesClient.aMax = plc.readInt32("DB1.38.0");
|
|
|
+// MesClient.param1.setText(String.valueOf(MesClient.aMax));
|
|
|
+//// }
|
|
|
+//
|
|
|
+// if(MesClient.sortA < cur){
|
|
|
+// MesClient.sortA = cur;
|
|
|
+// if (cur == 9){
|
|
|
+// plc.writeUInt16("DB40.2",2);
|
|
|
+// }
|
|
|
+// int fout = plc.readInt32("DB41.24.0");
|
|
|
+// Float sout = plc.readFloat32("DB41.28.0");
|
|
|
+//
|
|
|
+// int fmin = plc.readInt32("DB41.0.0") - plc.readInt32("DB41.8.0");
|
|
|
+// Float smin = plc.readFloat32("DB41.12.0") - plc.readFloat32("DB41.20.0");
|
|
|
+//
|
|
|
+// int fmax = plc.readInt32("DB41.0.0") + plc.readInt32("DB41.4.0");
|
|
|
+// Float smax = plc.readInt32("DB41.12.0") + plc.readFloat32("DB41.16.0");
|
|
|
+//
|
|
|
+//// String fret = "NG";
|
|
|
+//// if(fout >= fmin && fout <= fmax){
|
|
|
+//// fret = "OK";
|
|
|
+//// }
|
|
|
+//// String sret = "NG";
|
|
|
+//// if(sout >= smin && sout <= smax){
|
|
|
+//// sret = "OK";
|
|
|
+//// }
|
|
|
+//
|
|
|
+// String qty = "0";
|
|
|
+// if(getResultDingA(plc).equals("OK")){
|
|
|
+// MesClient.aFinish++;
|
|
|
+// qty = "1";
|
|
|
+// }
|
|
|
+//
|
|
|
+// DecimalFormat decimalFormat = new DecimalFormat("#.###");
|
|
|
+// String soutstr = decimalFormat.format(sout);
|
|
|
+// String sminstr = decimalFormat.format(smin);
|
|
|
+// String smaxstr = decimalFormat.format(smax);
|
|
|
+// String foutstr = fout+"";
|
|
|
+// String fminstr = fmin+"";
|
|
|
+// String fmaxstr = fmax+"";
|
|
|
+//
|
|
|
+// MesClient.param2.setText(cur+"");
|
|
|
+// System.out.println("cur:"+cur);
|
|
|
+// if(!MesClient.product_sn.getText().isEmpty()){
|
|
|
+// JdbcUtils.insertProdData(MesClient.mes_gw, MesClient.mes_line_sn, MesClient.product_sn.getText(),"A",foutstr,soutstr,fminstr,sminstr,fmaxstr,smaxstr,qty,cur+"", MesClient.user_menu.getText());
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (cur == MesClient.aMax){
|
|
|
+// setPowerOffA(plc);
|
|
|
+// }
|
|
|
+//
|
|
|
+// upResult();
|
|
|
+ }
|
|
|
+
|
|
|
+ // B枪已停用
|
|
|
+ public static void getDataB(TCPMasterConnection connection){
|
|
|
+// try{
|
|
|
+// ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(100, 120);
|
|
|
+// request.setUnitID(1);
|
|
|
+// ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection);
|
|
|
+// transaction.setRequest(request);
|
|
|
+// transaction.execute();
|
|
|
+// ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse();
|
|
|
+// Register[] registers = response.getRegisters();
|
|
|
+// short[] dRegs = new short[300];
|
|
|
+// for (int i = 0; i < registers.length; i++) {
|
|
|
+// dRegs[100 + i] = registers[i].toShort();
|
|
|
+// }
|
|
|
+// HcPlcPointUtilB.PlcMesData mesData = HcPlcPointUtilB.readAllPoint(dRegs);
|
|
|
+// MesClient.maxb = mesData.setQty;
|
|
|
+// MesClient.param3.setText(String.valueOf(mesData.setQty));
|
|
|
+// String name = mesData.modelName;
|
|
|
+// MesClient.lblB.setText("拉铆枪B("+name+")");
|
|
|
+// if (MesClient.maxb <= 0){
|
|
|
+// MesClient.maxb = mesData.setQty;
|
|
|
+// MesClient.param3.setText(String.valueOf(mesData.setQty));
|
|
|
+// }
|
|
|
+// MesClient.param4.setText(String.valueOf(MesClient.sortB));
|
|
|
+// int currentOk = mesData.okQty;
|
|
|
+// if (currentOk == MesClient.sortB + 1){
|
|
|
+// MesClient.sortB = currentOk;
|
|
|
+// MesClient.bFinish = currentOk;
|
|
|
+// MesClient.param2.setText(String.valueOf(currentOk));
|
|
|
+// String fout = String.valueOf(mesData.tensionResult);
|
|
|
+// String sout = String.valueOf(mesData.dispResult);
|
|
|
+// String fmin = String.valueOf(mesData.tensionMin);
|
|
|
+// String smin = String.valueOf(mesData.dispMin);
|
|
|
+// String fmax = String.valueOf(mesData.tensionMax);
|
|
|
+// String smax = String.valueOf(mesData.dispMax);
|
|
|
+// name = mesData.modelName;
|
|
|
+// MesClient.lblB.setText("拉铆枪B("+name+")");
|
|
|
+// if (!MesClient.product_sn.getText().isEmpty()) {
|
|
|
+// JdbcUtils.insertProdData(
|
|
|
+// MesClient.mes_gw, MesClient.mes_line_sn, MesClient.product_sn.getText(),
|
|
|
+// "B", fout, sout, fmin, smin, fmax, smax,
|
|
|
+// "OK".equals(mesData.procDesc)?"1":"2",
|
|
|
+// String.valueOf(currentOk), MesClient.user_menu.getText()
|
|
|
+// );
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if (mesData.barcode != null && !mesData.barcode.isEmpty()) {
|
|
|
+// MesClient.product_sn.setText(mesData.barcode);
|
|
|
+// }
|
|
|
+// upResult();
|
|
|
+// }catch (Exception e){
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+
|
|
|
+//
|
|
|
+// int cur = plc.readInt32("DB51.32.0");
|
|
|
+//
|
|
|
+//// int cur = 9;
|
|
|
+//// if(MesClient.bMax <= 0){
|
|
|
+//// MesClient.bMax = plc.readInt32("DB1.568.0");
|
|
|
+// MesClient.param3.setText(String.valueOf(MesClient.bMax));
|
|
|
+//// }
|
|
|
+//
|
|
|
+// if(MesClient.sortB < cur){
|
|
|
+// MesClient.sortB = cur;
|
|
|
+//
|
|
|
+//
|
|
|
+// int fout = plc.readInt32("DB51.24.0");
|
|
|
+// Float sout = plc.readFloat32("DB51.28.0");
|
|
|
+//
|
|
|
+// int fmin = plc.readInt32("DB51.0.0") - plc.readInt32("DB51.8.0");
|
|
|
+// Float smin = plc.readFloat32("DB51.12.0") - plc.readFloat32("DB51.20.0");
|
|
|
+//
|
|
|
+// int fmax = plc.readInt32("DB51.0.0") + plc.readInt32("DB51.4.0");
|
|
|
+// Float smax = plc.readFloat32("DB51.12.0") + plc.readFloat32("DB51.16.0");
|
|
|
+//
|
|
|
+//// String fret = "NG";
|
|
|
+//// if(fout >= fmin && fout <= fmax){
|
|
|
+//// fret = "OK";
|
|
|
+//// }
|
|
|
+//// String sret = "NG";
|
|
|
+//// if(sout >= smin && sout <= smax){
|
|
|
+//// sret = "OK";
|
|
|
+//// }
|
|
|
+//
|
|
|
+// String qty = "0";
|
|
|
+// if(getResultDingB(plc).equals("OK")){
|
|
|
+// MesClient.bFinish++;
|
|
|
+// qty = "1";
|
|
|
+// }
|
|
|
+//
|
|
|
+// DecimalFormat decimalFormat = new DecimalFormat("#.###");
|
|
|
+// String soutstr = decimalFormat.format(sout);
|
|
|
+// String sminstr = decimalFormat.format(smin);
|
|
|
+// String smaxstr = decimalFormat.format(smax);
|
|
|
+// String foutstr = fout+"";
|
|
|
+// String fminstr = fmin+"";
|
|
|
+// String fmaxstr = fmax+"";
|
|
|
+//
|
|
|
+// MesClient.param4.setText(cur+"");
|
|
|
+// System.out.println("cur:"+cur);
|
|
|
+// if(!MesClient.product_sn.getText().isEmpty()){
|
|
|
+// JdbcUtils.insertProdData(MesClient.mes_gw, MesClient.mes_line_sn, MesClient.product_sn.getText(),"B",foutstr,soutstr,fminstr,sminstr,fmaxstr,smaxstr,qty,cur+"", MesClient.user_menu.getText());
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if (cur == MesClient.bMax){
|
|
|
+// PlcUtil.setPowerOffB(plc);
|
|
|
+// }
|
|
|
+//
|
|
|
+// upResult();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传总结果
|
|
|
+ public static void upResult(){
|
|
|
+ // 冷板绑定后且 A 枪完成才可提交
|
|
|
+ if(MesClient.bindLbStatus && !MesClient.curSn.isEmpty() && MesClient.aMax > 0 && MesClient.aMax == MesClient.sortA){
|
|
|
+// if(!MesClient.curSn.isEmpty() && MesClient.aMax > 0 && MesClient.aMax == MesClient.sortA && MesClient.bMax > 0 && MesClient.bMax == MesClient.sortB){
|
|
|
+ MesClient.finish_ok_bt.setEnabled(true);
|
|
|
+ MesClient.finish_ng_bt.setEnabled(true);
|
|
|
+
|
|
|
+ if(MesClient.tjStatus == 0 && MesClient.check_quality_result){
|
|
|
+ MesClient.tjStatus = 1;
|
|
|
+ MesClient.getUser();
|
|
|
+ String retf = "OK";
|
|
|
+ if(MesClient.aFinish != MesClient.aMax){
|
|
|
+ retf = "NG";
|
|
|
+ }
|
|
|
+ Boolean ret = DataUtil.sendQuality(MesClient.nettyClient, MesClient.product_sn.getText(),retf, MesClient.user20);
|
|
|
+ if(!ret){
|
|
|
+ MesClient.setMenuStatus(MesClient.tjFlagTextErr,-1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取控制模式
|
|
|
+ public static short getControlModel(S7PLC plc){
|
|
|
+ short control = 0;
|
|
|
+// try{
|
|
|
+// control = plc.read(1090);
|
|
|
+// }catch (Exception e){
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+
|
|
|
+ return control;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 每两个字符分割,并反转
|
|
|
+ private static String splitIntoChunks(String str, int chunkSize) {
|
|
|
+ if (str == null || str.isEmpty() || chunkSize <= 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ int length = str.length();
|
|
|
+ int arraySize = (length + chunkSize - 1) / chunkSize;
|
|
|
+ String[] result = new String[arraySize];
|
|
|
+
|
|
|
+ for (int i = 0, j = 0; i < length; i += chunkSize, j++) {
|
|
|
+ int endIndex = Math.min(i + chunkSize, length);
|
|
|
+ String sttr = str.substring(i, endIndex);
|
|
|
+ StringBuilder sb = new StringBuilder(sttr);
|
|
|
+ String reversedStr = sb.reverse().toString(); // 字符串反转
|
|
|
+ result[j] = reversedStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ return String.join("",result);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 远程开机待定
|
|
|
+ public static Boolean setPowerOn(S7PLC plc){
|
|
|
+ Boolean ret = false;
|
|
|
+ try{
|
|
|
+ plc.writeBoolean("DB40.0.0",true);
|
|
|
+// plc.writeBoolean("DB50.0.0",true); // B枪已停用
|
|
|
+// plc.writeBoolean("DB14.0.1",false);
|
|
|
+ ret = true;
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ ret = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 远程关机
|
|
|
+ public static Boolean setPowerOff(S7PLC plc){
|
|
|
+ Boolean ret = false;
|
|
|
+ try{
|
|
|
+ plc.writeBoolean("DB40.0.0",false);
|
|
|
+// plc.writeBoolean("DB50.0.0",false); // B枪已停用
|
|
|
+// plc.writeBoolean("DB14.0.1",true);
|
|
|
+ ret = true;
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ ret = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 远程关机
|
|
|
+ public static Boolean setPowerOffA(S7PLC plc){
|
|
|
+ Boolean ret = false;
|
|
|
+ try{
|
|
|
+ plc.writeBoolean("DB40.0.0",false);
|
|
|
+// plc.writeBoolean("DB14.0.1",true);
|
|
|
+ ret = true;
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ ret = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ // B枪已停用
|
|
|
+ public static Boolean setPowerOffB(S7PLC plc){
|
|
|
+// Boolean ret = false;
|
|
|
+// try{
|
|
|
+// plc.writeBoolean("DB50.0.0",false);
|
|
|
+// ret = true;
|
|
|
+// }catch (Exception e){
|
|
|
+// e.printStackTrace();
|
|
|
+// ret = false;
|
|
|
+// }
|
|
|
+// return ret;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重置任务
|
|
|
+ public static void setTask(S7PLC plc,int setNumA,int setNumB){
|
|
|
+ try{
|
|
|
+/* // 设置任务 A
|
|
|
+// plc.writeInt32("DB1.38.0",setNumA);
|
|
|
+ plc.writeUInt16("DB1.DBW54",1);
|
|
|
+ plc.writeUInt16("DB1.DBW584",1);
|
|
|
+ plc.writeInt32("DB1.184.0",0);
|
|
|
+
|
|
|
+ // 设置任务 B
|
|
|
+// plc.writeInt32("DB1.568.0",setNumB);
|
|
|
+ plc.writeInt32("DB1.714.0",0);*/
|
|
|
+ plc.writeUInt16("DB40.2",1);
|
|
|
+ plc.writeUInt16("DB40.4",setNumA);
|
|
|
+ plc.writeInt32("DB41.32.0",0);
|
|
|
+
|
|
|
+// plc.writeUInt16("DB50.2",1); // B枪已停用
|
|
|
+// plc.writeUInt16("DB50.4",setNumB);
|
|
|
+// plc.writeInt32("DB51.32.0",0);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getResultDingA(S7PLC plc){
|
|
|
+ String ret = "OK";
|
|
|
+ try{
|
|
|
+
|
|
|
+ /* boolean retang = plc.readBoolean("DB1.314.1");
|
|
|
+ boolean retang2 = plc.readBoolean("DB1.314.3");*/
|
|
|
+ byte retang = plc.readByte("DB41.26");
|
|
|
+ int rent = (int) retang;
|
|
|
+ if(rent == 0){
|
|
|
+ ret = "NG";
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ ret = "NG";
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ // B枪已停用
|
|
|
+ public static String getResultDingB(S7PLC plc){
|
|
|
+// String ret = "OK";
|
|
|
+// try{
|
|
|
+// byte retang = plc.readByte("DB51.26");
|
|
|
+// int rent = (int) retang;
|
|
|
+// if(rent == 0){
|
|
|
+// ret = "NG";
|
|
|
+// }
|
|
|
+// }catch (Exception e){
|
|
|
+// e.printStackTrace();
|
|
|
+// ret = "NG";
|
|
|
+// }
|
|
|
+// return ret;
|
|
|
+ return "OK";
|
|
|
+ }
|
|
|
+}
|