FswModbusTest.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.jeesite.modules;
  2. import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
  3. import com.jeesite.common.config.Global;
  4. import com.jeesite.common.lang.StringUtils;
  5. import com.jeesite.modules.mes.entity.MesProductFsw;
  6. import com.jeesite.modules.mes.service.MesProductFswService;
  7. import org.springframework.boot.SpringApplication;
  8. import org.springframework.context.ConfigurableApplicationContext;
  9. import java.nio.charset.StandardCharsets;
  10. import java.util.Date;
  11. /**
  12. * FSW Modbus 直采测试:不看 4740 / 3600 / 质量校验,直接读点位并写入 mes_product_fsw。
  13. * <p>
  14. * 右键运行 main 即可。
  15. */
  16. public class FswModbusTest {
  17. // ========== 点位 ==========
  18. private static final int ADDR_SN_START = 3620;
  19. private static final int SN_REGISTER_COUNT = 60;
  20. private static final int ADDR_FEED_SPEED = 3740;
  21. private static final int ADDR_FEED_RATE = 3780;
  22. private static final int ADDR_SPINDLE_SPEED = 3820;
  23. private static final int ADDR_SPINDLE_RATE = 3860;
  24. private static final int ADDR_WELD_PRESSURE = 4040;
  25. private static final int ADDR_WELD_OFFSET = 4590;
  26. private static final int ADDR_B_AXIS_ANGLE = 4742;
  27. /** 落库固定产线 / 工位 */
  28. private static final String LINE_SN = "XT";
  29. private static final String OPRNO = "OP230B";
  30. /**
  31. * 指定工件码;为空则读 3620,仍为空则用 FSW_TEST_时间戳(保证一定能落库)
  32. */
  33. private static final String FORCE_SN = "";
  34. public static void main(String[] args) {
  35. System.out.println("========== FSW 直采(无视前置条件)→ mes_product_fsw ==========");
  36. ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
  37. ModbusTcp plc = null;
  38. try {
  39. MesProductFswService fswService = ctx.getBean(MesProductFswService.class);
  40. String host = Global.getConfig("mes.fsw.modbus.host");
  41. if (StringUtils.isBlank(host)) {
  42. host = "10.0.20.220";
  43. }
  44. int port = parseInt(Global.getConfig("mes.fsw.modbus.port"), 502);
  45. int unitId = parseInt(Global.getConfig("mes.fsw.modbus.unitId"), 1);
  46. System.out.println("连接 Modbus " + host + ":" + port + " unitId=" + unitId);
  47. plc = new ModbusTcp(unitId, host, port);
  48. String sn = StringUtils.isNotBlank(FORCE_SN) ? FORCE_SN.trim() : readWorkpieceCode(plc);
  49. if (StringUtils.isBlank(sn)) {
  50. sn = "FSW_TEST_" + System.currentTimeMillis();
  51. System.out.println("3620 无工件码,使用临时 sn=" + sn);
  52. } else {
  53. System.out.println("工件码 sn=" + sn);
  54. }
  55. short feedSpeed = plc.readInt16(ADDR_FEED_SPEED);
  56. short feedRate = plc.readInt16(ADDR_FEED_RATE);
  57. short spindleSpeed = plc.readInt16(ADDR_SPINDLE_SPEED);
  58. short spindleRate = plc.readInt16(ADDR_SPINDLE_RATE);
  59. short pressure = plc.readInt16(ADDR_WELD_PRESSURE);
  60. short pyl = plc.readInt16(ADDR_WELD_OFFSET);
  61. short jd = plc.readInt16(ADDR_B_AXIS_ANGLE);
  62. System.out.println("进给速度(3740)=" + feedSpeed);
  63. System.out.println("进给倍率(3780)=" + feedRate);
  64. System.out.println("主轴转速(3820)=" + spindleSpeed);
  65. System.out.println("主轴倍率(3860)=" + spindleRate);
  66. System.out.println("焊接压力(4040)=" + pressure);
  67. System.out.println("偏移量(4590)=" + pyl);
  68. System.out.println("B轴角度(4742)=" + jd);
  69. MesProductFsw entity = new MesProductFsw();
  70. entity.setSn(sn);
  71. entity.setOprno(OPRNO);
  72. entity.setLineSn(LINE_SN);
  73. entity.setFeedSpeed(String.valueOf(feedSpeed));
  74. entity.setFeedRate(String.valueOf(feedRate));
  75. entity.setSpindleSpeed(String.valueOf(spindleSpeed));
  76. entity.setSpindleRate(String.valueOf(spindleRate));
  77. entity.setPressure(String.valueOf(pressure));
  78. entity.setPyl(String.valueOf(pyl));
  79. entity.setJd(String.valueOf(jd));
  80. entity.setCreateDate(new Date());
  81. fswService.save(entity);
  82. System.out.println(">>> 已保存 mes_product_fsw id=" + entity.getId()
  83. + " sn=" + entity.getSn()
  84. + " oprno=" + entity.getOprno()
  85. + " lineSn=" + entity.getLineSn());
  86. } catch (Exception e) {
  87. System.err.println("直采失败: " + e.getMessage());
  88. e.printStackTrace();
  89. } finally {
  90. if (plc != null) {
  91. try {
  92. plc.close();
  93. } catch (Exception ignore) {
  94. }
  95. }
  96. ctx.close();
  97. System.out.println("========== 结束 ==========");
  98. }
  99. }
  100. private static String readWorkpieceCode(ModbusTcp plc) {
  101. byte[] bytes = plc.readHoldRegister(ADDR_SN_START, SN_REGISTER_COUNT);
  102. if (bytes == null || bytes.length == 0) {
  103. return null;
  104. }
  105. String raw = new String(bytes, StandardCharsets.US_ASCII);
  106. StringBuilder sb = new StringBuilder(raw.length());
  107. for (int i = 0; i < raw.length(); i++) {
  108. char c = raw.charAt(i);
  109. if (c == 0 || c == ' ') {
  110. continue;
  111. }
  112. if (c >= 32 && c < 127) {
  113. sb.append(c);
  114. }
  115. }
  116. String sn = sb.toString().trim();
  117. return sn.isEmpty() ? null : sn;
  118. }
  119. private static int parseInt(String val, int defaultVal) {
  120. if (StringUtils.isBlank(val)) {
  121. return defaultVal;
  122. }
  123. try {
  124. return Integer.parseInt(val.trim());
  125. } catch (Exception e) {
  126. return defaultVal;
  127. }
  128. }
  129. }