tokio-modbus icon indicating copy to clipboard operation
tokio-modbus copied to clipboard

RTU Custom Request Mismatching Function Code

Open EzekielDaun opened this issue 3 months ago • 1 comments

Thanks for this awesome crate!

I can use read_holding_registers without issue. However, when I send a custom request with function code 0x03 (to mimic read_holding_registers), the call

let request = tokio_modbus::Request::Custom(3, Cow::Owned([0x01, 0x52, 0x00, 0x01].to_vec()));
ctx.call(request).await??;

returns:

Error: mismatching function codes: 3 Ok(ReadHoldingRegisters([47826]))

I skimmed through the source code but still couldn’t figure out where this mismatch is coming from. And the value 47826 is correct.

Am I misunderstanding how the custom request is supposed to be used?

EzekielDaun avatar Oct 01 '25 05:10 EzekielDaun

Are you sure that the server also sends back 0x03?

The check is generally done here: https://github.com/slowtec/tokio-modbus/blob/4a061c4d0f16835606b85fb2a62285e3967cba2c/src/service/rtu.rs#L84-L94

flosse avatar Oct 21 '25 00:10 flosse