Send command button – change register bit

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

Tagged: 

Viewing 13 posts - 1 through 13 (of 13 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 1 year, 4 months 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.

    #10564
    victorlam
    Participant

    (ushort)Val(101) | ((Cmd > 0 ? 1 : 0) << 2)
    what’s the meaning for the formula used?

    #10568
    manjey73
    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 positions

    That is, this formula is only for setting the 2nd bit to 1

    • This reply was modified 9 months, 2 weeks ago by manjey73.
    #10570
    manjey73
    Participant

    If 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

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