CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version Spanish version French version
It is currently Wed Sep 17, 2014 4:29 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Retain Variables
PostPosted: Tue Jul 02, 2013 11:28 pm 
Offline

Joined: Thu Feb 07, 2013 5:21 pm
Posts: 11
I need to save setting that are entered in Wachendorff Opus A1 RVC display and would like to use Retain Variable Flag. I have set the flag for the Variables that I want to save after power off, for Retain and Persistant. But it does not work. I have not found any good documentation on using Retained variables. Can anyone lead in me in the right direction on what I need to do in order to use RETAIN variables and save the settings that are made. Any thoughts would be appreciated. Thanks

Scott


Top
 Profile  
 
 Post subject: Re: Retain Variables
PostPosted: Wed Jul 03, 2013 11:20 am 
Offline
Frequent User
Frequent User
User avatar

Joined: Tue Jun 06, 2006 9:03 am
Posts: 293
Location: Netherlands
It depends on whether the PLC has a battery backed up memory and if the battery is OK.
With the CODESYS Control RTE on PC based systems many tiimes there is no battery backup possible. In that case it is possible to save the retain data to a file and restore it after starting up again. Maybe that also works in your case.
You could try the following:

1)
Add the following libraries to your project:
- Standard.library
- CmpApp.library

2)
Use the following code. In this case you have to manually set the xSave bit to TRUE to save it (could be a SAVE button which you create in your visualisation).
Then do a warm reset and start the PLC again. Then you can test if the counter value is restored.

Code:
PROGRAM PLC_PRG
VAR
   xStartUp : BOOL := TRUE;
   xSaveRetain: BOOL;
   pApplication : POINTER TO CmpApp.APPLICATION;
   diMyResult : DINT;
   ton1 : TON;
END_VAR
VAR RETAIN
   iCounter: INT;
END_VAR

Code:

IF xStartUp THEN
   pApplication := CmpApp.AppFindApplicationByName('Application', ADR(diMyResult));
   CmpApp.AppRestoreRetainsFromFile(pApplication, 'C:\MyRetainData.ret');
   xStartUp := FALSE;
END_IF

ton1(IN:= NOT(ton1.Q), PT:= T#2S, Q=> , ET=> );
IF ton1.Q THEN
   iCounter := iCounter + 1;
END_IF
   
IF xSaveRetain THEN
   AppStoreRetainsInFile(pApplication, 'C:\MyRetainData.ret');
   xSaveRetain := FALSE;
END_IF


Top
 Profile  
 
 Post subject: Re: Retain Variables
PostPosted: Thu Jul 11, 2013 2:16 pm 
Offline

Joined: Thu Feb 07, 2013 5:21 pm
Posts: 11
TimvH, Thanks for the information an I think I almost got it, but it is not working yet. I have added the code example you posted to my program and as soon as I download the program to the device I get an Exception error, "Exception Detected see _._". I am not sure why but in the original sample code you posted you have a variable "diMyResult" and was wondering what that variable needs to be set too. I posted the code I wrote below hoping you see a problem. Also, do you know where I can find any documentation on the CmpApp Library that goes into some detail on how the functions work. I can't find anything that was helpful. Thanks again. ScottJones




Code:
     pResult : RTS_IEC_RESULT ;
   retname : STRING;
   pApp : POINTER TO CmpApp.APPLICATION;]



Code:
pApp := cmpapp.AppGetCurrent (ADR(pResult));
retname := concat (pApp^.szName, '.ret');

IF firststart THEN
   
   Heartbeat := ADR(CANStack^.m_uiHeartBeatTime); // Initialize the CAN Network Heartbeat
   Heartbeat^ := 2000;
   
   // Restore Retained Values
   
   pApp := CmpApp.AppFindApplicationByName ('Application', ADR(pResult));
   CmpApp.AppRestoreRetainsFromFile (pApp, retname );
   
      
   firststart := FALSE;
   
END_IF


Code:
IF SaveRetain THEN
   pResult:= AppStoreRetainsInFile (pApp, retname );
   SaveRetain := FALSE;
   
END_IF


Top
 Profile  
 
 Post subject: Re: Retain Variables
PostPosted: Fri Jul 12, 2013 12:31 pm 
Offline
Frequent User
Frequent User
User avatar

Joined: Tue Jun 06, 2006 9:03 am
Posts: 293
Location: Netherlands
It is probably related to the target that you get an exception, because I don't see any wrong coding.
Maybe you can set a breakpoint in the first line and step through the application to see when the exception occurs.
The "call stack" (menu: View) maybe also gives you an idea where the program was halted.

A reason could be that you haven't specified the correct filepath + name.
Can you write files to your controller, if yes, how do you specify the filename?


Top
 Profile  
 
 Post subject: Re: Retain Variables
PostPosted: Sun Jul 14, 2013 11:52 pm 
Offline

Joined: Thu Feb 07, 2013 5:21 pm
Posts: 11
Tim, Thank you for the help. I was able to get it working. I had originally had the variables initialized when they were declared, but deleted the initialization values when I started working on getting the Retain Variables to save to a file. When I did that, I created a division by zero error that I did not see right away. Also, I am not sure this is specific to Wachendorff displays but I did not need to use the Restore Retain function. Retains are restored automatically when the display is powered up. Again, thanks for you help it really helped me figure my problem out. Scott Jones


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group