The correct way to set or clear a bit in a holding register is to use Modbus function code 22 (16 hex). Ignition’s Modbus V2 OPC-UA driver supports that. The problem is that very few Modbus slaves support this function code. Even Schneider/Modicon’s M340 and Control Microsystems SCADAPacks don’t support it.
There is certainly a timing problem with setting or clearing a bit in the last read copy of a holding register and then writing it back out using FC 16 or FC 6. But timing problems are all over the place in industrial automation. Where are the block writes to ensure that structured, related data in written at once in anyone’s implementation of OPC? What’s preventing an HMI from clobbering half of a Modbus floating point number? Worse than that, some AB PLCs will update their discrete inputs during a logic scan.
What I’m saying is that the timing problem is not solved in industrial automation, and won’t be, anytime soon. As you continue to work on your Modbus OPC_UA server, consider that you will do no additional harm by supporting bit writes to registers using FC 16 or FC 6, and make me, at least, a happier camper.
If you’re aware and OK with the fact that a timing issue exists then you can create a global scripting function that does a read/mask/write and use it on any driver.
I’m not really comfortable with introducing a timing issue that isn’t explicitly invoked and obvious to the user, but you’re right that other timing issues are a reality. Maybe if I implemented this it would be as an option that you had to explicitly enable?
Support for writing to a bit on Modbus devices that don’t support the MaskWriteRegister function.
Essentially, a non-atomic, two request operation: Read the value of the register (1st request), turn a bit on or off, then write the value back to the register (2nd request).
The tricky thing is that you’re not guaranteed that nobody touched the register after you read it, meaning your write could very likely clobber the value that it changed to.
Yes, Kevin. Putting a check box under the advanced section of the OPC-UA device configuration page that said “use FC 6 for bit masked writes” would do the trick. It would save me and probably other integrators from a lot of busy work and possible errors.