Main Content

maskWrite

Perform mask write operation on a holding register

Description

example

maskWrite(m,address,andMask,orMask) writes data to Modbus object m to a holding register at address address, using the indicated mask values. The function can set or clear individual bits in a specific holding register. It is a read/modify/write operation, and uses a combination of an AND mask, an OR mask, and the current contents of the register.

example

maskWrite(m,address,andMask,orMask,serverId) additionally specifies the serverId as the address of the server to send the write command to.

Examples

collapse all

You can modify the contents of a holding register using the maskWrite function. The function can set or clear individual bits in a specific holding register. It is a read/modify/write operation, and uses a combination of an AND mask, an OR mask, and the current contents of the register.

Create the AND and OR variables.

andMask = 6
orMask = 0

Set bit 0 at address 20, and perform a mask write operation. Since the andMask is a 6, that clears all bits except for bits 1 and 2. Bits 1 and 2 are preserved.

maskWrite(m,20,andMask,orMask)

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,6,0,3)

Input Arguments

collapse all

Register address to perform mask write operation on, specified as a double. Address must be the first argument after the object name. This example sets bit 0 at address 20 and performs a mask write operation.

Example: maskWrite(m,20,andMask,orMask)

Data Types: double

AND value to use in mask write operation, specified as a double. andMask must be the second argument after the object name. The valid range is 0–65535.

This example sets bit 0 at address 20 and performs a mask write operation, using 6 as the AND value.

Example: maskWrite(m,20,6,0)

Data Types: double

OR value to use in mask write operation, specified as a double. orMask must be the third argument after the object name. The valid range is 0–65535.

This example sets bit 0 at address 20 and performs a mask write operation, using 0 as the OR value.

Example: maskWrite(m,20,6,0)

Data Types: double

Address of the server to send the mask write command to, specified as a double. Server ID must be specified after the object name, address, AND mask, and OR mask. If you do not specify a serverId, the default of 1 is used. Valid values are 0–255, with 0 being the broadcast address. This example sets bit 0 at address 20 and performs a mask write operation at server ID 3.

Example: maskWrite(m,20,6,0,3)

Data Types: double

Tips

The function algorithm works as follows:

 Result = (register value AND andMask) OR (orMask AND (NOT andMask))

For example:

                 Hex    Binary
Current contents  12   0001 0010
And_Mask          F2   1111 0010
Or_Mask           25   0010 0101
(NOT And_Mask)    0D   0000 1101

Result            17   0001 0111

If the orMask value is 0, the result is simply the logical ANDing of the current contents and the andMask. If the andMask value is 0, the result is equal to the orMask value.

Extended Capabilities

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

Version History

Introduced in R2017a

expand all