ceremcem / modbus_lib

Lightweight and easy to merge Modbus RTU Slave library for microcontrollers
MIT License
15 stars 7 forks source link

MODBUS Exception Responses #2

Closed unreal79 closed 2 years ago

unreal79 commented 2 years ago

According to _Modbus_Application_Protocol_V11b3.pdf:

Function Code Field: In a normal response, the server echoes the function code of the original request in the function code field of the response. All function codes have a most–significant bit (MSB) of 0 (their values are all below 80 hexadecimal). In an exception response, the server sets the MSB of the function code to 1. This makes the function code value in an exception response exactly 80 hexadecimal higher than the value would be for a normal response.

Currently modbus_lib_send_error does not comply to the Modbus standard. It is easily patchable:

        uint8_t res[MB_EXCEPTION_LENGTH] = {
            config->address, 
            g_modbus_lib_received_telegram[1] + 0x80,
            error_code
        };
ceremcem commented 2 years ago

Thanks for the report and the patch. I spent some time to understand how the g_modbus_lib_received_telegram[1] **+ 0x80** line sets the MSB but I failed to do so. Is there any reason for that line not to be g_modbus_lib_received_telegram[1] | 0x80?

unreal79 commented 2 years ago

Sorry for the misleading ** part (I was trying to emphasize the +0x80 inside code section, but failed). Edited.

I see no reason not to use | 0x80 -- it's a tiny bit faster then addition.

ceremcem commented 2 years ago

Sorry for being unresponsive for a while. Would you like to create a PR for this fix to get credit?

unreal79 commented 2 years ago

No, submit the commit yourself please.