0

I am using Python 2.6 to communicate with a device on a serial port. I want to make sure the device is still active every 10 seconds. The best way I've found to do this is to start a thread that pings the device every so often (other methods, to be designed later, will send configuration commands out the serial port, thus the global). Below is my code inspired from here.

When I enter "quit", the ConnectionThread seemingly only loses the 10 second timer and continuously spams the PING message. What is causing this, and how do I just end the entire thread?

import serial
import threading

ser = 0

class ConnectionThread(threading.Thread):
    def __init__(self, event, **kwargs):
        threading.Thread.__init__(self)
        self.stopped = event
        for key, value in kwargs.items():
            setattr(self, key, value)

    def run(self):
        while not self.stopped.wait(10):
            ser.write("{0},{1}\r".format(self.name,"PING"))

def main():           
    global ser
    commport = input("Enter COM port:")

    try:            
        ser = serial.Serial(
                            port = 'COM{0}'.format(commport),
                            baudrate = 9600
                            )
    except:
        print "Unable to open com port!"

    stopFlag = threading.Event()
    thread = ConnectionThread(stopFlag)
    thread.start()  

    while True:
        n = raw_input("Enter input:")
        if n == "quit":
            stopFlag.set()
            break

if __name__ == "__main__":
    main()
Community
  • 1
  • 1
  • so before you "quit", it works fine but after, it just "PINGS" infinitely? – Daniel Aug 06 '14 at 18:29
  • Correct. After "quit", the ping message gets sent continuously with no 10 second delay. – Oilyraincloud Aug 06 '14 at 18:30
  • @Oilyraincloud Does this still happen if you remove the serial port piece of this, and just `print` something in that loop? I can't reproduce it that way from my machine. – dano Aug 06 '14 at 18:53
  • @dano Yes, it does still happen. I was suspicious of the serial port causing some issues, but it appears to not be the problem. The case is the same if I just `print` to the console. – Oilyraincloud Aug 06 '14 at 18:58
  • 3
    Which version of Python are you using? Prior to 2.7 Event.wait() always returned None. With python 2.7 on my system it works fine. – Jonathan Villemaire-Krajden Aug 06 '14 at 19:03
  • 1
    I'm with @JonathanVillemaire-Krajden on this. With Python 2.7 I can't reproduce, but with 2.6 I can. Maybe add a `if self.stopped.isSet(): return` inside the `while` loop and see if the problem goes away? – dano Aug 06 '14 at 19:05
  • You can `print sys.version` from inside the program to confirm which version of Python is actually running. – Robᵩ Aug 06 '14 at 19:06
  • Ah yes, it is 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]. I'll edit my post so that it reflects the proper version. Also, @dano, that solves my problem. – Oilyraincloud Aug 06 '14 at 19:12
  • My version confusion came from using a grammar version of 2.7 in PyDev. I assumed the interpreter version would be the same. What a noob. – Oilyraincloud Aug 06 '14 at 19:19

1 Answers1

1

Because you're using Python 2.6, Event.wait() always returns None. You'll need to explicilty check the state of the Event inside your while loop:

while not self.stopped.wait(10):
    if self.stopped.isSet():
        return
    ser.write("{0},{1}\r".format(self.name,"PING"))
dano
  • 91,354
  • 19
  • 222
  • 219