Controller fails when trying to write a file

When trying to write a small text file with a function block written in C#, the controller fails, and shows this error. What does it mean? Thanks. PLC Error

Hello, this means that your PLC did not manage to write the file within 100ms , exceeding your configured watchdog time.

This actually is a mechanism to protect the Applications as the runtime will go to stop if the watchdog is exceeded.
Take care not to programm blocking funktions if you use cyclic realtime tasks.

Have a look at the Threads section if you need to spawn long synchronous processes. https://github.com/PLCnext/CSharpExamples

Thank you Oliver. Yes, I created a low priority Thread and so far seems to be working.

Hi Oliver, So I was able to write a file using a thread but I was not able to pass parameters, I just set file name and data as constant. How do I pass the INPUTS and OUTPUTS to the ServerClass? For instance EXCUTE, DONE and DATA1? Do you have any example of this? Note, the code below is to read a file. Thanks using System; using System.IO; using System.Iec61131Lib; using Iec61131.Engineering.Prototypes.Types; using Iec61131.Engineering.Prototypes.Variables; using Iec61131.Engineering.Prototypes.Methods; using Iec61131.Engineering.Prototypes.Common; using System.Threading; using Eclr; namespace TestReadFile2 { [FunctionBlock] public class ReadMyFile { [InOut] public bool startThread; [Input] public bool EXECUTE; [Output] public bool DONE; [Output] public IecString80 DATA1; private bool ExecutePreviousState; // used for rising edge detection private const string TextFileName = “TextFile.txt”; //private const string BinaryFileName = “BinaryFile.bin”; [Initialization] public void __Init() { // // TODO: Initialize the variables of the function block here // } [Execution] public void __Process() { if (startThread) { // Create the thread object, passing in the // serverObject.StaticMethod method using a // ThreadStart delegate. Thread StaticCaller = new Thread( new ThreadStart(ServerClass.StaticMethod)); StaticCaller.Priority = ThreadPriority.Lowest; StaticCaller.Start(); startThread = false; } } } public class ServerClass { public static void StaticMethod() { if (EXECUTE && !ExecutePreviousState) { try { // read data from files //byte binaryData = File.ReadAllBytes(BinaryFileName); string textData = File.ReadAllText(TextFileName); // Put data to parameters // … for IecString DATA1.s.Init(textData); // … for binary data //Utils.CopyByteArrayToAny(binaryData, ref DATA2); DONE = true; return textData; } catch (Exception ex) { Console.WriteLine(“Error reading a file: {0}”, ex.ToString()); } } if (!EXECUTE) { DONE = false; } ExecutePreviousState = EXECUTE; } }

Hey Alex, as this is a static method you have no reference to your funktionblock class properties.

Best look it up at MSDN for complete explanations how to work with static classes ect. You will have to pass the variables or a reference of the FB to your Server class. something like this might work: StaticCaller.Start( Data ); … public static void StaticMethod( type yourData ){}…

kind regards,
Oliver

Thanks Oliver, Yes, need to understand more C#. My code is still not working but it’s hard to get feedback, so how do I see the output of the console? For instance from the code below. Thanks! catch (Exception ex)


| {
| Console.WriteLine(“Error reading a file: {0}”, ex.ToString());
| }