Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: Andrew S on June 22, 2019, 03:56:18 PM

Title: subroutines and async processes
Post by: Andrew S on June 22, 2019, 03:56:18 PM
I know you can't run async processes with subroutines, but I'm wondering if there's a way to create reusable code using programs.

I have a project that will run on multiple PLCS, and each PLC may have a varying number of inputs depending on requirements. I'm wanting to use FILELOG and MQTTPUB to log and send notifications when these inputs change state. I'm using stage programming to make this all work, and it works fine if I copy/paste my stages in a large program. Since the code is essentially identical for each input I'd really like to be able to code it once and reuse it but I don't know how to use 'parameters' to achieve my goal.

Am I thinking of this the right way?
Andrew.
Title: Re: subroutines and async processes
Post by: BobO on June 22, 2019, 04:26:49 PM
I know you can't run async processes with subroutines, but I'm wondering if there's a way to create reusable code using programs.

I have a project that will run on multiple PLCS, and each PLC may have a varying number of inputs depending on requirements. I'm wanting to use FILELOG and MQTTPUB to log and send notifications when these inputs change state. I'm using stage programming to make this all work, and it works fine if I copy/paste my stages in a large program. Since the code is essentially identical for each input I'd really like to be able to code it once and reuse it but I don't know how to use 'parameters' to achieve my goal.

Am I thinking of this the right way?
Andrew.

Absolutely. Reusability in a PLC is always challenging, but yes it is possible. The biggest issue would be that you might want to invoke the single resource from multiple places in the program. To handle that you'll need some form of lock/unlock where each 'client' locks access to the logging program, then writes the request to a global memory area, invokes the logging program, then frees it when complete.

An alternative approach might be that the logging program is always running and you use a FIFO to hand stuff to it. The logging app constantly checks the FIFO to see if there is any work to do, then pulls the request from the FIFO and processes. Conveniently, we just added a FIFO instruction.  ;)
Title: Re: subroutines and async processes
Post by: Controls Guy on June 22, 2019, 06:40:59 PM
Dang, I read your first paragraph and was composing a response, but it was your second paragraph!

Further, if you need prioritization beyond first-come-first-served, you could implement that either by the client routine entering a priority in  the request, or including the information that lets the 'subroutine' evaluate comparative request priorities.
Title: Re: subroutines and async processes
Post by: Andrew S on June 22, 2019, 08:50:26 PM
Thanks for the great feedback gentlemen. I've managed to get it working. On to the next challenge!