-> Hier kostenlos registrieren
Hallo,
ich möchte aus einer .txt Datei etwas einlesen und in ein Array of String Speichern.
Das Beispiel von Backhoff "fbFileCopy" klappt wunderbar, nur hier wird Byteweise gelesen und die Datei halt kopiert.
( http://infosys.beckhoff.de/index.ph...tem/html/tcplclibsys_fb_filegets.htm&id=23774)
Ich versuche es mit fb_FileGets , habe den Code dementsprechend umgeschrieben, es gibt auch keinen Fehler aber den String der aus sLine kommt kann ich nicht zwischenspeichern.
umgeschriebener fbFileCopy :
Im Anhang ist ein Screenshot, hier sieht man das in sLine der Wert/Stiring "2999 0 str.." steht aber dass dieser nicht in die Variable strBuffer geschrieben wird.
Was mache ich Falsch ?
Vielen Dank im Vorraus
Anhang: Screenshot, Twincat3 Projekt
ich möchte aus einer .txt Datei etwas einlesen und in ein Array of String Speichern.
Das Beispiel von Backhoff "fbFileCopy" klappt wunderbar, nur hier wird Byteweise gelesen und die Datei halt kopiert.
( http://infosys.beckhoff.de/index.ph...tem/html/tcplclibsys_fb_filegets.htm&id=23774)
Ich versuche es mit fb_FileGets , habe den Code dementsprechend umgeschrieben, es gibt auch keinen Fehler aber den String der aus sLine kommt kann ich nicht zwischenspeichern.
Code:
PROGRAM MAIN
VAR
bSchreiben : BOOL ;
bLesen: BOOL;
Lesen : BOOL;
bLesenBusy,bLesenError : BOOL;
nLesenErrorID : UDINT ;
DateiLesen : fbRead;
END_VAR
VAR_INPUT
END_VAR
---------------------
DateiLesen(
sSrcNetId:= '',
sSrcPathName:= 'C:\Users\10469310\Documents\2.txt',
bExecute:= bLesen,
tTimeOut:=T#2S ,
bBusy=>bLesenBusy ,
bError=> bLesenError,
nErrId=> nLesenErrorID);
Code:
FUNCTION_BLOCK fbRead
VAR_INPUT
sSrcNetId : T_AmsNetId;(* TwinCAT network address of the source file *)
sSrcPathName : T_MaxString;(* Source file path and name *)
// sDestNetId : T_AmsNetId;(* TwinCAT network address of the destination file *)
// sDestPathName : T_MaxString; (* Destination file path and name *)
bExecute : BOOL; (* Rising edge start fb execution *)
tTimeOut : TIME := DEFAULT_ADS_TIMEOUT;(* Max. ADS timeout time *)
END_VAR
VAR_OUTPUT
bBusy :BOOL;(* TRUE => File copy execution in progress, FALSE => File copy execution idle *)
bError :BOOL;(* TRUE => Error, FALSE => No error *)
nErrId :UDINT;(* Error code *)
END_VAR
VAR
fbFileOpen :FB_FileOpen;
fbFileClose :FB_FileClose;
fbFileRead :FB_FileRead;
fbFileWrite :FB_FileWrite;
fbFileGets : FB_FileGets;
hSrcFile :UINT := 0;(* File handle of the source file *)
hDestFile :UINT := 0;(* File handle of the destination file *)
Step :DWORD;
RisingEdge :R_TRIG;
buffRead :ARRAY[1..1000] OF BYTE;(* Buffer *)
cbReadLength :UDINT := 0;
// ab hier
strBuffer : STRING ;
bEOF : BOOL;
zeile : ARRAY[0..40] OF STRING;
i: INT;
END_VAR
-----------------------------------------------------------
RisingEdge(CLK:=bExecute);
CASE Step OF
0: (* Idle state *)
IF RisingEdge.Q THEN
bBusy := TRUE;
bError:= FALSE;
nErrId:=0;
Step := 1;
hSrcFile:=0;
i:= 0;
END_IF
1: (* Open source file *)
fbFileOpen( bExecute := FALSE );
fbFileOpen( sNetId := sSrcNetId, sPathName := sSrcPathName,
nMode := FOPEN_MODEREAD OR FOPEN_MODETEXT,// Textmodus für FB_FileGets //
ePath := PATH_GENERIC, tTimeout := tTimeOut, bExecute := TRUE );
Step := Step + 1;
2:
fbFileOpen( bExecute := FALSE );
IF NOT fbFileOpen.bBusy THEN
IF fbFileOpen.bError THEN
nErrId := fbFileOpen.nErrId;
bError := TRUE;
Step := 50;
ELSE
hSrcFile := fbFileOpen.hFile;
Step := Step + 1;
//
END_IF
END_IF
3: (* Read data from source file *)
// Buffer zurücksetzten
fbFileGets(bExecute := FALSE);
fbFileGets( sNetId:= sSrcNetId, hFile:= hSrcFile,
bExecute:= TRUE, tTimeout:= tTimeOut ,
sLine=> strBuffer,
bEOF=> bEOF);
Step := Step + 1;
4:
fbFileGets( bExecute:= FALSE );
IF NOT fbFileGets.bBusy THEN
IF fbFileGets.bError THEN
nErrId := fbFileGets.nErrId;
bError := TRUE;
Step := 50;
ELSE
fbFileGets(sLine => zeile[i]);
zeile[i] := strBuffer ;
i := i +1 ;
Step := Step + 1;
END_IF
END_IF
5:
IF fbFileGets.bEOF THEN (* Check if the EOF flag ist set *)
Step := 50; (* Cleanup: close the destination and source files *)
ELSE
Step := 3; (* Repeat reading/writing *)
END_IF
40: (* Close source file *)
fbFileClose( bExecute := FALSE );
fbFileClose( sNetId:=sSrcNetId, hFile:=hSrcFile, bExecute:=TRUE, tTimeout:=tTimeOut );
Step := Step + 1;
41:
fbFileClose( bExecute := FALSE );
IF NOT fbFileClose.bBusy THEN
IF fbFileClose.bError THEN
nErrId := fbFileClose.nErrId;
bError := TRUE;
END_IF
Step := 50;
hSrcFile := 0;
END_IF
50: (* Error or ready => Cleanup *)
IF ( hSrcFile <> 0 ) THEN
Step := 40; (* Close the file*)
ELSE
Step := 0; (* Ready *)
bBusy := FALSE;
END_IF
END_CASE
Im Anhang ist ein Screenshot, hier sieht man das in sLine der Wert/Stiring "2999 0 str.." steht aber dass dieser nicht in die Variable strBuffer geschrieben wird.
Was mache ich Falsch ?
Vielen Dank im Vorraus
Anhang: Screenshot, Twincat3 Projekt