|
@@ -2,11 +2,15 @@ package com.mes.ui;
|
|
|
|
|
|
|
|
import com.github.xingshuangs.iot.protocol.common.buff.EByteBuffFormat;
|
|
import com.github.xingshuangs.iot.protocol.common.buff.EByteBuffFormat;
|
|
|
import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
|
|
import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
public class ModbusUitl {
|
|
public class ModbusUitl {
|
|
|
|
|
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ModbusUitl.class);
|
|
|
|
|
+
|
|
|
private static final int REG_PRESS_FORCE = 110;
|
|
private static final int REG_PRESS_FORCE = 110;
|
|
|
private static final int REG_PRESS_DISPLACEMENT = 112;
|
|
private static final int REG_PRESS_DISPLACEMENT = 112;
|
|
|
private static final int REG_PRESS_RESULT = 114;
|
|
private static final int REG_PRESS_RESULT = 114;
|
|
@@ -16,54 +20,94 @@ public class ModbusUitl {
|
|
|
private static final int REG_ENABLE_START = 119;
|
|
private static final int REG_ENABLE_START = 119;
|
|
|
|
|
|
|
|
private static final EByteBuffFormat DINT_FORMAT = EByteBuffFormat.DC_BA;
|
|
private static final EByteBuffFormat DINT_FORMAT = EByteBuffFormat.DC_BA;
|
|
|
|
|
+ private static final int PLC_POLL_INTERVAL_MS = 300;
|
|
|
private static int lastFinishStatus = 0;
|
|
private static int lastFinishStatus = 0;
|
|
|
- public static void getParamData(ModbusTcp modbusTcp){
|
|
|
|
|
- try{//扫完二维码后,启动设备
|
|
|
|
|
- if(MesClient.tjFlag == 1){
|
|
|
|
|
- int qdstatus = getEnable(modbusTcp);
|
|
|
|
|
- if(qdstatus != 1){
|
|
|
|
|
- changeEnable(MesClient.modbusTcp,1);
|
|
|
|
|
- //(设备接受到允许启动信号,运行状态变为1)允许启动信号设置为0
|
|
|
|
|
|
|
+ private static int lastRunStatus = 0;
|
|
|
|
|
+ private static boolean finishPulseRecorded = false;
|
|
|
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public static int getPlcPollIntervalMs(){
|
|
|
|
|
+ return PLC_POLL_INTERVAL_MS;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void resetPlcEdgeStatus(){
|
|
|
|
|
+ lastRunStatus = 0;
|
|
|
|
|
+ lastFinishStatus = 0;
|
|
|
|
|
+ finishPulseRecorded = false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void syncPlcEdgeStatus(ModbusTcp modbusTcp){
|
|
|
|
|
+ Integer runStatus = getRunStatus(modbusTcp);
|
|
|
|
|
+ Integer finishStatus = getFinishStatus(modbusTcp);
|
|
|
|
|
+ if(runStatus >= 0){
|
|
|
|
|
+ lastRunStatus = runStatus;
|
|
|
|
|
+ }
|
|
|
|
|
+ if(finishStatus >= 0){
|
|
|
|
|
+ lastFinishStatus = finishStatus;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void getParamData(ModbusTcp modbusTcp){
|
|
|
|
|
+ try{
|
|
|
|
|
+ if(MesClient.tjFlag != 1 && MesClient.tjFlag != 2){
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //运行状态等于1,运行中
|
|
|
|
|
|
|
+ keepEnableOn(modbusTcp);
|
|
|
|
|
+
|
|
|
Integer runStatus = getRunStatus(modbusTcp);
|
|
Integer runStatus = getRunStatus(modbusTcp);
|
|
|
Integer finishStatus = getFinishStatus(modbusTcp);
|
|
Integer finishStatus = getFinishStatus(modbusTcp);
|
|
|
|
|
+ if(finishStatus < 0){
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if(runStatus == 1){
|
|
|
|
|
- if(MesClient.tjFlag == 1){
|
|
|
|
|
- changeEnable(MesClient.modbusTcp,0);
|
|
|
|
|
- MesClient.tjFlag = 2;
|
|
|
|
|
- readPressParam(modbusTcp);
|
|
|
|
|
- MesClient.setMenuStatus(MesClient.tjFlagText2,0);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if(runStatus >= 0 && runStatus == 1 && lastRunStatus != 1 && MesClient.tjFlag == 1){
|
|
|
|
|
+ MesClient.tjFlag = 2;
|
|
|
|
|
+ readPressParam(modbusTcp);
|
|
|
|
|
+ MesClient.setMenuStatus(MesClient.tjFlagText2, 0);
|
|
|
|
|
+ logPlcSnapshot("运行开始", modbusTcp);
|
|
|
}
|
|
}
|
|
|
- //运行结束
|
|
|
|
|
|
|
+
|
|
|
if(MesClient.tjFlag == 2){
|
|
if(MesClient.tjFlag == 2){
|
|
|
readPressParam(modbusTcp);
|
|
readPressParam(modbusTcp);
|
|
|
- if(finishStatus == 1 && lastFinishStatus != 1){
|
|
|
|
|
- //存Map
|
|
|
|
|
- savePressRivetData();
|
|
|
|
|
- if(MesClient.pressRivetMap.size() == MesClient.PRESS_RIVET_UPLOAD_SIZE){
|
|
|
|
|
- if(!MesClient.uploadPressRivetData("压铆参数上传成功,请扫下一件", "压铆参数上传失败,请重试")){
|
|
|
|
|
- MesClient.tjStatus = 1;
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- }else{
|
|
|
|
|
- MesClient.tjFlag = 1;
|
|
|
|
|
- MesClient.setMenuStatus("已采集第" + MesClient.pressRivetMap.size() + "颗拉铆,等待下一颗",0);
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(finishStatus == 0){
|
|
|
|
|
+ finishPulseRecorded = false;
|
|
|
|
|
+ }else if(!finishPulseRecorded){
|
|
|
|
|
+ finishPulseRecorded = true;
|
|
|
|
|
+ logPlcSnapshot("压铆完成", modbusTcp);
|
|
|
|
|
+ savePressRivetData();
|
|
|
|
|
+ log.info("[PLC] 采集第{}颗,当前共{}颗", MesClient.pressRivetIndex, MesClient.pressRivetMap.size());
|
|
|
|
|
+ if(MesClient.pressRivetMap.size() == MesClient.PRESS_RIVET_UPLOAD_SIZE){
|
|
|
|
|
+ changeEnable(modbusTcp, 0);
|
|
|
|
|
+ logPlcSnapshot("九颗完成撤信号", modbusTcp);
|
|
|
|
|
+ if(!MesClient.uploadPressRivetData("压铆参数上传成功,请扫下一件", "压铆参数上传失败,请重试")){
|
|
|
|
|
+ MesClient.tjStatus = 1;
|
|
|
}
|
|
}
|
|
|
- changeEnable(modbusTcp,0);
|
|
|
|
|
|
|
+ }else{
|
|
|
|
|
+ MesClient.tjFlag = 1;
|
|
|
|
|
+ MesClient.setMenuStatus("已采集第" + MesClient.pressRivetMap.size() + "颗拉铆,等待下一颗", 0);
|
|
|
|
|
+ keepEnableOn(modbusTcp);
|
|
|
|
|
+ logPlcSnapshot("完成采集继续放行", modbusTcp);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ if(runStatus >= 0){
|
|
|
|
|
+ lastRunStatus = runStatus;
|
|
|
|
|
+ }
|
|
|
lastFinishStatus = finishStatus;
|
|
lastFinishStatus = finishStatus;
|
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private static void keepEnableOn(ModbusTcp modbusTcp){
|
|
|
|
|
+ if(getEnable(modbusTcp) != 1){
|
|
|
|
|
+ changeEnable(modbusTcp, 1);
|
|
|
|
|
+ logPlcSnapshot("保持允许启动", modbusTcp);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static void readPressParam(ModbusTcp modbusTcp){
|
|
public static void readPressParam(ModbusTcp modbusTcp){
|
|
|
try{
|
|
try{
|
|
|
String force = getPressForce(modbusTcp);
|
|
String force = getPressForce(modbusTcp);
|
|
@@ -74,7 +118,8 @@ public class ModbusUitl {
|
|
|
MesClient.pressParamValue[1] = force;
|
|
MesClient.pressParamValue[1] = force;
|
|
|
MesClient.pressParamValue[2] = displacement;
|
|
MesClient.pressParamValue[2] = displacement;
|
|
|
MesClient.pressParamValue[3] = result;
|
|
MesClient.pressParamValue[3] = result;
|
|
|
- MesClient.tjResult = "NG".equalsIgnoreCase(result) ? "NG" : "OK";
|
|
|
|
|
|
|
+ MesClient.tjResult = isNgResult(result) ? "NG" : "OK";
|
|
|
|
|
+ MesClient.refreshPressParamFields();
|
|
|
|
|
|
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
@@ -127,15 +172,99 @@ public class ModbusUitl {
|
|
|
|
|
|
|
|
public static String getPressResult(ModbusTcp modbusTcp){
|
|
public static String getPressResult(ModbusTcp modbusTcp){
|
|
|
try{
|
|
try{
|
|
|
- byte[] data = modbusTcp.readHoldRegister(REG_PRESS_RESULT, 1);//本次结果
|
|
|
|
|
- String ret = new String(data, StandardCharsets.US_ASCII);
|
|
|
|
|
- return ret == null ? "" : ret.trim().replace("\u0000", "");
|
|
|
|
|
|
|
+ byte[] data = modbusTcp.readHoldRegister(REG_PRESS_RESULT, 1);
|
|
|
|
|
+ return parsePressResult(data);
|
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
return "";
|
|
return "";
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private static void logPlcSnapshot(String event, ModbusTcp modbusTcp){
|
|
|
|
|
+ try{
|
|
|
|
|
+ short forceRaw = modbusTcp.readInt16(REG_PRESS_FORCE);
|
|
|
|
|
+ Integer dispRaw = modbusTcp.readInt32(REG_PRESS_DISPLACEMENT, DINT_FORMAT);
|
|
|
|
|
+ byte[] resultBytes = modbusTcp.readHoldRegister(REG_PRESS_RESULT, 1);
|
|
|
|
|
+ String normalResult = resultBytes == null || resultBytes.length < 2
|
|
|
|
|
+ ? "" : toAsciiResult(resultBytes[0], resultBytes[1]);
|
|
|
|
|
+ String swappedResult = resultBytes == null || resultBytes.length < 2
|
|
|
|
|
+ ? "" : toAsciiResult(resultBytes[1], resultBytes[0]);
|
|
|
|
|
+ String parsedResult = parsePressResult(resultBytes);
|
|
|
|
|
+ int runStatus = getRunStatus(modbusTcp);
|
|
|
|
|
+ int finishStatus = getFinishStatus(modbusTcp);
|
|
|
|
|
+ int enableStatus = getEnable(modbusTcp);
|
|
|
|
|
+ String message = String.format(
|
|
|
|
|
+ "[PLC] %s | tjFlag=%d mapSize=%d | run=%d finish=%d enable=%d | forceRaw=%d force=%s | dispRaw=%d disp=%s | resultBytes=%s normal=%s swapped=%s parsed=%s",
|
|
|
|
|
+ event,
|
|
|
|
|
+ MesClient.tjFlag,
|
|
|
|
|
+ MesClient.pressRivetMap.size(),
|
|
|
|
|
+ runStatus,
|
|
|
|
|
+ finishStatus,
|
|
|
|
|
+ enableStatus,
|
|
|
|
|
+ forceRaw,
|
|
|
|
|
+ getPressForce(modbusTcp),
|
|
|
|
|
+ dispRaw,
|
|
|
|
|
+ getPressDisplacement(modbusTcp),
|
|
|
|
|
+ bytesToHex(resultBytes),
|
|
|
|
|
+ normalResult,
|
|
|
|
|
+ swappedResult,
|
|
|
|
|
+ parsedResult
|
|
|
|
|
+ );
|
|
|
|
|
+ System.out.println(message);
|
|
|
|
|
+ log.info(message);
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ System.out.println("[PLC] 日志打印失败: " + e.getMessage());
|
|
|
|
|
+ log.warn("PLC snapshot log failed", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String bytesToHex(byte[] data){
|
|
|
|
|
+ if(data == null || data.length == 0){
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
|
+ for(byte b : data){
|
|
|
|
|
+ sb.append(String.format("%02X ", b));
|
|
|
|
|
+ }
|
|
|
|
|
+ return sb.toString().trim();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String parsePressResult(byte[] data){
|
|
|
|
|
+ if(data == null || data.length < 2){
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ String normal = toAsciiResult(data[0], data[1]);
|
|
|
|
|
+ String swapped = toAsciiResult(data[1], data[0]);
|
|
|
|
|
+ if("KO".equalsIgnoreCase(normal) && "OK".equalsIgnoreCase(swapped)){
|
|
|
|
|
+ return "OK";
|
|
|
|
|
+ }
|
|
|
|
|
+ if("GN".equalsIgnoreCase(normal) && "NG".equalsIgnoreCase(swapped)){
|
|
|
|
|
+ return "NG";
|
|
|
|
|
+ }
|
|
|
|
|
+ if(isKnownResult(normal)){
|
|
|
|
|
+ return normal;
|
|
|
|
|
+ }
|
|
|
|
|
+ if(isKnownResult(swapped)){
|
|
|
|
|
+ return swapped;
|
|
|
|
|
+ }
|
|
|
|
|
+ return normal;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String toAsciiResult(byte high, byte low){
|
|
|
|
|
+ String ret = new String(new byte[]{high, low}, StandardCharsets.US_ASCII);
|
|
|
|
|
+ return ret == null ? "" : ret.trim().replace("\u0000", "");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean isKnownResult(String result){
|
|
|
|
|
+ return "OK".equalsIgnoreCase(result)
|
|
|
|
|
+ || "NG".equalsIgnoreCase(result)
|
|
|
|
|
+ || "KO".equalsIgnoreCase(result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean isNgResult(String result){
|
|
|
|
|
+ return "NG".equalsIgnoreCase(result) || "KO".equalsIgnoreCase(result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static Integer getLocalRemoteStatus(ModbusTcp modbusTcp){
|
|
public static Integer getLocalRemoteStatus(ModbusTcp modbusTcp){
|
|
|
try{
|
|
try{
|
|
|
return (int) modbusTcp.readInt16(REG_LOCAL_REMOTE_STATUS);//MES屏蔽
|
|
return (int) modbusTcp.readInt16(REG_LOCAL_REMOTE_STATUS);//MES屏蔽
|