# Extracting bit values into DIA channels from a MODBUS value

**URL:** <https://forums.digi.com/t/extracting-bit-values-into-dia-channels-from-a-modbus-value/16286>\
**Category:** Python\
**Tags:** modbus-tcp, dia, digi-esp-for-python, python\
**Created:** [February 18, 2016, 11:00pm UTC](https://forums.digi.com/t/extracting-bit-values-into-dia-channels-from-a-modbus-value/16286 "2016-02-18T23:00:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stirlsilver](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@stirlsilver](https://forums.digi.com/u/stirlsilver)\
**Post date:** [February 18, 2016, 11:00pm UTC](https://forums.digi.com/t/extracting-bit-values-into-dia-channels-from-a-modbus-value/16286/1 "2016-02-18T23:00:02Z")

</div>

Hi,  
I am currently trying to create a DIA project which grabs MODBUS data and pushes it onto DeviceCloud. I’ve been able to successfully read all analogue values (16 bit integers) and push them to the cloud without a problem. i.e.

- name: mbus  
driver: devices.modbus.mbus\_udp\_device:MBusUDPDevice  
settings:  
poll\_rate\_sec: 15  
udp\_peer: “(‘127.0.0.1’,502)”  
trace: “debug”  
round: 2  
poll\_list:  
- poll: “PLC\_AI”  
pollinfo:  
uid: 1  
fnc: 3  
ofs: 1  
cnt: 4  
channels:  
- parse:  
nam: “Blade\_angle”  
ofs: 0  
frm: “H”

The problem I am having is that digital values have been coded into a 16 bit integer (i.e. 1000 0010 0101 1111). I can read the value fine as a single integer, But how do I split out these individual bits as individual boolean channels?

I’ve tried the following but it doesn’t quite work:  
- poll: “PLC\_DI”  
pollinfo:  
uid: 1  
fnc: 3  
ofs: 0  
cnt: 1  
- parse:  
nam: “0”  
ofs: 0  
frm: “H”  
typ: “bool”  
expr: “bool(%d&1)”  
- parse:  
nam: “1”  
ofs: 0  
frm: “H”  
typ: “bool”  
expr: “bool(%d&2)”  
- parse:  
nam: “2”  
ofs: 0  
frm: “H”  
typ: “bool”  
expr: “bool(%d&4)”

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [February 19, 2016, 4:09pm UTC](https://forums.digi.com/t/extracting-bit-values-into-dia-channels-from-a-modbus-value/16286/2 "2016-02-19T16:09:20Z")

</div>

try using the bit to hex converter options in python.

---

<div class="post-metadata">

**Author:** ![stirlsilver](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@stirlsilver](https://forums.digi.com/u/stirlsilver)\
**Post date:** [February 25, 2016, 10:03pm UTC](https://forums.digi.com/t/extracting-bit-values-into-dia-channels-from-a-modbus-value/16286/3 "2016-02-25T22:03:45Z")

</div>

Thanks for the assistance mvut. I ended up figuring it out. I created a transform for each bit and used the following expressions:  
(c[0]&(1\<\<8))!=0  
(c[0]&(1\<\<9))!=0  
(c[0]&(1\<\<10))!=0

And so on.
