Forum Home › Forums › Communicating with Devices › Modbus › Send command button – change register bit
Tagged: formula
- This topic has 12 replies, 5 voices, and was last updated 9 months, 2 weeks ago by
manjey73.
-
AuthorPosts
-
April 30, 2020 at 7:08 am #6786
cooldjmc
ParticipantHi, please if some one can help me if is possible to send command to change only one bit in a modbus register and not modify other register bits. For example if i have one register 0110 0110 1000 0110 and i want to change only the second bit to 0 is is possible to use one Button to send command (something like SetBit? )?
Thanks.
April 30, 2020 at 4:16 pm #6787Mikhail
ModeratorHi,
Set a formula for the output channel.
Something like this (not tested):
(ushort)Val(101) | ((Cmd > 0 ? 1 : 0) << 2)
There the input channel #101 stores the current value of the register.April 30, 2020 at 5:44 pm #6788cooldjmc
ParticipantThank you Mikhail for your quick response, i will try that and post the results.
Have a nice weekend!
January 17, 2022 at 5:02 pm #9926gabeirinhas
Participantwhen I use this formula in the output channels.
(ushort)Val(321) | ((Cmd > 0 ? 1 : 0) << 0)
(ushort)Val(321) | ((Cmd > 0 ? 1 : 0) << 1)
(ushort)Val(321) | ((Cmd > 0 ? 1 : 0) << 2)
(ushort)Val(321) | ((Cmd > 0 ? 1 : 0) << 3)
(ushort)Val(321) | ((Cmd > 0 ? 1 : 0) << 4)
(ushort)Val(321) | ((Cmd > 0 ? 1 : 0) << 5)
(ushort)Val(321) | ((Cmd > 0 ? 1 : 0) << 6)
(ushort)Val(321) | ((Cmd > 0 ? 1 : 0) << 7)For 1 byte I get the activation of each of the channels without losing the value of the channel.
I need to know how to do the same but to disable each of the bits.
I use command type: standard
I use command values: execute
Is there any way to activate a specific bit of the byte with a formula or with off-on without losing the byte values.
Thank you
January 17, 2022 at 5:05 pm #9927gabeirinhas
ParticipantBasically I need to know how to disable the bits without losing the rest of the byte values.
January 18, 2022 at 1:21 pm #9934Mikhail
ModeratorTry, for example
Cmd > 0 ? ((ushort)Val(101) | 0x0002) : ((ushort)Val(101) & 0xFFFD)
0x0002 hex is 00000000_00000010 binary
0xFFFD hex is 11111111_11111101 binary-
This reply was modified 1 year, 4 months ago by
Mikhail.
January 18, 2022 at 1:31 pm #9938gabeirinhas
Participantit works
Thank you very much for the help
January 18, 2022 at 2:52 pm #9939manjey73
Participantpublic double SetBit(double n, int index, double setvalue) { long nn = Convert.ToInt64(n); return setvalue > 0 ? nn | (1 << index) : nn & ~(1 << index); }
I do not know, is this what you need or not? Just writing a bit to a number. You need to add Formulas to the Reference Books
January 19, 2022 at 5:56 pm #9948gabeirinhas
ParticipantMihail’s solution is perfect for me.
Thanks for the help
January 20, 2022 at 6:53 am #9955Mikhail
ModeratorSolution provided by @manjey73 is more generic. We will include SetBit function in the configuration template.
August 28, 2022 at 3:05 am #10564victorlam
Participant(ushort)Val(101) | ((Cmd > 0 ? 1 : 0) << 2)
what’s the meaning for the formula used?August 28, 2022 at 9:25 am #10568manjey73
Participant@victorlam In this case, in the existing channel 101, you change the 2nd bit to 1 if it was zero, or it remains 1 if it was in 1 and you write 0
| – or
<< 2 – Shift the zero bit by 2 positionsThat is, this formula is only for setting the 2nd bit to 1
-
This reply was modified 9 months, 2 weeks ago by
manjey73.
August 28, 2022 at 9:29 am #10570manjey73
ParticipantIf the setBit formula has already been added to the database, it allows you to change the bit to both 0 and 1
SetBit(Val(101), 2, Cmd)
When transmitting 0, bit number 2 will be reset, when transmitting 1, bit number 2 will be set. The remaining bits of the channel will not be changed
-
This reply was modified 1 year, 4 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.