Forum Home › Forums › Understanding the Software › Using Formulas › Channel Read Bits
 This topic has 24 replies, 3 voices, and was last updated 3 months, 1 week ago by Mikhail.

AuthorPosts

July 28, 2024 at 3:04 am #15036rapidscadaaksParticipant
Hi
I have connected a device to Rapid Scada using Modbus.
There are 8 Statuses stored in the same address 1080.
Status A is in BIT01
Status B is in BIT23
Status C is in BIT46
Status D is in BIT79
Status E is in BIT10
Status F is in BIT1112
Status G is in BIT13
Status H is in BIT1415Using Bitmask, individual channels have been automatically created by Rapid Scada for each BIT with GetBit formula.
Please suggest formula to get 2 and 3 BITS in one Channel as required aboveMany Thanks
July 28, 2024 at 6:34 am #15037manjey73ParticipantIf you always need to pull out 2 bits each, then you can make one formula, just like getBit. If your pairs may differ, then you will need to make a formula and install it with your hands. Modify getBit into a different formula
public double GetTwoBit(double val, int n)
{
ulong ulVal = (ulong)val;
return (ulVal >> n) & 3ul;
}Here 3ul is the number 3 (00000011) and by shifting the bits by the right amount you make a logical one with two bits at once, and not one as in getBit. You can look at how much you need to shift in a calculator or even on a piece of paper.
 This reply was modified 3 months, 2 weeks ago by manjey73.
July 28, 2024 at 6:38 am #15039manjey73Participantpublic double GetThreeBit(double val, int n)
{
ulong ulVal = (ulong)val;
return (ulVal >> n) & 7ul;
}Here the number 7 (00000111) is used as a mask
July 28, 2024 at 6:43 am #15040manjey73Participantpublic double GetAnyBits(double val, int n, int mask = 1)
{
ulong ulVal = (ulong)val;
return (ulVal >> n) & (ulong)mask;
}I haven’t checked, but the mask should work by default. The mask is 1. Which fully corresponds to the getBit formula
You can set 3, 7, and so on.
n is the offsetJuly 28, 2024 at 7:04 am #15041rapidscadaaksParticipantThanks Manjey
Since I am new, I have few questions:
1. I will add the GetAnyBits .. to the scripts by editing the Scripts source code
2. For usage, lets say I want the 3rd, 4th and 5th bit, what would the syntax be?Thanks again
July 28, 2024 at 7:25 am #15042manjey73Participant00111000
The 0th bit is on the right. You need to shift by 3
GetAnyBits(Val(XXX), 3, 7) like this
 This reply was modified 3 months, 2 weeks ago by manjey73.
July 28, 2024 at 7:44 am #15044rapidscadaaksParticipantHi Manjey
Thanks.
As per manual of the device, Register 1080 size is 2 therefore has 16 bits and I need to read the (bit 3, bit 4 and bit 5), (bit 6 and bit 7) and so on.
I am however not clear of the (XXX) and 7 in the syntax you provided.
I also request you for Syntax for the examples above
Please help
Thanks in advance
July 28, 2024 at 8:01 am #15045manjey73ParticipantGetAnyBits(Val(1080), 3, 7)
Val(XXX) – Where is the XXX number of your channel00000111 is the number 7 in the bit representation
When you need to select 3 bits in a value, you need to make a logical AND with the bits you need, and depending on which bits from the beginning you want to compare, you need to shift the bits you need so that they turn out to be starting from the zero bit00001111 is the number 15, which will correspond to four consecutive bits
00000011 is the number 3, which will correspond to two consecutive bits
01010110 – Your certain number for checking 3 bits. You need to shift this representation to position 0 (n = 2) 00010101 and make AND with the number 7 (00000111)
July 28, 2024 at 10:57 am #15046rapidscadaaksParticipantHi Manjey
Many thanks. I have understood.
Now there is only a small issue and request for your input.
The value which i am getting after the formula is in decimal eg 01 is shown as 1; 10 is coming as 2. Please suggest weak in the script so that I get the output as 01 or 10.Thanks
July 28, 2024 at 11:08 am #15047manjey73Participant00000010 in the bit representation is the number 2 in the decimal representation. Do you need to see the number in some other format?
July 28, 2024 at 11:32 am #15048rapidscadaaksParticipantyes please. I need to see it is 10 i.e. in bit representation
Thanks
July 28, 2024 at 2:31 pm #15049manjey73ParticipantSet the HEX format in the channel, perhaps this will be enough for you.
There is a choice there
Hexadecimal
Hexadecimal 2 digits
Hexadecimal 4 digits
Hexadecimal 8 digits This reply was modified 3 months, 2 weeks ago by manjey73.
July 28, 2024 at 3:12 pm #15051manjey73Participant00000010 is a binary display of bits of a number, I don’t even know if there is support for such a display in the system. I wrote some kind of formula for this and displayed it as a string
July 28, 2024 at 3:45 pm #15052manjey73ParticipantFormat Channel – String
Data Type = ASCIIpublic string BitsView(double number)
{
byte[] data = BitConverter.GetBytes(Convert.ToUInt16(number));
uint N = BitConverter.ToUInt16(data, 0);
string bits = Convert.ToString(N, 2);
return bits;
}Make a calculation channel with the specified parameters. Use the specified formula. Displaying a string in the channel, but only for a byte. In order for the whole number to be displayed correctly, the formula needs to be refined.
July 29, 2024 at 3:47 am #15053rapidscadaaksParticipantMany Thanks for all your help

AuthorPosts
 You must be logged in to reply to this topic.