
PLC_Novice (Customer) asked a question.
How do I write to an individual bit in a holding register
Hi there,
I have a 16 bit holding register and I want to write to the individual bit 0. How do I write to an individual bit? I earlier saw a video on casting which shows how to read individual bits from a input register. Would the same method apply in writing to an individual bit in a holding register? i.e., do I put a " : " after the offset address?
I am attaching herewith the register details and its google transalation along with the write command I am trying. I am trying to write it to the PLC remotely via VPN so wanted to confirm before writing it to the PLC.
Thanks
The Modbus communications protocol does not support that functionality. While the Do-more PLC has that capability (we wrote the firmware and programming software), Modbus is an open standard that supports specific functions.
If your slave device has Holding Registers that represent writable bit masks, typically that means that you are expected to write the entire HR, or the HR's are also mapped to Coil type (writable bits). Check for the latter in your slave's documentation (or examples on how to set the bit you are talking about).
If that's not the case, the way you typically modify a single bit while maintaining the state of the other 15 bits is READ/MODIFY/WRITE code pattern. OR, if you are expected to set all 16 bits (in your case, 3 bits), you just write the entire 16 bit value (e.g. 0x0001 to enable bit0 but disable bit 1 and 2, or 0x0007 to enable bit0, 1, and 2, or whatever).
But say you want to maintain Bit1 and Bit2, but set Bit0 to a 1, you would do the following
MRX Holding Register 53886 V0 OnSuccess Set C0 // read current value in the slave of HR 453886
// next rung set Bit0 in V0 while maintaining other 15 bits
STRPD C0 // leading edge triggered contact w/OnSuccess C0
MATH V0 "(V0 & 0xFFFE) | 0x0001" // maintains the bit values in the top 15 bits of V0 by doing a bitwise and (&) then bitwise OR (|) the bit0 equal to 1
MWX Holding Register 53886 V0 // write the value back, but with bit0 set
Thank you very much for your elaborate response. I will try the methods and will be able to update here the result by eary next week.
Thanks a ton!
I just realized you could simply use bit casting and SET/RST instructions instead of MATH. My previous answer (using a MATH expression) was the C++ way. Below is the Do-more way 😁
MRX V0 OnSuccess C0
STRPD C0
SET V0:0 // this would SET bit 0
RST V0:1 // this would CLEAR bit1
// bits 2 through 15 would remain as-is, but you can easily tweak any/all of them to a 1 or 0 using SET or RST, respectively
MWX
My previous answer (using a MATH expression) was the C++ way. Below is the Do-more way
I was running C through my wetware as I read your earlier response.
I was imagining embedded Port/Bit manipulation routines.
Hi there,
"MRX V0 OnSuccess C0
STRPD C0
SET V0:0 // this would SET bit 0
RST V0:1 // this would CLEAR bit1"
Tried this but it didn't work.. I am a bit confused as it appears the above instructions would set the bit 0 to 1 and bit 1 to 0 in the Do-more memory but how would it write the same to the device's memory which I am trying to control?
I am attaching herewith few pictures which might help you figure out what I did..
STRPD probably was not allowing power flow so I tried with ST1.. still didn't work. So, I am a bit confused if I have misunderstood the instructions provided by you?
I have MRX the device register and stored the value in V624 and set success bit C610 as you can see below
Even though it was reading the value the success bit was showing off so that may have prevented the power flow in the rung, as you can see below
Since STRPD was not allowing power flow in the rung so I used ST1 but still I think it was not writing 1 to bit 0 of the device..
Could you look at it for me please?
Thanks
It would be better to explain what you're actually trying to do when the Modbus register is read from your remote device. At present time, you're reading from a remote device, writing to local memory in location V624 and when that is successful, changing the value of memory V624. I'm sure this isn't what you're intending. Are you trying to turn something on/off based on the status of the remote Modbus device?
Yes. As you can see in the 2nd attachment of my original post.. bit 0 enables/ disables anti back flow (export of power back into the grid). Actually, my device is a solar inverter (installed in a factory) which when the solar system produces more power than what is required by the factory exports the excess amount back to the grid. Enabling the bit 0 on the solar inverter will prevent excess power flowing back into the grid and that is what I want to achieve as my customer doesn't want excess power flowing back into the grid.
Hope this would explain the situation. Let me know if I am required to provide further information.
Thanks
From your description, it is still not clear exactly what you're trying to do. In your original post you were using the MWX instruction. In your second post, you were using the MRX instruction.
1) Are you trying to read or write to a device using Modbus TCP? Or both read and write?
2) What is this device?
3) If you're trying to write to this device, are you only trying to change bit 0 in the device or bits 0 and 1? If this is a control Word for the device in question, do the other bits do anything?
4) Are you trying to control any outputs directly at the PLC? If so what are the addresses of these outputs?
Ok.
1) Are you trying to read or write to a device using Modbus TCP? Or both read and write? - I am trying to write
2) What is this device? - This is a solar inverter
3) If you're trying to write to this device, are you only trying to change bit 0 in the device or bits 0 and 1? - Yes, only bit 0. If this is a control Word for the device in question, do the other bits do anything? - Bits 3 to 15 don't do anything.
4) Are you trying to control any outputs directly at the PLC? If so what are the addresses of these outputs? - No. I am not trying to control any outputs at the PLC.
Hopefully, that makes it clearer. Let me know if I need to clarify further.
Attaching herewith the device modbus register again if in case the attachment in the original post is not accessible.
Thanks
1
Attached will do what you're trying to do. Remember though, if there are other important bits in that Word, they have to be accounted for as well.