Step7 - Problems with FC39 "STRNG_R"

RMA

Member
Join Date
Sep 2004
Location
North of Hamburg, Germany
Posts
2,052
Can somebody have a look at the appended code. I think I've got to the point where I'm seeing what I expect to see instead of what's really there!

I've got a floating point number in the IDB for a Cognex camera in the form of a seven character STRING. I need to convert this to a Step7 REAL, so that I can send it to the management information PC.

I've used FC39 before (it's in the IEC Blocks in the Standard Library), but for some reason, it's not working this time. The Return Value is sitting at (something like) 9.404e-38, which I assume is the smallest value which a REAL can take.

I'm pretty sure it'll be a trivial fault, probably in the shuffling around of the individual characters, but as I said, I think I'm seeing what I expect to see now!

Thanks in advance

Roy
 
Oops - sorry

In a well-intentioned attempt to make things clearer, before posting I edited the program and in NW2 moved the insertion of the decimal point in character position 3 up before the copying of characters 3 and 4 to positions 4 and 5 respectively. This of course has the effect of inserting two decimal points.

This is not the problem (although it would have been!). The running version of the program moves characters 3 and 4 before the decimal point is inserted in position 3!

Sorry for creating confusion!

Roy
 
Hello Roy;
Have you used FC39 in conjunction with this specic equipment before?

I do a lot of Modbus and Profibus connections with Rockwell and GE Fanuc PLCs. Everytime a REAL value I am attempting to read is showing up as x.xx e+/- 38 I tend to suspect a Little Endian/Big Endian problem.

Could you attemp to swap the bytes in the string before calling FC39?

Hope this helps,
Daniel Chartier
 
I've only given the code a quick eyeball but there doesn't seem to be code to set the max string length/actual string length.
 
I knew I'd been here before ...

but I couldn't find anything using the Search feature (for reasons unknown, I very rarely find anything, even when I know it's there!).

Anyway, I called up the list of all my posts and eventually found this one .

Unfortunately, the solution we found there doesn't seem to work this time round, but it does suggest that the problem might have something to do with building the STRING in a Temp.

I'll try using a DB instead and see if that helps.

@Daniel,

I don't think it's the Byte order problem, because I'm writing the length directly into Local Bytes 0 & 1 (the STRING is declared as the first Temp). If that is the problem though, it should be easier to see in a DB where you can look at things directly.

Cheers

Roy

Edit:

@Simon, I set the max and actual length bytes at the start of NW2 - hey, I even commented it!
 
Last edited:
Oops! I just wasn't expecting an access to LB 0 and 1 as this relies on you (or someone else) changing this if another temp variable is added prior to TEMP_STRING.
 
Roy, Here's my implementation. I just created the temp_string byte by byte using hardcoded data and the data from the source string.

Code:
FUNCTION FC 999 : VOID
TITLE =Convert 7 Character ASCII STRING to REAL
//Converts value from Cognex Camera Data to REAL for transmission to AMS PC.
//Data Format must be "abc,xyz" and data must be stored in a DB (usually the IDB 
//for the Cognex Camera).
//In this version, leading zeroes must be present, a check is made that the 
//fourth character is a "," and if not, then the result is set to 0.000000e+000.
VERSION : 0.1

VAR_INPUT
  Start_of_STRING : INT ; //Pointer to start of Character STRING representing Floating point number.
  DB_Nr : BLOCK_DB ; 
END_VAR
VAR_OUTPUT
  Ergebnis_REAL : REAL ; 
END_VAR
VAR_TEMP
  Temp_String : STRING  [14 ]; 
END_VAR
BEGIN
NETWORK
TITLE =Get ASCII data.
	  L	 #Start_of_STRING; //Get Byte number of Start of STRING
	  SLD   3; //Convert to Byte.Bit Format
	  LAR1  ; 
	  OPN   #DB_Nr; 
//Form result string
	  L	 '+'; // +
	  T	 #Temp_String[1]; 
	  L	 DBB [AR1,P#0.0]; // +a
	  T	 #Temp_String[2]; 
	  L	 '.'; // +a.
	  T	 #Temp_String[3]; 
	  L	 DBB [AR1,P#1.0]; // +a.b
	  T	 #Temp_String[4]; 
	  L	 DBB [AR1,P#2.0]; // +a.bc
	  T	 #Temp_String[5]; 
	  L	 DBB [AR1,P#3.0]; 
	  L	 ','; //is 4th char of source a comma, if not then exit
	  ==I   ; 
	  JC	ok; 
	  L	 0.000000e+000; 
	  T	 #Ergebnis_REAL; 
	  JU	Exit; 

ok:   L	 DBB [AR1,P#5.0]; // +a.bcd
	  T	 #Temp_String[6]; 
	  L	 DBB [AR1,P#6.0]; // +a.bcde
	  T	 #Temp_String[7]; 
	  L	 DBB [AR1,P#7.0]; // +a.bcdef
	  T	 #Temp_String[8]; 
	  L	 '0'; 
	  T	 #Temp_String[9]; // +a.bdcef0
	  T	 #Temp_String[10]; // +a.bdcef00
	  L	 'E'; 
	  T	 #Temp_String[11]; // +a.bdcef00E
	  L	 '+'; 
	  T	 #Temp_String[12]; // +a.bdcef00E+
	  L	 '0'; 
	  T	 #Temp_String[13]; // +a.bdcef00E+0
	  L	 '2'; 
	  T	 #Temp_String[14]; // +a.bdcef00E+02
	  LAR1  P##Temp_String; //set string length to 14
	  L	 14; 
	  T	 B [AR1,P#0.0]; 
	  T	 B [AR1,P#1.0]; 
	  CALL "STRNG_R" (
		   S						:= #Temp_String,
		   RET_VAL				  := #Ergebnis_REAL);
Exit: SET   ; 
	  SAVE  ; 
END_FUNCTION
 
Thanks Simon,

I copied your code and wanted to try it out, but I can't persuade the STL editor to let me enter a source code Text. Even if I first open an existing source file, when I click on new the editor switches back to the normal mode.

I've looked under Options etc., but I can't find anywhere to switch the editor over to accepting a source file - where do I find the magic button?
 
You have to Insert an STL source code object in the source folder, not the blocks folder. Once you've created one, you can open it and copy the code from the post and paste it in. You then compile the source code and it will generate a new block in the blocks folder.

Edit: I'll report back on what is wrong with your orignal block after I've examined it later tonight.
 
Last edited:
Near, but not quite

You slipped a bit in the middle and skipped the character following the comma (actually a decimal point) but even after correcting that things are not all sweetness and light!

At least we're getting a floating point number out of FC39 now, unfortunately it's not correct!

FC999.JPG



As you can see, the input value is 288.260, the build up of the new formatted STRING also looks to be OK, but at the end we're getting 1.837e-40 out of FC39, instead of 2.8826e+02!
 
Yes, this is a puzzler. I've corrected the code and used a full stop instead of a comma and run it in the simulator. The monitoring screen shows the same values in the accumulator as yours, but mine produces the correct result from FC39

I can't attach the screen shot at present so I'll post it later.
I'm calling the FC out of OB1 and using MD600 for the resultant floating point result.

Edit: Lightbulb moment..... are you sure you've got FC39 in the CPU .........
 
Last edited:
I've just run your original posted code (having moved the decimal point insertion) and it produces the correct result from FC39. It all points to FC39 not being in the CPU (if this is the case, then you've been caught by this one before - get rid of OB121 in the CPU, quick) :)
 
Your lightbulb moment was correct - as I instantly recognised on reading it!:oops: I think I need a yellow Post-it note on my laptop, it usually seems to happen when I need some standard Siemens FB/FC, but it happens too often!

After downloading FC39 I went back to my original program and it worked fine as well - aaargh!

Thanks a lot,

Roy

PS This is one of only three cells on the line where the results of the CPU going into STOP at the wrong time can be catastrophic, so the customer insists on having OB121 loaded. Everywhere else it's not loaded, but at the time it matters, I tend to forget which cell I'm working on!
 
Last edited:

Similar Topics

Hi all, I will try and give as much info as possible to begin with but I am a graduate thrown in at the deep end so bear with me please. Upon...
Replies
12
Views
5,299
Hi Im trying to use SFC20 BLK Move in a piece of SCL code in Step7. I need to copy/move one string[16] in a DB to another DB place ! If a block...
Replies
5
Views
13,499
Would anybody like to have a look at the attatched source file and see if they can come up with a suggestion for what's wrong with it. Initially...
Replies
10
Views
5,219
I've got a production line station which has two Cognex cameras in it to check that components are present and correctly orientated. The second...
Replies
7
Views
3,957
Can anybody see what's wrong with the following screen dump? I need to display an integer in a text field (a 20 character String). Originally I...
Replies
30
Views
10,428
Back
Top Bottom