BabyLover

Bit access in Words in CX-Programmer using structured text

10 posts in this topic

Hello,

I'm new to CX-Programming and I could really use your help.

I want to make function block that will check the bit status from a word input variable. I made input variable and internal variable array of bool(32) and I thought I can just use the code example:

array[1]:=word.1 etc... but it is not allowed in CX-programmer.

My goal is rearranging  the bits status from the input word to the output word. I don't need just making the 0 to 1 or otherwise...

Thank you in advance.

Share this post


Link to post
Share on other sites

Make bit arrays:

bitarray.jpg.5b592bf8eeb6c03518bbf798f5e

The structured text above will make W2.00 be equal to W0.05.

Of course if you go over 15, the bits roll into the next word as all addresses are 16 bits starting with bit 0.  For instance Input_Bit_Array[16] is pointing to W1.00.

Share this post


Link to post
Share on other sites

Hello , 

Thank you for the answer. This sure works. The problem is that I want this to be a function block and in the FB I can't make input to be an array and give addresses. Is it possible somehow to convert the input word to array of boolean inside the FB ?

Share this post


Link to post
Share on other sites

Make your variable an IN/OUT variable.  You can make it a bit array then.

bitarray2.jpg.bad6b2d1b1f5a233d532dcad32bitarray3.jpg.217fe9d105816f4ac2b1af4cb0

The FB above should do the same thing that my sample program above did.

 

1 person likes this

Share this post


Link to post
Share on other sites

Hello Mr Walsh,

I played a little bit with data types. I created data type that consists array of boolean[64]. Then I created arrays of the data type[60] that I use in my FB. In the FB the data types are defined as IN OUT variables. I'm getting error about indexing of the arrays. Here are some pictures:

5f07345426af7_datatype.PNG.e91ad655833d3

5f0734635f379_Errorcode.PNG.51d42fd039e6

errors.PNG.61f5148b6fe1258b8e03a4a155d7f

Can you see what am I doing wrong here?

Share this post


Link to post
Share on other sites

It won't let you use a variable for the index of the bits when it is a structure.  This is ok:

In.CheckLWord[4]

This is not:

In.CheckLword[j]

Not sure why, but that is how it is.  CXP will only let you use variable indexes for arrays and apparently it does not recognize an array of an array. The unexpected syntax will go away after this issue is resolved.  

Sorry.

 

1 person likes this

Share this post


Link to post
Share on other sites

Posted (edited)

1 hour ago, Michael Walsh said:

It won't let you use a variable for the index of the bits when it is a structure.  This is ok:

In.CheckLWord[4]

This is not:

In.CheckLword[j]

Not sure why, but that is how it is.  CXP will only let you use variable indexes for arrays and apparently it does not recognize an array of an array. The unexpected syntax will go away after this issue is resolved.  

Sorry.

  

You can reference the index of the structure with a variable IF you give the structure an address in the symbol table.

 

symbol_table.thumb.png.e974ff8e95d9aa006

 

in_out_variable.thumb.png.ea90af358677f7

 

Edited by chelton
duplicate image

Share this post


Link to post
Share on other sites

Apologies Michael, You are correct. 

Share this post


Link to post
Share on other sites

Thank you for the help. I guess I can make it with one big array of boolean(3840) and fill the D memory chronologically with 64 so I can get the bits in the array with the right order. And then I can reorder them as I wish.

Share this post


Link to post
Share on other sites

Does the function block have to be st?

you could achieve the same thing easily is ladder using ir method.

 

can send example if need be 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now