Read Bit State In One Word


raining010

Recommended Posts

Hi, everyone

I use DAQFactory Pro to read a equipment via RS485 and Modbus protocol. I defined different channels to read different modbus registers, for example, 41000, 41002. 41000 contains 16 off bits, 41000.1 means run signal of one motor, 41000.2 means run signal of second motor, but in channel definition, I can't fill 41000.1 into column 'chn#'.

How could I fill decimal into chn# and display its value by LED?

Thanks

James

Link to comment
Share on other sites

41000.1 is not a valid Modbus address. 41000 is. I'm guessing that the engineer that did your documentation decided to use the .1, .2 notation to indicate the bit within that 16 bit register. To read this, create a single channel to read the entire 41000 register. Then, to display the status of the .1 (assuming we are numbering from 0), you would use the testbit() function:

testbit(myChannel, 1)

That said, I'm guessing the docs are actually numbering from 1, so you probably need to subtract one.

Link to comment
Share on other sites

I'd forgotten about testbit(). I always do either

MyBitWord & 1 //(2, 4, 8, etc)[/CODE]

or

[CODE]MyBits = To.Bit(MyBitWord)[/CODE]

and then reference MyBits[0] thru MyBits[15] or [31]. Is there any particular advantage to any of the methods?

Link to comment
Share on other sites

MyBitWord & 1 works fine, but is a little less readable. I mean, I know my 2 exponents up to 16 without issue, but I'd be hard pressed to rattle off 2^29 off the top of my head. Better would be MyBitWord & (1 << 29) for the 30th bit. But testbit does the same thing internally and is the most readable.

To.Bit() does something completely different and is useful if you want to work with arrays in bulk. For example, you could quickly figure out how many of the bits were on by using sum().

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.