石墨消解仪后端服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.7 KiB

  1. package com.iflytop.gd.hardware.drivers;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.iflytop.gd.hardware.comm.can.A8kCanBusService;
  4. import com.iflytop.gd.hardware.comm.modbus.ModbusMasterService;
  5. import com.iflytop.gd.hardware.exception.HardwareException;
  6. import com.iflytop.gd.hardware.type.A8kPacket;
  7. import com.iflytop.gd.hardware.type.CmdId;
  8. import com.iflytop.gd.hardware.type.driver.HeaterRodSlavedId;
  9. import lombok.RequiredArgsConstructor;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.stereotype.Component;
  12. import java.util.Map;
  13. import java.util.concurrent.ConcurrentHashMap;
  14. @Slf4j
  15. @Component
  16. @RequiredArgsConstructor
  17. public class HeaterRodDriver {
  18. private final A8kCanBusService canBus;
  19. private final ModbusMasterService modbusMaster;
  20. private static final int SV_ADDRESS = 0x2000;
  21. private static final int PV_ADDRESS = 0x2010;
  22. private static final int CONTROL_REGISTER = 0x2106;
  23. private static final int DP_ADDRESS = 0x2107;
  24. private static final int DEVICE_ADDRESS_REGISTER = 0x2112;
  25. private final Map<Integer, Integer> dpCache = new ConcurrentHashMap<>();
  26. private static final int DEFAULT_DP = 1;
  27. public void setDeviceAddress(HeaterRodSlavedId heaterRodId, int newAddress) throws Exception {
  28. int slaveId = heaterRodId.getSlaveId();
  29. if (!modbusMaster.writeRegister(slaveId, DEVICE_ADDRESS_REGISTER, newAddress)) {
  30. throw new Exception("Failed to set device address for heater rod with ID: " + heaterRodId.name());
  31. }
  32. log.info("Device address for heater rod with ID: {} set to: {}", heaterRodId.name(), newAddress);
  33. }
  34. private int getDecimalPoint(int slaveId) throws Exception {
  35. return dpCache.computeIfAbsent(slaveId, id -> {
  36. try {
  37. short[] response = modbusMaster.readHoldingRegisters(id, DP_ADDRESS, 1);
  38. if (response != null && response.length > 0) {
  39. return (int) response[0];
  40. }
  41. } catch (Exception e) {
  42. log.warn("Failed to read DP for slave ID: {}, using default DP: {}", id, DEFAULT_DP);
  43. }
  44. return DEFAULT_DP;
  45. });
  46. }
  47. public void openPower(HeaterRodSlavedId heaterRodId) throws Exception
  48. {
  49. int slaveId = heaterRodId.getSlaveId();
  50. if (!modbusMaster.writeRegister(slaveId, CONTROL_REGISTER, 1)) { // 1 = Start
  51. throw new Exception("Failed to start heater rod with ID: " + heaterRodId.name());
  52. }
  53. }
  54. public void closePower(HeaterRodSlavedId heaterRodId) throws Exception
  55. {
  56. int slaveId = heaterRodId.getSlaveId();
  57. if (!modbusMaster.writeRegister(slaveId, CONTROL_REGISTER, 2)) { // 2 = Stop
  58. throw new Exception("Failed to stop heater rod with ID: " + heaterRodId.name());
  59. }
  60. log.info("{} stopped.", heaterRodId.name());
  61. }
  62. public void setTemperature(HeaterRodSlavedId heaterRodId, double temperature) throws Exception {
  63. int slaveId = heaterRodId.getSlaveId();
  64. int decimalPoint = getDecimalPoint(slaveId);
  65. int scaledTemperature = (int) (temperature * Math.pow(10, decimalPoint));
  66. if (!modbusMaster.writeRegister(slaveId, SV_ADDRESS, scaledTemperature)) {
  67. throw new Exception("Failed to set temperature for heater rod with ID: " + heaterRodId.name());
  68. }
  69. log.info("{} started with temperature: {}", heaterRodId.name(), temperature);
  70. }
  71. public double getTemperature(HeaterRodSlavedId heaterRodId) throws Exception {
  72. int slaveId = heaterRodId.getSlaveId();
  73. int decimalPoint = getDecimalPoint(slaveId);
  74. short[] response = modbusMaster.readHoldingRegisters(slaveId, PV_ADDRESS, 1);
  75. if (response == null || response.length == 0) {
  76. throw new Exception(StrUtil.format("Failed to read temperature {}", heaterRodId.name()));
  77. }
  78. double temperature = response[0] / Math.pow(10, decimalPoint);
  79. log.info("{} GET temperature {}", heaterRodId.name(), temperature);
  80. return temperature;
  81. }
  82. /**
  83. * 获取电流
  84. * @return
  85. */
  86. public Integer getCurrent(HeaterRodSlavedId heaterRodId) throws HardwareException
  87. {
  88. A8kPacket packet = canBus.callcmd(heaterRodId.getMid(), CmdId.adc_read_adc, heaterRodId.getAdcCurrentIndex());
  89. Integer current = packet.getContentI32(0);
  90. return current;
  91. }
  92. // ==== ==== ==== ==== ==== ==== application ==== ==== ==== ==== ==== ====
  93. public void open(HeaterRodSlavedId heaterRodId, double temperature) throws Exception {
  94. this.openPower(heaterRodId);
  95. this.setTemperature(heaterRodId, temperature);
  96. }
  97. public void close(HeaterRodSlavedId heaterRodId) throws Exception {
  98. this.closePower(heaterRodId);
  99. }
  100. }