Contenido principal

maskWrite

R2026b

Perform mask write operation on a holding register

Description

maskWrite(modbusClientObj,address,andMask,orMask) sets or clears individual bits in a holding register at a specified address on the Modbus server connected to the client specified by modbusClientObj. The function performs a read-modify-write operation using a combination of andMask, orMask, and the current contents of the register.

The function computes the new register value as:

newRegisterVal = (registerVal & andMask) | (orMask & ~andMask)
If the orMask value is 0, the new register value is registerVal & andMask. If the andMask value is 0, the new register value is equal to the orMask value.

example

maskWrite(modbusClientObj,address,andMask,orMask,serverId) sends the command to the server specified by serverId.

example

Examples

collapse all

Suppose you want to clear all bits of a holding register at address 20 except bits 1 and 2. Set andMask to 6 so that only bits 1 and 2 are preserved, and set orMask to 0 so that no additional bits are turned on.

Create the AND and OR variables.

andMask = 6
orMask = 0

Perform a mask write operation. Since the andMask is 6 and orMask is 0, the function clears all bits except for bits 1 and 2.

maskWrite(m,20,andMask,orMask)

For example, if the current register value at address 20 is 18 (0x12), the function computes the new register value as follows:

                     Hex    Binary
registerVal           12   0001 0010
andMask               06   0000 0110
orMask                00   0000 0000
~andMask              F9   1111 1001

newRegisterVal        02   0000 0010

Use the serverId argument to specify the address of the server to send the mask write command to.

Set bit 0 at address 20 and perform a mask write operation at server ID 3.

maskWrite(m,20,48,1,3)

For example, if the current register value at address 20 is 18 (0x12), the function computes the new register value as follows:

                     Hex    Binary
registerVal           12   0001 0010
andMask               30   0011 0000
orMask                01   0000 0001
~andMask              CF   1100 1111

newRegisterVal        11   0001 0001

Input Arguments

collapse all

Modbus client, specified either as an icomm.interface.modbus.tcpip.Modbus or icomm.interface.modbus.serialrtu.Modbus object, based on the transport layer used for communication. Create the client using the modbus function.

Starting address for mask write operation, specified as a positive scalar. Use 1-based addressing because the function subtracts 1 from the specified address.

Example: maskWrite(m,20,48,1) sets bit 0 at address 20 and performs a mask write operation.

Data Types: double

AND value to use in mask write operation, specified as a positive scalar. The valid range is 0–65535.

Example: maskWrite(m,20,6,0) performs a mask write operation, using 6 as the AND value.

Data Types: double

OR value to use in mask write operation, specified as a positive scalar. The valid range is 0–65535.

Example: maskWrite(m,20,6,1) sets bit 0, preserves bits 1 and 2, and clears all other bits at address 20, using 1 as the OR value.

Data Types: double

Server identifier for mask write operation, specified as a nonnegative scalar. The server identifier corresponds to the unit identifier in Modbus TCP and the server address in Modbus Serial RTU. If you do not specify a serverId, the function uses the default value of 1. Valid values are 0–255, with 0 being the broadcast address.

Example: maskWrite(m,20,48,1,3) sets bit 0 at address 20 and performs a mask write operation at server ID 3.

Data Types: double

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2017a

expand all