Help - Search - Members - Calendar
Full Version: Dereferencing pointers in ST
Forums.MrPLC.com > PLCs and Supporting Devices > Omron > CX-Programmer
EirikV
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
Berti Baker
QUOTE(EirikV @ Feb 1 2009, 05:43 PM) [snapback]78308[/snapback]

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

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 wow.gif

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]... lookaround.gif 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 colgate.gif

rgds
Bertie
BITS N BYTES
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..
smile.gif



BITS N BYTES
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.

smile.gif
EirikV
Berti Baker, yeah, I'm kind of a C dork smile.gif 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 smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.