Project Description

I am interfacing industrial PLC's with the Raspberry Pi 3 in order to read the efficiency of the given line, and display live results. In essence I need to do the following things:

  1. Get notified when the the line is running. A simple input to the Raspberry Pi will do. However, I need to continuously note the time since the PLC was turned on, presumably using time.
  2. While keeping track of how much time has passed, other inputs to the RPi must be detected. These may represent parts that the machine produces. For the sake of simplicity, let us say that this is a button press.
  3. I then need to perform calculations every 10 seconds based on the number of parts produced and the time it took for these to be produced.


In this example I am using a button press (GPIO.input(17)) to simulate when the line will start. Next, I am using another button (GPIO.input(27)) to simulate the number of parts produced by the machine.

import time

GPIO.setup(17, GPIO.IN)
GPIO.setup(27, GPIO.IN)

timer_a = time.time()

    if GPIO.input(27) == 0:  #machine starts working
       flag_work = 1
       while flag_work == 1:
           if time.time() - timer_a > 10
               timer_a = time.time()
               # perform calculations here every 10 seconds

    if GPIO.input(17) == 0:
       parts_produced += 1
       sleep(0.2)            # debouncing purposes

Problem Description

As can be seen from the above example, the program execution remains inside while loop since the flag remains set to 1. I am looking for a way to keep track of the time that has passed while reading up to a potentially large number of inputs per second. Which is the best way to these both simultaneously?

Should multi threading, interrupts or callbacks be used in such a case?

Any suggestions on how to approach such a problem would be appreciated.

  • The Pi is irrelevant to the question. The potential solutions will be the same whichever box you choose to use. – joan Jul 24 '18 at 10:04
  • You have 4 cores, use them... multithreading makes the processing and acquisition independent and efficient. – Mark Setchell Jul 24 '18 at 11:43
  • Thanks for the comments. So in such a case as above, multi-threading is preferred over multiprocessing? – rrz0 Jul 25 '18 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.