# Reading bool array from S7 DB

**URL:** <https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750>\
**Category:** Ignition\
**Created:** [October 2, 2019, 4:37pm UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750 "2019-10-02T16:37:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![smenon6](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@smenon6](https://forum.inductiveautomation.com/u/smenon6)\
**Post date:** [October 2, 2019, 4:37pm UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/1 "2019-10-02T16:37:43Z")

</div>

Hi,

In my setup i have the following  
Ignition : 8.0.3  
Siemens controller : S7-1212C fw 3.0.2 (192.168.13.30)  
PC : 192.168.13.40

I have created an array of boolean tags(10) in my data block DB1. The address spans from 4.0 to 5.1. I am able to read/write the tags individually when i create a new opc tag like

dummy1 (address [test]DB1,X4.0)  
dummy2 (address [test]DB1,X5.1) etc

but this does not seem to work when i create a **new data type** and use the parameter value to provide the index.

index = 1  
new tag (address **[test]DB1,X{index\*0.1+4.0}**)

when I try the above for the tag data, I am receiving data from DB1 address 4.7 instead. The above approach works well for a int and real data array.

Attached is a captured wireshark trace, where i tried changing the Boolean address from 4.1 to 5.1 in Ignition . In the trace the request seems to be for a complete byte instead of a bit.

[bool\_tag\_5\_1.rar](https://us1.discourse-cdn.com/inductiveautomation/original/2X/6/619d4ac9c8ca8ba56f49c4899a41b4d91a78a231.rar) (13.4 KB)

Saurabh Menon

 ![S7%20DB%20parameters](https://us1.discourse-cdn.com/inductiveautomation/original/2X/2/2507af53627e993544902204781292ca81427657.png) ![ignition_bool_tag](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/11f7228cdb92b5c6016e2c7fd2404a734b86fce3.png)

---

<div class="post-metadata">

**Author:** ![chi](https://avatars.discourse-cdn.com/v4/letter/c/0ea827/32.png) [@chi](https://forum.inductiveautomation.com/u/chi)\
**Post date:** [October 2, 2019, 9:04pm UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/2 "2019-10-02T21:04:53Z")

</div>

> [@smenon6](#):
>
> index = 1  
> new tag (address **[test]DB1,X{index\*0.1+4.0}** )

I'm a little surprised that your UDT works at all. With index = 1 your parameter expression is `(1*0.1+4.0)`. The floating point numbers (0.1 and 4.0) are treated as integer, that result in `(1*0+4)=4` so the final opc address is `[test]DB1,X4`.  
To use your parameter as bit address, the parameter expression should be `[test]DB1,X4.{index}`.  
If you want to use bit numbers \> 7 with offset X4.0, the expression  
`[Test]DB1,X{4+index-index%8}.{index%8}`  
should do the trick.

Even when you only want to read a single bit, the driver will always request the full byte.

---

<div class="post-metadata">

**Author:** ![smenon6](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@smenon6](https://forum.inductiveautomation.com/u/smenon6)\
**Post date:** [October 3, 2019, 1:24am UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/3 "2019-10-03T01:24:16Z")

</div>

@chi

Thanks for the help. I had to slightly tweak the equation to [Test] DB1, X{4+index/8}.{index%8} this is good for array index starting with 0.

My array in S7 udt is bool[1 to 30]

---

<div class="post-metadata">

**Author:** ![AndyP](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@AndyP](https://forum.inductiveautomation.com/u/AndyP)\
**Post date:** [October 3, 2019, 11:43am UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/4 "2019-10-03T11:43:45Z")

</div>

You could just address the DWORD with the data type set to array of bools and then you would have each bit in one element. If i remember correctly the bytes are reversed (little endIan as opposed to big endIan) but we have used this before to read Siemens IO card data into a diagnostics page.

---

<div class="post-metadata">

**Author:** ![smenon6](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@smenon6](https://forum.inductiveautomation.com/u/smenon6)\
**Post date:** [October 3, 2019, 12:57pm UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/5 "2019-10-03T12:57:48Z")

</div>

@chi

I am facing a slight problem with the above approach. I am expecting {index/8} to give me a integer value, but there is a rounding off happening that is causing some of the tags to be misaligned. For eg when the index is 6/7. Any ways to overcome this

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 3, 2019, 1:06pm UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/6 "2019-10-03T13:06:06Z")

</div>

Try:

```auto
[Test] DB1,X{4+(index-(index%8))/8}.{index%8}

```

---

<div class="post-metadata">

**Author:** ![chi](https://avatars.discourse-cdn.com/v4/letter/c/0ea827/32.png) [@chi](https://forum.inductiveautomation.com/u/chi)\
**Post date:** [October 3, 2019, 5:19pm UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/7 "2019-10-03T17:19:22Z")

</div>

> [@smenon6](#):
>
> My array in S7 udt is bool[1 to 30]

Ignitions driver uses the legacy S7-300 protocol and knows nothing about the data type definition in the plc. Even a bool[101..130] array would still start at the bit address `DB1.DBX4.0`.  
If you want the parameter in Ignition to match your array index in the plc try

```auto
[Test]DB1,X{4+( (index-1) -( (index-1) %8))/8}.{ (index-1) %8}

```

(i did not test this expression)

---

<div class="post-metadata">

**Author:** ![smenon6](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@smenon6](https://forum.inductiveautomation.com/u/smenon6)\
**Post date:** [October 4, 2019, 8:40am UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/8 "2019-10-04T08:40:12Z")

</div>

@pturmel

Thanks for this, this works.

---

<div class="post-metadata">

**Author:** ![smenon6](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@smenon6](https://forum.inductiveautomation.com/u/smenon6)\
**Post date:** [October 4, 2019, 8:50am UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/9 "2019-10-04T08:50:27Z")

</div>

@AndyP  
Thanks for the suggestion. Actually i had thought about this initially, the problem is that in Siemens to access a bit of a word i have to use “variable\_name”.%x"bit\_number", I cannot assign the bit number as a dynamic variable. This problem is easy to overcome with bool\_array.

---

<div class="post-metadata">

**Author:** ![AndyP](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@AndyP](https://forum.inductiveautomation.com/u/AndyP)\
**Post date:** [October 4, 2019, 3:13pm UTC](https://forum.inductiveautomation.com/t/reading-bool-array-from-s7-db/28750/10 "2019-10-04T15:13:29Z")

</div>

No Problem. In an earlier project where we were monitoring the Safety Inputs form A Siemens Safety PLC we used a variable for the word then just used Boolean expression tags in the UDT pull out the bit. I know this sounds clumsy but it worked very well in practice. We had 20 odd S7 Safety PLCs to monitor and ended up needing about 100 instance of the UDT. This made the instances very easy to implement. To make it easy for site mods to naming we also loaded all the IO names and descriptions into a data table and used these for displaying rollovers and comments in the diagnostics pages. I’ve attached the UDT if you are interested. It was for Inputs but would work equally well for DBs.

[Siemens\_UDT.xml](https://us1.discourse-cdn.com/inductiveautomation/original/2X/0/0d6d0f0be555f667e47e26a7929ef3612d94e66c.xml) (8.1 KB)
