Sign in to follow this  
Followers 0
EirikV

Dereferencing pointers in ST

5 posts in this topic

Hi, Like the topic indicates, I want to dereference a pointer fed in to a function block, and I also want to performe some arithmetic on the pointer and dereference it again. I cannot figure out how to do this in structured text. Is it even possible? Eirik

Share this post


Link to post
Share on other sites
Hi Eirik, To my best knowledge this is NOT possible in Omron ST. Of course this is the more powerful functions of C languages, but also the potential cause of 'memory overruns' and accidental data corruption if abused I know the ladder language supports Indirect Addressing which you might be able to use, and I also note when using Arrays there are warnings in the manual that [addressing array elements with an index greater than array size will produce unpredictable results]... I assume this means it will overwrite the array which I suppose is a kind of dereferencing... Maybe if you post your problem the inventive programmers will be stimulated rgds Bertie

Share this post


Link to post
Share on other sites
This can be achieved using index registers DR,IR. Prior to function block execution load the Index Register pointer with MOVR and the Data Register value using MOV. Then MOV DR,IR to a memory location, I call this the scratchpad. Within the function block declare a variable with the same memory address the scratchpad. The FB variable can Internal OR In/Out. If the variable is internal then you must use AT in the declaration. After function block execution move data from the scratchpad back to the current memory address. The DR pointer can be indexed to point to the next memory location you want the FB to act on. For example, say you want the FB to modify single words of data starting at 3000. Set the scratchpad as an unused data area, in this example 6000. MOVR 3000 IR1. Set IR pointer to 3000 MOV &0 DR1. Set DR pointer to 0. MOV DR1,IR1 6000 . Move data from offset DR1 of 3000 to the scratchpad 6000. Now execute your function block on the scratchpad 6000. After FB has executed MOV 6000 DR1,IR1. [This copies data from the scratchpad back to the current DR1,IR1 memory location] Now increment DR1 by 1. The next execution of the FB will now act on the next word and move data from 3001 to 6000. I have used this technique to process STRINGS as ARRAYS within FB's. In that case after loading the pointers I use the MOV$ DR1,IR1 3000 to move a string to the scratchpad. Execute the FB and modify the scratchpad string. Then MOV$ 3000 DR1,IR1 Just have to be careful that memory areas don't overlap and clear the scratchpad after each execution..

Share this post


Link to post
Share on other sites
Here is an simple example of using indirect addressing with Structured Text. Prior to fb execution data is moved from 3000 + DR1 to scratchpad 6000. After fb execution it is written from the scratchpad 6000 back to the original location 3000 + DR1. The fb is very simple in this case, just adds 2 to the 3000 + DR1 location. IndirectAddressing_in_Structured_Text.cxp

Share this post


Link to post
Share on other sites
Berti Baker, yeah, I'm kind of a C dork So I just miss those kind of things in the PLC. Many thanks BITS N BYTES for your replies and example program. I will have a look at it Edited by EirikV

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
Sign in to follow this  
Followers 0