S7 pointer inquirey

lad_stl

Member
Join Date
Dec 2013
Location
giza
Posts
19
hiii every body ,i try to write FB that copy length of data from data block to another data block but when i run it gives no compile error but the result of the execution is wrong(no data copy).please help me where is the error in the code

moh.jpg

the code is:
L #dbrows //number of bytes to copy
L -1
+I

T #temp_count

L P##p1 //pointer to source DB
LAR1
L W [AR1,P#0.0]
T #db_nr1
OPN DB [#db_nr1]
L D [AR1,P#2.0]
LAR1


L P##p2 //pointer to dest. DB
LAR2
L W [AR2,P#0.0]
T #db_nr2
OPN DB [#db_nr2]
L D [AR2,P#2.0]

LAR2


loop: L W [AR1,P#0.0]

T W [AR2,P#0.0]

+AR2 P#2.0
+AR1 P#2.0
L #temp_count
L -1
+I
T #temp_count
L 0
==I
JCN loop
BE
 
Last edited:
Can you give an example of the data contained in the DB's before and after the copying takes place. You can only have one global DB open at a time so the code in your loop will always be using the last DB that was opened which is dbnr_2

Your image is unreadable - post the source code of the FB and also post the call so it is readable.
 
Last edited:
You have hier an example (... sorry for the german mnemonics) 🍻

Code:
DATA_BLOCK "a"
TITLE =
VERSION : 0.1
  STRUCT 	
   a : ARRAY  [0 .. 3 ] OF INT  := 23;	
  END_STRUCT ;	
BEGIN
   a[0] := 23; 
   a[1] := 0; 
   a[2] := 0; 
   a[3] := 0; 
END_DATA_BLOCK

DATA_BLOCK "b"
TITLE =
VERSION : 0.1
  STRUCT 	
   b : INT  := 45;	
  END_STRUCT ;	
BEGIN
   b := 45; 
END_DATA_BLOCK

FUNCTION "ANY" : VOID
TITLE =
//ERSATZ "P##..."   fĂĽr Pointer auf Elemente in  Strukturen , UDT , Arrays , Multiinstanzen...etc
VERSION : 0.1
VAR_INPUT
  LAR1 : ANY ;	//ALLE mögliche Daten
END_VAR
VAR_TEMP
  PNTR : DWORD ;	//POINTER
  DBNO : DWORD ;	//DBNO
END_VAR
BEGIN
NETWORK
TITLE =SIOAN
//Hier wird der Speicherbereich fĂĽr die Benutzung des Pointers ausserhalb der "ANY" Funktion korrigiert und (Pointer) gleich im AR1 geladen 
      T     #PNTR; //Lade Pointerdaten    aus AKKU1
      TAK   ; 
      T     #DBNO; // Lade DB Nr aus AKKU2

      L     P##PNTR; //Lade Pointer auf #PNTR Anfang
      LAR1  ; 
      L     B [AR1,P#0.0]; 
      L     B#16#87; 
      ==D   ; //Speicherbereich abfragen
      SPBN  VL; //IF THEN  
      L     B#16#86; 
      T     B [AR1,P#0.0]; 
VL:   U(    ; 
      L     #DBNO; 
      L     L#0; 
      <>D   ; // #DBNO auf NULL abfragen  
      )     ; 
      U(    ; 
      L     #DBNO; 
      L     DINO; 
      ==D   ; // #DBNO = DINO abfragen
      )     ; 
      U(    ; 
      L     B [AR1,P#0.0]; 
      L     B#16#84; 
      ==D   ; //Speicherbereich abfragen  
      )     ; 
      SPBN  DI; //IF THEN  
      L     B#16#85; 
      T     B [AR1,P#0.0]; 
DI:   L     #PNTR; 
      LAR1  ; 
END_FUNCTION

FUNCTION_BLOCK FB 1
TITLE =
VERSION : 0.1
VAR_INPUT
  z : ARRAY  [0 .. 3 ] OF INT ;	
END_VAR
VAR
  x : INT ;	
END_VAR
VAR_TEMP
  v : INT ;	
  y : WORD ;	
END_VAR
BEGIN
NETWORK
TITLE =
      CALL "ANY" (
           LAR1                     := #x);
      AUF   "a"; 
      L     3; 
      T     W [AR1,P#0.0]; 
      L     #x; 
      T     MD     0; 
NETWORK
TITLE =
      CALL "ANY" (
           LAR1                     := "a".a[0]);
      AUF   "a"; 
      L     7; 
      T     W [AR1,P#0.0]; 
      L     "a".a[0]; 
      T     MD     4; 
NETWORK
TITLE =
      CALL "ANY" (
           LAR1                     := "b".b);
      AUF   "b"; 
      L     15; 
      T     W [AR1,P#0.0]; 
      L     "b".b; 
      T     MD     8; 
     BE    ; 
NETWORK
TITLE =
      CALL "ANY" (
           LAR1                     := #z[0]);
[COLOR="blue"]      L     0; 
u:    U(    ; //WHILE   
      T     #v; 
      L     4; 
      <I    ; 
      )     ; 
      SPBN  o; //IF THEN 
      L     31; 
      T     W [AR1,P#0.0]; 
      +AR1  P#2.0; 
      L     #v; 
      +     1; 
      SPA   u; //LOOP
o:    NOP   0; [/COLOR]
      L     #z[3]; 
      T     MD    12; 
      BE    ; 
END_FUNCTION_BLOCK

DATA_BLOCK DB 1
TITLE =
VERSION : 0.0
 FB 1
BEGIN
   z[0] := 0; 
   z[1] := 0; 
   z[2] := 0; 
   z[3] := 0; 
   x := 0; 
END_DATA_BLOCK

FUNCTION FC 2 : VOID
TITLE =
VERSION : 0.1
VAR_TEMP
  z : ARRAY  [0 .. 3 ] OF INT ;	
  v : INT ;	
END_VAR
BEGIN
NETWORK
TITLE =
      CALL "ANY" (
           LAR1                     := #z[0]);
      L     4;    
[COLOR="Blue"]u:    T     #v; 
      L     255; 
      T     W [AR1,P#0.0]; 
      +AR1  P#2.0; 
      L     #v; 
      LOOP u[/COLOR]
      L     #z[3]; 
      T     MD    12; 
      BE    ; 
END_FUNCTION

ORGANIZATION_BLOCK OB 1
TITLE = "Main Program Sweep (Cycle)"
VERSION : 0.1
VAR_TEMP
  OB1_EV_CLASS : BYTE ;	//Bits 0-3 = 1 (Coming event), Bits 4-7 = 1 (Event class 1)
  OB1_SCAN_1 : BYTE ;	//1 (Cold restart scan 1 of OB 1), 3 (Scan 2-n of OB 1)
  OB1_PRIORITY : BYTE ;	//Priority of OB Execution
  OB1_OB_NUMBR : BYTE ;	//1 (Organization block 1, OB1)
  OB1_RESERVED_1 : BYTE ;	//Reserved for system
  OB1_RESERVED_2 : BYTE ;	//Reserved for system
  OB1_PREV_CYCLE : INT ;	//Cycle time of previous OB1 scan (milliseconds)
  OB1_MIN_CYCLE : INT ;	//Minimum cycle time of OB1 (milliseconds)
  OB1_MAX_CYCLE : INT ;	//Maximum cycle time of OB1 (milliseconds)
  OB1_DATE_TIME : DATE_AND_TIME ;	//Date and time OB1 started
END_VAR
BEGIN
NETWORK
TITLE =
      CALL FB     1 , DB     1 ;
NETWORK
TITLE =
      CALL FC     2 ;
END_ORGANIZATION_BLOCK
 
Last edited:
More simple example (try to use symbolics everytime you can) : 🍻

Code:
DATA_BLOCK "b1"
TITLE =Sioan
VERSION : 0.0
 FB 1
BEGIN
   x := FALSE; 
   a[0] := 0; 
   a[1] := 0; 
   a[2] := 0; 
   a[3] := 0; 
END_DATA_BLOCK

DATA_BLOCK "b2"
TITLE =Sioan
VERSION : 0.1
  STRUCT     
   a : BOOL ;    
   b : ARRAY  [0 .. 3 ] OF INT  := 0;    
  END_STRUCT ;    
BEGIN
   a := FALSE; 
   b[0] := 2; 
   b[1] := 22; 
   b[2] := 222; 
   b[3] := 2222; 
END_DATA_BLOCK

DATA_BLOCK "b3"
TITLE =Sioan
VERSION : 0.1
  STRUCT     
   x : BOOL ;    
   y : ARRAY  [0 .. 3 ] OF INT  := 6, 66, 666, 6666;    
  END_STRUCT ;    
BEGIN
   x := FALSE; 
   y[0] := 6; 
   y[1] := 66; 
   y[2] := 666; 
   y[3] := 6666; 
END_DATA_BLOCK

FUNCTION_BLOCK FB 1
TITLE =Sioan
VERSION : 0.1
VAR_INPUT
  x : BOOL ;    
  a : ARRAY  [0 .. 3 ] OF INT ;    
END_VAR
VAR_TEMP
  e : INT ;    
END_VAR
BEGIN
NETWORK
TITLE = Sioan
      U     #x; 
      SPBN  o; 
      L     MW     0; 
      T     "b2".b[0]; 
      CALL "BLKMOV" (
           SRCBLK                   := "b2".b,
           RET_VAL                  := #e,
           DSTBLK                   := #a);
o:    NOP   0; 
END_FUNCTION_BLOCK

ORGANIZATION_BLOCK OB 1
TITLE = "Main Program Sweep (Cycle)"
VERSION : 0.1
VAR_TEMP
  OB1_EV_CLASS : BYTE ;    //Bits 0-3 = 1 (Coming event), Bits 4-7 = 1 (Event class 1)
  OB1_SCAN_1 : BYTE ;    //1 (Cold restart scan 1 of OB 1), 3 (Scan 2-n of OB 1)
  OB1_PRIORITY : BYTE ;    //Priority of OB Execution
  OB1_OB_NUMBR : BYTE ;    //1 (Organization block 1, OB1)
  OB1_RESERVED_1 : BYTE ;    //Reserved for system
  OB1_RESERVED_2 : BYTE ;    //Reserved for system
  OB1_PREV_CYCLE : INT ;    //Cycle time of previous OB1 scan (milliseconds)
  OB1_MIN_CYCLE : INT ;    //Minimum cycle time of OB1 (milliseconds)
  OB1_MAX_CYCLE : INT ;    //Maximum cycle time of OB1 (milliseconds)
  OB1_DATE_TIME : DATE_AND_TIME ;    //Date and time OB1 started
END_VAR
BEGIN
NETWORK
TITLE =Sioan
      CALL FB     1 , "b1"  (
           x                        := E      0.0,
           a                        := "b3".y);
END_ORGANIZATION_BLOCK
 
If you just ...play with this sample you'll understand how copy/past-ing works in S7
( ... Eureka effect )
:D 🍻

Code:
DATA_BLOCK "b"
TITLE =
VERSION : 0.1
  STRUCT 	
   a : REAL ;	
   b : ARRAY  [0 .. 5 ] OF INT  := 0, 1, 2, 3, 4, 5;	
  END_STRUCT ;	
BEGIN
   a := 0.000000e+000; 
   b[0] := 0; 
   b[1] := 1; 
   b[2] := 2; 
   b[3] := 3; 
   b[4] := 4; 
   b[5] := 5; 
END_DATA_BLOCK


FUNCTION FC 1 : VOID
TITLE =
VERSION : 0.1
VAR_INPUT
  IN : ANY ;	//Any pointer
  SET : INT ;	//How many elements ?
END_VAR
VAR_OUTPUT
  OUT : ANY ;	//"BLKMOV" Any
END_VAR
VAR_TEMP
  saveAR1 : DWORD ;	//save AR1
END_VAR
BEGIN
NETWORK
TITLE =
      TAR1  #saveAR1; 
NETWORK
TITLE =
      L     P##IN; 
      LAR1  ; 
      L     P##OUT; 
      LAR2  ; 
      L     D [AR1,P#0.0]; 
      T     D [AR2,P#0.0]; 
      L     D [AR1,P#4.0]; 
      T     D [AR2,P#4.0]; 
      L     W [AR1,P#8.0]; 
      T     W [AR2,P#8.0]; 
NETWORK
TITLE =
      L     P##OUT; 
      LAR1  ; 
[COLOR="Red"]      L     #SET; 
      T     W [AR1,P#2.0]; [/COLOR]
      L     B [AR1,P#6.0]; 
      L     B#16#87; 
      ==D   ; 
      JCN   VL; 
      L     B#16#86; 
      T     B [AR1,P#6.0]; 
VL:   A(    ; 
      L     W [AR1,P#4.0]; 
      L     L#0; 
      <>D   ; 
      )     ; 
      A(    ; 
      L     W [AR1,P#4.0]; 
      L     DINO; 
      ==D   ; 
      )     ; 
      A(    ; 
      L     B [AR1,P#6.0]; 
      L     B#16#84; 
      ==D   ; 
      )     ; 
      JCN   DI; 
      L     B#16#85; 
      T     B [AR1,P#6.0]; 
DI:   L     D [AR1,P#6.0]; 
      LAR1  ; 
NETWORK
TITLE =
      LAR1  #saveAR1; 
END_FUNCTION


FUNCTION_BLOCK "fb"
TITLE =
VERSION : 0.1
VAR_INPUT
  x : ARRAY  [0 .. 5 ] OF INT ;	
END_VAR
VAR
  y : ARRAY  [0 .. 5 ] OF INT ;	
END_VAR
VAR_TEMP
  z : ARRAY  [0 .. 5 ] OF INT ;	
  Any1 : ANY ;	
  Any2 : ANY ;	
  err : INT ;	
END_VAR
BEGIN
NETWORK
TITLE =
      L     0; 
      T     #y[0]; 
      T     #y[1]; 
      T     #y[2]; 
      T     #y[3]; 
      T     #y[4]; 
      T     #y[5]; 
NETWORK
TITLE =
      CALL FC     1 (
           IN                       := [B]"b".b[2][/B],//play with this
           [COLOR="Red"]SET                      := 2[/COLOR],//play with this[B]![/B]
           OUT                      := [COLOR="Blue"]#Any1[/COLOR]);
NETWORK
TITLE =
      CALL FC     1 (
           IN                       := [B]#y[1][/B],//play with this
           [COLOR="red"]SET                      := 12345[/COLOR],//play with this
           OUT                      := [COLOR="SeaGreen"]#Any2[/COLOR]);
NETWORK
TITLE =
      CALL "BLKMOV" (
           SRCBLK                   := [COLOR="Blue"]#Any1[/COLOR],
           RET_VAL                  := #err,
           DSTBLK                   := [COLOR="SeaGreen"]#Any2[/COLOR]);
NETWORK
TITLE =
      L     #y[0]; 
      T     MW     0; 
      L     #y[1]; 
      T     MW     2; 
      L     #y[2]; 
      T     MW     4; 
      L     #y[3]; 
      T     MW     6; 
      L     #y[4]; 
      T     MW     8; 
      L     #y[5]; 
      T     MW    10; 
      L     #err; 
      T     MW    12; 
END_FUNCTION_BLOCK


DATA_BLOCK "i"
TITLE =
VERSION : 0.0
"fb"
BEGIN
   x[0] := 0; 
   x[1] := 0; 
   x[2] := 0; 
   x[3] := 0; 
   x[4] := 0; 
   x[5] := 0; 
   y[0] := 0; 
   y[1] := 0; 
   y[2] := 0; 
   y[3] := 0; 
   y[4] := 0; 
   y[5] := 0; 
END_DATA_BLOCK


ORGANIZATION_BLOCK OB 1
TITLE = "Main Program Sweep (Cycle)"
VERSION : 0.1
VAR_TEMP
  OB1_EV_CLASS : BYTE ;	//Bits 0-3 = 1 (Coming event), Bits 4-7 = 1 (Event class 1)
  OB1_SCAN_1 : BYTE ;	//1 (Cold restart scan 1 of OB 1), 3 (Scan 2-n of OB 1)
  OB1_PRIORITY : BYTE ;	//Priority of OB Execution
  OB1_OB_NUMBR : BYTE ;	//1 (Organization block 1, OB1)
  OB1_RESERVED_1 : BYTE ;	//Reserved for system
  OB1_RESERVED_2 : BYTE ;	//Reserved for system
  OB1_PREV_CYCLE : INT ;	//Cycle time of previous OB1 scan (milliseconds)
  OB1_MIN_CYCLE : INT ;	//Minimum cycle time of OB1 (milliseconds)
  OB1_MAX_CYCLE : INT ;	//Maximum cycle time of OB1 (milliseconds)
  OB1_DATE_TIME : DATE_AND_TIME ;	//Date and time OB1 started
  w : ARRAY  [0 .. 5 ] OF INT ;	
END_VAR
BEGIN
NETWORK
TITLE =
      CALL "fb" , "i"   (
           x                        := #w);
END_ORGANIZATION_BLOCK
 
Last edited:
There is no need to save AR2 too .
it's saved in the "CALL FC1" macro !
 
Last edited:

Similar Topics

data in data block is INT data type. i can only open one global DB so how can i copy from one to another one. i am sorry about the image but i...
Replies
2
Views
1,606
I have a word in some DB which I want to load to AR1 and use as a pointer. In order to do this I need to write L DBxy.DBW xy SLD 3 LAR1 I...
Replies
3
Views
533
I am trying to access the value of a pointer via OPC UA in Codesys. I can share it directly and in a struct but I cant access the value of it when...
Replies
5
Views
1,609
Why does my deconstruction of the input pointer only work with its own instance DB not inside the multi instance FB...See .doc The pointer at the...
Replies
8
Views
2,342
Hi All, in many library function of TiaPortal some data must be write using the pointer format........ eg.: P#DB90.DBX0.0 WORD 10 is it...
Replies
5
Views
2,818
Back
Top Bottom