How to Save a csv file on SD Card

InTheBox
2012-03-23
2016-08-26
  • InTheBox - 2012-03-23

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

    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

     
  • KiranK - 2012-03-23

    InTheBox hat geschrieben:
    Hi, I need to work with csv files and save them on SD-Card.
    In "CoDeSys -> Help -> SD_WRITE" section there is this note:
    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

     
  • InTheBox - 2012-03-23

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

    No, I'm not.

    KiranK hat geschrieben:
    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.

     
  • KiranK - 2012-03-23

    InTheBox hat geschrieben:
    No, I'm not.
    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

    WriteReadFile.pro [32.43 KiB]

     
  • swtroelofs - 2012-03-27

    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/'

     
  • InTheBox - 2012-03-27

    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.

     
  • Anonymous - 2016-08-26

    Originally created by: barkamharikrishna

    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);
    
     

Log in to post a comment.