Send command button – change register bit

Forum Home Forums Communicating with Devices Modbus Send command button – change register bit

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #6786
    cooldjmc
    Participant

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

    #6787
    Mikhail
    Moderator

    Hi,

    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.

    #6788
    cooldjmc
    Participant

    Thank you Mikhail for your quick response, i will try that and post the results.

    Have a nice weekend!

    #9926
    gabeirinhas
    Participant

    when 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

    #9927
    gabeirinhas
    Participant

    Basically I need to know how to disable the bits without losing the rest of the byte values.

    #9934
    Mikhail
    Moderator

    Try, 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 3 months, 4 weeks ago by Mikhail.
    #9938
    gabeirinhas
    Participant

    it works

    Thank you very much for the help

    #9939
    manjey73
    Participant
    public 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

    #9948
    gabeirinhas
    Participant

    Mihail’s solution is perfect for me.

    Thanks for the help

    #9955
    Mikhail
    Moderator

    Solution provided by @manjey73 is more generic. We will include SetBit function in the configuration template.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.