# Channel Read Bits

Viewing 15 posts - 1 through 15 (of 25 total)
• Author
Posts
• #15036
Participant

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 BIT0-1
Status B is in BIT2-3
Status C is in BIT4-6
Status D is in BIT7-9
Status E is in BIT10
Status F is in BIT11-12
Status G is in BIT13
Status H is in BIT14-15

Using 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 above

Many Thanks

#15037
manjey73
Participant

If 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 2 weeks ago by manjey73.
#15039
manjey73
Participant

public double GetThreeBit(double val, int n)
{
ulong ulVal = (ulong)val;
return (ulVal >> n) & 7ul;
}

Here the number 7 (00000111) is used as a mask

#15040
manjey73
Participant

public 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 offset

#15041
Participant

Thanks 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

#15042
manjey73
Participant

00111000

The 0th bit is on the right. You need to shift by 3

GetAnyBits(Val(XXX), 3, 7) like this

• This reply was modified 2 weeks ago by manjey73.
#15044
Participant

Hi 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

#15045
manjey73
Participant

GetAnyBits(Val(1080), 3, 7)
Val(XXX) – Where is the XXX number of your channel

00000111 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 bit

00001111 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)

#15046
Participant

Hi 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

#15047
manjey73
Participant

00000010 in the bit representation is the number 2 in the decimal representation. Do you need to see the number in some other format?

#15048
Participant

yes please. I need to see it is 10 i.e. in bit representation

Thanks

#15049
manjey73
Participant

Set the HEX format in the channel, perhaps this will be enough for you.

There is a choice there

• This reply was modified 2 weeks ago by manjey73.
#15051
manjey73
Participant

00000010 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

#15052
manjey73
Participant

Format Channel – String
Data Type = ASCII

public 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.

#15053