Multiple tasks using a shared resource

RSmith
2020-05-05
2020-05-13
  • RSmith - 2020-05-05

    I'm using a single RS232 comms POU to send commands to multiple devices (all syringe pumps). The first pump is connected directly to the PLC via RS232. That pump can then forward commands to other dasiy chained pumps over RS485. The different pumps are all operating independently.

    The problem is the RS232 POU can be called from multiple tasks. What is the best way of locking the POU so only one task can send a command at a time? I'd want the other tasks to wait until the comms is free before continuing their process.

     
  • Quirzo - 2020-05-13

    If your other tasks can wait, I would simply create a variable like "alreadySending" and when it's TRUE, other tasks should wait.When it's FALSE, the next who is the fastest would set it to true again and send the command.

    If that won't work, you would need to create a queue system that takes all given commands in and handles them one-by-one. Then when the command is sent, the command would be taken from the queue and then the calling tasks would be notified somehow.

     
  • RSmith - 2020-05-13

    Thanks quirzo. I was concerned that if two or more tasks are waiting, then they would both try to access the RS232 task on the same scan cycle when it became available. I wouldn't know which command was actually sent.

    Instead I created a queue as an array where each task writes to it's own index in the array. If a bit is set then I know one or more commands are in the array and work through them one-by-one. The order the commands are sent doesn't matter to me so I don't need FIFO. Priority is always given to the arrayindex[0].

     
    πŸ‘
    1

    Last edit: RSmith 2020-05-13

Log in to post a comment.