Hi all,

I have been trying to create a Python program to connect to a serial port and read data from it. There will be a circuit sending the data through the port to PC and the Python program suppose to read and process the data. The data is continuous. The thing I did is something like below:

while (connecting_is_open):
[INDENT]read_the_data[/INDENT]

But this will result in an infinite loop as long as the circuit is sending the data. This is when I need user interrupt to break the loop; something like when the above code is running continuously and I want to stop it by user interrupt. How can I do that?

I say this coming from C++ so it may not be entirely right.

while(connecting_is_open && !user_input)

read_the_data

If the variable userinput is waiting for the user to press a key (like Heftiger implied), then you'd need to use threading in order to process waiting for a keystroke and running a continuous loop side-by-side. That being said, yes, you can use conditional testing each time the loop runs, or you can set it to infinite and then break out when your program wants, like so:

while True:  # or while 1:
    read_the_data  # do your thing here
    if end:  # some conditional to end the loop
        break  # will break out of the loop

Just watch out that your break isn't inside any other while or for loops inside the overall while loop, as it breaks out of the deepest-nested loop that it's in.

Just a note:
The easiest way to break out of the innermost loop of nested loops is to put them into a function and use return instead of break.

Thank you all for your nice suggestions. But I already settled the problem. What I did is developing a GUI using wxPython. I have two button events (to start receiving and to stop receiving). The moment the receiving button is pressed, a thread which open the port connection and data will be received. And if stop button is pressed anytime during the receiving process, the thread will be killed. Unfortunately, the moment the thread is killed, a sys.exit() is sent to main thread and the whole program exited. I am still trying to figure out how I can avoid that.

the moment the thread is killed, a sys.exit() is sent to main thread and the whole program exited. I am still trying to figure out how I can avoid that.

You can use multiprocessing for Python versions > 2.5, but you have to install it for 2.5 and below (named pyprocessing) http://developer.berlios.de/projects/pyprocessing
An example which kills the process after 5 seconds but lets the program continue to run. Note that this works the same if you are using an infinite while() loop or anything else.

import subprocess
import time
from processing import Process

class TestClass():
   def test_f(self, name):
      subprocess.Popen("ping www.google.com", shell=True)
         
if __name__ == '__main__':
    CT=TestClass()
    p = Process(target=CT.test_f, args=('P',))
    p.start()

    ## sleep for 5 seconds and terminate
    time.sleep(5.0)
    p.terminate()

    print "\n program is still running"

Hi all,

I have been trying to create a Python program to connect to a serial port and read data from it. There will be a circuit sending the data through the port to PC and the Python program suppose to read and process the data. The data is continuous. The thing I did is something like below:

while (connecting_is_open):
[INDENT]read_the_data[/INDENT]

But this will result in an infinite loop as long as the circuit is sending the data. This is when I need user interrupt to break the loop; something like when the above code is running continuously and I want to stop it by user interrupt. How can I do that?

Try:

while (connecting_is_open):
    read_the_data
    if(connecting_is_closed):
        break

break is such an ugly construct! Structure your while loops better. That way they are more natural to read and I don't have to spend a second or two figuring out "Why is he break-ing here?".

This article has been dead for over six months. Start a new discussion instead.