catch the serial exception and force zeroes

This commit is contained in:
deflax 2018-08-23 11:45:12 +03:00 committed by GitHub
parent c433d24b9b
commit 887591113e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,10 +7,13 @@ from sys import argv
def query_arduino(): def query_arduino():
global serial global serial
serial = serial.Serial('/dev/ttyACM0', 9600)
serial.write('1')
query = serial.readline().strip('\r\n').split()
fo = open('/etc/scripts/.arduino.db', 'wb') fo = open('/etc/scripts/.arduino.db', 'wb')
try:
serial = serial.Serial('/dev/ttyACM0', 9600)
serial.write('1')
query = serial.readline().strip('\r\n').split()
except:
query = ("0", "0")
fo.write(','.join(query)) fo.write(','.join(query))
fo.close() fo.close()
@ -22,12 +25,12 @@ def print_arduino(pmode):
if __name__ == "__main__": if __name__ == "__main__":
mode = argv mode = argv
if mode[1] == 'temp': try:
print_arduino(0) if mode[1] == 'temp':
elif mode[1] == 'humid': print_arduino(0)
print_arduino(1) elif mode[1] == 'humid':
elif mode[1] == 'query': print_arduino(1)
query_arduino() elif mode[1] == 'query':
else: query_arduino()
except IndexError:
print('Usage: script.py [temp] [humid]') print('Usage: script.py [temp] [humid]')