CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

FILE.READ repeats itself to fill the buffer
https://forum.codesys.com/viewtopic.php?f=11&t=7077
Page 1 of 1

Author:  Anas [ Tue Jul 19, 2016 2:52 pm ]
Post subject:  FILE.READ repeats itself to fill the buffer

Hello everyone :!:

have you ever encounered this problem below before :?:

when reading from large files (I am using FILE_ACCESS library), and the size of buffer cannot accomodate all data in the file, and consequently for getting all data from the file, I set the file pointer position (FILE.SetPos) to where it stopped (clearly multiples of buffer size) and read again and so on until end of file is reached (FILE.xEOF).

The very weird behaviour which I observe is that when using the function block (FILE.SetPos), and when the file is all read BUT there are still empty bytes in the buffer, then somehow the buffer is filled from the function (FILE.READ) from the end of the file all the way backward with the size of the empty bytes. plus that i never get ''FILE.xEOF'' TRUE :shock: .

Exmaple:

If file is composed of 100 bytes and i have buffer of size 80. I read the first 80 bytes and in the next cycle the position is set to 80 , and now, however, there are only 20 bytes left, it will read the residue 20 bytes and other last 60 other bytes in the file to fill the rest of the buffer. :?

I really appreciate any contributions for a solution.

Regards,

Anas

Author:  scott_cunningham [ Wed Jul 20, 2016 4:58 am ]
Post subject:  Re: FILE.READ repeats itself to fill the buffer

I think the file read is performing a byte copy from the hardware buffer for fast response. This means it does not first clear the answer to 0 bytes and then copy - it just copies over the old. So for you example, the answer was 80 bytes - and then you tell it to read again - so it copies 20 bytes and stops, leaving the rest of the answer filled with old data.

As a work around for not seeing an end of file, you could first read the file size and then determine how many times you must read at 80 bytes and what the leftover is (in your example 20). This is how I solved it once.

Author:  Anas [ Fri Jul 22, 2016 10:36 am ]
Post subject:  Re: FILE.READ repeats itself to fill the buffer

Hello scott cunningham!

That was genius !! Absolutely brilliant , it worked like magic :D :D

Thank you so much for your reply, super helpful !!


Best Regards,

Anas

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/