S& String from Variable offset in DB to fixed adress in DB

RheinhardtP

Lifetime Supporting Member + Moderator
Join Date
Oct 2004
Location
Perth
Posts
562
Hi Gents,

I am struggling with the following piece of code.

I have variable Strings i want to move to a general Data area as i go through different steps. I have a general offset of 26 bytes for each step. Within this structure i have my string which i want to rettrieve.

Here is my Code:

L #CurrentStepNo
L 1
-I
L 26
*I
SLW 3
LAR1 //Calculate offset in Source DB

OPN "SeqData"

LAR1 P##AnySrc
L W#16#1002 //type = byte
T W [AR1,P#0.0]
L DBB 16 //Length of string [16]
T W [AR1,P#2.0]
L 1 //From DB 1
T W [AR1,P#4.0]
L P#DBX 8.0 //Offset in DB 8.0
T D [AR1,P#6.0]


CALL "BLKMOV" //Copy chars in string
SRCBLK :=#AnySrc
RET_VAL:=MW110
DSTBLK :=#CurrentSeq.StepDescr



I want to be able to change my offset DBX8.0 dynamically using my calculation at the top for the offset. Should i do the same calculation before i load to T D [AR1,P#6.0]?? I have marked the line which i want to be able to calculate a different offset for in red.

Many Thanks

RP
 
For start it should be SLD 3 and not SLW 3 as the pointer is a double word.

To make the word offset variable, you would need to create a temp double word to store the address and build it, such as

L D#16#84000000
T TEMP_DW
L VARIABLE
SLD 3
OD
T TEMP_DW

and use this in the code

T W [AR1,P#4.0]
L TEMP_DW
T D [AR1,P#6.0]
 
I think this is enough.

Code:
      L     #CurrentStepNo
      ITD   
      L     1
      -D    
      L     26
      *D    
      //if you need constant offset addition of 8 (or something else) just add it in here
      SLD   3
      L     P#DBX 0.0
      OD   
      T         D[AR1,P#6.0]
 
Udt

Attached is my UDT structure.


I use this UDT in mutiple steps in DB 1.

Step1 UDT1
Step2 UDT1
---ect

Struct.JPG
 
Fb

Thank you for your responses.

@PeterW: Thank you Peter i will try this and revert back to the forum with my results.

@L D:I use my pointer code in a FB that has an instance DB. However my pointer looks at another external DB.

Rheinhardt
 
Last edited:
Here's a more generic approach which uses the fact that you have declared a UDT for your Step sructure.

If you use the "magic number" method (by that I mean you have hard coded things like 26 as the size of the UDT and P#8.0 because that is where the string starts within the UDT) then if you ever change the UDT you will have to find all of the magic numbers and change them.

You can use the UDT in your code and let the editor work out the magic numbers for you each time it generates an access to a UDT or an element of the UDT. Of course nothing is for free so there is more processing required and if you change the UDT you will have to update each block that references the UDT. The updating of the blocks is the key in that S7 will update all the relevant offsets for the UDT accesses.

Have a look at this simple example:
 
Last edited:
Hi LD,

Ur code works perfectly only i must admit i have some questions, i think this type of coding is a bit above my knowledge base.👨🏻‍🏫👨🏻‍🏫👨🏻‍🏫

1. How do you generate the source adress offset if i look in FC100 i can see nothing that make sense for this attribute.

2. If i were to add string how would i change this offset?

Thank you for assistance in this matter !!

RP
 
The offset for the string is not generated - FC100 takes a copy of the whole UDT from the the source DB and copies it to the STAT area of the FB. The named variable for the string can then be used - you never need to use the offset.

If you change the UDT, update the global DB, update the FB and it's instance DB.
 

Similar Topics

I am using Kepware to connect to a Keyence KV PLC. I am trying to read a string variable DM20021. Kepware error" string is not valid for DM20021"...
Replies
0
Views
594
Long story short, I am creating a display to display an array of Strings from PLC [topic]VarStrA_RPHD.Desc[VarStrB] totally 100 strings...
Replies
1
Views
1,408
Hi, In Twincat3 x64 is it possible to get value of variable of which name is read from a string variable? VAR VAL1 :REAL:=555...
Replies
2
Views
1,221
Hi! i try to use string as variable name in a forloop my problem is tahat my if insturktion not allow strings as variable name. my declaration...
Replies
7
Views
2,381
Good day. The problem is the following, I am using the Machine shop parker program to program an HMI that communicates with a logix5573 PLC. I can...
Replies
0
Views
1,267
Back
Top Bottom