SD_WRITE

../_images/1e552815e38aabaf0a33139000eba54e1

The AC500 control system contains a memory card of the type “SD Memory Card” (in short SD card) as external storage medium which is accessed by the PLC like a floppy disk drive. The SD card is used to transfer data between a commercially available PC with SD card interface and the AC500 control system.

Read and write accesses take quite long time, since they are handled in the background by the internal file system of the operating system. When performing a write access, the current file is always copied to a backup file.

In this context it has to be observed that the Function Blocks are mutually interlocked, i.e. it must be ensured that only one Function Block is active at the same time.

The Function Block SD_WRITE writes a data set to a file USRDATxx.DAT on the SD Card.

The Function Block writes a data set to a file on the SD card: …\UserData\PM5x1\UserDat\USRDATxx.DAT   (see also Structure of the file USRDATXX.DAT on the SD Card Structure of the File USRDATXX.DAT on the SD Card)

Access to the SD Card is only possible by using the Function Blocks SD_WRITE and SD_READ.

The inputs ATTRIB, FILENO, FORMAT, ADRVAR and NVAR determine how many values should be written to which file and in which format on the SD card as well as from which source address they should be read. To create a readable and EXCEL-compatible file, the individual values are stored in ASCII format, automatically separated by a semicolon. The last value is automatically terminated by a <CR><LF>.

When a write operation of a data set is started (FALSE/TRUE edge at input EN), the data of the data set must not be changed until the end of the write procedure (DONE = TRUE). Storing a data set on the SD Card can take several PLC cycles.

Input EN is not evaluated again until the ready message DONE = TRUE is available, i.e. the state of EN is ignored during writing.

After the write operation is completed, the Function Block outputs DONE, ERR and ERNO are updated. The storage was successful, if DONE = TRUE and ERR = FALSE. If DONE = TRUE and ERR = TRUE, an error occurred. The error type is signalized at output ERNO.

After storing a data set on the SD Card, the Function Block outputs are valid for one cycle. In the next cycle, the outputs DONE, ERR and ERNO are reset to zero. A new FALSE/TRUE edge at input EN starts a new write operation.

Note: In case of a power failure during the write access, the file USRDATxx.DAT will be corrupted. In order to backup at least the already stored data sets, the file USRDATxx.BAK has to be copied from the SC Card prior to restarting the program. The file can be renamed to USRDATxx.DAT on the PC and can then be used for further storage.

Writing example 1:

To store user data to the SD card in a data file without sectors, proceed as follows:

  1. Insert the SD card.

  2. Write a data set by calling the Function Block SD_WRITE with the following settings: EN := TRUE (* FALSE/TRUE edge triggers write operation *) ATTRIB := 2 (* write append *) FILENO := 0..99 (* number of the file to be written *) SEG := address of the variable sector name (* any *) FORMAT := data format NVAR := number of data in the data set ADRVAR := address of the first variable to be written If no appropriate file can be found, it will be created. The write process is successfully completed if output DONE = TRUE and output ERR = FALSE. A write error is indicated with ERR = TRUE and ERNO <> 0.

  3. Further data sets can be written with the same Function Block settings after the completion message (output DONE=TRUE) is displayed. This process is started with a FALSE/TRUE edge at input EN.

Writing example 2:

To store user data to the SD card in a data file with sectors, proceed as follows:

  1. Insert the SD card.

  2. Write the sector label by calling the Function Block SD_WRITE with the following settings: EN := TRUE ATTRIB := 3 (* write sector *) FILENO := 0..99 (* number of the file to be written *) SEG := address of the variable sector name If no appropriate file can be found, it will be created. The sector is successfully written when the output DONE:=TRUE and the output ERR:=FALSE. A write error is indicated with ERR = TRUE and ERNO <> 0.

  3. Write a data set by calling the Function Block SD_WRITE with the following settings: EN := TRUE (* FALSE/TRUE edge triggers write operation *) ATTRIB := 2 (* write append *) FILENO := 0..99 (* number of the file to be written *) SEG := address of the variable sector name FORMAT := data format NVAR := number of data in the data set ADRVAR := address of the first variable to be written The write process is successfully completed if output DONE = TRUE and output ERR = FALSE. A write error is indicated with ERR = TRUE and ERNO <> 0.

  4. Further data sets can be written with the same Function Block settings after the completion message (output DONE=TRUE) is displayed. This process is started with a FALSE/TRUE edge at input EN.

  5. If you want to write further sectors and data sets, repeat steps 2…4. Note:

The file USRDATxx.DAT is saved as USRDATxx.BAK for each write process and a “Open file / Write file / Close file” procedure is performed.

Deleting a file:

To delete a data file from the SD card, proceed as follows:

  1. Insert the SD card.

  2. Call the Function Block SD_WRITE with the following settings: EN := TRUE ATTRIB := 1 (* delete *) FILENO := 0..99 (* number of the file to be deleted *) SEG, FORMAT, NVAR, ADRVAR &ndash; any

Available as of PLC runtime system: V1.0
Included in library: SysInt_AC500_V10.lib
Type Function Block with historical values