Writing a string to a file using FB_FilePuts in TwinCAT

karlek

Member
Join Date
Jul 2014
Location
Varazdin, Croatia
Posts
68
Hello to everyone,

I am trying to achieve simple writing of a string to a text file in TwinCAT. The code I am using can be found below. Problem is that fPuts FB call gives me an error code 0x703 ("Unknown or invalid nMode or ePath parameter."). Beckhoff online documentation states that for using FB_FilePuts "The file must previously have been opened in the text mode." So where am I making a mistake?

Thank you in advance

PROGRAM MAIN
VAR

anInput1 AT %I* : INT;
napon : INT;
cb1anIn1 AT %Q* : BYTE:=1;

otvoriDatoteku : FB_FileOpen;
zatvoriDatoteku : FB_FileClose;
fPuts : FB_FilePuts;

hFile : UINT;

bFileOpen : BOOL;
bFileOpenBusy : BOOL;
bFileOpenError : BOOL;
nFileOpenErrId : UDINT;

bFileCloseBusy : BOOL;
bFileCloseError : BOOL;
nFileCloseErrId : UDINT;

bFilePuts : BOOL;
bFilePutsBusy : BOOL;
bFilePutsError : BOOL;
nFilePutsErrId : UDINT;

END_VAR

otvoriDatoteku(bExecute:=FALSE);

otvoriDatoteku(
sNetId:='5.11.48.178.1.1',
sPathName:='c:\TestFile.txt',
nMode:=FOPEN_MODEAPPEND OR FOPEN_MODETEXT,
ePath:=PATH_GENERIC,
bExecute:=TRUE,
bBusy=>bFileOpenBusy,
bError=>bFileOpenError,
nErrId=>nFileOpenErrId);

hFile:=otvoriDatoteku.hFile;

fPuts(bExecute:=FALSE);

fPuts(
sNetId:='5.11.48.178.1.1',
hFile:=hFile,
sLine:='linija1$L',
bExecute:=TRUE,
bBusy=>bFilePutsBusy,
bError=>bFilePutsError,
nErrId=>nFilePutsErrId);

zatvoriDatoteku(bExecute:=FALSE);

zatvoriDatoteku(
sNetId:='5.11.48.178.1.1',
hFile:=hFile,
bExecute:=TRUE );
 
You are making a simple mistake:

Function blocks usually need more than one scan cycle to finish their tasks to perform the desired task, in order to improve what you are getting I would do:

Code:
PROGRAM MAIN
VAR

anInput1 AT %I* : INT;
napon : INT;
cb1anIn1 AT %Q* : BYTE:=1;

otvoriDatoteku : FB_FileOpen;
zatvoriDatoteku : FB_FileClose;
fPuts : FB_FilePuts;

hFile : UINT;

bFileOpen : BOOL;
bFileOpenBusy : BOOL;
bFileOpenError : BOOL;
nFileOpenErrId : UDINT;

bFileCloseBusy : BOOL;
bFileCloseError : BOOL;
nFileCloseErrId : UDINT;

bFilePuts : BOOL;
bFilePutsBusy : BOOL;
bFilePutsError : BOOL;
nFilePutsErrId : UDINT;

BState : BYTE := 0;

END_VAR

CASE BState OF
  0:
    otvoriDatoteku(bExecute:=FALSE);
    BState := BState + 1;

  1:
    otvoriDatoteku(
    sNetId:='5.11.48.178.1.1',
    sPathName:='c:\TestFile.txt',
    nMode:=FOPEN_MODEAPPEND OR FOPEN_MODETEXT,
    ePath:=PATH_GENERIC,
    bExecute:=TRUE,
    bBusy=>bFileOpenBusy,
    bError=>bFileOpenError,
    nErrId=>nFileOpenErrId);
    BState := BState + 1;

  2:
    otvoriDatoteku();
    IF (NOT otvoriDatoteku.bError AND NOT otvoriDatoteku.bBusy)
    THEN
       hFile:=otvoriDatoteku.hFile;
       fPuts(bExecute:=FALSE);
       BState := BState + 1;
    END_IF

  3:
    fPuts(
    sNetId:='5.11.48.178.1.1',
    hFile:=hFile,
    sLine:='linija1$L',
    bExecute:=TRUE,
    bBusy=>bFilePutsBusy,
    bError=>bFilePutsError,
    nErrId=>nFilePutsErrId);

...

I'm sure you have got the main idea.

Good luck!
 
If you want a function to be processed you must call it, the same happens with FB's, you have to execute them to ensure they will do something.

Good luck!
 

Similar Topics

How can i write aHint or dialog Text box when runtime manger already running .
Replies
0
Views
1,646
Using FTV 9 SE Local Windows 10 Trying to use a little VB to write to a string. Keep getting an error. 'Taggroup\Tagname is a string Dim Note...
Replies
1
Views
2,014
Could somebody please point me in the right direction? I'm trying to incorporate a table into each motor's faceplate. It's a simple, two...
Replies
2
Views
3,205
PLC Gurus, I need a little bit help here.. A little background, we're working on a project using PlantPAx from Rockwell.. We use some of their...
Replies
4
Views
3,383
Hello All This is my first Transaction Manager project. I have a working configuration in Transaction Manager 10.20 that successfully writes...
Replies
3
Views
7,632
Back
Top Bottom