CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Sun Nov 19, 2017 4:21 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Thu Aug 16, 2012 10:12 am 
Offline

Joined: Wed Jun 27, 2012 8:39 am
Posts: 3
Hi,

Can you please provide me a working sample code for Write and Read File using Codesys v3 ?

Thanks,

Naganatha Subramanian R


Top
   
PostPosted: Fri Feb 22, 2013 11:10 pm 
Offline

Joined: Fri Oct 26, 2012 7:24 pm
Posts: 13
Did you solve that?


Top
   
PostPosted: Sat Feb 23, 2013 2:28 pm 
Offline
Frequent User
Frequent User
User avatar

Joined: Tue Jun 06, 2006 9:03 am
Posts: 403
It is advisable to use the CAA_FILE.library.
In the help of V3 is an example.
Libraries > System Libraries > CAA Libraries > CAA_FILE.library > Examples > File - Standard (EXAMPLE)


Top
   
PostPosted: Sun Feb 24, 2013 4:28 pm 
Offline
Frequent User
Frequent User

Joined: Thu Jun 14, 2007 9:49 am
Posts: 985
old post

_________________
shooter@home.nl
computer shooter
paul deelen
jacob van wassenaerstraat 29
NL 5224 GG 's-Hertogenbosch
skype shooter paul.deelen
phone +31736139136


Top
   
PostPosted: Thu Feb 28, 2013 9:56 am 
Offline

Joined: Fri Oct 26, 2012 7:24 pm
Posts: 13
Thank you so much for the answer.

I don't have the example, can you send me it?
Is it possible to attacc in a post?

Thank you again!


Top
   
PostPosted: Fri Mar 01, 2013 7:53 pm 
Offline
Frequent User
Frequent User
User avatar

Joined: Tue Jun 06, 2006 9:03 am
Posts: 403
Below you can find the copy from the help. I didn't test this and have no idea whether it works directly without compile errors.
You should off course also add the USE CASE library "File access".

Please also check the store at http://store.codesys.com. After signing in you can download a package with a "File Utilities" example.

Code:
PROGRAM FILE.STANDARD_PRG
VAR
xFileStdInit: BOOL:=FALSE;
uiFileStdState: UINT:=0;
sFileName: CAA.FILENAME:= 'TestFile.txt';
hFile: CAA.HANDLE;
sFileTestString: STRING:='Hello caa library user';
sFileString: STRING:='';
szFileSize1: CAA.SIZE := 0;
szFileSize2: CAA.SIZE := 0;
filop: FILE.Open;
filwr: FILE.Write;
filrd: FILE.Read;
filcl: FILE.Close;
END_VAR

IF NOT xFileStdInit THEN
filop(xExecute:=FALSE);
filcl(xExecute:=FALSE);
filwr(xExecute:=FALSE);
filrd(xExecute:=FALSE);
xFileStdInit:=TRUE;
uiFileStdState:=0;
ELSE
CASE uiFileStdState OF
0: (* create a new file *)
filop.sFileName:=sFileName;
filop.eFileMode:=FILE.MRDWR;
filop.xExclusive:=TRUE;
filop( xExecute:=TRUE);
IF filop.xDone THEN
hFile:=filop.hFile;
uiFileStdState:=1;
END_IF
IF filop.xError THEN
(* error handling*)
;
END_IF
1:(* write text in the file *)
filwr.hFile:=hFile;
filwr.pBuffer:=ADR(sFileTestString);
szFileSize1:=SIZEOF(sFileTestString);
filwr.szSize:=szFileSize1;
filwr.udiTimeOut:=100000; (* 100ms Timeout *)
filwr( xExecute:=TRUE);
IF filwr.xDone THEN
uiFileStdState:=2;
END_IF
IF filwr.xError THEN
(* error handling*)
;
END_IF
2:(* read file - TestFile.txt*)
filrd.hFile:=hFile;
filrd.udiTimeOut:=100000; (* 100ms Timeout *)
filrd.pBuffer:=ADR(sFileString);
filrd.szBuffer:=255;
filrd( xExecute:=TRUE);
IF filrd.xDone THEN
szFileSize2:=filrd.szSize;
IF szFileSize2 = szFileSize1 THEN
uiFileStdState:=3;
ELSE
(* error handling*)
;
END_IF
END_IF
IF filrd.xError THEN
(* error handling*)
;
END_IF
3: (* close file - TestFile.txt *)
filcl.hFile:=hFile;
filcl( xExecute:=TRUE);
IF filcl.xDone THEN
uiFileStdState:=4;
END_IF
IF filcl.xError THEN
(* error handling*)
;
END_IF
4: (* end of example *)
;
END_CASE
END_IF




Top
   
PostPosted: Mon Mar 04, 2013 9:05 am 
Offline

Joined: Fri Oct 26, 2012 7:24 pm
Posts: 13
Thank you so much!

I will test it!


Top
   
PostPosted: Sat Mar 16, 2013 7:16 pm 
Offline

Joined: Sun Feb 24, 2013 10:47 am
Posts: 29
Hello - from a new user!
I have integrated the relevant statements from the "official" example for opening a file with the CAA File library. The statement "filop.eFileMode:=FILE.MRDWR" produces an error (in a German language system):
---------------------------------------
[FEHLER] TestTemplate: InitTheTest(Zeile 7, Spalte 1 (Impl)): C0032: Typ 'Unbekannter Typ: 'FILE.MRDWR'' kann nicht in Typ 'MODE' konvertiert werden
---------------------------------------
The tooltip on ".eFileMode" shows me:
------------------------------------------
VAR_INPUT Open.eFileMode : MODE
file mode: FILE_MWRITE - write, FILE_MREAD - read, FILE_MWRRD - read & write, FILE_MAP

------------------------------------------
When I use this file mode value "FILE_MWRRD, the same error - with this sentence - will be shown.

Where is my mistake - I have no idea?!

System: CODESYS V3.5 SP2 Patch 2 on a Windows 7 system.


Top
   
PostPosted: Sun Mar 17, 2013 9:15 am 
Offline
Frequent User
Frequent User
User avatar

Joined: Tue Jun 06, 2006 9:03 am
Posts: 403
The example is from a somewhat older library.
You should now use:
filop.eFileMode:=file.MODE.MRDWR;


Top
   
PostPosted: Sun Mar 17, 2013 6:00 pm 
Offline

Joined: Sun Feb 24, 2013 10:47 am
Posts: 29
Hello TinvH,

thank you - this is the solution that works! 3S has some problems with its online help maintenance.

With regards, Rudolf.


Top
   
PostPosted: Thu Nov 27, 2014 10:53 am 
Offline

Joined: Fri Oct 24, 2014 12:25 pm
Posts: 14
thank you so much it was helpful for me too :)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 11 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited