S7: Problem with Pointer and indirect addressing

douyi

Member
Join Date
Aug 2005
Location
Toronto
Posts
123
Hi guys,

Disappeared few weeks for writing code, I'm back with a problem.

There is a repeating work for different group of signals, so I decide to go with FC (I don't need IDB), and the signals in different DB but as the same UDT. so it's like

DB1.G1.f1
DB1.G1.f2
...
DB1.G1.f5
...
DB1.G5.f1
...
DB1.G5.f5
.
.
.
DB10.G5.f1
...
DB10.G5.f5


G is the name of UDT, and f is the elements of the UDT (Boolean).
Because it is too much parameters, I want to use a pointer to the start address, so within the FC I can add shift and get the right element. So the input is an "any" data type which is a pointer should including the DB name and the Address info. and inside the FC it's like
[AR1,p#0.0] for elements 1
[AR1,p#0.1] for elements 2
[AR1,p#0.2] for elements 3
[AR1,p#0.3] for elements 4
[AR1,p#0.4] for elements 5

When it's running, it seems the pointer pass some other values and the logic doesn't work. I simplyfied the logic to test it like

A [AR1,p#0.0]
= DB100.DBX0.0

A [AR1,p#0.1]
= DB100.DBX0.1

...

A [AR1,p#0.5]
= DB100.DBX0.5

Then I turned the bits on outside of the FC,But all the bits in DB100.DBB0 is always off.

Did i do wrong with the indirect addressing or something else?

Thanks for help.
 
As far as I remember when you pass an ANY pointer to a function then you get an address to the data in the previous instance local data where the ANY pointer data is stored.

Code:
FUNCTION FC1: VOID 
VAR_INPUT
ANY_DATA: ANY;
END_VAR
 
BEGIN
L p#ANY_DATA;
LAR1;
L D[AR1,P#6.0]; //<-here is your pointer
LAR1;
//now you can go with your code
END_FUNCTION
 
Last edited:
jacekd said:
As far as I remember when you pass an ANY pointer to a function then you get an address to the data in the previous instance local data where the data is stored.

But it should be ok, the DBxxx.DBXxxx.x address is unique. The point is, the value in the ANY point is not right, or it passed some other address.
 
Do you mean useing AR1,AR2 together to accomodate 6 bytes ?

BTW, an address given by an ANY pointer can be located inside a Datablock. You have to open explicitly such a block before accessing it indirectly.
 
jacekd said:
Do you mean useing AR1,AR2 together to accomodate 6 bytes ?

BTW, an address given by an ANY pointer can be located inside a Datablock. You have to open explicitly such a block before accessing it indirectly.

I want to use two different DB in one FC, so I need two pointer to do that. That's the reason I need AR1 and AR2.

About the opn DB, I'm suffering from that. That's way I got a fault which is "SF" - in another post. after read your post I just realized it's because of that.

But how can I get to know which DB it should be opened? as long as I passed the pointer, it should have the db info. the point is, how to extract that?

Thanks a lot.
 
Here is a function that will return the individual components of an any pointer. As you can see, the db number is stored in offset P#4.0

Code:
[left] FUNCTION FC 3 : VOID
TITLE =
VERSION : 0.1 


VAR_INPUT
  pAny : ANY ;  
END_VAR
VAR_OUTPUT
  byId : BYTE ; 
  byType : BYTE ;   
  iSize : INT ; 
  iDBNumber : INT ; 
  dwPointer : DWORD ;   
END_VAR
BEGIN
NETWORK
TITLE =get individual components of an any pointer

	  L	 P##pAny; 
	  LAR1  ; 
	  L	 B [AR1,P#0.0]; 
	  T	 #byId; 
	  L	 B [AR1,P#1.0]; 
	  T	 #byType; 
	  L	 W [AR1,P#2.0]; 
	  T	 #iSize; 
	  L	 W [AR1,P#4.0]; 
	  T	 #iDBNumber; 
	  L	 D [AR1,P#6.0]; 
	  T	 #dwPointer; 
END_FUNCTION
[/left]
 
Thanks guys, I tried, it works, and I also solved the problem because of that (another thread), but here comes another question:

about the opn DB: if I have to operate in two DBs, now what I do is

when I'm using DB100, I use opn DB100,
then next line if I use DB200, I use opn DB200,
if I use DB100 at the 3rd line, I use opn DB100 again.

and there is no program error on that. Is there any good way to do that?

Thanks again.
 
You can have two DB's open at the same time as follows:

Code:
	  L	 100
	  T	 #iGlobalDB
	  L	 101
	  T	 #iInstanceDB
	  OPN   DB [#iGlobalDB]			 //opens DB100 for global db
	  OPN   DI [#iInstanceDB]		   //opens DB101 for instance db
	  A	 DBX [AR1,P#0.0]			 //bit in global DB
	  =	 DIX [AR2,P#0.0]			 //bit in instance DB
	  A	 DBX [AR1,P#0.1]
	  =	 DIX [AR2,P#0.1]

(Note that if you are using multiple instance function blocks, you need to save AR2 and the instance DB number before your processing and then restore them afterwards)
 
Thanks.

I know how to save and reload AR2, but I don't know how to save and reload Instance DB number. Can you show me that?

Thanks again.
 
You can save/restore the dbs as follows:

Code:
L DBNO //get current global DB
T #iSavedGDB //save in temp area
L DINO //get current instance DB
T #iSavedIDB //save in temp area
 
 
 
OPN DB [#iSavedGDB] //restore global db
OPN DI [#iSavedIDB] //restore instance DB
 
Something interesting, it works, but not always.

I can switch over between two DBs, and I can see it correctly shown in DB1 and DB2 column. But the value didn't pass to DB, but at least it passed to somewhere in correct DB.

So I changed back to use internal bits. At the begining, I get status (values) from DB and give to internal bits, on the end I give them back, no more fancy stuff and working fine.
 
writing to outputs

Hello all,

a need to move a lot of data to the pqw area
Normally you can do this

L db49.dbd0
t pqd128
L db49.dbd4
t pqd132 and so on.

I thried this
cal SFC 20
SRCBLK = P#DB49.DBX0.0 Byte 128
RETVAL = #TempInt
DSTBLK = P#Q128.0 Dword 32

There are no problems to compile it but it does not work
What goes wrong
 

Similar Topics

Hi guys, I'm new here and I just started to work as junior automation engineer. It will be much more easier now I have access to this site. Wish...
Replies
3
Views
1,480
Hy Experts, I have some problem with this intsructions: - CREA_DBL - READ_DBL - WRIT_DBL I want to create a recipie management in 314 CPU via...
Replies
3
Views
3,538
i write code STL and simulate but result Q2.7 not set , i dont know why :scratch: , help me plz thanks LAR1 P#Q2.7 S [AR1,P#0.0] or LAR P#2.7...
Replies
3
Views
1,697
Hi! I use the following code (from FC) to copy words between DBs given an word offset address and nbr of words to copy. But I found out it fails...
Replies
3
Views
2,852
Hi! I modified some code, probably from member LD, to make a function element := ReadFromArray(firstElement : ANY, offset : INT) It seems...
Replies
2
Views
4,149
Back
Top Bottom