2
I made a code in python, but it hangs after a while running, someone can help me optimize it to not crash?
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import serial
import time
locations=['/dev/ttyACM0']
while 1:
arquivo = open('temperaturaumidade.txt','r')
conteudo = arquivo.readlines()
for device in locations:
try:
print "Trying...",device
arduino = serial.Serial(device, 9600)
break
except:
print "Failed to connect on",device
try:
time.sleep(1)
conteudo.append(arduino.readline())
print arduino.readline()
conteudo.append('\n')
arquivo = openarquivo = open('temperaturaumidade.txt','w')
arquivo.writelines(conteudo)
except:
print "Failed to read!"
arquivo.close()
To be able to optimize your code, you need to tell how it should work. The connection with Arduino is unique, that is, while your application runs, no other will be able to connect to the board? What is the pattern of messages sent by Arduino, can you put his code too? You make twice the call to
arduino.readline()
, one to store in file, another to display in terminal. Is that correct? Arduino sends messages in duplicate form?– Woss
Make sure the crash is not happening at the time of opening the file. add a Try execption also at the file opening. and also if he couldn’t open it will have nothing to close too.
– Leandro Ribeiro