I'm using the CAA-File library in CodeSyS to write data to a text file. When I write a string to the file it works fine, but if I combine 2 strings with CONCAT, I get a strange file output with random characters (see below). Does anyone know how to fix this? Thank you!
For example lString = 'test'
works fine, but lString = CONCAT('str1', 'str2')
does not.
PROGRAM initDataLogger
VAR_INPUT
lString : STRING;
END_VAR
VAR
lString : STRING:= CONCAT('str1', 'str2');
xFileStdInit: BOOL:=FALSE;
uiFileStdState: UINT:=0;
sFileName: CAA.FILENAME:= '/var/www/html/TestFile.txt';
hFile: CAA.HANDLE;
szFileSize1: CAA.SIZE := 0;
szFileSize2: CAA.SIZE := 0;
filop: FILE.Open;
filwr: FILE.Write;
filcl: FILE.Close;
loop: INT := 0;
fString: STRING;
END_VAR
IF NOT xFileStdInit THEN
filop(xExecute:=FALSE);
filcl(xExecute:=FALSE);
filwr(xExecute:=FALSE);
xFileStdInit:=TRUE;
uiFileStdState:=0;
ELSE
CASE uiFileStdState OF
0: (* create a new file *)
filop.sFileName:=sFileName;
filop.eFileMode:=FILE.MODE.MAPPD;
filop.xExclusive:=TRUE;
filop( xExecute:=TRUE);
IF filop.xDone THEN
hFile:=filop.hFile;
uiFileStdState:=1;
END_IF
IF filop.xError THEN
(* error handling*)
;
END_IF
1:(* write text in the file *)
//fString := CONCAT('$R$L', lString);
fString := lString;
filwr.hFile:=hFile;
filwr.pBuffer:=ADR(fString);
szFileSize1:=SIZEOF(fString);
filwr.szSize:=szFileSize1;
filwr.udiTimeOut:=100000; (* 100ms Timeout *)
IF GVL.logData THEN
filwr( xExecute:=TRUE);
END_IF
IF filwr.xDone THEN
uiFileStdState:=2;
GVL.logData := FALSE;
END_IF
IF filwr.xError THEN
(* error handling*)
;
END_IF
2: (* close file - TestFile.txt *)
filcl.hFile:=hFile;
filcl( xExecute:=TRUE);
IF filcl.xDone THEN
uiFileStdState:=3;
END_IF
IF filcl.xError THEN
(* error handling*)
;
END_IF
3: (* end of example *)
xFileStdInit := FALSE;
END_CASE
END_IF
For lString = CONCAT('str1', 'str2')
I expect str1str2
as output but I get this:
str1str2sys??v$??v??v?
?vd?n??
?v?`v??vd
?vK
?v?`v8
?v?v4??v?
I found it out myself. It had to do with the string size. For example: STRING(4)
outputs fine with a four letter word, but with a two letter word it adds two random character's.