주요 콘텐츠

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 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.

example

maskWrite(modbusClientObj,address,andMask,orMask,serverId) additionally specifies serverId to send the command to a specific server.

example

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. The function performs 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

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

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,48,1,3)

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. You can create the client using the modbus function.

Register address to perform mask write operation on, 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

Address of the server to send the mask write command to, specified as a nonnegative scalar. If you do not specify a serverId, the function uses the default as 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

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

expand all

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

Version History

Introduced in R2017a

expand all