Modbus function code 22

K

Thread Starter

Koby

I need some help understanding Modbus function code 22 0x16. Is it correct? Can it set and clear bits at the same time?

If the intent of the function is to turn off the bits that are 0 in the "AND" parameter and turn on the bits that are a 1 in the "OR" parameter the example pseudo code and data example appear to be wrong.

Modbus_Application_Protocol_V1_1b.pdf Sample:
Result = (Current Contents AND And_Mask) OR (Or_Mask AND (NOT And_Mask))<pre>
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</pre>
Notice that:
* The sample data is constructed as such that it never clears any bits in "Current Contents" Bits 1 and 4 (Zero Base) are on in "Current Contents" and "Result" so this is a poor example.

* The result fails to set the 5th (Zero Base) bit in the result that is set in the "Or_Mask".

I believe the pseudo code should be:
Result = (Current Contents AND And_Mask) OR (Or_Mask) in the case where true bit mask are provided.

Will you please provide sample data where bits are set and cleared in "Current Contents".
 
L

Lynn August Linse

>I need some help understanding Modbus function code 22 0x16.

I see no one answered you. This is a very rare function to exist in devices. I've never used this function, and I assume only Schneider PLC support, so you should see if Schneider tech support has any app notes on it. Most people use the individual coil writes to do this in an obvious, but less efficient manner.

If a third party product supports, I wouldn't be surprised if they do it 'wrong', meaning do it as they document, not necessarily as the Schneider PLC does it.

If you are trying to implement this in your product, make sure you buy a small Schneider PLC and do it the same way.
 
M

Mario Bergeron

>I need some help understanding Modbus function code 22 0x16. Is it correct? Can it set and clear bits at the same time?

Your suggested equation could be a valid function for someone who would like to perform AND operations and OR operations on some words. But the Modbus spec are what they are.

The only difference between the two formulas are the case you just mention on Bit 5.

To better understand this function, I would personally write the spec this way:

---------------------------------------------
Function code: 22 (0x16) Mask Write Register

This function code is used to modify the contents of a specified holding register using a combination of an UNSELECTED mask, an SETBIT mask, and the register's current contents. The function can be used to set or clear individual bits in the register.

The request specifies the holding register to be written, the data to be used as the UNSELECTED mask, and the data to be used as the SETBIT mask. Registers are addressed starting at zero. Therefore registers 1-16 are addressed as 0-15.
The function’s algorithm is:

Result = (Current Contents AND UNSELECTED_Mask) OR (SETBIT_Mask AND (NOT UNSELECTED_Mask))

For example:<pre>
Hex Binary
Current Contents= 12 0001 0010
UNSELECTED_Mask = F2 1111 0010
SETBIT_Mask = 25 0010 0101
(NOT UNSELECTED_Mask)= SELECTED_Mask= 0D 0000 1101
Result = 17 0001 0111
</pre>
Note:
If the UNSELECTED_Mask value is zero (the SELECTED_Mask value is one), the result is equal to the SETBIT_Mask value. If the UNSELECTED_Mask value is one (the SELECTED_Mask value is zero), the result is equal to the current contents (bits unchanged).

...

---------------------------------------------

Has you can see, you choose the bits you want to set and set them to the value that you want, leaving the rest intact.
 
I still did not understand the logic how to generate the AND mask and OR mask values when changing multiple bits at the same time. Can somebody help me how the F2 and 25 are got in the given example when the 1st and 3rd bits are being changed.
 
M

Mario Bergeron

> I still did not understand the logic how to generate the AND mask and OR mask values when changing multiple bits at
> the same time. Can somebody help me how the F2 and 25 are got in the given example when the 1st and 3rd bits are being changed.

Following the example above:

We want to control bits 1, 3 and 4 (Starting counting at 1 this time), here is what we do.<pre>
Assume the register has the current value:
Current Contents = 0001 0010 = 12

We start by slecting our bits:
SELECTED_Mask = 0000 1101 = 0D
UNSELECTED_Mask = NOT SELECTED_Mask = 1111 0010 = F2 = the 'And_Mask'

We want to set bit 1 to 1, bit 3 to 1 and bit 4 to 0, so:

SETBIT_Mask = **** 01*1

It does not mater what the other bits are, and in the example we have:

SETBIT_Mask = 0010 0101 = 25 = the 'Or_Mask'

Plug this in the equation and the result is:

Result = 0001 0111 = 17
</pre>
You have successfully set the bits 1, 3 and 4!
 
Top