At a glance

In this example we use the following EFBs in order to perform some actions on a file:

  • CREATE_FILE,

  • OPEN_FILE,

  • SEEK_FILE

  • WR_DATA_TO_FILE

  • RD_FILE_TO_DATA

  • CLOSE_FILE

Declaration of variables

The table below shows the details of the variables to be declared for the parameters used:

Variable name

Type

Comment

close_req

EBOOL

Enable CLOSE_FILE function bit

create_req

EBOOL

Enable CREATE_FILE function bit

fileDesc

DINT

FileDesc is the number automatically assigned when a file is opened. This unique and temporary identification number will be assigned by the OPEN_FILE function.

NbBytesRd

UDINT

Specify the size effectively read of the variable.

read_req

EBOOL

Enable RD_FILE_TO_DATA function bit

variable

INT

variable read in the file

seek_req

EBOOL

Enable SEEK_FILE function bit

write_req

EBOOL

Enable WR_DATA_TO_FILE function bit

variable_tab

ARRAY[0..9] OF INT

Array for the different values of the variables written in the file

cmd_write

EBOOL

write file bit

GO_STORE

EBOOL

Bit used to start the creation of the file

cmd_seek

EBOOL

seek file bit

Error_WR

BOOL

Error during execution of WR_DATA_TO_FILE function bit

cmd_close

EBOOL

close file bit

GO_RESTORE

EBOOL

Bit used to start the re-opening of the file

open_req

EBOOL

Enable OPEN_FILE function bit

cmd_read

EBOOL

read file to data bit

Done_WR

BOOL

Writing in the file operation completed bit

Done_OPEN

BOOL

Opening the file operation completed bit

Status_WR

WORD

Error code (useful when Error_WR:=1)

Done_SEEK

BOOL

Seeking the file operation completed bit

Error_SEEK

BOOL

Error during execution of SEEK_FILE function bit

Status_SEEK

WORD

Error code (useful when Error_SEEK:=1)

Done_CREATE

BOOL

Creating the file operation completed bit

Error_CREATE

BOOL

Error during execution of CREATE_FILE function bit

Status_CREATE

WORD

Error code (useful when Error_CREATE:=1)

Done_CLOSE

BOOL

Closing the file operation completed bit

Error_CLOSE

BOOL

Error bit for CLOSE_FILE function

Status_CLOSE

WORD

Error code (useful when Error_CLOSE:=1)

Error_OPEN

BOOL

Error during execution of OPEN_FILE function bit

Status_OPEN

WORD

Error code (useful when Error_OPEN:=1)

Done_RD

BOOL

Reading the file operation completed bit

Error_RD

BOOL

Error during execution of RD_FILE_TO_DATA function bit

Status_RD

WORD

Error code (useful when Error_RD:=1)

Code

This code perform the following actions :

  • Create the file or seek it if it is already created,

  • Write data in the file,

  • Close the file,

  • Open the closed file,

  • Read the data written in the file,

  • Close the file.

(*********************************************************
Create a file 'TEST.bin' if doesn't exist and open it in
append mode(SEEK).
Write a data and close the file
*********************************************************)
(******* Begin CREATE part *******)
if RE(GO_STORE) then (* Set GO_STORE to start *)
  create_req := 1;
end_if;
CREATE (REQ := create_req,(* Rising edge on REQ input to start the CREATE *)
SLOT := 0,
FILENAME := 'TEST.bin',
MODEFLAG := 2,(* Read/Write mode *)
DONE => Done_CREATE, 
ERROR => Error_CREATE,
STATUS => Status_CREATE,
FILEDESC => fileDesc);
create_req := 0;
if GO_STORE then
   if (Done_CREATE and not Error_CREATE) then 
     GO_STORE := 0 ;
     seek_req := 1;
     cmd_seek := 1;
   end_if;
end_if;
(******* End CREATE part *******)
(******* Begin SEEK part *******)
SEEK (REQ := seek_req;(* Rising edge on REQ input to start the SEEK *)
FILEDESC := fileDesc
OFFSET := 0,
WHENCE := 2;
(* SEEK_END *)
DONE => Done_SEEK,
ERROR => Error_SEEK,
STATUS => Status_SEEK);
seek_req := 0;
if cmd_seek  then
   if (Done_SEEK and not Error_SEEK) then
     cmd_seek := 0;
     write_req := 1;
     cmd_write := 1;
   end_if;
 end_if;
(******* End SEEK part *******)
(******* Begin WRITE part *******)
variable_tab[0] := variable_tab[0] + 1;
WRITE (REQ := write_req,(* Rising edge on REQ input to start the WRITE *)
FILEDESC := fileDesc,
VARIABLE := variable_tab,
DONE => Done_WR,
ERROR => Error_WR,
STATUS => Status_WR);
write_req := 0;
if cmd_write then
  if (Done_WR and not Error_WR) then
    cmd_write := 0;
    close_req := 1;
    cmd_close := 1;
  end_if;
end_if;
(******* End WRITE part *******)
(******* Begin CLOSE part *******)
CLOSE (REQ := close_req,(* Rising edge on REQ input to start the CLOSE *)
FILEDESC := fileDesc,
DONE => Done_CLOSE,
ERROR => Error_CLOSE,
STATUS => Status_CLOSE);
close_req := 0;
if cmd_close then
  if (Done_CLOSE and not Error_CLOSE) then
    cmd_close := 0;
  end_if;
end_if;
(******* End CLOSE part *******)
(*********************************************************
Open the file 'TEST.bin', 
read the first data and close the file
**********************************************************
(******* Begin OPEN part *******)
if RE(GO_RESTORE) then(* Set GO_RESTORE to start *)
  open_req := 1;
end_if;
OPEN (REQ := open_req,(* Rising edge on REQ input to start the OPEN *)
SLOT := 0,
FILENAME := 'TEST.bin',
MODEFLAG := 0,
DONE => Done_OPEN,
ERROR => Error_OPEN,
STATUS => Status_OPEN,
FILEDESC => fileDesc);
open_req := 0;
if GO_RESTORE then
  if (Done_OPEN and not Error_OPEN) then
    GO_RESTORE := 0 ;
    read_req := 1;
    cmd_read := 1;
  end_if;
end_if;
(******* End OPEN part *******)
(******* Begin READ part *******)
READ (REQ := read_req,(*  Rising edge on REQ input to start the READ *)
FILEDESC := fileDesc,
DONE => Done_RD,
ERROR => Error_RD,
STATUS => Status_RD,
VARIABLE => variable,
NBBYTESRD => NbBytesRd);
read_req := 0;
if cmd_read then
  if (Done_RD and not Error_RD) then
    cmd_read := 0;
    close_req := 1;(* Same CLOSE part than the CREATE/SEEK/WRITE/CLOSE *)
    cmd_close := 1;
  end_if;
end_if;
(******* End READ part *******)