BOOL array to USINT

pasbra

Member
Join Date
Oct 2021
Location
Québec
Posts
31
Hi,

I must move an array of 16 Bool to an array of 2 USINT. I able to do this as attach pisture, but it's not cute!

How I can do this with "COP" function or loop instruction?

I use CCW software and micro 820 PLC. I try to do this also on studio 5000, but I dont able.

Thank for your help

Pascal

Bool to USINT.PNG
 
How I can do this with ... loop instruction?
Code:
Datalogix[1] := 0;
Datalogix[2] := 0;
dint_mask := 1;
for index := 0 to 7
  Datalogix[1] := Datalogix[1] + (dint_mask *  ANY_TO_DINT(Output[index]));
  Datalogix[2] := Datalogix[2] + (dint_mask *  ANY_TO_DINT(Output[index+8]));
  dint_mask := dint_mask + dint_mask;
  if dint_mask = 128 then
    dint_mask := -128;
  end_if;
end_for;
Still not cute. Or ...
Code:
Datalogix[1] := 0;
Datalogix[2] := 0;
for index := 0 to 15
  subscript := 1 + ANY_TO_DINT(index.8);
  Datalogix[subscript] := Datalogix[subscript] + (dint_mask *  ANY_TO_DINT(Output[index]));
  dint_mask := dint_mask + dint_mask;
  if dint_mask = 128 then
    dint_mask := -128;
  elif dint_mask = -256 then
    dint_mask := 1;
  end_if;
end_for;
and even less cute
 
If i would do it with loops i would probably do it something like this (untested):
Code:
USINTArray[0] := 0;
USINTArray[1] := 0;


FOR i := 0 to 7 DO
  IF BoolArray[i] THEN
    USINTArray[0] := USINTArray[0] + SHL(1, i);
  END_IF;
END_FOR;

FOR i := 8 to 15 DO
  IF BoolArray[i] THEN
    USINTArray[1] := USINTArray[1] + SHL(1, i-8);
  END_IF;
END_FOR;
 
Last edited:
Whilst there is more that one to skin a cat, for 16 bools, i'd just leave it to what you have done.

KISS.


+1 for this. If the COP cannot transfer bits between B3 and N7, like it can in RSLogix 500, writing a loop is only there for people like me who (A) have an irrational aversion to brute force, and (B) have no trouble parsing non-simple code.
 
I need to modify, little bit your code ojz0r because SHL instruction need DINT. The code work whell. I must watch SHL instruction so mush...

The code is now:
Code:
USINTArray[0] := 0;
USINTArray[1] := 0;
FOR i := 0 to 7 DO 
  IF BoolArray[i] THEN // Si le bit est vrai, on procède au décalage vers la gauche
   Resultat_DINT:= SHL(1, i); // La variable Resultat doit etre de type DINT...
   USINTArray[0] := USINTArray[0] + ANY_TO_USINT(Resultat_DINT);//Resultat_USINT; //+ SHL(1, i);
  END_IF;
END_FOR;
FOR i := 8 to 15 DO // Pour la 2e variable, même principe...
  IF BoolArray[i] THEN
   Resultat_DINT:= SHL(1, i-8);
   USINTArray[1] := USINTArray[1] + ANY_TO_USINT(Resultat_DINT); //SHL(1, i-8);
  END_IF;
END_FOR;

I test other code later drbitboy.

Thank a lot for your help.

Pascal
 
First the 16 BOOL is that a true array of 16 or a INT data type?
Data type INT and an array of 1 BOOL
if it's a INT data type just use the COP function
Source is the INT data type
Destination is a USINT[0] data type
Lenght woud be 2
bits 0 -7 would be in USINT [0]
bits 8 - 15 would be in USINT [1]
 
I need to modify, little bit your code ojz0r because SHL instruction need DINT. The code work whell. I must watch SHL instruction so mush...

Good, didnt know about the dint type requirement.
However that is how i would do it if i had to use loops. I still think your original method is the clearest (kiss as mentioned) and probably less resource heavy.

Incidently my favourite language :)
 
I looked closer and drbitboys smart solution with one loop and that is even shorter keeping it in one loop and add 8 to index in the BoolArray:

Code:
USINTArray[0] := 0;
USINTArray[1] := 0;
FOR i := 0 to 7 DO
  IF BoolArray[i] THEN // Si le bit est vrai, on procède au décalage vers la gauche
   Resultat_DINT:= SHL(1, i); // La variable Resultat doit etre de type DINT...
   USINTArray[0] := USINTArray[0] + ANY_TO_USINT(Resultat_DINT);//Resultat_USINT; //+ SHL(1, i);
  END_IF;
  IF BoolArray[i+8] THEN
   Resultat_DINT:= SHL(1, i);
   USINTArray[1] := USINTArray[1] + ANY_TO_USINT(Resultat_DINT); //SHL(1, i);
  END_IF;
END_FOR;
 
You are the first person I can remember who says this. I very much sympathize with this, although I choose ladder for certain applications.


Yes Pascal/Structured Text is definately a personal favorite, but since its not for everyone most of the code will still be in function block diagram for clients.
 
You guys are not alone. I have done a lot in Pascal dialects in the past and thus had no trouble learning ST. I use ladder for boolean code that people with more electrical than programming knowledge must read. And it makes for easy visual debugging when going online. I prefer to do things like loops, calculations and state machines in ST.
 
First the 16 BOOL is that a true array of 16 or a INT data type?
Data type INT and an array of 1 BOOL
if it's a INT data type just use the COP function
Source is the INT data type
Destination is a USINT[0] data type
Lenght woud be 2
bits 0 -7 would be in USINT [0]
bits 8 - 15 would be in USINT [1]

I’m onboard this.
 

Similar Topics

I received the following message via PM, and am posting here publicly to help others. ============================================ I had a...
Replies
10
Views
1,026
I need to move a large Bool array to Word array in Schneider Control Expert. Is there an easier way to do this? For example: Bool[128] array to...
Replies
4
Views
1,354
I am trying to make a function that count the number of True BOOL inn a specific Array and return the count to an INT AlarmArrayActiveAlarms. I...
Replies
8
Views
4,442
Hi out there is it possible to Compare a bool array to a bool, in studio 5000. I'm think that if One of the bool's in my array is true, then i...
Replies
10
Views
6,376
We are using an array of bools for our fault code logic, and I need to reset all the bits in the array when the operator presses the fault reset...
Replies
2
Views
2,146
Back
Top Bottom