|
|
@@ -5,35 +5,75 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
-import java.nio.charset.Charset;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
+/**
|
|
|
+ * UT5310 绝缘耐压仪通信(Modbus RTU,参考 UNI-T Programming Manual MODBUS RTU)。
|
|
|
+ */
|
|
|
public class Ut5310ScpiService {
|
|
|
|
|
|
public static final Logger log = LoggerFactory.getLogger(Ut5310ScpiService.class);
|
|
|
|
|
|
- private static final Charset COMMAND_CHARSET = StandardCharsets.US_ASCII;
|
|
|
- private static final String START_COMMAND = "FUNC:START";
|
|
|
- private static final String FETCH_COMMAND = "FETCh?";
|
|
|
+ public enum Protocol {
|
|
|
+ MODBUS, SCPI, AT, AUTO
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 测试结果起始寄存器 0x0100:步骤1电压,每步占5个寄存器 */
|
|
|
+ private static final int REG_RESULT_START = 0x0100;
|
|
|
+ /**
|
|
|
+ * 每步 Modbus 寄存器布局(手册 3.1):
|
|
|
+ * +0,+1 测试电压(32位浮点,单位 kV)
|
|
|
+ * +2,+3 测试电流或电阻(32位浮点;仪器原始单位:绝缘步骤=MΩ,耐压步骤=mA;显示时转为GΩ/μA)
|
|
|
+ * +4 判读结果(16位整数,0=测试中;0x0003=PASS 等)
|
|
|
+ */
|
|
|
+ private static final int REGS_PER_STEP = 5;
|
|
|
+ /** 启停测试寄存器 0x0500:写 0x0002 启动,0x0000 停止 */
|
|
|
+ private static final int REG_START_STOP = 0x0500;
|
|
|
+ private static final int CMD_START = 0x0002;
|
|
|
+ private static final int CMD_STOP = 0x0000;
|
|
|
+ /** 判读码:PASS(手册示例 0x0003) */
|
|
|
+ private static final int SORT_PASS = 0x0003;
|
|
|
|
|
|
private static String portName = "COM1";
|
|
|
private static int baudRate = 9600;
|
|
|
private static int dataBits = 8;
|
|
|
private static int stopBits = SerialPort.ONE_STOP_BIT;
|
|
|
private static int parity = SerialPort.NO_PARITY;
|
|
|
- private static int readTimeoutMs = 1000;
|
|
|
- private static int testTimeoutMs = 60000;
|
|
|
+ private static int readTimeoutMs = 2000;
|
|
|
+ private static int testTimeoutMs = 300000;
|
|
|
private static int fetchIntervalMs = 500;
|
|
|
+ private static Protocol protocol = Protocol.MODBUS;
|
|
|
+ private static int modbusSlaveId = 1;
|
|
|
+ /** 单次 Modbus 读取的最大步骤数(寄存器块长度) */
|
|
|
+ private static int modbusMaxSteps = 4;
|
|
|
+ /** 测试文件实际步骤数,全部步骤判读非0后才提交结果(如 PANEL_01=2:绝缘电阻+直流耐压) */
|
|
|
+ private static int modbusExpectedSteps = 2;
|
|
|
+ /** false=仅读取PLC/工装已触发的测试结果,不在Modbus上重复发START */
|
|
|
+ private static boolean modbusSendStart = false;
|
|
|
|
|
|
private static SerialPort serialPort;
|
|
|
- private static InputStream inputStream;
|
|
|
- private static OutputStream outputStream;
|
|
|
private static volatile boolean connected = false;
|
|
|
+ private static volatile Protocol lastUsedProtocol;
|
|
|
|
|
|
public static synchronized void configure(String port, int baud, int data, int stop, String parityName,
|
|
|
int readTimeout, int testTimeout, int fetchInterval) {
|
|
|
+ configure(port, baud, data, stop, parityName, readTimeout, testTimeout, fetchInterval,
|
|
|
+ "MODBUS", 1, 4, false, 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static synchronized void configure(String port, int baud, int data, int stop, String parityName,
|
|
|
+ int readTimeout, int testTimeout, int fetchInterval,
|
|
|
+ String protocolName, int slaveId, int maxSteps,
|
|
|
+ boolean sendStart) {
|
|
|
+ configure(port, baud, data, stop, parityName, readTimeout, testTimeout, fetchInterval,
|
|
|
+ protocolName, slaveId, maxSteps, sendStart, 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static synchronized void configure(String port, int baud, int data, int stop, String parityName,
|
|
|
+ int readTimeout, int testTimeout, int fetchInterval,
|
|
|
+ String protocolName, int slaveId, int maxSteps,
|
|
|
+ boolean sendStart, int expectedSteps) {
|
|
|
if (port != null && !port.trim().isEmpty()) {
|
|
|
portName = port.trim();
|
|
|
}
|
|
|
@@ -54,6 +94,40 @@ public class Ut5310ScpiService {
|
|
|
if (fetchInterval > 0) {
|
|
|
fetchIntervalMs = fetchInterval;
|
|
|
}
|
|
|
+ protocol = parseProtocol(protocolName);
|
|
|
+ if (slaveId >= 0 && slaveId <= 255) {
|
|
|
+ modbusSlaveId = slaveId;
|
|
|
+ }
|
|
|
+ if (maxSteps > 0 && maxSteps <= 20) {
|
|
|
+ modbusMaxSteps = maxSteps;
|
|
|
+ }
|
|
|
+ modbusSendStart = sendStart;
|
|
|
+ if (expectedSteps > 0 && expectedSteps <= 20) {
|
|
|
+ modbusExpectedSteps = expectedSteps;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Protocol getLastUsedProtocol() {
|
|
|
+ return lastUsedProtocol;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static synchronized String getPortName() {
|
|
|
+ return portName;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 仅更新串口号;若变更则断开,下次通信时按新口重连 */
|
|
|
+ public static synchronized void setPortName(String port) {
|
|
|
+ if (port == null || port.trim().isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String next = port.trim();
|
|
|
+ if (!next.equalsIgnoreCase(portName)) {
|
|
|
+ portName = next;
|
|
|
+ disconnect();
|
|
|
+ log.info("UT5310 port set to {}", portName);
|
|
|
+ } else {
|
|
|
+ portName = next;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public static synchronized boolean isConnected() {
|
|
|
@@ -65,16 +139,15 @@ public class Ut5310ScpiService {
|
|
|
try {
|
|
|
serialPort = SerialPort.getCommPort(portName);
|
|
|
serialPort.setComPortParameters(baudRate, dataBits, stopBits, parity);
|
|
|
- serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, readTimeoutMs, readTimeoutMs);
|
|
|
+ serialPort.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 0, 0);
|
|
|
if (!serialPort.openPort()) {
|
|
|
connected = false;
|
|
|
log.info("UT5310 open port failed: {}", portName);
|
|
|
return false;
|
|
|
}
|
|
|
- inputStream = serialPort.getInputStream();
|
|
|
- outputStream = serialPort.getOutputStream();
|
|
|
connected = true;
|
|
|
- log.info("UT5310 connected: {} {}bps", portName, baudRate);
|
|
|
+ log.info("UT5310 connected: {} {}bps, protocol={}, slave={}",
|
|
|
+ portName, baudRate, protocol, modbusSlaveId);
|
|
|
return true;
|
|
|
} catch (Exception e) {
|
|
|
connected = false;
|
|
|
@@ -86,10 +159,6 @@ public class Ut5310ScpiService {
|
|
|
|
|
|
public static synchronized void disconnect() {
|
|
|
connected = false;
|
|
|
- closeQuietly(inputStream);
|
|
|
- closeQuietly(outputStream);
|
|
|
- inputStream = null;
|
|
|
- outputStream = null;
|
|
|
if (serialPort != null) {
|
|
|
try {
|
|
|
serialPort.closePort();
|
|
|
@@ -111,76 +180,318 @@ public class Ut5310ScpiService {
|
|
|
throw new IOException("UT5310 not connected");
|
|
|
}
|
|
|
|
|
|
- drainInput();
|
|
|
- writeCommand(START_COMMAND);
|
|
|
- log.info("UT5310 command sent: {}", START_COMMAND);
|
|
|
+ lastUsedProtocol = protocol == Protocol.AUTO ? Protocol.MODBUS : protocol;
|
|
|
+ System.out.println("[UT5310] 使用协议: Modbus RTU, 站号=" + modbusSlaveId
|
|
|
+ + ", 等待步骤数=" + modbusExpectedSteps);
|
|
|
+ log.info("UT5310 using Modbus RTU, slave={}, expectedSteps={}", modbusSlaveId, modbusExpectedSteps);
|
|
|
+
|
|
|
+ Ut5310TestResult result = runModbusTest();
|
|
|
+ if (result != null) {
|
|
|
+ result.printToConsole();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Ut5310TestResult runModbusTest() throws IOException {
|
|
|
+ ModbusRtuClient client = new ModbusRtuClient(serialPort, modbusSlaveId, readTimeoutMs);
|
|
|
+ int registerCount = modbusMaxSteps * REGS_PER_STEP;
|
|
|
+
|
|
|
+ // 先尝试直接读取(工装/PLC 可能已完成测试)
|
|
|
+ Ut5310TestResult existing = tryReadModbusResult(client, registerCount);
|
|
|
+ if (existing != null) {
|
|
|
+ System.out.println("[UT5310] 读取到已有测试结果(未发START)");
|
|
|
+ return existing;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (modbusSendStart) {
|
|
|
+ System.out.println("[UT5310] Modbus 写 0x0500: STOP -> START");
|
|
|
+ log.info("Modbus write 0x0500 STOP then START");
|
|
|
+ client.writeSingleRegister(REG_START_STOP, CMD_STOP);
|
|
|
+ sleepQuiet(200);
|
|
|
+ client.writeSingleRegister(REG_START_STOP, CMD_START);
|
|
|
+ sleepQuiet(500);
|
|
|
+ int runState = readStartStopState(client);
|
|
|
+ System.out.println("[UT5310] 0x0500 当前值=0x" + Integer.toHexString(runState).toUpperCase());
|
|
|
+ log.info("Modbus 0x0500 state=0x{}", Integer.toHexString(runState));
|
|
|
+
|
|
|
+ existing = tryReadModbusResult(client, registerCount);
|
|
|
+ if (existing != null) {
|
|
|
+ return existing;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("[UT5310] 仅读取Modbus结果,不发送START( mes.ut5310_modbus_send_start=false )");
|
|
|
+ log.info("Modbus read-only mode, skip START command");
|
|
|
+ }
|
|
|
|
|
|
long deadline = System.currentTimeMillis() + testTimeoutMs;
|
|
|
- String lastResponse = "";
|
|
|
+ int pollCount = 0;
|
|
|
while (System.currentTimeMillis() < deadline) {
|
|
|
- sleep(fetchIntervalMs);
|
|
|
- writeCommand(FETCH_COMMAND);
|
|
|
- lastResponse = readResponse();
|
|
|
- log.info("UT5310 fetch response: {}", lastResponse);
|
|
|
-
|
|
|
- Ut5310TestResult result = parseFetchResult(lastResponse);
|
|
|
+ pollCount++;
|
|
|
+ int[] regs = client.readHoldingRegisters(REG_RESULT_START, registerCount);
|
|
|
+ if (pollCount == 1 || pollCount % 10 == 0) {
|
|
|
+ logPollProgress(pollCount, regs);
|
|
|
+ }
|
|
|
+ Ut5310TestResult result = parseModbusResult(regs);
|
|
|
if (result != null) {
|
|
|
return result;
|
|
|
}
|
|
|
+ sleepQuiet(fetchIntervalMs);
|
|
|
}
|
|
|
- throw new IOException("UT5310 test timeout, last response: " + lastResponse);
|
|
|
+
|
|
|
+ if (modbusSendStart) {
|
|
|
+ try {
|
|
|
+ client.writeSingleRegister(REG_START_STOP, CMD_STOP);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("Modbus stop ignored: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new IOException("UT5310 Modbus 超时:"
|
|
|
+ + formatCompletionStatus(null)
|
|
|
+ + "。请增大 mes.ut5310_test_timeout_ms(当前" + testTimeoutMs + "ms)");
|
|
|
}
|
|
|
|
|
|
- public static Ut5310TestResult parseFetchResult(String response) {
|
|
|
- if (response == null) {
|
|
|
- return null;
|
|
|
+ /** 轮询时打印各步骤实时电压、电阻/电流、判读(判读仅该步骤结束后才有值) */
|
|
|
+ private static void logPollProgress(int pollCount, int[] regs) {
|
|
|
+ int steps = Math.min(modbusExpectedSteps, regs.length / REGS_PER_STEP);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(String.format("[UT5310] 轮询#%d:", pollCount));
|
|
|
+ for (int i = 0; i < steps; i++) {
|
|
|
+ int base = i * REGS_PER_STEP;
|
|
|
+ float volt = ModbusRtuClient.registersToFloat(regs[base], regs[base + 1]);
|
|
|
+ float rawData = ModbusRtuClient.registersToFloat(regs[base + 2], regs[base + 3]);
|
|
|
+ float displayData = convertStepData(i, rawData);
|
|
|
+ int sort = regs[base + 4];
|
|
|
+ sb.append(String.format(" [%d]%s 电压=%.3fkV %s=%s 判读=%s",
|
|
|
+ i + 1, stepTypeName(i), volt, stepDataUnit(i),
|
|
|
+ formatStepData(i, displayData), sortingToChinese(sort)));
|
|
|
}
|
|
|
- String raw = response.trim();
|
|
|
- if (raw.isEmpty()) {
|
|
|
+ System.out.println(sb);
|
|
|
+ int[] sorting = extractSorting(regs);
|
|
|
+ if (modbusExpectedSteps > 1 && sorting[0] != 0 && sorting.length > 1 && sorting[1] == 0) {
|
|
|
+ System.out.println("[UT5310] 步骤1(" + stepTypeName(0) + ")已判读完成,等待步骤2("
|
|
|
+ + stepTypeName(1) + ")结束后再提交...");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String formatCompletionStatus(int[] sorting) {
|
|
|
+ if (sorting == null) {
|
|
|
+ return "未完成全部" + modbusExpectedSteps + "个测试步骤";
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder("已完成");
|
|
|
+ int done = 0;
|
|
|
+ for (int i = 0; i < modbusExpectedSteps && i < sorting.length; i++) {
|
|
|
+ if (sorting[i] != 0) {
|
|
|
+ done++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.append(done).append('/').append(modbusExpectedSteps).append("步");
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Ut5310TestResult tryReadModbusResult(ModbusRtuClient client, int registerCount) throws IOException {
|
|
|
+ int[] regs = client.readHoldingRegisters(REG_RESULT_START, registerCount);
|
|
|
+ return parseModbusResult(regs);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Ut5310TestResult parseModbusResult(int[] regs) {
|
|
|
+ int[] sorting = extractSorting(regs);
|
|
|
+ float[] voltages = extractVoltage(regs);
|
|
|
+ float[] values = extractValue(regs);
|
|
|
+ if (!isSortingComplete(sorting, voltages, values)) {
|
|
|
return null;
|
|
|
}
|
|
|
- String normalized = raw.toUpperCase();
|
|
|
- if (normalized.contains("FAIL") || normalized.contains("NG")) {
|
|
|
- return new Ut5310TestResult(false, "FAIL", raw);
|
|
|
+ return buildModbusResult(sorting, voltages, values);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int readStartStopState(ModbusRtuClient client) {
|
|
|
+ try {
|
|
|
+ int[] state = client.readHoldingRegisters(REG_START_STOP, 1);
|
|
|
+ return state[0];
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("Read 0x0500 failed: {}", e.getMessage());
|
|
|
+ return -1;
|
|
|
}
|
|
|
- if (normalized.contains("PASS") || normalized.equals("OK") || normalized.startsWith("OK,")) {
|
|
|
- return new Ut5310TestResult(true, "PASS", raw);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 从寄存器块提取各步骤判读值(每步偏移+4) */
|
|
|
+ private static int[] extractSorting(int[] regs) {
|
|
|
+ int steps = regs.length / REGS_PER_STEP;
|
|
|
+ int[] sorting = new int[steps];
|
|
|
+ for (int i = 0; i < steps; i++) {
|
|
|
+ sorting[i] = regs[i * REGS_PER_STEP + 4];
|
|
|
}
|
|
|
- return null;
|
|
|
+ return sorting;
|
|
|
}
|
|
|
|
|
|
- private static void writeCommand(String command) throws IOException {
|
|
|
- outputStream.write((command + "\n").getBytes(COMMAND_CHARSET));
|
|
|
- outputStream.flush();
|
|
|
+ /** 从寄存器块提取各步骤测试电压(kV,每步偏移+0/+1) */
|
|
|
+ private static float[] extractVoltage(int[] regs) {
|
|
|
+ int steps = regs.length / REGS_PER_STEP;
|
|
|
+ float[] values = new float[steps];
|
|
|
+ for (int i = 0; i < steps; i++) {
|
|
|
+ int base = i * REGS_PER_STEP;
|
|
|
+ values[i] = ModbusRtuClient.registersToFloat(regs[base], regs[base + 1]);
|
|
|
+ }
|
|
|
+ return values;
|
|
|
}
|
|
|
|
|
|
- private static String readResponse() throws IOException {
|
|
|
- StringBuilder response = new StringBuilder();
|
|
|
- byte[] buffer = new byte[256];
|
|
|
- long deadline = System.currentTimeMillis() + readTimeoutMs;
|
|
|
- while (System.currentTimeMillis() < deadline) {
|
|
|
- int len = inputStream.read(buffer);
|
|
|
- if (len > 0) {
|
|
|
- response.append(new String(buffer, 0, len, COMMAND_CHARSET));
|
|
|
- if (response.indexOf("\n") >= 0) {
|
|
|
- break;
|
|
|
- }
|
|
|
- } else if (response.length() > 0) {
|
|
|
- break;
|
|
|
+ /** 从寄存器块提取各步骤电流或电阻(仪器原始:绝缘=MΩ,耐压=mA) */
|
|
|
+ private static float[] extractValue(int[] regs) {
|
|
|
+ int steps = regs.length / REGS_PER_STEP;
|
|
|
+ float[] values = new float[steps];
|
|
|
+ for (int i = 0; i < steps; i++) {
|
|
|
+ int base = i * REGS_PER_STEP;
|
|
|
+ values[i] = ModbusRtuClient.registersToFloat(regs[base + 2], regs[base + 3]);
|
|
|
+ }
|
|
|
+ return values;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 全部配置步骤的判读寄存器均非0时,才视为整次测试结束并提交MES/PLC。
|
|
|
+ * 避免步骤1(绝缘电阻)完成后过早提交、工装抬起而步骤2(直流耐压)仍在进行。
|
|
|
+ */
|
|
|
+ static boolean isSortingComplete(int[] sorting, float[] voltages, float[] values) {
|
|
|
+ int expected = Math.min(modbusExpectedSteps, sorting.length);
|
|
|
+ if (expected <= 0) {
|
|
|
+ expected = 1;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < expected; i++) {
|
|
|
+ if (sorting[i] == 0) {
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
- return response.toString().trim();
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- private static void drainInput() {
|
|
|
- try {
|
|
|
- while (inputStream != null && inputStream.available() > 0) {
|
|
|
- inputStream.read();
|
|
|
+ /** PANEL_01 等测试文件:步骤1绝缘电阻,步骤2直流耐压 */
|
|
|
+ private static String stepTypeName(int stepIndex) {
|
|
|
+ switch (stepIndex) {
|
|
|
+ case 0:
|
|
|
+ return "绝缘电阻";
|
|
|
+ case 1:
|
|
|
+ return "直流耐压";
|
|
|
+ default:
|
|
|
+ return "测试";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String stepDataUnit(int stepIndex) {
|
|
|
+ switch (stepIndex) {
|
|
|
+ case 0:
|
|
|
+ return "电阻(GΩ)";
|
|
|
+ case 1:
|
|
|
+ return "电流(μA)";
|
|
|
+ default:
|
|
|
+ return "测试值";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 仪器原始值转显示单位:MΩ→GΩ,mA→μA */
|
|
|
+ private static float convertStepData(int stepIndex, float rawValue) {
|
|
|
+ if (stepIndex == 0) {
|
|
|
+ return rawValue / 1000f;
|
|
|
+ }
|
|
|
+ if (stepIndex == 1) {
|
|
|
+ return rawValue * 1000f;
|
|
|
+ }
|
|
|
+ return rawValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String formatStepData(int stepIndex, float displayValue) {
|
|
|
+ if (stepIndex == 0) {
|
|
|
+ return String.format("%.4f", displayValue);
|
|
|
+ }
|
|
|
+ if (stepIndex == 1) {
|
|
|
+ return String.format("%.2f", displayValue);
|
|
|
+ }
|
|
|
+ return String.format("%.6f", displayValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String sortingToChinese(int sortCode) {
|
|
|
+ if (sortCode == 0) {
|
|
|
+ return "0x0000(测试中)";
|
|
|
+ }
|
|
|
+ return sortingToJudge(sortCode) + "(0x" + Integer.toHexString(sortCode).toUpperCase() + ")";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Ut5310TestResult buildModbusResult(int[] sorting, float[] voltages, float[] values) {
|
|
|
+ List<Ut5310TestResult.StepData> steps = new ArrayList<>();
|
|
|
+ StringBuilder raw = new StringBuilder();
|
|
|
+ raw.append("ModbusRTU,slave=").append(modbusSlaveId).append('\n');
|
|
|
+
|
|
|
+ boolean allPass = true;
|
|
|
+ String totalJudge = "OK";
|
|
|
+ int activeSteps = 0;
|
|
|
+
|
|
|
+ for (int i = 0; i < sorting.length; i++) {
|
|
|
+ if (sorting[i] == 0) {
|
|
|
+ continue;
|
|
|
}
|
|
|
- } catch (Exception ignored) {
|
|
|
+ activeSteps++;
|
|
|
+ int stepNo = i + 1;
|
|
|
+ String stepJudge = sortingToJudge(sorting[i]);
|
|
|
+ if (!"OK".equals(stepJudge) && !"PASS".equals(stepJudge)) {
|
|
|
+ allPass = false;
|
|
|
+ totalJudge = stepJudge;
|
|
|
+ }
|
|
|
+ float volt = voltages[i];
|
|
|
+ float displayData = convertStepData(i, values[i]);
|
|
|
+ String typeName = stepTypeName(i);
|
|
|
+ String dataUnit = stepDataUnit(i);
|
|
|
+ String dataText = formatStepData(i, displayData);
|
|
|
+ raw.append(String.format("步骤%d(%s),判读=0x%04X(%s),电压=%.4fkV,%s=%s%n",
|
|
|
+ stepNo, typeName, sorting[i], stepJudge, volt, dataUnit, dataText));
|
|
|
+ steps.add(new Ut5310TestResult.StepData(
|
|
|
+ stepNo, typeName, stepJudge,
|
|
|
+ "", "", String.format("%.4f", volt), "",
|
|
|
+ dataText));
|
|
|
+ System.out.println(String.format("[UT5310] 步骤%d(%s): 判读=%s, 电压=%.4fkV, %s=%s",
|
|
|
+ stepNo, typeName, sortingToChinese(sorting[i]), volt, dataUnit, dataText));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (activeSteps == 0) {
|
|
|
+ return new Ut5310TestResult(false, "FAIL", raw.toString());
|
|
|
+ }
|
|
|
+ if (allPass) {
|
|
|
+ totalJudge = "OK";
|
|
|
+ }
|
|
|
+ System.out.println("[UT5310] 全部" + modbusExpectedSteps + "个测试步骤已完成,提交结果");
|
|
|
+ return new Ut5310TestResult(allPass, totalJudge, raw.toString(), totalJudge, steps);
|
|
|
+ }
|
|
|
+
|
|
|
+ static String sortingToJudge(int sortCode) {
|
|
|
+ if (sortCode == SORT_PASS) {
|
|
|
+ return "OK";
|
|
|
+ }
|
|
|
+ switch (sortCode) {
|
|
|
+ case 0x0100: return "短路";
|
|
|
+ case 0x0101: return "电弧";
|
|
|
+ case 0x0110: return "接地失败";
|
|
|
+ case 0x0111: return "过压";
|
|
|
+ case 0x1000: return "上限不良";
|
|
|
+ case 0x1001: return "下限不良";
|
|
|
+ case 0x1010: return "充电下限";
|
|
|
+ default: return "NG(0x" + Integer.toHexString(sortCode).toUpperCase() + ")";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private static Protocol parseProtocol(String value) {
|
|
|
+ if (value == null) {
|
|
|
+ return Protocol.MODBUS;
|
|
|
+ }
|
|
|
+ String v = value.trim().toUpperCase();
|
|
|
+ if ("SCPI".equals(v)) {
|
|
|
+ return Protocol.SCPI;
|
|
|
+ }
|
|
|
+ if ("AT".equals(v) || "TEST".equals(v)) {
|
|
|
+ return Protocol.AT;
|
|
|
+ }
|
|
|
+ if ("AUTO".equals(v)) {
|
|
|
+ return Protocol.AUTO;
|
|
|
+ }
|
|
|
+ return Protocol.MODBUS;
|
|
|
+ }
|
|
|
+
|
|
|
private static int toStopBits(int stop) {
|
|
|
if (stop == 2) {
|
|
|
return SerialPort.TWO_STOP_BITS;
|
|
|
@@ -202,7 +513,7 @@ public class Ut5310ScpiService {
|
|
|
return SerialPort.NO_PARITY;
|
|
|
}
|
|
|
|
|
|
- private static void sleep(int millis) throws IOException {
|
|
|
+ private static void sleepQuiet(int millis) throws IOException {
|
|
|
try {
|
|
|
Thread.sleep(millis);
|
|
|
} catch (InterruptedException e) {
|
|
|
@@ -210,18 +521,4 @@ public class Ut5310ScpiService {
|
|
|
throw new IOException("UT5310 test interrupted", e);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private static void closeQuietly(Object closeable) {
|
|
|
- if (closeable == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- if (closeable instanceof InputStream) {
|
|
|
- ((InputStream) closeable).close();
|
|
|
- } else if (closeable instanceof OutputStream) {
|
|
|
- ((OutputStream) closeable).close();
|
|
|
- }
|
|
|
- } catch (Exception ignored) {
|
|
|
- }
|
|
|
- }
|
|
|
}
|