Skip to content

Error for writing to file with C# function block

Dear people,

When I create a streamwriter to create a text file within a C# function block the PLC program will fail. Does anyone know why this is? The code is below here. In the engineering environment there comes an error with "eClr exception unhandled"

PS. System.IO is included.

Kind regards,

Bart Minten

Capture

Comments

  • Hello Bart,


    does the folder you want to create the file in Exist?
    Also for debuging Try to catch the file exception.

    Aso have a look at the Output.log file for more info.

    I will try to reproduce it in a bit.

    You can use the Outputlog as well for your application.

    using eclr;
    Log.Info("Something{0}",var);

    kind regards,
    Oliver

  • Hello Bart,

    perhaps you can try reading the contents of the file first. And then you can implement the StreamWriter similarly.

    Please see the Code below, I'm not testing it, but it should work:

     

    StreamReader streamReader;

    string path = "/opt/temp/MyTestString.txt"

    if(File.Exists(path))
    {
    try
    {
    streamReader = new StreamReader(path);
    }
    catch (Exception e)
    {
    string ErrorMessage = e.Message;
    }

    string content = "";
    while (!streamReader.EndOfStream)
    {
     content += streamReader.ReadLine();
    }
    streamReader.Close();
    streamReader.Dispose();

    }

    Best Regards

    Eduard

  • First I will check the code that you just sent. This message below was before that.

     

    Dear Oliver,

    It seems like the streamwriter can not find the path (/opt/temp/MyText.txt) that already exists on the PLCnext. Do you know why this is? Below here I will put the output.log of the last messages from where the error occurred.

    Kind regards, 

    Bart Minten

    Capture1

  • Dear Oilver,

    This if statement  |

                                v

    string path = "/opt/temp/MyText.txt"

    if(File.Exists(path))

     

    returned a false, but the path exists in the PLCnext.

     

    Has it maybe something to do with the permissions of the PLC program? Is the PLC program allowed to do this kind of things in the file directory of the PLCnext?

     

    Kind regards,

    Bart Minten

  • Hello Bart,

    maybe the permissions on the folder are the problem here?

    Check with "ls -la" that the plcnext_firmware user has read and write access to that folder

    chown admin:plcnext /opt/YourFolder
    or
    plcnext_firmware:plcnext

    Also "using" does make sure the Stream/File is disposed even on an Exception. but it does not Catch the exception
    Still I would recommend to handle all the IO Exceptions that might occure with Try Catch.
    that way you can asure that your RealTime application is not impacted by the FBs execution or errors...

    so
    try{using(..){

    }}
    catch(...){...}


    kind regards,
    Oliver

Sign In or Register to comment.