3

Goal

I am programming an Allen-Bradley / Rockwell CompactLogix PLC in SCL. I would like to determine the size of Arrays at runtime. It would be possible to enter the Array lengths as constants into the code before compiling. However, the reusability would be improved greatly if the lengths of the arrays could be determined automatically.

Problem

There is the function SIZE(Source,Dimtovary,Size) which does exactly what I need although only for SINT[] INT[] DINT[] REAL[] structure and STRING. Unfortunately I need the length of BOOL[].

"The SIZE instruction finds the number of elements (size) in the designated dimension of the Source array or string operand and places the result in the Size operand. The instruction finds the size of one dimension of an array."

Pseudo code

Int_array := INT[16];
Bool_array := BOOL[64];

SIZE(Int_array[0],0,Int_array_len);
// Works, Int_array_len contains 16

SIZE(Bool_array[0],0,Bool_array_len); 
// Isn't compilable becaus size(); isn't defined for boolean arrays

Environment

Question

Is there a way to determine the length of a boolean array for example BOOL[64]?

Additionally, is there a fundamental reason why SIZE(Source,Dimtovary,Size); doesn't work with boolean arrays?

KasparJohannes
  • 520
  • 2
  • 7
  • 26
  • 1
    I'm far from an expert in this topic (more of the opposite), but a bit of searching around tells me BOOL arrays are very limited, and people tend to avoid them for this reason. Usually the workarounds involve using arrays of DINTs instead and handling the bit manipulations appropriately. – Dan Mašek Apr 18 '18 at 00:31

1 Answers1

2

The answer is simply no; it is not possible to get the size of a BOOL[] array.

As @DanMašek suggested correctly, BOOL[] arrays are very limited. It is even recommended using UDTs containing members of type BOOL instead.

Unfortunately, I still have no solution to get the length of multiple BITs arranged in some way and loop through them in a for loop.

KasparJohannes
  • 520
  • 2
  • 7
  • 26
  • If you use a UDT to wrap a single BOOL, then unfortunately each wrapped BOOL ends up filling a SINT under the hood as BOOLs map to the bits of a hidden SINT within the UDT. You can see this by exporting the UDT and looking at the XML. – VoteCoffee Sep 08 '21 at 15:13
  • You can loop through them by copying them to a SINT array first, and then iterate through the bits of each SINT in the SINT array. SIZE isn't reliable though, as the SINT array size (x8) would match the BOOL array size is if it was hardcoded. – VoteCoffee Sep 08 '21 at 15:23
  • Alternatively, you can write a packing routine and use the for to set the first bool of a SINT array of the same size as the BOOL array to the BOOL value, and then another unpacking routine and a for to copy it back from the SINT array to the BOOL array. This allows the index to stay meaningful but takes more memory. – VoteCoffee Sep 08 '21 at 15:25
  • I still agree with avoiding BOOL[], but if you don't have a choice, those are workarounds. – VoteCoffee Sep 08 '21 at 15:26