News:

  • September 28, 2026, 01:29:01 PM

Login with username, password and session length

Author Topic: subroutines and async processes  (Read 8875 times)

Andrew S

  • Hero Member
  • *****
  • Posts: 144
subroutines and async processes
« 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.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6176
  • Yes Pinky, Do-more will control the world!
Re: subroutines and async processes
« Reply #1 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.  ;)
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3626
  • Darth Ladder
Re: subroutines and async processes
« Reply #2 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.
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

Andrew S

  • Hero Member
  • *****
  • Posts: 144
Re: subroutines and async processes
« Reply #3 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!