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
fo = open('/etc/scripts/.arduino.db', 'wb')
try:
serial = serial.Serial('/dev/ttyACM0', 9600) serial = serial.Serial('/dev/ttyACM0', 9600)
serial.write('1') serial.write('1')
query = serial.readline().strip('\r\n').split() query = serial.readline().strip('\r\n').split()
fo = open('/etc/scripts/.arduino.db', 'wb') 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
try:
if mode[1] == 'temp': if mode[1] == 'temp':
print_arduino(0) print_arduino(0)
elif mode[1] == 'humid': elif mode[1] == 'humid':
print_arduino(1) print_arduino(1)
elif mode[1] == 'query': elif mode[1] == 'query':
query_arduino() query_arduino()
else: except IndexError:
print('Usage: script.py [temp] [humid]') print('Usage: script.py [temp] [humid]')