ModbusRequestDecoder implements ModbusPduDecoder { @Override public ModbusPdu decode(ByteBuf buffer) throws DecoderException { int code = buffer.readByte(); FunctionCode functionCode = FunctionCode .fromCode(code) .orElseThrow(() -> new DecoderException("invalid function code: " + code)); return decodeResponse(functionCode, buffer); }  @Override ModbusPdu decode(ByteBuf buffer);  }
@Test public void testDecodeWriteMultipleRegistersRequest() { ModbusRequestEncoder encoder = new ModbusRequestEncoder(); WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest( 0, 64, new byte[128] ); ByteBuf encoded = encoder.encode(request, Unpooled.buffer()); ModbusRequestDecoder decoder = new ModbusRequestDecoder(); WriteMultipleRegistersRequest decoded = (WriteMultipleRegistersRequest) decoder.decode(encoded); assertNotNull(decoded); assertEquals(decoded.getAddress(), 0); assertEquals(decoded.getQuantity(), 64); assertEquals(decoded.getValues().readableBytes(), 128); }