3

I am creating a unit that will do a number of things, one of them counting cycles of a machine. While I will be transferring this over to ladder logic (CoDeSys), I am putting my ideas into Python first.

I will have a count running, with just a simple

    counter += 1
    print("counter")

to keep track of what cycle I'm on. However, I want to be able to reset this count at any time, preferably by typing "RESET" I understand how to use the input command,

    check = input()

however, I do not know how to let the program run while it is searching for an input, or whether or not this is possible at all. Thank you in advance for any answer.

If it helps to understand, here is the code. The big gap is where the problem is. http://pastebin.com/TZDsa4U4

1
  • I don't think I understand your problem completely but if check=="RESET": global counter ;counter=0 Commented Jun 24, 2015 at 20:52

1 Answer 1

2

If you only want to signal a reset of the counter, you can catch KeyboardInterrupt exception.

while True:
    counter = 0
    try:
        while True:
            counter += 1
            print("counter")
    except KeyboardInterrupt:
        pass
Sign up to request clarification or add additional context in comments.

2 Comments

I see that KeyboardInterrupt only checks for Ctrl+C and Delete. What about other inputs?
@RishavKundu: that's why I said if he only wanted to signal a reset. He could take user input in the exception handler, but it depends if he wants his other code running at the same time. Then he'll need threads and mutexes and all that jazz.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.