Help using FB_FileOpen for Twincat (Beckhoff)

paicolman

Member
Join Date
Mar 2010
Location
Bassersdorf
Posts
2
Hi,

I want to write a string into a file. This has to happen within a funcion (FUN, no FB) since I want to use it to log stuff to a file, triggered by the mein program.

My problem is:

If I step through my code, it works fine, but as soon as I run it normally, the FB_FileOpen keeps bBusy HI until it gets a timeout, regardless of timeout values or twincat pace (I'm using the simulation, not the real CX). Any idea what I'm doing wrong?

Here´s my code:

FUNCTION LogInfo : BOOL
VAR_INPUT
sID : STRING;
sInfo : STRING;
END_VAR
VAR
sMsgToLog : STRING; Step: SINT;
hFileHandle: UINT;
fbCreateLogFile : FB_OpenLogFile;
fbFileClose :FB_FileClose;
CreateOrAppend: DWORD;
fbFileOpen : FB_FileOpen;
fbFilePutString : FB_FilePuts;
x: INT;
END_VAR



fbFileOpen(bExecute := FALSE);

fbFileOpen( bExecute:=TRUE,
sNetId := '192.168.1.2.1.1',
sPathName:='C:\ADSLOG.TXT',
ePath := PATH_GENERIC,
tTimeout := T#2S,
nMode := CreateOrAppend);

WHILE (fbFileOpen.bBusy) DO
x := x + 1;
fbFileOpen(bExecute:=FALSE);
END_WHILE

hFileHandle:=fbFileOpen.hFile;

sMsgToLog := 'Whatever I need to write';

fbFilePutString(bExecute := FALSE);
fbFilePutString(bExecute := TRUE,
sNetId := '192.168.1.2.1.1',
hFile := hFileHandle,
sLine := sMsgToLog);

WHILE (fbFilePutString.bBusy) DO
fbFilePutString(bExecute:=FALSE);
END_WHILE

fbFileClose(bExecute := FALSE);
fbFileClose(bExecute := TRUE,
hFile := hFileHandle);

WHILE (fbFileClose.bBusy) DO
fbFileClose(bExecute:=FALSE);
END_WHILE
 
The way you have coded this means the plc program will not execute it's logic while it waits for the file open/write/close - is this your intention?

The example I've seen on the Beckhoff site uses a state machine to allow other processing to take place whilst the file access is being done. My guess is that the file processing is done outside of the plc scan - when you single step the file access is done between steps but when you run there is no time allocated because the plc cycle never completes.
 
Thanks, that might be the problem. That would mean that I cannot implement it the way I wanted (open / write / close to a file within a single "FUN").
 
You cannot call function blocks from within a function when the fb instances are declared as local variables of the function - local variables do not persist between calls of the function (created on the stack, so discarded when the function exits)

Functions are intended to be lightweight POUs with no memory - they always return the same output given the same input

Put it all in a function block, and it should work fine. Look at Beckhoff library blocks for some clues on structuring a fb - they use a state machine implemented by a CASE statement. Do not forget to call fb's that are edge triggered with the execute (or read or write or whatever) input off,once they have completed or they will never detect the rising edge next time you use them! Single stepping should make things clearer.

Regards

Roger
 

Similar Topics

After replacing the 70 with the 525, the PLC can read from the drive and recognizes it as online, but no commands are being listened to. PLC is...
Replies
1
Views
556
I'm new to Crimson (this is my first week, and I just ran into my first road block. I'm trying to display the position of a rail car, running in...
Replies
3
Views
1,158
Hello, I'm struggling with getting an email to send using an eweb module on a controllogix system. Any chance I could get someone here to help?
Replies
4
Views
1,856
Hi all I am trying to set up a lhttp instruction to connect to an api https (with our IT) this will send data back to the plc? I have an api https...
Replies
9
Views
1,786
please help i didn't solve this problem with rslogix. I counted but i am not using timer. How i will fix this situation? Please anyone solve this...
Replies
18
Views
4,915
Back
Top Bottom