Python code locking with time (Running in Rasbian)

Asked

Viewed 767 times

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?

  • 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.

1 answer

0

Well, let’s split up. The main problem is that you are performing multiple file access at the same time, as well as serial ports.

Serial port identification can be performed outside the while loop as the port will not change.

As for the txt file, you don’t need to read all the lines when you just want to add a new content, you can open it in append mode, which adds new values at the end of the file.

An example of how the code would look with some modifications:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import serial
import time

locations=['/dev/ttyACM0']

# Identifica a porta serial e efetuando a conexao com o arduino
for device in locations:  
    try:  
         print "Trying...",device
         arduino = serial.Serial(device, 9600) 
         break
    except:  
        print "Failed to connect on",device
while 1:
    try:
        time.sleep(1)
        # note o modo a+
        arquivo = open('temperaturaumidade.txt','a+')
        valor = "%s\n"%(arduino.readline())
        print(valor)
        arquivo.write(valor)
    except:  
        print "Failed to write!" 
    arquivo.close() #Fecha o arquivo para que ele esteja disponivel para outros processos
  • It’s interesting to call arduino.close() at the end as well. And it is worth noting that in this way the connection with the serial port is open throughout the execution of the program. If another application needs this connection, it won’t (depending on the application, this may be necessary).

  • In this case you would have to open the connection inside the while as well. You can save the port name to a variable, so you don’t have to search the door every time. Unless another program is going to use the serial port, I don’t see why opening and closing the connection at all times, operating systems usually don’t handle it very well. So much so that in linux there is the possibility to share the port, just use the device with prefix "cu" instead of "tty", example /dev/cu.usbserial0 instead of /dev/tty.usbserial0 (you can check if it exists with a "ls /dev/ |grep cu".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.