3

I am not an experienced programmer, I have a problem with my code, I think it's a logical mistake of mine but I couldn't find an answer at http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/whilestatements.html . What I want is to check if the serial device is locked, and the different between conditions that "it is locked" and "it isn't locked" is that there are 4 commas ,,,, in the line which contains GPGGA letters. So I want my code to start if there isn't ,,,, but I guess my loop is wrong. Any suggestions will be appreciated. Thanks in advance.

import serial
import time
import subprocess


file = open("/home/pi/allofthedatacollected.csv", "w") #"w" will be "a" later
file.write('\n')
while True:
    ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
    checking = ser.readline();
    if checking.find(",,,,"):
        print "not locked yet"
        True
    else:
        False    
        print "locked and loaded"

. . .

Tolga Varol
  • 1,036
  • 1
  • 16
  • 21

2 Answers2

7

Use break to exit a loop:

while True:
    ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
    checking = ser.readline();
    if checking.find(",,,,"):
        print "not locked yet"
    else:
        print "locked and loaded"
        break

The True and False line didn't do anything in your code; they are just referencing the built-in boolean values without assigning them anywhere.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
3

You can use a variable as condition for your while loop instead of just while True. That way you can change the condition.

So instead of having this code:

while True:
    ...
    if ...:
        True
    else:
        False    

... try this:

keepGoing = True
while keepGoing:
    ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
    checking = ser.readline();
    if checking.find(",,,,"):
        print "not locked yet"
        keepGoing = True
    else:
        keepGoing = False    
        print "locked and loaded"

EDIT:

Or as another answerer suggests, you can just break out of the loop :)

Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
  • Thank you for your answer, I couldn't think of assigning them to a variable :) – Tolga Varol Aug 25 '14 at 11:00
  • 1
    @TolgaVarol: Using `break` is far less error prone and straightforward, however. – Martijn Pieters Aug 25 '14 at 11:11
  • @MartijnPieters You might be right and your solution is simpler and thank you again for that, but I asked the question as True or False, so if someone look at this post and if their problem is about only True False conditions his answer would be better for them. – Tolga Varol Aug 25 '14 at 11:14
  • @TolgaVarol: I disagree with that; the correct way to solve the problem you are trying to solve is to use `break`. I tried to both answer why your code didn't work and how to do it correctly; both are important. – Martijn Pieters Aug 25 '14 at 11:20
  • This answer worked for me because I have an inner `for` loop, so assigning a variable to the outer `while` statement enabled me to exit both when the `for` loop succeeded. – Erich Feb 14 '21 at 21:35