Good luck on finding the culprit.
The PLC firmware behaves as if everything is a single thread. So there is no real mechanism to allow custom functions to have a life of their own.
About all you can do is ensure that your custom functions execute quickly and evoke them carefully (only when needed).
I never poll for an external event in a custom function nor waste time using Delay statements. I, typically, implement this type of code using a state machine using the PLC Sequencer logic and custom functions to handle what can't be done in Ladder logic.
I've got a big system that uses external A/D converter connected to the PLC via RS-485. This RS-485 protocol is similar to ModBus in that a command is sent to request data from the A/D converter. If I don't get a response from the A/D converter or I get a crappy response, then I set a RELAY to indicate that the A/D converter has failed. This same RELAY is used by the Ladder logic to block any future calls to the A/D read custom function.
I divide big time consuming custom functions up into smaller pieces. I use RELAYS set by each custom function to start the next custom function on the following scan cycle. This is typical:
- The custom function to get raw A/D data is invoked once a minute. If there is a timeout or crappy data is returned, this function sets one or more RELAY(s) to indicate the failure.
- On the next cycle another custom function converts the A/D data from an ASCII string to several scaled integer values.
- On the next cycle a custom function validates the integer values. If the values are wacky, I RELAY(s) are set to indicate that either the failure. The system has fall back modes to keep things safe in the event of sensor failures.
- On the next scan cycle I manage active control of heaters, chillers, fans and lighting based on the validated A/D values.
- On the last scan a custom function updates a LCD display.
This may sound like a lot of work, and it is. However it allows me to do a lot of computing without causing the PLC to become non-responsive to rapidly changing inputs.
Have a good weekend,
Gary D