| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package com.jeesite.modules;
- import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
- import com.jeesite.common.config.Global;
- import com.jeesite.common.lang.StringUtils;
- import com.jeesite.modules.mes.entity.MesProductFsw;
- import com.jeesite.modules.mes.service.MesProductFswService;
- import org.springframework.boot.SpringApplication;
- import org.springframework.context.ConfigurableApplicationContext;
- import java.nio.charset.StandardCharsets;
- import java.util.Date;
- /**
- * FSW Modbus 直采测试:不看 4740 / 3600 / 质量校验,直接读点位并写入 mes_product_fsw。
- * <p>
- * 右键运行 main 即可。
- */
- public class FswModbusTest {
- // ========== 点位 ==========
- private static final int ADDR_SN_START = 3620;
- private static final int SN_REGISTER_COUNT = 60;
- private static final int ADDR_FEED_SPEED = 3740;
- private static final int ADDR_FEED_RATE = 3780;
- private static final int ADDR_SPINDLE_SPEED = 3820;
- private static final int ADDR_SPINDLE_RATE = 3860;
- private static final int ADDR_WELD_PRESSURE = 4040;
- private static final int ADDR_WELD_OFFSET = 4590;
- private static final int ADDR_B_AXIS_ANGLE = 4742;
- /** 落库固定产线 / 工位 */
- private static final String LINE_SN = "XT";
- private static final String OPRNO = "OP230B";
- /**
- * 指定工件码;为空则读 3620,仍为空则用 FSW_TEST_时间戳(保证一定能落库)
- */
- private static final String FORCE_SN = "";
- public static void main(String[] args) {
- System.out.println("========== FSW 直采(无视前置条件)→ mes_product_fsw ==========");
- ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
- ModbusTcp plc = null;
- try {
- MesProductFswService fswService = ctx.getBean(MesProductFswService.class);
- String host = Global.getConfig("mes.fsw.modbus.host");
- if (StringUtils.isBlank(host)) {
- host = "10.0.20.220";
- }
- int port = parseInt(Global.getConfig("mes.fsw.modbus.port"), 502);
- int unitId = parseInt(Global.getConfig("mes.fsw.modbus.unitId"), 1);
- System.out.println("连接 Modbus " + host + ":" + port + " unitId=" + unitId);
- plc = new ModbusTcp(unitId, host, port);
- String sn = StringUtils.isNotBlank(FORCE_SN) ? FORCE_SN.trim() : readWorkpieceCode(plc);
- if (StringUtils.isBlank(sn)) {
- sn = "FSW_TEST_" + System.currentTimeMillis();
- System.out.println("3620 无工件码,使用临时 sn=" + sn);
- } else {
- System.out.println("工件码 sn=" + sn);
- }
- short feedSpeed = plc.readInt16(ADDR_FEED_SPEED);
- short feedRate = plc.readInt16(ADDR_FEED_RATE);
- short spindleSpeed = plc.readInt16(ADDR_SPINDLE_SPEED);
- short spindleRate = plc.readInt16(ADDR_SPINDLE_RATE);
- short pressure = plc.readInt16(ADDR_WELD_PRESSURE);
- short pyl = plc.readInt16(ADDR_WELD_OFFSET);
- short jd = plc.readInt16(ADDR_B_AXIS_ANGLE);
- System.out.println("进给速度(3740)=" + feedSpeed);
- System.out.println("进给倍率(3780)=" + feedRate);
- System.out.println("主轴转速(3820)=" + spindleSpeed);
- System.out.println("主轴倍率(3860)=" + spindleRate);
- System.out.println("焊接压力(4040)=" + pressure);
- System.out.println("偏移量(4590)=" + pyl);
- System.out.println("B轴角度(4742)=" + jd);
- MesProductFsw entity = new MesProductFsw();
- entity.setSn(sn);
- entity.setOprno(OPRNO);
- entity.setLineSn(LINE_SN);
- entity.setFeedSpeed(String.valueOf(feedSpeed));
- entity.setFeedRate(String.valueOf(feedRate));
- entity.setSpindleSpeed(String.valueOf(spindleSpeed));
- entity.setSpindleRate(String.valueOf(spindleRate));
- entity.setPressure(String.valueOf(pressure));
- entity.setPyl(String.valueOf(pyl));
- entity.setJd(String.valueOf(jd));
- entity.setCreateDate(new Date());
- fswService.save(entity);
- System.out.println(">>> 已保存 mes_product_fsw id=" + entity.getId()
- + " sn=" + entity.getSn()
- + " oprno=" + entity.getOprno()
- + " lineSn=" + entity.getLineSn());
- } catch (Exception e) {
- System.err.println("直采失败: " + e.getMessage());
- e.printStackTrace();
- } finally {
- if (plc != null) {
- try {
- plc.close();
- } catch (Exception ignore) {
- }
- }
- ctx.close();
- System.out.println("========== 结束 ==========");
- }
- }
- private static String readWorkpieceCode(ModbusTcp plc) {
- byte[] bytes = plc.readHoldRegister(ADDR_SN_START, SN_REGISTER_COUNT);
- if (bytes == null || bytes.length == 0) {
- return null;
- }
- String raw = new String(bytes, StandardCharsets.US_ASCII);
- StringBuilder sb = new StringBuilder(raw.length());
- for (int i = 0; i < raw.length(); i++) {
- char c = raw.charAt(i);
- if (c == 0 || c == ' ') {
- continue;
- }
- if (c >= 32 && c < 127) {
- sb.append(c);
- }
- }
- String sn = sb.toString().trim();
- return sn.isEmpty() ? null : sn;
- }
- private static int parseInt(String val, int defaultVal) {
- if (StringUtils.isBlank(val)) {
- return defaultVal;
- }
- try {
- return Integer.parseInt(val.trim());
- } catch (Exception e) {
- return defaultVal;
- }
- }
- }
|