Page 1 of 1

RPi crashes after python script

Posted: Thu Aug 30, 2012 5:29 pm
by Simoff
Hi everyone,
I use a Python script to read Data from an device at a usb port and write the data to an Mysql database.
first it seemed to run fine, but after executing the script for several times my pi crashes and does not respond to anything (no video output, no ssh, no anything)
i tried to put everything in try constructs to catch a failure and prevent crashing but it had no effect.
any ideas?!?
oh by the way i use a fresh wheezy version

thanks
Simoff

Re: RPi crashes after python script

Posted: Fri Aug 31, 2012 5:32 am
by timhoffman
Can you post a small concise sample of the code your running that causes the crash ?
Without this it's going to be hard to help.

Re: RPi crashes after python script

Posted: Fri Aug 31, 2012 10:16 am
by Simoff
of course but I dont think its a problem of the script. when it crashes the "Out : " line is printed and no fail is displayed

Code: Select all

###############################
#Imports
###############################

import serial
import MySQLdb

##############################
#Open database & build cursor
##############################

conn = MySQLdb.connect (
	host = "localhost",
	user = 'root',
	passwd = "here was the password",
	db = "DB1")

cursor = conn.cursor()




###############################
#Create Serial
###############################

ser = serial.Serial(
	port = '/dev/ttyUSB0',
	baudrate=19200,
	timeout=10)



##############################
#begin communication
##############################

try:
	ser.write('c')

	data = ser.read(2)
except:
	print 'Fail0'



if data == '01':

	ser.write('e')

	data = ser.read(15)

	if len(data) == 15:
		
		write1 = "INSERT INTO table1(date, data1, data2) VALUES (NOW(),"+data[1:8]+", 3)"
		write2 = "INSERT INTO table2(date, data1, data2) VALUES (NOW(),"+data[8:14]+", 4)"

		try:
			cursor.execute(write1)
			conn.commit()
		except:
			print 'Fail1'
			conn.rollback()

		try:
			cursor.execute(write2)
			conn.commit()
		except:
			print 'Fail2'
			conn.rollback()

try:
	ser.write('d')

	data = ser.read(2)

	if len(data) > 0:
		print 'Out: ',data

except:
	print 'Fail3'

############################
#close connections & ports
############################

try:
	ser.close()
	cursor.close()
	conn.close()

except: 
	print 'last Fail'

Re: RPi crashes after python script

Posted: Fri Aug 31, 2012 11:09 am
by BlackJack
@Simoff: Can you rule out power problems? Maybe the database access → SD card activity and the external USB device is too much for the power supply‽ My system becomes unstable if I have SD card activity, ethernet connection, wireless USB adapter, and a Tinkerforge master brick with just a temperature sensor plugged in. Power supply is a phone charger with 1A. An external powered USB hub solved this problem.

Your exception handling is quite bad by the way. You replace informative tracebacks by ``print`` saying barely more than something has failed. And then move on as if there where no problem. Which may lead to other problems which cause the program to stop with an uncaught exception anyway. If there's an exception raised in the first ``try`` block for instance you print "Fail0" and then the program will inevitably stop with a `NameError` because `data` is not defined. Without the ``try``/``except`` you would know a) if the write or the read failed and b) most likely a message giving a clue *why* it failed.

A bare ``except`` without any concrete exception(s) is a „code smell”. If don't know how to handle an exception don't catch it. Printing something like "error" or "failed" and then just go on isn't adequate error handling in most cases. If you do that, you have to be able to explain why it is okay to go on despite the error.

Re: RPi crashes after python script

Posted: Fri Aug 31, 2012 12:43 pm
by Simoff
I can rule out the power problem, I also use my device on a powered USB Hub and i use a USB charger with 1,5A.

to the code:
how you can see on my code im new on python. :)
first i did not had that kind of try constructs (only at writing the mysql data, where i did a rollback at an exception), after the system crashs i just added them to see if there are any kind of errors accuring - but there werent - even when cthe system crashed

i think its some kind of python issue, because everything is done before crashing (writing the data to the mysql database and displaying the data)

Re: RPi crashes after python script

Posted: Fri Aug 31, 2012 1:06 pm
by BlackJack
@Simoff: If there would have been an exception you would have seen the program stopping with a traceback without the additional ``try``/``except`` blocks.

I can not image how Python crashes the Raspi.

I would try to execute just the DB code and just the serial communication code to narrow down the problem. And maybe the whole program via the `trace` module from the standard library to get an idea which lines definitely will get executed and which might hang the system.

Re: RPi crashes after python script

Posted: Fri Aug 31, 2012 1:47 pm
by Simoff
i will try that.... and let you know what i found out

Re: RPi crashes after python script

Posted: Wed Sep 05, 2012 2:33 pm
by Simoff
ok, i now tried a routine only with the serial part (and writing to a file) - and the pi crashes.
here the code:

Code: Select all

###############################
#Imports
###############################

import serial
import MySQLdb
from time import *


###############################
#Create Serial & build variables
###############################

ser = serial.Serial(
	port = '/dev/ttyUSB0',
	baudrate=19200,
#	parity = serial.PARITY_EVEN,
	writeTimeout = 2,
	timeout=5)


data = "0"
file = open("rec.txt", "a")

##############################
#begin communication
#############################

date_str =  strftime("%d.%m.%Y  %H:%M:%S",localtime())
print >> file, "__________________________"
print >> file, date_str


ser.write('c')

buff = ser.read(2)


if buff == 'ok':
	print >> file, "config ok"	
	
	ser.write('e')


	data = ser.read(15)


else:
	print >> file, "failure in config"

print >> file, "Data: "+data+" with length: "+str(len(data))

ser.write('d')

buff = ser.read(2)

if buff == 'ok':
	print >> file, "closing ok"

else:
	print >> file, "failure at closing"

print >> file, ""
file.close()

############################
#close connections & ports
############################

ser.flush()
ser.flushInput()
ser.flushOutput()
# flushs were just added to prevent some kind of overflow

ser.close()
any idea? its driving me crazy

Re: RPi crashes after python script

Posted: Wed Sep 05, 2012 2:54 pm
by bulldog5046
Are you printing alot of data to screen?

python crashes my Pi too if i print all of the servo updates to screen...

Re: RPi crashes after python script

Posted: Wed Sep 05, 2012 2:59 pm
by Simoff
no, but how to see in the code above i write into a .txt file or on top into a MySQL database. Is there an issue with pythons output?

Re: RPi crashes after python script

Posted: Wed Sep 05, 2012 3:08 pm
by BlackJack
Neither printing to standard output connected to a terminal nor writing into a regular file should crash the Raspi. Regardless of the programming language. Python has no problem writing to files. What could be done wrong with that? I don't think this is a Python problem. It may be a problem with the serial module, but certainly not one of the Python implementation.

Re: RPi crashes after python script

Posted: Thu Sep 06, 2012 8:15 am
by Simoff
damn, why didnt i rule that one out further. i will try by using a pc instead of the serial device and check it.

Re: RPi crashes after python script

Posted: Sun Sep 09, 2012 6:50 pm
by Simoff
ok - it was the device. connected to a computer the python script ran the whole weekend without any crash. i searched the whole time for an not existing error *facepalm*


and edit: it seems to be the internal ftdi chip wich provides the problem - just found this here in the forum *double-facepalm*