S7 Static Addressing

henry5674

Member
Join Date
Jun 2006
Location
swindon
Posts
40
Hi,
i am writing a FB to control a Conveyor Segment, this will be duplicated depending on how many Segments are in the system.
I am looking to access the bits of a word i have declared in the static area 'wErrorCode' but whats the address in code?or will i have to reference the DB directly.I hope this makes sense :)
 
Hello Henry;


Inside the FB code use local declarations (I will presume that it is in Word format):

L #wErrorCode
T MW22

From another block you can access the data by the symbolic name of your instance DB, say "Conveyor101":

L Conveyor101.wErrorCode
T DB13.DBW20

or by the number of the IDB (Say DB32) and the offset of the static value in the declaration table (say your declaration table shows wErrorCode at byte offset 28):

L DB32.DBW28
T MW100


Hope this helps,
Daniel Chartier
 
Hi Daniel,
What i'm trying to do is access the Bits of wErrorCode in the FB(its Word 4.0 in the static area) so i want access bits 4.1,4.2,4.3 etc. If i place wErrorCode in Local Data (LW4) i can use 'L' as in L4.0 ,L4.1,L4.2 etc how to i address the static version?
 
Henry5674 - please see below the source code for several methods for accessing the individual bits in your static variable. You can create the block by copying the code into a source file and compiling it - you can then view it in the block editor. Note that I have used M0.0 and M1.0 just for illustration purposes for each method.

Code:
FUNCTION_BLOCK FB 1
TITLE =
VERSION : 0.1

VAR_INPUT
  wInputBits : WORD ; 
END_VAR
VAR_OUTPUT
  wOutputBits : WORD ; 
END_VAR
VAR
  wErrorCode : WORD ; 
END_VAR
VAR_TEMP
  ErrorCode : STRUCT  
   Bit0p0 : BOOL ; 
   Bit0p1 : BOOL ; 
   Bit0p2 : BOOL ; 
   Bit0p3 : BOOL ; 
   Bit0p4 : BOOL ; 
   Bit0p5 : BOOL ; 
   Bit0p6 : BOOL ; 
   Bit0p7 : BOOL ; 
   Bit1p0 : BOOL ; 
   Bit1p1 : BOOL ; 
   Bit1p2 : BOOL ; 
   Bit1p3 : BOOL ; 
   Bit1p4 : BOOL ; 
   Bit1p5 : BOOL ; 
   Bit1p6 : BOOL ; 
   Bit1p7 : BOOL ; 
  END_STRUCT ; 
  lErrorCode : WORD ; 
END_VAR
BEGIN
NETWORK
TITLE =Copy to input parameter to wErrorCode for testing
	  L	 #wInputBits; 
	  T	 #wErrorCode; 
NETWORK
TITLE =Method 1
//Copy wErrorCode into a structure of named bits using indirect addressing to 
//calculate the address of the named structure. You can refer to the bits using 
//your own defined name. If the structure is moved within the local data area, 
//the code will still work.
	  LAR1  P##ErrorCode; 
	  L	 #wErrorCode; 
	  T	 W [AR1,P#0.0]; 
	  A	 #ErrorCode.Bit0p0; 
	  =	 M	  0.0; 
	  A	 #ErrorCode.Bit1p0; 
	  =	 M	  1.0; 
NETWORK
TITLE =method 2
//Copy wErrorCode into a local variable called lErrorCode which is also a word. 
//Use indirect addressing to refer to the bits within the local copy of the error 
//code using address register 1. You cannot refer to the bits by name, but you 
//use [ar1,p#x.y]. If the structure is moved within the local data area, the 
//code will still work.
	  L	 #wErrorCode; 
	  T	 #lErrorCode; 
	  LAR1  P##lErrorCode; 
	  A	  [AR1,P#0.0]; 
	  =	 M	  0.0; 
	  A	  [AR1,P#1.0]; 
	  =	 M	  1.0; 
NETWORK
TITLE =method 3
//Use indirect addressing to refer to the bits of wErrorCode variable in the 
//instance data block using address register 1. You cannot refer to the bits by 
//name, but you use [ar1,p#x.y]. If wErrorCode is moved in the static data 
//definition, the code will still work. Note that ar2 is added to the indirect 
//address to support multiple instances.
	  L	 DIW	0; 
	  LAR1  AR2; 
	  L	 P##wErrorCode; 
	  +AR1  ; 
	  A	 DIX [AR1,P#0.0]; 
	  =	 M	  0.0; 
	  A	 DIX [AR1,P#1.0]; 
	  =	 M	  1.0; 
NETWORK
TITLE =Method 4
//Copy wErrorCode into an unused local data word, in this example, LW4, the then 
//use L4.x or L5.y to refer to the bits. You cannot refer to the bits by name. If 
//the local data area is changed, you may overwrite a variable. 
	  L	 #wErrorCode; 
	  T	 LW	 4; 
	  A	 L	  4.0; 
	  =	 M	  0.0; 
	  A	 L	  5.0; 
	  =	 M	  1.0; 
NETWORK
TITLE =Method 5
//Copy wErrorCode into a structure of named bits using the address of the 
//structure from the editor in this example, LW0. You can refer to the bits by 
//name. If the local data area is changed, you may overwrite a variable or lose 
//the ability to refer to the bits by name. 
	  L	 #wErrorCode; 
	  T	 LW	 0; 
	  A	 #ErrorCode.Bit0p0; 
	  =	 M	  0.0; 
	  A	 #ErrorCode.Bit1p0; 
	  =	 M	  1.0; 
NETWORK
TITLE =copy MW0 to output for testing only
	  L	 MW	 0; 
	  T	 #wOutputBits; 
END_FUNCTION_BLOCK
 
Thanks LD[AR2,P#0.0],
I thought this might be tricky :), I have to admit my STL is a little rusty, but methods 2 or 3 seem what i'm looking for. This give's me a chance to get stuck into some real S7 code. I am correct in thinking you are creating a pointer (not used them in S7 but have in C++) to get the address in memory of the static var, thus allowing the structure to moved? i will complile and play tommorrow.
Again thanks
Henry
 
If wErrorCode contains bits which correspond to particular errors, I would use method 1 and assign a meaningful name to each error bit in the structure.

Pointers are typically used to calculate addresses at run time, so yes, you use pointers to calculate the actual address of where a variable is located.
 
Last edited:

Similar Topics

Hi all, I am at the moment writing a plc program in siemens for the first time. I am using the static area for addressing all the required tags...
Replies
9
Views
8,227
guys: I have a problem with my IP configuration. my organization networking has 2 subnets. 10.0.0.25 and 10.1.0.25 both under 255.255.255.0 all...
Replies
2
Views
563
Hi Yes, I'm stuck again. Trying to define a Function Block. What I've put in there so far has been a straight copy/paste from the code (and that...
Replies
22
Views
2,828
Hi; In our Corrugator plant, there are automatic WIP conveyors having two moving carts also. System contains 17 items (server, two PCs, Siemens...
Replies
6
Views
2,416
HI All.It the matter of losing the JOB. I want to write a script in Archestra or in intouch where i have to perform the Tag validation. I need...
Replies
2
Views
1,371
Back
Top Bottom