Modbus Mask Write: Preserve Neighbouring Bits When Changing One Flag
An HMI changes one display preference, then another setting unexpectedly returns to its old state. The problem may be a whole-register write built from an earlier read. When several independent flags share one holding register, the change needs to identify both the intended bit and the bits that must remain untouched.
Where the exact device and client support it, Modbus Mask Write Register provides a way to express that intent. Start with the register map and a simulator test. Do not assume a protocol function is implemented simply because an instrument communicates over Modbus.

Key takeaways
Separate a single-bit change from replacing the complete register value.
Confirm function support and writable-bit meanings for the installed device.
Test preservation of neighbouring bits as well as the requested change.
1. Recognise the stale-read problem
Consider a hypothetical simulator with two unrelated display preferences stored in the same word. Client A reads the word and prepares a replacement that changes its own preference. Before that replacement arrives, client B changes a different preference. If A then sends its old word with one edited bit, B’s newer preference can be overwritten.
A faster poll rate does not establish ownership of the word. The practical question is whether the application sends a complete replacement or a targeted operation, and which other clients or device logic can change the same location. Write these ownership assumptions into the commissioning plan.
2. Confirm the function and the device map
The Modbus specification identifies Mask Write Register as decimal function 22, hexadecimal 0x16. Decimal function 16 is 0x10 and performs a different operation. Record both representations in the test sheet so a tool’s display format cannot quietly select the wrong function.
Check the installed model, firmware and register-map revision. Establish the address basis, writable fields, reserved bits, permitted combinations and any documented unlock or commit procedure. If the manual does not confirm mask-write support at the intended register, leave compatibility unverified and ask the manufacturer or ProSense for clarification.
3. Work through one bit before testing
Section 6.16 defines the result using the current word, an AND mask and an OR mask:
Result = (Current AND AND_mask) OR (OR_mask AND NOT AND_mask).
For each bit, an AND-mask value of one retains the current bit; an AND-mask value of zero selects the corresponding OR-mask bit. Keep calculations within the 16-bit register width.
Hypothetical simulator example: set bit 3, counting the least significant bit as bit 0. Use AND 0xFFF7 and OR 0x0008. With current contents 0x0032, the result is 0x003A. The other fifteen bits remain as they were when the operation was processed. Clearing bit 3 uses the same AND mask with OR 0x0000.

Do not turn this illustrative mask into a field command without the device map. A bit position has no universal meaning: the same position may be reserved, read-only or associated with a consequential command in another instrument.
4. Build a preservation test
Use an authorised simulator or isolated spare. Define a harmless test flag and keep production outputs, protection settings and motor commands outside the exercise.
Record the starting word, target bit, masks and expected word. Include at least two starting patterns so a test cannot pass merely because neighbouring bits already match an accidentally written value.
Introduce a controlled change to a different bit before processing the targeted operation. Check that this newer neighbouring state survives. Document the exact test order rather than relying on two screenshots taken at unspecified times.
Capture the matched request and response, then obtain a fresh supported register read. Compare every bit against the expected preservation rule. Keep exceptions, timeouts and unreadable results as unresolved outcomes.
Repeat with the target already set, then with an authorised clear operation. Agree on restoration and rollback steps before testing, and record which checks remain outstanding.
5. Keep the limits visible
A targeted write does not allocate ownership across the system. Another writer can still change the word afterwards, and device logic may impose its own behaviour. If a read-back differs, examine the sequence of operations before deciding that either the masks or the instrument are faulty.
Do not automatically fall back to an uncoordinated read–modify–write sequence when mask writes are unavailable. Choose an approach supported by the manufacturer, such as a documented separate parameter or controlled ownership of writes. Test that approach explicitly; do not describe it as equivalent without evidence.
For safety-related or operationally consequential settings, application-specific design, authorisation and validation belong with competent people and the current device requirements. A generic Modbus register operation is not a certified safety function.
6. Prepare a useful enquiry
Send the exact device and client versions, register-map reference, intended bit meaning and a sanitised simulator trace. Include the starting word, masks, expected result and fresh read-back. Remove credentials, security keys and customer identifiers. State whether the unresolved issue is function support, addressing, preservation or later overwriting.
Need help with your application?
For an instrument or interface enquiry, include the exact models, documentation and a sanitised test record. Confirm device and software compatibility for your application.

Comments