Re: Logging von Meldungen oder Variablen in Datei
Posted: 01 Aug 2018, 17:36
Hallo Kai,
Folgendes Programm sollte für deine Anforderungen geignet sein:
Für das Transferieren des Strings in ein Byte-Array ist folgender C-Baustein geeignet:
freundliche Grüße
Walter
Folgendes Programm sollte für deine Anforderungen geignet sein:
Code: Select all
PROGRAM RevolutionPiFilewriteProgram
VAR CONSTANT
/* Append new information to the end of the file. */
Linux_O_APPEND : DINT := 1024;
/* If the file does not exist, create it. If the O_CREAT option is used, then you must include the third parameter (mode). */
Linux_O_CREAT : DINT := 64;
/* Combined with the O_CREAT option, it ensures that the caller must create the file. If the file already exists, the call will fail. */
Linux_O_EXCL : DINT := 128;
/* Open the file so that it is read only. */
Linux_O_RDONLY : DINT := 0;
/* Open the file so that it can be read from and written to. */
Linux_O_RDWR : DINT := 2;
/* Initially clear all data from the file. */
Linux_O_TRUNC : DINT := 512;
/* Open the file so that it is write only. */
Linux_O_WRONLY : DINT := 1;
END_VAR
VAR
INIT : BOOL:=TRUE;
writeCounter : DINT := 0;
fileDescriptor : DINT := 0;
data : ARRAY[0..63] of BYTE;
flagsWriteAppend : DINT := -1;
flagsCreateTruncWrite : DINT := -1;
flags : DINT :=-1;
mode : DINT := 0;
my_string : STRING[64] := 'Hello World';
END_VAR
IF INIT THEN
/* write access, append to file content */
flagsWriteAppend := IOR(LINUX_O_WRONLY, LINUX_O_APPEND);
/* write access, create file and truncate file to length 0 */
flagsCreateTruncWrite := IOR(LINUX_O_WRONLY, LINUX_O_CREAT, LINUX_O_TRUNC);
/* read and write access for the user */
fileDescriptor := System_open(pathname := '/tmp/file.log', flags := flagsWriteAppend, ENO => ENO);
IF NOT(ENO) THEN /* file does not exist yet */
fileDescriptor := System_open(pathname := '/tmp/file.log', flags := flagsCreateTruncWrite, mode := mode, ENO => ENO);
END_IF;
INIT:=FALSE;
END_IF;
/* Transfering string to BYTE array */
data := StrToByteArr(my_string);
System_write(fd := fileDescriptor, data := data, count := LEN(my_string));
System_close(fd := fileDescriptor);
END_PROGRAM
Für das Transferieren des Strings in ein Byte-Array ist folgender C-Baustein geeignet:
Code: Select all
#ifndef LC_PROT_LCFU___STRTOBYTEARR__C
#define LC_PROT_LCFU___STRTOBYTEARR__C
#include <lcfu___strtobytearr.h>
#include "stdio.h"
/* Functions */
void lcfu___STRTOBYTEARR(LC_TD_Function_STRTOBYTEARR* LC_this, LcCgChar LC_VD_MYSTRING[65], struct _lcoplck_epdb_1_impl* pEPDB)
{
memcpy(LC_this->LC_VD_STRTOBYTEARR, LC_VD_MYSTRING, 64);
/* Vendor Code */
}
#endif
freundliche Grüße
Walter