PDA

View Full Version : STL programming seems to shift 2 characters


drip
March 15th, 2011, 06:47 PM
Hello,
I need some help with some STL programming for siemens simatic 5.4. I am bring in an ascii code from a module and it is being stored into a DB as individual characters. I am trying to convert these select a range of characters to make a string. I want to take the first 10 characters and store them as one string then take characters 12-23 to store as another string.

I am using the code below as a FC and entering the source and target DB as well as the start position. The problem is when I convert the characters to a string my position is off by 2 characters. I am pointing to the character located in db4.dbx0.0 but I get the character located in db4.dbx2.0. So I can never bring in the first 2 characters from my serial number because I am reading the 3rd character with the 0 placement.

EX- serial - 123456789 in db 4 starting at 0.0
target db5
source db4
target position 0
source position 0

serial in db 5 starting at 0.0 is 3456789









L B#16#10 // Load the syntax ID and
T LB 0 // transfer it to the "Source" ANY pointer

L B#16#2 // Load data type Byte and
T LB 1 // transfer it to the "Source" ANY pointer

L #NumberOfBytes // Load number of bytes to transfer
T LW 2 // transfer it to the "Source" ANY pointer

L #SourceDB // Specify source DB
T LW 4 // transfer it to the "Source" ANY pointer

L #StartAdrSource //Start of the source
SLD 3
OD DW#16#84000000
T LD 6 // transfer it to the "Source" ANY pointer

Network 2

L B#16#10 // Load the syntax ID and
T LB 10 // transfer it to the "Target" ANY pointer

L B#16#2 // Load data type Byte and
T LB 11 // transfer it to the "Target" ANY pointer

L #NumberOfBytes // Load 160 bytes
T LW 12 // transfer it to the "Target" ANY pointer

L #TargetDB // Specify target DB
T LW 14 // transfer them to the "Target" ANY pointer

L #StartAdrTarget //Start of the source
SLD 3
OD DW#16#84000000
T LD 16 // transfer it to the "Target" ANY pointer

Network 3
CALL "BLKMOV"
SRCBLK :=#Source
RET_VAL:=#Errorcode
DSTBLK :=#Target
NOP 0

Network 4
A L 20.7
SAVE
BEC

Network 5

CLR
SAVE

TurpoUrpo
March 15th, 2011, 11:20 PM
Look at string datatype in s7 help. There is lenght information stored in it, so your first char in string is at offset of 2.

JesperMP
March 16th, 2011, 06:08 AM
I think you are making it unnecessarily complicated with the ANY pointer.
You can manipulate the individual characters of a string, just make sure that max length is set in byte 1, and actual length are is set in byte 2.

Here is an example.
#str1 is a locally declared STRING address in an FB.

L B#16#20 // max length 20hex=32dec
T #str1[1]
L B#16#5 // actual length 5hex=5dec
T #str1[2]
L 'h' // 'hello'
T #str1[3]
L 'e'
T #str1[4]
L 'l'
T #str1[5]
L 'l'
T #str1[6]
L 'o'
T #str1[7]

L D[AR2,P#0.0]
March 16th, 2011, 07:56 AM
str1[1] references the first char in the string, not the first byte that contains the max string length.

JesperMP
March 16th, 2011, 08:51 AM
I made a test, and it worked.
I will test again.

JesperMP
March 16th, 2011, 08:59 AM
You are right LD.
Dont know how I could get it to work the first time.
The 1st and 2nd byte cannot be accessed symbolically.
It must look something like this:
L B#16#20 // string max length. May be omitted if the stringth if already initialised.
T DB1.DBB 0

L B#16#5 // string actual length
T DB1.DBB 1

L 'h' // 'hello'
T "test_DB1".string1[1]
L 'e'
T "test_DB1".string1[2]
L 'l'
T "test_DB1".string1[3]
L 'l'
T "test_DB1".string1[4]
L 'o'
T "test_DB1".string1[5]

JesperMP
March 16th, 2011, 09:02 AM
I think that I got it to work the other way because I had offset the HMI (which I used to display the string value) by 2 bytes.
But that is a cludge.