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

How to Save a csv file on SD Card
https://forum.codesys.com/viewtopic.php?f=2&t=4827
Page 1 of 1

Author:  InTheBox [ Fri Mar 23, 2012 12:46 pm ]
Post subject:  How to Save a csv file on SD Card

Hi, I need to work with csv files and save them on SD-Card.
In "CoDeSys -> Help -> SD_WRITE" section there is this note:

Access to the SD Card is only possible by using the function blocks SD_WRITE and SD_READ.

Actually I found examples that don't seem to use this block.
If it is possible to write on SD-Cards just using SysFileOpen - SysFileWrite - SysFileClose, how can I know the "partition" on which the plc "mounts" the card? I tried many combinations of full (e.g.: "C:\...\..\MyFile.csv") and relative (e.g.: "\MyFile.csv", "MyFile.csv"") paths with no results.

Where am I wrong?
Thank you very much.

Alessandro

Author:  KiranK [ Fri Mar 23, 2012 1:47 pm ]
Post subject:  Re: How to Save a csv file on SD Card

InTheBox wrote:
Hi, I need to work with csv files and save them on SD-Card.
In "CoDeSys -> Help -> SD_WRITE" section there is this note:

Access to the SD Card is only possible by using the function blocks SD_WRITE and SD_READ.

Actually I found examples that don't seem to use this block.
If it is possible to write on SD-Cards just using SysFileOpen - SysFileWrite - SysFileClose, how can I know the "partition" on which the plc "mounts" the card? I tried many combinations of full (e.g.: "C:\...\..\MyFile.csv") and relative (e.g.: "\MyFile.csv", "MyFile.csv"") paths with no results.

Where am I wrong?
Thank you very much.

Alessandro


Hi,
My first Question:
Are u trying to run this in simulation mode at ur PC ?
If yes then it is not possible .
If NO, then I think you your path which u have specfied (e.g.: "C:\...\..\MyFile.csv") and relative (e.g.: "\MyFile.csv", "MyFile.csv"") is wrong.
You should mention in this way '\data\MyFile.csv'
data is the folder in which u have kept ur file
In your case kindly mention the name of the file in which the file is kept i have just given an example
Hope this helps :)
regards

Author:  InTheBox [ Fri Mar 23, 2012 2:24 pm ]
Post subject:  Re: How to Save a csv file on SD Card

KiranK wrote:
Are u trying to run this in simulation mode at ur PC ?

No, I'm not.

KiranK wrote:
If NO, then I think you your path which u have specfied (e.g.: "C:\...\..\MyFile.csv") and relative (e.g.: "\MyFile.csv", "MyFile.csv"") is wrong.

Is the path "MyFile.csv" wrong too? Isn't it supposed to be in the current directory?
"SysFileOpen" should create the file if it doesn't exist.
This function returns a file number > 0, but even after writing and closing the file, I'm not able to find it. I tried with the "filedir" command too, with no results.
If my SD-Card contains a directory called "\Data\" (sub-directory of root), is it enough to indicate "\Data\MyFile.csv" as path?
I did this way but in the card there is no trace of the file, although the functions return no errors.

Thank you.

Author:  KiranK [ Fri Mar 23, 2012 3:52 pm ]
Post subject:  Re: How to Save a csv file on SD Card

InTheBox wrote:
KiranK wrote:
Are u trying to run this in simulation mode at ur PC ?

No, I'm not.

KiranK wrote:
If NO, then I think you your path which u have specfied (e.g.: "C:\...\..\MyFile.csv") and relative (e.g.: "\MyFile.csv", "MyFile.csv"") is wrong.

Is the path "MyFile.csv" wrong too? Isn't it supposed to be in the current directory?
"SysFileOpen" should create the file if it doesn't exist.
This function returns a file number > 0, but even after writing and closing the file, I'm not able to find it. I tried with the "filedir" command too, with no results.
If my SD-Card contains a directory called "\Data\" (sub-directory of root), is it enough to indicate "\Data\MyFile.csv" as path?
I did this way but in the card there is no trace of the file, although the functions return no errors.

Thank you.


Hi,
As per my exp when the file SysFileOpen will not create a new file but it will return a value(u are right)
This will only open the file.
Path what u have specfied '\Data\MyFile.csv' is correct(u should use single quotes '')
Now i belive u are expecting that the system should read and creat a file then NO
it will just read the file and you have to specfy another file in the same directory say '\Data\MyFile1.csv' and use write function kindly refer the attched sample plc code for read/write functions.
I hope this should work
an tel me what happens ok
Regards

Author:  swtroelofs [ Tue Mar 27, 2012 9:39 am ]
Post subject:  Re: How to Save a csv file on SD Card

I have a PLC with a linux os. When I write a file to an USB stick I use the following path:
'/media/usb0/MyFile.csv'

Also note that linux is CaSe sensitive. I once lost half a day because i used: '/Media/usb0/' :?

Author:  InTheBox [ Tue Mar 27, 2012 9:58 am ]
Post subject:  Re: How to Save a csv file on SD Card

Thank you for the answers.
I just discovered that my ABB PLC doesn't give complete conformity to some standard CoDeSys libraries involved with hardware (SD-card handling in my case).
I'm gonna switch to ABB proprietary libraries.

Bye.

Author:  barkamharikrishna [ Fri Aug 26, 2016 6:45 am ]
Post subject:  Re: How to Save a csv file on SD Card

Please see belwo code to create csv file and write data to csv file

VAR
s_filePath : STRING := '/opt/CoDeSysControl/log/'; (*Path to save .csv file in to the ROM of controller *)
END_VAR



t_handle := SysFileOpen(szFile := s_filePath,am := AM_APPEND_PLUS,pResult := ADR(t_error));
S_WriteBuffer := ('');
S_WriteBuffer := concat(S_WriteBuffer,WORD_TO_STRING(u16_ErrNum));
S_WriteBuffer := concat(S_WriteBuffer,'-');
S_WriteBuffer := concat(S_WriteBuffer, BYTE_TO_STRING(u8_ErrCode));
S_WriteBuffer := concat(S_WriteBuffer,'-');
S_WriteBuffer := concat(S_WriteBuffer, BYTE_TO_STRING(u8_ErrClass));
S_WriteBuffer := concat(S_WriteBuffer,'#');

S_WriteBuffer := concat(S_WriteBuffer,'$n');
SysFileWrite(hFile := t_handle,pbyBuffer := ADR(S_WriteBuffer),ulSize := len(S_WriteBuffer),pResult := ADR(t_error));
SysFileClose(hFile := t_handle);

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