Skip to content

Add extended response filter support to ModbusBridge - #438

Merged
Miq1 merged 2 commits into
eModbus:masterfrom
roelandkluit:master
Aug 3, 2026
Merged

Miq1 merged 2 commits into
eModbus:masterfrom
roelandkluit:master

Conversation

@roelandkluit

Copy link
Copy Markdown
Contributor

Introduced addresponseExFilter and removeresponseExFilter methods to enable adding and removing extended response filters (MBSExResponseWorker) for specific aliasIDs.

Added a new type alias MBSExResponseWorker in ModbusServer.h to support more complex filtering based on read start address and count. Updated the ServerData structure to include a responseExFilter member and initialized it in constructors.

Modified the bridgeWorker method to invoke responseExFilter if set, falling back to responseFilter otherwise if set. Added logging and updated comments to reflect the new functionality.

These changes enhance the ModbusBridge class, enabling more advanced and flexible filtering mechanisms for Modbus messages after receiving the request, allowing to handle the response with the original request address and count in mind.

Introduced `addresponseExFilter` and `removeresponseExFilter` methods to enable adding and removing extended response filters (`MBSExResponseWorker`) for specific `aliasID`s.

Added a new type alias `MBSExResponseWorker` in `ModbusServer.h` to support more complex filtering based on read start address and count. Updated the `ServerData` structure to include a `responseExFilter` member and initialized it in constructors.

Modified the `bridgeWorker` method to invoke `responseExFilter` if set, falling back to `responseFilter` otherwise. Added logging for better debugging and updated comments to reflect the new functionality.

These changes enhance the `ModbusBridge` class, enabling more advanced and flexible filtering mechanisms for Modbus messages.
@Miq1

Miq1 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

I am afraid I have some objections against your PR.

Primarily it does not add real benefit as compared to the existing filter hooks, since the extraction of both address and length can easily be done within a regular filter callback.

Second, and even more relevant, the proposed change will try to extract these values even from Modbus messages that may have a completely different structure and hence will result in invalid data.

@Miq1 Miq1 self-assigned this Aug 2, 2026
Simplified the extended response filter hook logic in `ModbusBridgeTemp.h` by passing the entire request message
to `responseExFilter`, removing the individual parameters.

Updated the `MBSExResponseWorker` function signature in `ModbusServer.h` to accept two `ModbusMessage` parameters
(`responseMsg` and `requestMsg`)
@roelandkluit

Copy link
Copy Markdown
Contributor Author

Changed to include the full request message reference along the response.

@Miq1

Miq1 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

How is this now supposed to work? Where do you get the matching request and response messages when calling the filter?

@roelandkluit

Copy link
Copy Markdown
Contributor Author

These are populated in src/ModbusBridgeTemp.h, bridgeWorker, line 314-319

@Miq1

Miq1 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Can you give me an example on how to make a use from the extended callback?

@roelandkluit

Copy link
Copy Markdown
Contributor Author

I have implemented it like the following snipit. It allows to change the response parsing to be depended upon the request:

// filterResponseEx() - to be applied to returning responses
ModbusMessage filterResponseEx(ModbusMessage response, ModbusMessage request)
{
    if (request.getFunctionCode() == 0x04)
    {
        uint16_t address;       // Initial address requested
        uint16_t words;         // Number of records requested

        request.get(2, address);
        request.get(4, words);

        //Serial.print("Got Addr: "); Serial.print(address); Serial.print(", Count: "); Serial.print(words); Serial.print(" --> ");
        //PrintResponse(response);
        ModbusRequestResponseMatcher::CheckForDataCaptureNeed(address, words, response);

        // Just dump out response
        HEXDUMP_N("Response", response.data(), response.size());    
    }

    // In any case forward the response
    return response;
}

@Miq1

Miq1 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

I see, thank you. So basically who would need the plain response filter any more? How about replacing it with yours? I actually did not see the availability of the causing request for a response when writing the filter hooks 😆

@roelandkluit

Copy link
Copy Markdown
Contributor Author

I see, thank you. So basically who would need the plain response filter any more? How about replacing it with yours? I actually did not see the availability of the causing request for a response when writing the filter hooks 😆

You still need the original one in case you want to modify the request. And also to ensure this is not breaking existing implementations, I would advice to keep both.

@Miq1
Miq1 merged commit e10cc33 into eModbus:master Aug 3, 2026
14 checks passed
@Miq1

Miq1 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

@roelandkluit Thank you very much for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants