Pass ANY Pointer to FB inside FB

bara_hence

Member
Join Date
Aug 2007
Location
Ockelbo
Posts
225
Ok have read some of the old threads but this subject still confuses me..

What i want to do is:

I have a FB wich i would like to pass what to send and what to recieve to I would like to be able to pass it either as a constant (P#DB666.DBX66.6 BYTE 66) or as a ANY variable wich I have build in the calling FC or FB..

In the FB i would like to pass the pointer on to a send FB.

According to this post http://www.plctalk.net/qanda/showpost.php?p=169469&postcount=8

I have to do like this:

Code:
FUNCTION_BLOCK FB 316
TITLE =
VERSION : 0.1
 
VAR_INPUT
pAny : ANY ; 
END_VAR
VAR_TEMP
AnyPointer : STRUCT 
wType : WORD ; 
iQuantity : INT ; 
iDBNumber : INT ; 
dwAreaPointer : DWORD ; 
END_STRUCT ; 
dwSavedAr2 : DWORD ; 
bCallFC316 : BOOL ; 
END_VAR
BEGIN
NETWORK
TITLE =passing on an any pointer (FB)
	 TAR2 #dwSavedAr2; //save current value of AR2
	 L	 P##pAny; //pointer to parameter in instance
	 AD	DW#16#7FFFF; //mask off area
	 TAR2 ; //get current instance
	 +D	; //add parameter offset
	 LAR2 ; //for indirect address
	 CDB ; //global DB instance DB swap
//****
//no stat or parameter access allowed
//****
	 L	 W [AR2,P#0.0]; 
	 T	 #AnyPointer.wType; 
	 L	 W [AR2,P#2.0]; 
	 T	 #AnyPointer.iQuantity; 
	 L	 W [AR2,P#4.0]; 
	 T	 #AnyPointer.iDBNumber; 
	 L	 D [AR2,P#6.0]; 
	 T	 #AnyPointer.dwAreaPointer; 
	 CDB ; //DB's back to how they were
	 LAR2 #dwSavedAr2; //ar2 back to how it was

Then the confusion starts.. could I now call the other FB using the struct AnyPointer as an input? The other FB is BSEND so I cannot alter this..
 
Change the input to an any pointer and copy that pointer to the local temp stack. Then you can pass the pointer in the tempstack to BSEND.

Code:
// Load pointer into adressregister 1
      L     P##iInputPointer
      L     #tDIOffset
      +D                                // Add offset for multiinstance compatibility
      LAR1  

//Load adressregister 2 with temporary pointer
      LAR2  P##tAnyFrom

// Move data to temppointer
      L     W [AR1,P#0.0]
      T     W [AR2,P#0.0]

//Load repetion factor.
      L     W [AR1,P#2.0]
      T     W [AR2,P#2.0]

//Load datablock number 
      L     W [AR1,P#4.0]  
      T     W [AR2,P#4.0]

//Load pointer into temppointer
      L     D [AR1,P#6.0]
      T     D [AR2,P#6.0]

// Restore adressregister 2
      LAR2  #tAR2
 
Last edited:
Bratt isnt your code just like the other code i posted?

That code also have an ANY as an input and the "new" Any pointer is made in the temp area?

Or am I missing something??
 
I was reading a little fast i thought that you had a problem with the code so i posted an example that i know works with BSEND.

The difference is the pointer in the tempstack.

The pointer in the tempstack in my example is of type any and not a struct.

It might work with struct but i know that it will work as an any.

BSEND
SD_1 :=tAnyFrom
 
Last edited:
How do I handle the len parameter for the BSEND block?

Is it possible to take the repetition factor and transfer to the len variable --

//Load repetion factor.
L W [AR1,P#2.0]
T W [AR2,P#2.0]
T #iLen

Or can the repetition factor be in another format than byte?
 
How do I handle the len parameter for the BSEND block?

Is it possible to take the repetition factor and transfer to the len variable --

//Load repetion factor.
L W [AR1,P#2.0]
T W [AR2,P#2.0]
T #iLen

Or can the repetition factor be in another format than byte?

Both the repetition factor and LEN is 2 bytes (WORD) so that will work just fine!

You can also do manipulation of LEN and the repetion factor if you for instance in some cases just want to send a small amount of data to keep the traffic down.

//Load repetion factor.
L 2 // Only send the first 2 bytes!
T W [AR2,P#2.0]
T #iLen

Note! If you do manipulation of the pointer you need to change the pointer and LEN before setting REQ and keep the pointer and LEN intact until send is done!
 
Last edited:
So the repetition factor can never be in number of words? Even if I do the parametrization like this when calling my fb:

P#DB666.DBX66.6 WORD 6

Sure I wouldnt do it like this but i would like my block to work even if I dont parametrisise it..
 
L B [AR1,P#1.0] In the any pointer is data type. You can do a check only allowing byte or do the change of LEN according to type!

Type.JPG
 
Last edited:
Anyways here are my pointer code for now..

I used the code from my first post to calculate the current instance and becausse AR2 was used I had? to use the absolute adresses to the temp area (I know bad practice but if the FB works it shouldnt be altered..)

Maybe I could load the temp pointer in AR1 ?


Code:
//Save AR2 
      TAR2  #dwSavedAR2
//Pointer to area for sending          
      L     P##SendPointer
//Mask off area         
      AD    DW#16#7FFFF
//Get current instance      
      TAR2
//Add offset                            
      +D
//For indirect access                                
      LAR2
//global DB instance DB swap                        
      CDB                              

// Move data to temppointer
      L     W [AR2,P#0.0]
      T     LW     8

//Load repetion factor.
      L     W [AR2,P#2.0]
      T     LW    10
      T     #wSendLen

//Load datablock number
      L     W [AR2,P#4.0]
      T     LW    12

//Load pointer into temppointer
      L     D [AR2,P#6.0]
      T     LW    14

      CDB                               //DB's back to how they were
// Restore adressregister 2
      LAR2  #dwSavedAR2
 
L B [AR1,P#1.0] In the any pointer is data type. You can do a check only allowing byte or do the change of LEN according to type!

b#16#00
NIL
Null pointer
b#16#01
BOOL
Bits
b#16#02
BYTE
Bytes (8 bits)
b#16#03
CHAR
Characters (8 bits)
b#16#04
WORD
Words (16 bits)
b#16#05
INT
Integers (16 bits)
B#16#06
DWORD
Words (32 bits)
b#16#07
DINT
Double integers (32 bits)
b#16#08
REAL
Floating-point numbers (32 bits)
b#16#09
DATE
Date
b#16#0A
TIME_OF_DAY (TOD)
Time of day
b#16#0B
TIME
Time
b#16#0C
S5TIME
Data type S5TIME
b#16#0E
DATE_AND_TIME (DT)
Date and time (64 bits)
b#16#13
STRING
String

Thanks.. I will think of a soulution for this.. The type check and calculation of the len parameter seems the neatest way..

But maybe this even isnt necessary according to the manual:

The start address and the maximum length of the data to be sent are specified by SD_1. You can determine the job-specific length of the data field with LEN. In this case, LEN replaces the length section of SD_1.

Does this mean that if Len = 0 the SD_1 ANY pointer is in charge??
 
Simple pass on any pointers example attached. For testing turn M1.0 or M1.1 on and monitor FB2 to see which any pointers have been passed on from FC1 to FB1 to FB2
 
Simple pass on any pointers example attached. For testing turn M1.0 or M1.1 on and monitor FB2 to see which any pointers have been passed on from FC1 to FB1 to FB2

Thanks just one question why is one of the ANY pointers defined as an in and the other as an in/out?

Or is it just for show?

Time for weekend break now but I will try your example later.. But it seems easy enough if im not out in the blue..

LAR1 P##pAny
L W#16#1002
T W [AR1,P#0.0]
L 66
T W [AR1,P#2.0]
L 666
T W [AR1,P#4.0]
L P#DBX 666.0
T D [AR1,P#6.0]

= P#DB666.DBX666.0 BYTE 66

Bu as said I should try this to se how it works thanks again.
 

Similar Topics

Sorry double posted thread.. The thread that is the correct is named Pass ANY Pointer to FB inside FB OT how to delete a thread?
Replies
0
Views
2,836
Hi, how to pass a pointer to a function ? I want wtrite a function that get the pointer and starts fill (few bytes) bytes from the pointer OB1...
Replies
4
Views
3,277
Hello, I am still new to PLC programming and I just got this job two year out of school so I don’t remember much. I was given a task were I have...
Replies
1
Views
165
So I'm having issues with a certain rung and one of my coworkers mentioned it may not allow the signal past the latch coil. For example in the...
Replies
27
Views
3,734
I’m a bit stuck on HMI (KTP-1200) programming… See the picture attached. The PASS or FAIL box should only appear when the toggle switch is...
Replies
7
Views
1,050
Back
Top Bottom