I'm new to Beckhoff/TwinCAT and following tutorials. While making a simple event logger, I hit a snag: after using FB_FileOpen, its bBusy stays True. This jams my state machine in FILE_OPEN. Any ideas on what I messed up? Here's my code:
VAR
fbFileOpen : FB_FileOpen;
fbFilePuts : FB_FilePuts;
RisingEdge : R_TRIG;
eWriteState : (If_Trigger_Detection,OpenFile_Trigger, OpenFile, WaitForTheEvent, WriteFileTriggner, WriteFile, CloseFile, Error);
stEventWriteToFile: ST_Event;
CsvString : T_MaxString;
END_VAR
RisingEdge(CLK:=TRUE);
CASE eWriteState OF
If_Trigger_Detection:
IF RisingEdge.Q THEN
eWriteState:=OpenFile_Trigger;
END_IF
OpenFile_Trigger:
fbFileOpen.bExecute := FALSE;
fbFileOpen(sPathName:= 'C:\TwinCAT\Eventslog.txt',nMode:=FOPEN_MODEAPPEND OR FOPEN_MODETEXT,bExecute := TRUE);
eWriteState:= OpenFile;
OpenFile:
fbFileOpen.bExecute := FALSE;
IF fbFileOpen.bError THEN
eWriteState := Error;
ELSIF NOT fbFileOpen.bBusy AND fbFileOpen.hFile <>0 THEN
eWriteState:= WaitForTheEvent;
END_IF
WaitForTheEvent:
//do nothing just wait
WriteFileTriggner:
CsvString := CreateCsvString (stEvent:= stEventWriteToFile);
//fbFilePuts(bExecute:=FALSE,
// sLine:=CsvString);
fbFilePuts.bExecute:=FALSE;
fbFilePuts(sLine := CsvString,bExecute := TRUE);
eWriteState := WriteFile;
WriteFile:
fbFilePuts(bExecute := FALSE);
IF NOT fbFilePuts.bBusy THEN
eWriteState := WaitForTheEvent;
END_IF
//CloseFile:
Error:
//Here we will write the code for error Handling
END_CASE
You have to keep calling
fbFileOpen()
also in theOpenFile
-state. Right now you're only setting thebExecute
-flag toFALSE
, but you're not actually calling the body of the function block, which means it is never executed, and thus stays in busy. Simply replace:with
Also, this question has already been answered here: TwinCAT 3: Write to File