ATEQ.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.mes.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.util.Arrays;
  5. public class ATEQ {
  6. private static final Logger log = LoggerFactory.getLogger(ATEQ.class);
  7. private SerialUtil serialUtil;
  8. public String deviceName;
  9. public ATEQ(SerialUtil serialUtil, String deviceName) {
  10. this.serialUtil = serialUtil;
  11. this.deviceName = deviceName;
  12. }
  13. public void replaceSerialUtil(SerialUtil serialUtil) {
  14. this.serialUtil.serialPort.closePort();
  15. this.serialUtil = serialUtil;
  16. }
  17. public boolean stop() {
  18. byte[] read = serialUtil.read("01 05 00 00 FF 00 8C 3A");
  19. boolean result = Arrays.equals(read, DataUtils.hexStringToByteArray("01 05 00 00 FF 00 8C 3A"));
  20. log.info("{}: 气密仪停止{}",deviceName, result ? "成功" : "失败");
  21. return result;
  22. }
  23. public boolean start() {
  24. byte[] read = serialUtil.read("01 05 00 01 FF 00 DD FA");
  25. boolean result = Arrays.equals(read, DataUtils.hexStringToByteArray("01 05 00 01 FF 00 DD FA"));
  26. log.info("{}: 气密仪开始{}",deviceName, result ? "成功" : "失败");
  27. return result;
  28. }
  29. public boolean resetFIFO() {
  30. byte[] read = serialUtil.read("01 05 00 02 FF 00 2D FA");
  31. boolean result = Arrays.equals(read, DataUtils.hexStringToByteArray("01 05 00 02 FF 00 2D FA"));
  32. log.info("{}: 重置FIFO{}", deviceName, result ? "成功" : "失败");
  33. return result;
  34. }
  35. // 读取实时结果
  36. public RealtimeResult readRealtimeResult() {
  37. byte[] read = serialUtil.read("01 03 00 30 00 0D 84 00");
  38. if(read.length > 30) {
  39. return new RealtimeResult(read);
  40. } else {
  41. this.serialUtil.reconnect();
  42. log.error("{}: 读取<实时结果>时, 返回的数据长度不够, 请检查串口连接", deviceName);
  43. return null;
  44. }
  45. }
  46. // 读取先入先出队列中的待处理结果
  47. public FIFOResult readFIFOResult() {
  48. byte[] read = serialUtil.read("01 03 00 10 00 0D 85 CA");
  49. if(read.length > 28) {
  50. return new FIFOResult(read);
  51. } else {
  52. log.error("{}: 读取<FIFO结果>时, 返回的数据长度不够, 请检查串口连接", deviceName);
  53. return null;
  54. }
  55. }
  56. // 读取最后结果
  57. public LastResult readLastResult() {
  58. byte[] read = serialUtil.read("01 03 00 11 00 0C 15 CA");
  59. if(read.length > 20) {
  60. return new LastResult(read);
  61. } else {
  62. log.error("{}: 读取<最后结果>时, 返回的数据长度不够, 请检查串口连接", deviceName);
  63. return null;
  64. }
  65. }
  66. // 获取参数, 传入程序号, 获取对应程序号的参数
  67. public Parameters getParams(Integer programNumber) {
  68. // 步骤1: 选择要编辑的程序
  69. String s1 = DataUtils.CRC16("01 10 30 04 00 01 02 " + DataUtils.intToWord(programNumber - 1));
  70. serialUtil.read(s1);
  71. // 步骤2: 写入要读取的标识符
  72. String s2 = "01 10 00 00 00 04 08 03 00 01 00 02 00 03 00 F6 F6"; // s2 表示读取3个参数, 分别是 填充时间、稳定时间、测试时间
  73. serialUtil.read(s2);
  74. // 步骤3: 读取数据
  75. byte[] read = serialUtil.read("01 03 00 00 00 09 85 CC");
  76. if(read.length > 22) {
  77. return new Parameters(read);
  78. } else {
  79. log.error("{}: 读取<参数>时, 返回的数据长度不够, 请检查串口连接", deviceName);
  80. return null;
  81. }
  82. }
  83. public static class RealtimeResult {
  84. public final Integer programNumber;
  85. public final Integer FIFO_Number;
  86. public final String testType;
  87. public final Integer stepCode;
  88. public final String step;
  89. public final Double pressureValue;
  90. public final Double leakValue;
  91. public final Boolean isCycleEnd;
  92. public final Boolean isPass;
  93. public final Boolean isKeyExist;
  94. public final String pressureUnit;
  95. public final String leakUnit;
  96. public RealtimeResult(byte[] data) {
  97. this.programNumber = DataUtils.bytesToInt(Arrays.copyOfRange(data, 3, 5)) + 1;
  98. this.FIFO_Number = DataUtils.bytesToInt(Arrays.copyOfRange(data, 5, 7));
  99. this.testType = getTestTypeStr(DataUtils.bytesToInt(Arrays.copyOfRange(data, 7, 9)));
  100. int state = DataUtils.bytesToInt(Arrays.copyOfRange(data, 9, 11));
  101. this.isCycleEnd = DataUtils.getBit(state, 5) == 1;
  102. this.isPass = DataUtils.getBit(state, 0) == 1;
  103. this.isKeyExist = DataUtils.getBit(state, 15) == 1;
  104. this.stepCode = DataUtils.bytesToInt(Arrays.copyOfRange(data, 11, 13));
  105. this.step = getStepStr(this.stepCode);
  106. this.pressureValue = DataUtils.bytesToInt(Arrays.copyOfRange(data, 13, 17)) / 1000.0;
  107. this.pressureUnit = getUnit(DataUtils.bytesToInt(Arrays.copyOfRange(data, 17, 21)));
  108. this.leakValue = DataUtils.bytesToInt(Arrays.copyOfRange(data, 21, 25)) / 1000.0;
  109. this.leakUnit = getUnit(DataUtils.bytesToInt(Arrays.copyOfRange(data, 25, 29)));
  110. }
  111. @Override
  112. public String toString() {
  113. return "实时结果{" +
  114. " 压力值: " + pressureValue +
  115. ", 泄漏值: " + leakValue +
  116. ", FIFO数量: " + FIFO_Number +
  117. ", 当前步骤: " + step +
  118. ", 循环是否结束: " + isCycleEnd +
  119. ", 测试通过: " + isPass +
  120. ", 是否关键存在: " + isKeyExist +
  121. ", 程序号: " + programNumber +
  122. ", 测试类型: " + testType +
  123. '}';
  124. }
  125. }
  126. public static class FIFOResult {
  127. public final Integer programNumber;
  128. public final String testType;
  129. public final Double pressureValue;
  130. public final Double leakValue;
  131. public final Boolean isPass;
  132. public final Boolean isTestLeak;
  133. public final Boolean isReferLeak;
  134. public final String pressureUnit;
  135. public final String leakUnit;
  136. public FIFOResult(byte[] data) {
  137. this.programNumber = DataUtils.bytesToInt(Arrays.copyOfRange(data, 3, 5)) + 1;
  138. this.testType = getTestTypeStr(DataUtils.bytesToInt(Arrays.copyOfRange(data, 5, 7)));
  139. int state = DataUtils.bytesToInt(Arrays.copyOfRange(data, 7, 9));
  140. this.isPass = DataUtils.getBit(state, 0) == 1;
  141. this.isTestLeak = DataUtils.getBit(state, 1) == 1;
  142. this.isReferLeak = DataUtils.getBit(state, 2) == 1;
  143. this.pressureValue = DataUtils.bytesToInt(Arrays.copyOfRange(data, 11, 15)) / 1000.0;
  144. this.pressureUnit = getUnit(DataUtils.bytesToInt(Arrays.copyOfRange(data, 15, 19)));
  145. this.leakValue = DataUtils.bytesToInt(Arrays.copyOfRange(data, 19, 23)) / 1000.0;
  146. this.leakUnit = getUnit(DataUtils.bytesToInt(Arrays.copyOfRange(data, 23, 27)));
  147. }
  148. @Override
  149. public String toString() {
  150. return "FIFO结果{" +
  151. " 压力值: " + pressureValue +
  152. ", 泄漏值: " + leakValue +
  153. ", 测试通过: " + isPass +
  154. ", 测试泄漏: " + isTestLeak +
  155. ", 参考泄漏: " + isReferLeak +
  156. ", 程序号: " + programNumber +
  157. ", 测试类型: " + testType +
  158. '}';
  159. }
  160. }
  161. // 最后结果类
  162. public static class LastResult {
  163. public final Integer programNumber; // 程序号
  164. public final String testType; // 测试类型
  165. public final Double pressureValue; // 压力值
  166. public final Double leakValue; // 泄漏值
  167. public final Boolean isPass; // 测试是否通过(true表示OK, false表示NG)
  168. public final Boolean isTestLeak;
  169. public final Boolean isReferLeak;
  170. public final String pressureUnit; // 压力单位
  171. public final String leakUnit; // 测试单位
  172. public LastResult(byte[] data) {
  173. this.programNumber = DataUtils.bytesToInt(Arrays.copyOfRange(data, 3, 5)) + 1;
  174. this.testType = getTestTypeStr(DataUtils.bytesToInt(Arrays.copyOfRange(data, 5, 7)));
  175. int state = DataUtils.bytesToInt(Arrays.copyOfRange(data, 7, 9));
  176. this.isPass = DataUtils.getBit(state, 0) == 1;
  177. this.isTestLeak = DataUtils.getBit(state, 1) == 1;
  178. this.isReferLeak = DataUtils.getBit(state, 2) == 1;
  179. this.pressureValue = DataUtils.bytesToInt(Arrays.copyOfRange(data, 11, 15)) / 1000.0;
  180. this.pressureUnit = getUnit(DataUtils.bytesToInt(Arrays.copyOfRange(data, 15, 19)));
  181. this.leakValue = DataUtils.bytesToInt(Arrays.copyOfRange(data, 19, 23)) / 1000.0;
  182. this.leakUnit = getUnit(DataUtils.bytesToInt(Arrays.copyOfRange(data, 23, 27)));
  183. }
  184. @Override
  185. public String toString() {
  186. return "最后结果{" +
  187. " 压力值: " + pressureValue +
  188. ", 泄漏值: " + leakValue +
  189. ", 测试通过: " + isPass +
  190. ", 测试泄漏: " + isTestLeak +
  191. ", 参考泄漏: " + isReferLeak +
  192. ", 程序号: " + programNumber +
  193. ", 测试类型: " + testType +
  194. ", 压力单位: " + pressureUnit +
  195. ", 泄漏单位: " + leakUnit +
  196. '}';
  197. }
  198. }
  199. public static class Parameters {
  200. public final Integer fillTime; // 填充时间
  201. public final Integer stabilizeTime; // 稳定时间
  202. public final Integer testTime; // 测试时间
  203. public Parameters(byte[] data) {
  204. this.fillTime = DataUtils.bytesToInt(Arrays.copyOfRange(data, 5, 9)) / 1000;
  205. this.stabilizeTime = DataUtils.bytesToInt(Arrays.copyOfRange(data, 11, 15)) / 1000;
  206. this.testTime = DataUtils.bytesToInt(Arrays.copyOfRange(data, 17, 21)) / 1000;
  207. }
  208. @Override
  209. public String toString() {
  210. return "参数{" +
  211. "填充时间: " + fillTime +
  212. "s, 稳定时间: " + stabilizeTime +
  213. "s, 测试时间: " + testTime +
  214. "s}";
  215. }
  216. }
  217. private static String getTestTypeStr(Integer testTypeCode) {
  218. String s = "未知";
  219. if(testTypeCode == 1) {
  220. s = "泄漏";
  221. }
  222. return s;
  223. }
  224. private static String getStepStr(Integer stepCode) {
  225. String s = "未知";
  226. switch (stepCode) {
  227. case 0:
  228. s = "预填充";
  229. break;
  230. case 1:
  231. s = "预倒置";
  232. break;
  233. case 4:
  234. s = "填充";
  235. break;
  236. case 5:
  237. s = "稳定化";
  238. break;
  239. case 6:
  240. s = "测试";
  241. break;
  242. case 7:
  243. s = "输出";
  244. break;
  245. case 65535:
  246. s = "无步骤";
  247. break;
  248. }
  249. return s;
  250. }
  251. private static String getUnit(Integer unitCode) {
  252. String s = "";
  253. switch (unitCode) {
  254. case 0:
  255. s = "cm3/s";
  256. break;
  257. case 1000:
  258. s = "cm3/min";
  259. break;
  260. case 2000:
  261. s = "cm3/h";
  262. break;
  263. case 6000:
  264. s = "Pa";
  265. break;
  266. case 8000:
  267. s = "Pa/s";
  268. break;
  269. case 11000:
  270. s = "Bar";
  271. break;
  272. case 12000:
  273. s = "kPa";
  274. break;
  275. case 50000:
  276. s = "ml/s";
  277. break;
  278. case 51000:
  279. s = "ml/min";
  280. break;
  281. case 52000:
  282. s = "ml/h";
  283. break;
  284. }
  285. return s;
  286. }
  287. }