Twincat : FileOpen/write/close

Vilse

Member
Join Date
Jun 2014
Location
Linköping
Posts
4
Hi

I want to create, open, write and close a .txt file. And it works fine. BUT I can't do it again. I have to reactive the hole system before I can do it again.

I'm not an experienced programmer.

Any thoughts?

/David


------Code--------

FUNCTION_BLOCK FB_savefile

VAR
fbFileWrite : FB_FileWrite;
fbclose : FB_FileClose;
fbfileopen : FB_FileOpen;
Pathname: STRING;
initierat: BOOL := FALSE;
END_VAR

IF (runs > 1)
THEN Pathname := concat(concat(concat('c:\dill\',firstrun),startdatestr),'.txt');
ELSE Pathname := concat(concat('c:\dill\',firstrun),'.txt');
END_IF

IF (initierat = FALSE)
THEN fbfileopen(sNetId := '169.254.182.198.1.1', sPathName := Pathname,
ePath := PATH_GENERIC, nMode := FOPEN_MODEWRITE OR FOPEN_MODETEXT, bExecute := TRUE);

END_IF

IF (fbfileopen.bBusy = FALSE AND fbfileopen.bExecute = TRUE)

THEN fbfileopen.bExecute := FALSE;
hFiledill := fbfileopen.hFile;
initierat := TRUE;
END_IF
fbfilewrite.bExecute := FALSE;
IF (skrivarray = TRUE AND fbfileopen.bExecute = FALSE)
THEN fbfilewrite(sNetId := '169.254.182.198.1.1', hFile := hfiledill,
pWriteBuff := ADR(arr2), cbWriteLen := SIZEOF(arr2),
bExecute := TRUE);

END_IF

IF (fbfilewrite.bBusy = FALSE AND fbfilewrite.bExecute = TRUE)
THEN fbfilewrite.bExecute := FALSE;
fbclose.bExecute := FALSE;
fbclose(sNetId := '169.254.182.198.1.1', hfile := hfiledill, bExecute := TRUE);
hFiledill := 0;

END_IF

IF (fbclose.bBusy = FALSE AND fbclose.bExecute = TRUE)
THEN fbclose.bExecute := FALSE;
initierat := FALSE;
skrivarray:= FALSE;
END_IF

-------------------------------------
 
Perhaps your code is leaving the file "open". When you close the app, it probably releases that lock. When your code is done with writing to the file, try moving or renaming the file in Windows Explorer. If Windows says "file in use", that is your clue. I didn't poke thru your PLC code and have never interacted w/ files from TwinCAT code since I always have an HMI program in VB.net which handles file I/O.
 

Similar Topics

There's already been some discussion about writing to a file in TwinCAT, but I didn't see that someone encountered problem similar to mine. My...
Replies
1
Views
3,181
Hi I'm doing a simple writing to .CSV file. I want to write the headers, but I have hard time indentifying when the file is empty. One idea was...
Replies
1
Views
1,997
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...
Replies
3
Views
6,588
Hi guys, I get an 'Identifier expected' error on this line fbFileOpen(' ','Hard Disk\My_Projects\Tilt6_1.txt',FOPEN_MODEREAD OR...
Replies
16
Views
4,680
I'm trying to control a device via MODBUS RTU and the ModbusRtuMasterV2_PcCOM in Twincat 3. I've configured a device with the right com port and...
Replies
7
Views
183
Back
Top Bottom