前提・実現したいこと
python3.7で、マルチスレッドを使い電圧計を作成しています(arduino側より値出力)。
[測定開始]ボタンでデータの取り込みを開始し、[測定終了]で、データのサンプリングを
終了させたいと考えています。
発生している問題・エラーメッセージ
発生している問題・エラーメッセージ
最初の1回目は問題なく動作します。
再起動させたとき「カーネル停止しました。再起動中」と表示され、
カーネル再起動後は通常に動作します。
該当のソースコード
ソースコード
python3.7
1import tkinter as tk 2import datetime 3import serial 4import time 5import threading 6 7ser = serial.Serial("COM6",9600) 8 9def serial_data_recv(): 10 global stop_flag 11 global thread 12 stop_flag = True 13 14 num = 0 15 16 # 通信最初の50要素から読み(通信のゴミを読み飛ばす) 17 print("通信バッファクリア中……") 18 for i in range(50): 19 ser.readline().rstrip() #\r\n削除 20 21 print("測定開始") 22 23 f_now = datetime.datetime.now() #現在時刻取得 24 # ファイル名を時刻.csvに設定 25 recv_data = f_now.strftime('%Y%m%d_%H%M') + ".csv" 26 with open(recv_data,'w') as f: #ファイルオープン 27 while True: 28 if stop_flag == False: 29 break 30 else: 31 line = ser.readline().rstrip() #\r\n削除 32 s_line = str(line) #文字列化 33 w_line = s_line.strip("b'") #受信文字から"b、'"を削除 34 35 fl_line = float(w_line) 36 SEN_OUT = (fl_line * 5.0)/1023 # 0-5vに変換 37 SEN_OUT2 = round(SEN_OUT,2) 38 now = datetime.datetime.now() #現在時刻取得 39 40 #ファイルフォーマットを整えて書き込み 41 file_time0 = now.strftime('%H') 42 f.write(str(file_time0)) 43 f.write(":") 44 file_time1 = now.strftime('%M') 45 f.write(str(file_time1)) 46 file_time2 = now.strftime('%S') 47 f.write(":") 48 f.write(str(file_time2)) 49 f.write(",") 50 f.write(str(SEN_OUT2)) #受信データ書き込み 51 f.write(",") 52 f.write("V") 53 f.write(",\n") #","と改行挿入 54 55 print(" " + (str(SEN_OUT2)) + " V") 56 num += 1 57 58 f.close() 59 ser.close() 60 print("測定終了") 61 time.sleep(5) 62 root.destroy() 63 64def stop(): 65 global stop_flag 66 global thread 67 stop_flag = False 68 69 70root = tk.Tk() 71root.title("検査 静電容量") 72root.geometry("250x220+1000+10") 73 74thread_1 = threading.Thread(target = serial_data_recv) 75thread_2 = threading.Thread(target = stop) 76 77button1 = tk.Button(root, text = "測定開始", font=("MSゴシック", 14), 78 command = thread_1.start) 79button1.place(x = 80, y = 30) 80 81button2 = tk.Button(root, text = "測定終了", font=("MSゴシック", 14), 82 command = thread_2.start) 83button2.place(x = 80, y = 70) 84 85 86#label01 = tk.Label(root, text = "測定回数入力", font=("MSゴシック", 14)) 87#label01.place(x = 63, y = 70) 88 89#e_box01 = tk.Entry(width = 4, font = ("MSゴシック", 20)) 90#e_box01.place(x = 100, y = 100) 91 92#r_box = tk.Text(root, font = ("MSゴシック", 20)) 93#r_box = tk.Text(root) 94#r_box.place(x = 63, y = 150, width = 100, height = 30) 95 96root.mainloop() 97
試したこと
前回のエラーメッセージ対策で色々といじっていたら、こうなってしまった。
## ここにより詳細な情報を記載してください。
開発環境 spyder3 windows10(64ビット)
回答1件
あなたの回答
tips
プレビュー