PROGRAM ST_MemoryDump
VAR
Read : BOOL; (* Read command *)
Write : BOOL; (* Write command *)
i : INT; (* Auxiliary variable *)
j : INT;(* Auxiliary variable *)
CaseNr : USINT; (* Case number *)
ENr : USINT; (* Error number *)
FPos : DINT; (* File position *)
Fp : eFILEP; (* File pointer *)
Offset : ARRAY[0..1] OF UDINT; (* Offset address *)
Ptr : @USINT; (* Internal pointer *)
AStr : STRING[ 64 ]; (* Ausiliary string *)
SPos : UDINT; (* String position *)
Filename : @STRING; (* Path and name dump file *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_MemoryDump"
// *****************************************************************************
// The write command executes the dump of the DB100 backup memory area on the
// defined file. The read command restores it.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// Initialize all the variables.
IF (SysFirstLoop) THEN
Filename:=ADR('D:/Dir/Dump.txt'); //Path and name dump file
END_IF;
// -------------------------------------------------------------------------
// CHECK EXECUTION ERROR
// -------------------------------------------------------------------------
// On execution error the command is ended and a spy message is printed out.
IF (ENr <> 0) THEN
i:=SysVsnprintf(ADR(AStr), SIZEOF(AStr), ADR('Error:%d'), USINT_TYPE, ADR(ENr));
i:=SysCVsnprintf(ADR(AStr), SIZEOF(AStr), ADR(', On Case:%d'), USINT_TYPE, ADR(CaseNr));
i:=SysWrSpyData(SPY_ASCII, 0, 16#00000001, ADR('Er'), ADR(AStr)); //Ascii mode
ENr:=0; //Error number
CaseNr:=0; //Case number
END_IF;
// -------------------------------------------------------------------------
// PROGRAM CASES
// -------------------------------------------------------------------------
// Execute the program cases.
CASE (CaseNr) OF
// ---------------------------------------------------------------------
// CHECK THE COMMAND
// ---------------------------------------------------------------------
// Check the command to execute. by setting the proper flag the related
// command will be executed.
0:
IF (Read) THEN Read:=FALSE; CaseNr:=10; RETURN; END_IF;
IF (Write) THEN Write:=FALSE; CaseNr:=20; RETURN; END_IF;
// ---------------------------------------------------------------------
// READ DUMP FILE
// ---------------------------------------------------------------------
// Start dump file read sequence.
10:
FPos:=0; //File position
Offset[0]:=0; //Offset address
CaseNr:=CaseNr+1; //Case number
// ---------------------------------------------------------------------
// Open the file in read, if file it's not present exit. The file
// management must terminate in the case execution, always terminate
// program execution with the file closed.
11:
Fp:=SysFfopen(Filename, ADR('r')); //File pointer
IF (Fp = NULL) THEN ENr:=10; RETURN; END_IF;
// Set file position and reads how many characters from position to end.
IF (Sysfseek(Fp, FPos, ID_SEEK_SET) = eEOF) THEN i:=Sysfclose(Fp); ENr:=11; RETURN; END_IF;
j:=SysFGetIChars(Fp); // Number of characters
// Reading a line of report, if number of characters is equal to or
// greater than a record length it's read completely, otherwise are read
// only the characters available. This is a record example.
// 0 1 2 3 4 5 6
// 0123456789012345678901234567890123456789012345678901234567890
// -------------------------------------------------------------
// 00000000: 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
IF (j > 61) THEN j:=61; END_IF;
i:=Sysfread(ADR(AStr), 1, j, Fp); //Read data
FPos:=FPos+j; //File position
IF (Sysfclose(Fp) = eEOF) THEN ENr:=12; RETURN; END_IF;
// Check if read the desired number of characters.
IF (i <> j) THEN ENr:=13; RETURN; END_IF;
// Address acquisistion, it's the first field of record.
IF NOT(SysVsscanf(ADR(AStr), ADR('%08x'), UDINT_TYPE, ADR(Offset[1]))) THEN ENr:=14; RETURN; END_IF;
// If the acquired address is correct point the memory.
IF (Offset[0] <> Offset[1]) THEN ENr:=15; RETURN; END_IF;
SPos:=9; //String position
// Report data read loop.
FOR i:=0 TO 15 DO
Ptr:=ADR(SysDataBk)+Offset[0]; //Internal pointer
IF NOT(SysVsscanf(ADR(AStr)+SPos, ADR('%02x'), USINT_TYPE, Ptr)) THEN ENr:=16; RETURN; END_IF;
SPos:=SPos+3; //String position
Offset[0]:=Offset[0]+1; //Offset address
IF (Offset[0] >= 2048) THEN CaseNr:=CaseNr+1; RETURN; END_IF;
// Skip the column separator character.
IF (i = 7) THEN SPos:=SPos+2; END_IF;
END_FOR;
CaseNr:=CaseNr+1; //Case number
// ------------------------------------------------------------------
// Every dump record terminates with , control them.
12:
IF (TO_UDINT(SysStrFind(ADR(AStr), ADR('$r$n'), FIND_DEFAULT)-ADR(AStr)) <> SPos) THEN ENr:=17; RETURN; END_IF;
// Check if read sequence has been completed.
IF (Offset[0] < 2048) THEN CaseNr:=CaseNr-1; RETURN; END_IF;
CaseNr:=0; //Case number
Read:=FALSE; //Read command
// ---------------------------------------------------------------------
// WRITE DUMP FILE
// ---------------------------------------------------------------------
// Check if file is present, if yes it will be deleted.
20:
IF (SysGetFileLen(Filename) > 0) THEN
IF (SysFileRemove(Filename)) THEN END_IF;
END_IF;
Offset[0]:=0; //Offset address
CaseNr:=CaseNr+1; //Case number
// ---------------------------------------------------------------------
// Create a dump record.
// 0 1 2 3 4 5 6
// 0123456789012345678901234567890123456789012345678901234567890
// -------------------------------------------------------------
// 00000000: 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00
21:
j:=SysVsnprintf(ADR(AStr), SIZEOF(AStr), ADR('%08X:'), UDINT_TYPE, ADR(Offset[0]));
// Eseguo report dati in memoria.
FOR i:=0 TO 15 DO
Ptr:=ADR(SysDataBk)+Offset[0]; //Internal pointer
j:=SysCVsnprintf(ADR(AStr), SIZEOF(AStr), ADR(' %02X'), USINT_TYPE, Ptr);
Offset[0]:=Offset[0]+1; //Offset address
IF (Offset[0] >= 2048) THEN CaseNr:=CaseNr+1; RETURN; END_IF;
// Add the column separator character.
IF (i = 7) THEN
j:=SysCVsnprintf(ADR(AStr), SIZEOF(AStr), ADR('%s'), STRING_TYPE, ADR(' |'));
SPos:=SPos+2; //String position
END_IF;
END_FOR;
CaseNr:=CaseNr+1; //Case number
// ---------------------------------------------------------------------
// Open the file in append, if file it's not present it will be created.
// The file management must terminate in the case execution always
// terminate program execution with the file closed.
22:
Fp:=SysFfopen(Filename, ADR('a')); //File pointer
IF (Fp = NULL) THEN ENr:=20; RETURN; END_IF;
// Write data record on file.
j:=SysCVsnprintf(ADR(AStr), SIZEOF(AStr), ADR('%s'), STRING_TYPE, ADR('$r$n'));
IF (Sysfwrite(ADR(AStr), TO_INT(LEN(AStr)), 1, Fp) <> TO_INT(LEN(AStr))) THEN
ENr:=21; //Error number
i:=Sysfclose(Fp);
RETURN;
END_IF;
// Eseguo chiusura file.
IF (Sysfclose(Fp) = eEOF) THEN ENr:=22; RETURN; END_IF;
// Check if write sequence has been completed.
IF (Offset[0] < 2048) THEN CaseNr:=CaseNr-1; RETURN; END_IF;
CaseNr:=0; //Case number
Write:=FALSE; //Write command
// ---------------------------------------------------------------------
// CASE ERROR
// ---------------------------------------------------------------------
ELSE
ENr:=90; //Error number
END_CASE;
// [End of file]