Python スクリプトを使用して、3 つのファイルの内容を別の 3 つのファイルに転送しています。元のファイルは、raspian を実行している RPI に接続した 3 つの温度計からのデータです。スクリプトが行うことになっているのは、ファイルの内容を取得して移動し、別のプログラム (ComScript) がそれらを読み取って解析できるようにすることだけです。
私の問題は、スクリプトが開始する前に 1 つまたは複数の温度計が切断されると、スクリプトがフリーズすることです。スクリプトの実行中に温度計を外してもフリーズしません。
ここにコードがあります
import time
a = 1
while a == 1:
try:
tfile = open("/sys/bus/w1/devices/28-000004d2ca5e/w1_slave")
text = tfile.read()
tfile.close()
temperature = text
tfile2 = open("/sys/bus/w1/devices/28-000004d2fb20/w1_slave")
text2 = tfile2.read()
tfile2.close()
temperature2 = text2
tfile3 = open("/sys/bus/w1/devices/28-000004d30568/w1_slave")
text3 = tfile3.read()
tfile3.close()
temperature3 = text3
textfile = open("/home/pi/ComScriptPi/profiles/Temperature_parse/w1_slave1", "w ")
textfile2 = open("/home/pi/ComScriptPi/profiles/Temperature_parse/w1_slave2", "w ")
textfile3 = open("/home/pi/ComScriptPi/profiles/Temperature_parse/w1_slave3", "w ")
temperature = str(temperature)
temperature2 = str(temperature2)
temperature3 = str(temperature3)
textfile.write(temperature)
textfile2.write(temperature2)
textfile3.write(temperature3)
textfile.close()
textfile2.close()
textfile3.close()
print temperature
print temperature2
print temperature3
time.sleep(3)
except:
pass
例外パスを追加したのは、値が悪くても実行し続ける必要があるためです。温度計の1つが切断されると、Pythonが読み取ろうとしているファイルは空白ですが、まだそこにあります。