s7 pointer queiry(contiued)

lad_stl

Member
Join Date
Dec 2013
Location
giza
Posts
19
hiiiiiiiiiii all,

first i thank everyone helps me or tried to do.i have a problem in writing STL code using pointers in simatic s7 to sort array of datablock in descending order
i actually write code to do the following

1-FB has 3 inputs .one for the DB to be sorted(pointer format),second one for the destination DB(sorted)(also pointer format,the last i/p for length.
2-in my code i scan all the DB to get the maximum value element and then put it in the begging of the dest. array.then i put zero value in the place of the maximum in source DB.and i scan again the DB to get the maximum and put it in dest. array in next position and go in loops till finish.
3-i tried that on simulator and it gives no error but the output is all the dest. array is a copy of the maximum value
4-i want to know where is the mistake in my code or if there are any other criteria (logic) to do so.
5-here is my code


L #DB_LEN //LENGTH OF DATA BLOCK
T #TMP_LEN1 //COUNTER FOR SCANING DB
T #TMP_LEN2 //COUNTER FOR SCANING DB

L P##SRC_DB //un sorted DB
LAR1
L W [AR1,P#0.0]
T #SDB_NR
OPN DB [#SDB_NR]
L D [AR1,P#2.0]
Code:
LAR1
L W [AR1,P#0.0]
T #TMP_MAX

L P##ORD_DB // DB to put the sorted data
LAR2
L W [AR2,P#0.0]
T #DDB_NR
L D [AR2,P#2.0]
LAR2

LOOP: L #TMP_MAX
+AR1 P#2.0
L W [AR1,P#0.0]
<I
JCN RR
T #TMP_MAX
L #TMP_LEN1
L -1
+I
T #TMP_LEN1
L 0
==I
JC XX

JC LOOP
RR: TAK
T #TMP_MAX
L #TMP_LEN1
L -1
+I
T #TMP_LEN1
L 0
==I
JC XX

JC LOOP
XX: L #TMP_MAX
OPN DB [#DDB_NR]
T W [AR2,P#0.0]

TAR2
LAR1
+AR2 P#2.0
L 0
T #TMP_MAX

L #TMP_LEN2
L -1
+I
T #TMP_LEN2
T #TMP_LEN1
L 0
==I
JCN LOOP
SET
SAVE

 
SIMPLER ?
Maximum / minimum find search !

Code:
DATA_BLOCK "a"
TITLE =
VERSION : 0.1
  STRUCT 	
   r : REAL ;	
   a : ARRAY  [0 .. 6 ] OF 
   INT  := 0;	
  END_STRUCT ;	
BEGIN
   r := 0.000000e+000; 
   a[0] := 111; 
   a[1] := 23; 
   a[2] := 11; 
   a[3] := 7; 
   a[4] := 4; 
   a[5] := 6; 
   a[6] := 5; 
END_DATA_BLOCK


FUNCTION FC 2 : VOID
TITLE =
VERSION : 0.1
VAR_INPUT
  PNT : POINTER ;	
END_VAR
BEGIN
END_FUNCTION


FUNCTION FC 1 : VOID
TITLE =
VERSION : 0.1
VAR_TEMP
  v : INT ;	
  saveAR1 : DWORD ;	
END_VAR
BEGIN
NETWORK
TITLE =
      TAR1  #saveAR1; 
NETWORK
TITLE =
      OPN   "a"; 
      CALL FC     2 (
           PNT                      := "a".a);
      LAR1  ; 
      LAR2  ; 
      L     6; // number of comparisons
x:    T     #v; //LOOP variable
      L     DBW [AR1,P#0.0]; 
      +AR2  P#2.0; [COLOR="Silver"]//INT=P#2.0 ; REAL/DINT=P#4.0 ; [/COLOR]
      L     DBW [AR2,P#0.0]; 
      [COLOR="Red"][B]<I    ; [/B][/COLOR] [B][COLOR="Blue"]//>I    ;[/COLOR][/B]
      JCN   z; 
      TAR2  ; 
      LAR1  ; 
z:    L     #v; 
      LOOP  x; 
      L     DBW [AR1,P#0.0]; 
      T     MD     0; // TEST
NETWORK
TITLE =
      LAR1  #saveAR1; 
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 =
      A     I      0.0; 
      JCN   o; 
      CALL FC     1 ;
o:    NOP   0; 
END_ORGANIZATION_BLOCK
🍻
 
Last edited:
Code:
FUNCTION FC 1 : VOID
TITLE =
VERSION : 0.1
VAR_TEMP
  v : INT ;	
  saveAR1 : DWORD ;	
END_VAR
BEGIN
NETWORK
TITLE =
      TAR1  #saveAR1; 
NETWORK
TITLE =
      OPN   "a"; 
      CALL FC     2 (
           PNT                      := "a".a );
      LAR1  ; 
      LAR2  ; 
      L   [B][COLOR="Green"]  6[/COLOR][/B]; // number of comparisons
x:    T     #v; //LOOP variable
      L     DBW [AR1,P#0.0]; 
      +AR2  P#2.0; //INT=P#2.0 ; REAL/DINT=P#4.0 ; 
      L     DBW [AR2,P#0.0]; 
      <I    ;  //>I    ;
      JCN   z;
[COLOR="Green"]      T     DBW [AR1,P#0.0];
      POP ;  
      T     DBW [AR2,P#0.0];//[B]etc[/B][/COLOR] 
z:    L     #v; 
      LOOP  x; 
      L     DBW [AR1,P#0.0]; 
      T     MD     0; // TEST
NETWORK
TITLE =
      LAR1  #saveAR1; 
END_FUNCTION
It's very easy to complete this code , to a bubble sort (=deprecated way to sort:sleep:).
 
Last edited:
4-i want to know where is the mistake in my code or if there are any other criteria (logic) to do so.

Example coding errors
(a)Where is the line of code that writes the zero to the source db
(b)There is no opn DB for the source DB inside the loop, once the destination DB has been opened it stays opened.

I would code this type of algorithm using high level pseudo code (and test it if possible) before implementing it in STL

e.g.
Code:
FUNCTION_BLOCK FB88
VAR
aiSrcDB:ARRAY[1..10]OF INT:=1,2,3,50,40,6,4,5,300,8;
aiDestDB:ARRAY[1..10]OF INT:=0,0,0,0,0,0,0,0,0,0;
iSrcIndex:INT;
iDestIndex:INT;
iMax:INT;
iMaxIndex:INT;
iLen:INT;
bComplete:BOOL;
END_VAR

BEGIN
iLen:=10;
iDestIndex:=1;
bComplete:=False;
WHILE NOT bComplete DO
 //Assume complete   
 bComplete:=True;
 iMax:=0;
 //search source DB for max value
 FOR iSrcIndex:=1 TO iLen DO
  //if source DB contains a non-zero, we are not complete  
  IF aiSrcDB[iSrcIndex] <> 0 THEN bComplete:=False; END_IF;
  //check for max so far, if so then store index of max so far
  IF aiSrcDB[iSrcIndex] > iMax THEN 
     iMax:= aiSrcDB[iSrcIndex];
     iMaxIndex:=iSrcIndex;   
  END_IF;      
 END_FOR;   
 IF NOT bComplete THEN 
  //zero current max in source db  
  aiSrcDB[iMaxIndex]:=0;  
  //enter max in dest db
  aiDestDB[iDestIndex]:=iMax;
  //update dest index
  iDestIndex:=iDestIndex+1; 
 END_IF;    
    
END_WHILE;        

end_function_block
 
...(S7: )it is also dangerous to write conditional jumps one after the other (with no condition between), even immediately before "=", "S", "R" ... etc

;)
 
sorry for this mistake L D[AR2,P#0.0].i updated may code just now and here is my final code
( if you please help me to find where is the error in my code because that code is my own thinking)



L #DB_LEN //LENGTH OF DATA BLOCK
T #TMP_LEN1 //COUNTER FOR SCANING DB
T #TMP_LEN2 //COUNTER FOR SCANING DB

L P##SRC_DB
LAR1
L W [AR1,P#0.0]
T #SDB_NR
OPN DB [#SDB_NR]
L D [AR1,P#2.0]
LAR1
L W [AR1,P#0.0]
T #TMP_MAX

L P##ORD_DB
LAR2
L W [AR2,P#0.0]
T #DDB_NR
L D [AR2,P#2.0]
LAR2


LOOP: OPN DB [#SDB_NR]
L #TMP_MAX
+AR1 P#2.0
L W [AR1,P#0.0]
<I
JCN RR
T #TMP_MAX
TAR1
T #add_max //location of the max value
L #TMP_LEN1
L -1
+I
T #TMP_LEN1
L 0
==I
JC XX

JC LOOP
RR: TAK
T #TMP_MAX
TAR1
T #add_max //location of the max value
L #TMP_LEN1
L -1
+I
T #TMP_LEN1
L 0
==I
JC XX

JC LOOP
XX: L #add_max
L DW#16#1
SLD 3
-D
LAR1
OPN DB [#SDB_NR]
L 0
T W [AR1,P#0.0]
LAR1 P#DBX 0.0

L #TMP_MAX
OPN DB [#DDB_NR]
T W [AR2,P#0.0]
+AR2 P#2.0

L 0
T #TMP_MAX
L #DB_LEN
T #TMP_LEN1


L #TMP_LEN2
L -1
+I
T #TMP_LEN2
L 0
==I
JCN LOOP
SET
SAVE






 
sorry i am not qualified in high language programming.so if you please help me using LAD or STL. thanks for you guys
 
lad_stl said:
( if you please help me to find where is the error in my code because that code is my own thinking
...
sorry i am not qualified in high language programming.so if you please help me using LAD or STL. thanks for you guys

If you tell me what is the aim of this succession , maybe I can correct your own thinking . You are looking for someone very much smarter than yourself , someone who 'll monitoring your own thinking better than yourself , is it right ? ...'re you ready to non-critically subjugate your mind to another mind ?:cool: Or , are you just dialoguing... (y)
Code:
[COLOR="blue"]
[B]...[/B]
JC XX
JC LOOP
[/COLOR]
How do you think would work that?
Do you like ...?... spaghetti !
 
Last edited:
...(S7: )it is dangerous to write conditional jumps one after the other (with no condition between)
...(S7: )it is dangerous to write conditional jumps directly to "=", "S", "R"

;)

TRUST ME !
 
Last edited:
thanks alot dear L D[AR2,p#0.0]
i have got my mistake and the code is running well.
and this is my final code.

but under some conditions as the elements of the array should be positive intger
and still have problem withthe first element.

thanks again

code:

L #DB_LEN //LENGTH OF DATA BLOCK
T #TMP_LEN2 //COUNTER FOR SCANING DB
L -1
+I
T #TMP_LEN1 //COUNTER FOR SCANING DB


L P##SRC_DB
LAR1
L W [AR1,P#0.0]
T #SDB_NR
OPN DB [#SDB_NR]
L D [AR1,P#2.0]
LAR1
L W [AR1,P#0.0]
T #TMP_MAX

L P##ORD_DB
LAR2
L W [AR2,P#0.0]
T #DDB_NR
L D [AR2,P#2.0]
LAR2


LOOP: OPN DB [#SDB_NR]
L #TMP_MAX
+AR1 P#2.0
L W [AR1,P#0.0]
<I
JCN RR
T #TMP_MAX
TAR1
T #add_max //location of the max value
L #TMP_LEN1
L -1
+I
T #TMP_LEN1
L 0
==I
JC XX

JC LOOP
RR: TAK
T #TMP_MAX
L #TMP_LEN1
L -1
+I
T #TMP_LEN1
L 0
==I
JC XX

JC LOOP
XX: L #add_max
LAR1
OPN DB [#SDB_NR]
L 0
T W [AR1,P#0.0]
LAR1 P#DBX 0.0

L #TMP_MAX
OPN DB [#DDB_NR]
T W [AR2,P#0.0]
+AR2 P#2.0

L 0
T #TMP_MAX
L #DB_LEN
L -1
+I
T #TMP_LEN1


L #TMP_LEN2
L -1
+I
T #TMP_LEN2
L 0
==I
JCN LOOP
SET
SAVE

fb6_1.jpg fb6_2.jpg
 

Similar Topics

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
527
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,598
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,334
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,817
I have a very strange issue and I cannot figure out why it is not working. I read input of type pointer and store it to local temp type any. I...
Replies
4
Views
1,924
Back
Top Bottom