Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FSAVE on Power PMAC
#1
If any of you wanted a feature similar to FSAVE in Turbo PMAC, with which you could save a specific range of P-Variables to flash memory, you could implement it in C on Power PMAC in this way. This code is intended to be in a BGCPLC, but could be adapted to a Background C Program. Do not attempt to run it in a foreground C program.

It's intended for advanced C users only, so the commenting is minimal, but feel free to post your questions.

First, you have the following global flags; put them into global definitions.pmh under PMAC Script Language-->Global Includes:

Code:
global FSAVE_SuccessFlag=0; // =1 upon successful save, =-1 if not successful
global FREAD_SuccessFlag=0; // =1 upon successful read, =-1 if not successful
global FSAVE_Choice=0; // set to 1 anywhere to FSAVE; gets reset to 0 by the BGCPLC
global FREAD_Choice=0; // set to 1 anywhere to FREAD; gets reset to 0 by the BGCPLC
global StartingPNumber=1,EndingPNumber=10; // Adjust these; they are the first and last P-Variables that will be FSAVEd

Then, the BGCPLC:

Code:
#include <gplib.h>
#include <stdio.h>
#include <dlfcn.h>
#include "../../Include/pp_proj.h"

int FREAD(void);
int FSAVE(unsigned int startindex, unsigned int endindex);

void user_plcc()
{
    int FSAVE_Success=-3,FREAD_Success=-3;
    
    if((unsigned int)pshm->P[FSAVE_Choice] == 1)
    {
        FSAVE_Success = FSAVE((unsigned int)pshm->P[StartingPNumber], (unsigned int)pshm->P[EndingPNumber]);
        
        if(FSAVE_Success == 1)
            pshm->P[FSAVE_SuccessFlag] = 1.0;
        else if (FSAVE_Success == -1)
            pshm->P[FSAVE_SuccessFlag] = -1.0;
        
        pshm->P[FSAVE_Choice] = 0.0;
    }

    if((unsigned int)pshm->P[FREAD_Choice] == 1)
    {
        FREAD_Success = FREAD();

        if(FREAD_Success == 1)
            pshm->P[FREAD_SuccessFlag] = 1.0;
        else if (FREAD_Success==-1)
            pshm->P[FREAD_SuccessFlag] = -1.0;
        
        pshm->P[FREAD_Choice] = 0.0;
        
    }
}

#define echomode 0 // Format example: P1234 = 918230198.001
#define inbuffsize 128
#define outbuffsize 128
int FSAVE(unsigned int startindex, unsigned int endindex)
{
    unsigned int index; // For looping through variables to be saved
    unsigned int len = 0;
    char cmd[inbuffsize]="",resp[outbuffsize]="";
    FILE* fid; // File pointer
    
    if(endindex >= startindex)
        len = endindex - startindex;    
    else
        return -1;
        
    system ("mount -o remount,rw /opt"); // Unlock write protection on flash memory
    
    // Create a file to store the data you want to save
    fid = fopen("/opt/ppmac/usrflash/Project/Configuration/FSAVE.cfg","w");
    if(fid != NULL)
    {
        for(index = startindex; index <= endindex; index++)
        {
            sprintf(cmd, "P%u", index);
            GetResponse(cmd, resp, outbuffsize, echomode);
            fputs(resp, fid);
        }

        fclose(fid);
        sync();
        system ("mount -o remount,ro /opt"); // Lock write protection
        return 1;
    } else return -1;
}

int FREAD(void)
{
    FILE* fid; // File pointer
    char resp[outbuffsize]="";
    
    // Open the file to which we saved and read from it
    fid = fopen("/opt/ppmac/usrflash/Project/Configuration/FSAVE.cfg","r");
    if(fid == NULL) return -1;
    else
    {
        while(fgets(resp, outbuffsize, fid) != NULL)
            Command(resp); // Dump file contents to PMAC parser
        
        fclose(fid);    
    }
    return 1;
}

Basically, when the BGCPLC is running, you can write FSAVE_Choice=1 to start the save, and P-Variables between and including StartingPNumber and EndingPNumber will be saved to flash, meaning their value will be retained between reset or power-cycle. To read them after you reset or power-cycle (or at any time), write FREAD_Choice = 1.

Note that each use of FSAVE reduces the life of your flash memory slightly, so try not to abuse it. The flash memory chips are rated at a lifetime of 100,000 write/erase cycles.
Reply
#2
Hello CharlesP,

Is't possble to save and read data from a SD card instead of flash memory of PPmac? For the flash memory chips has a long life.
Reply
#3
Hi,

Yes of course! Please read my example starting on page 3 of the attached document.

Let me know if you have any questions.


Attached Files
.pdf   Saving Files on Power PMAC or External Media.pdf (Size: 232.35 KB / Downloads: 125)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)