AbstractModbusConfirm.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package wei.yigulu.modbus.domain.confirm;
  2. import lombok.Getter;
  3. import lombok.Setter;
  4. import lombok.experimental.Accessors;
  5. import wei.yigulu.modbus.domain.FunctionCode;
  6. import wei.yigulu.modbus.domain.ModbusPacketInterface;
  7. import wei.yigulu.modbus.domain.datatype.numeric.P_AB;
  8. import wei.yigulu.modbus.exceptiom.ModbusException;
  9. import java.math.BigDecimal;
  10. import java.nio.ByteBuffer;
  11. import java.util.List;
  12. /**
  13. * @program: protocol
  14. * @description: modbus的控制命令
  15. * @author: xiuwei
  16. * @create: 2021-04-26 16:14
  17. */
  18. @Getter
  19. @Setter
  20. @Accessors(chain = true)
  21. public abstract class AbstractModbusConfirm implements ModbusPacketInterface {
  22. /**
  23. * 客户端地址 一字节
  24. */
  25. protected Integer slaveId = 01;
  26. /**
  27. * 功能码 一字节 5,6,15,16
  28. */
  29. protected FunctionCode functionCode;
  30. /**
  31. * 下达数据的起始地址位 两字节
  32. */
  33. protected Integer startAddress;
  34. /**
  35. * 输出数据的数量(15,16) 两字节
  36. */
  37. protected Integer quantity;
  38. /**
  39. * 输出数据 (5,6) 两字节
  40. */
  41. protected byte[] b2= new byte[2];
  42. public Integer getLength() {
  43. return 8;
  44. }
  45. @Override
  46. public AbstractModbusConfirm encode(List<Byte> bytes) throws ModbusException {
  47. bytes.add((byte) (slaveId & 0xff));
  48. bytes.add((byte) (functionCode.getCode() & 0xff));
  49. new P_AB(BigDecimal.valueOf(startAddress)).encode(bytes);
  50. if (functionCode == FunctionCode.WRITE_COIL || functionCode == FunctionCode.WRITE_REGISTER) {
  51. bytes.add(b2[0]);
  52. bytes.add(b2[1]);
  53. }else {
  54. new P_AB(BigDecimal.valueOf(quantity)).encode(bytes);
  55. }
  56. return this;
  57. }
  58. /**
  59. * 解码
  60. *
  61. * @param byteBuf 字节缓冲区
  62. * @return
  63. */
  64. @Override
  65. public AbstractModbusConfirm decode(ByteBuffer byteBuf) throws ModbusException {
  66. slaveId = byteBuf.get() & 0xff;
  67. functionCode = FunctionCode.valueOf(byteBuf.get() & 0xff);
  68. if (functionCode.getCode() > 0x80) {
  69. int i = byteBuf.get() & 0xff;
  70. if (functionCode.getCode().equals(0x89) || functionCode.getCode().equals(0x90)) {
  71. switch (i) {
  72. case 1:
  73. throw new ModbusException("功能码异常");
  74. case 3:
  75. throw new ModbusException("输出数据数量或字节数异常");
  76. case 2:
  77. throw new ModbusException("地址异常或输出数量异常");
  78. case 4:
  79. throw new ModbusException("写入过程异常");
  80. }
  81. } else {
  82. switch (i) {
  83. case 1:
  84. throw new ModbusException("功能码异常");
  85. case 3:
  86. throw new ModbusException("数据值异常");
  87. case 2:
  88. throw new ModbusException("地址异常");
  89. case 4:
  90. throw new ModbusException("写入过程异常");
  91. }
  92. }
  93. }
  94. startAddress = new P_AB().decode(byteBuf).getValue().intValue();
  95. if (functionCode == FunctionCode.WRITE_COIL || functionCode == FunctionCode.WRITE_REGISTER) {
  96. b2 = new byte[2];
  97. byteBuf.get(b2);
  98. } else {
  99. quantity = new P_AB().decode(byteBuf).getValue().intValue();
  100. }
  101. return this;
  102. }
  103. }