2018/04/18 16:20 ソースコード更新しました
前提・実現したいこと
python3とkivyを使って、シリアルポートから受信したデータを解析したものをGUI上に表示・一定時間毎に更新させたいのですが、上手くいかず困っております。
発生している問題
シリアル受信~解析~コンソールに表示させるプログラムと、kivyを用いてGUIの枠を作成するプログラムを別々に作成することは出来たのですが、kivyのmainloopにシリアル受信~部分のプログラムを入れる方法が分からないといったような状況です。
以下のソースコードを実行すると取り合えずエラーは出ず、GUIの表示部分のみが実行され、GUIのウィンドウを閉じると解析部分が始まるといった動作です。
ご指導の程、何卒宜しくお願い致します。
該当のソースコード
上がpyファイル、下がkvファイルです。
python
1import serial 2import binascii 3import datetime 4import struct 5 6from kivy.app import App 7from kivy.uix.tabbedpanel import TabbedPanel 8from kivy.properties import StringProperty 9from kivy.core.text import LabelBase, DEFAULT_FONT 10from kivy.clock import Clock 11from kivy.event import EventDispatcher 12 13LabelBase.register(DEFAULT_FONT,"meiryo.ttc") 14 15class List(): 16 17 s = serial.Serial('COM5',9600) 18 19 list = [] 20 XB_list = [] 21 BT_list = [] 22 IO_list = [] 23 ML_list = [] 24 25 xbee = 0 26 btid = 0 27 reed = 0 28 ser = 0 29 time = 0 30 31 print("start") 32 33 def __init__(self): 34 pass 35 36 def receive_time(self,dt): 37 time = datetime.datetime.now() 38 self.time = (time.strftime('%Y年%m月%d日 %H:%M:%S')) 39 print(self.time) 40 41 def first_check(self): 42 self.list = [] 43 num = 0 44 print("check") 45 while num <= 2: 46 ser = self.s.read() 47 data = binascii.b2a_hex(ser) 48 dec = int(data,16) 49 self.list.append(dec) 50 num += 1 51 52 def length_read(self): 53 length = self.list[2] 54 55 if length == 35: 56 self.ser = self.s.read(length+1) 57 58 else: 59 print("read error") 60 pass 61 62 def sample(self): 63 xbee = self.ser[2:9] 64 btid = self.ser[18:30] 65 reed = self.ser[33:35] 66 67 self.xbee = str(binascii.hexlify(xbee), 'utf-8') 68 self.btid = btid.decode() 69 self.reed = reed.decode() 70 71 def add_list(self): 72 if((self.xbee in self.XB_list) == False): 73 self.XB_list.append(self.xbee) 74 75 else: 76 pass 77 78 if((self.btid in self.BT_list) == False): 79 self.BT_list.append(self.btid) 80 self.IO_list.append(self.reed) 81 self.ML_list.append(0) 82 83 elif((self.btid in self.BT_list) == True): 84 no = self.BT_list.index(self.btid) 85 self.IO_list[no] = self.reed 86 87class Mado(TabbedPanel): 88 print("window") 89 pass 90 91class TabbedPanelApp(App): 92 time = StringProperty() 93 94 def __init__(self, lst): 95 super().__init__() 96 self.lst = lst 97 98 def build(self): 99 self.title = 'test' 100 Clock.schedule_interval(lst.receive_time,5.0) 101 print("build") 102 return Mado() 103 104lst = List() 105tpa = TabbedPanelApp(lst) 106 107print("print") 108 109if __name__ =='__main__': 110 tpa.run() 111 112print("main2") 113while True: 114 lst.first_check() 115 lst.length_read() 116 lst.sample() 117 lst.add_list() 118 lst.receive_time() 119 print(lst.XB_list) 120 print(lst.BT_list) 121 print(lst.IO_list) 122 123
kviy
1# -*- coding: Shift-JIS -* 2#:kivy 1.10.0 3 4<PaddingBoxLayout@BoxLayout>: 5 padding: 10 6 7<Mado>: 8 size_hint: 1, 1 9 pos_hint: {'center_x': .5, 'center_y': .5} 10 do_default_tab: False 11 12 TabbedPanelItem: 13 text: 'XB_List' 14 Label: 15 text: 'XB ID 1' 16 17 TabbedPanelItem: 18 text: 'BT_List' 19 GridLayout: 20 cols: 2 21 rows: 2 22 Label: 23 text: 'BT ID 1' 24 Button: 25 text: 'BT ID 2' 26 27 TabbedPanelItem: 28 text: 'IO_List' 29 Label: 30 text: 'IO ' 31 32 TabbedPanelItem: 33 text: 'Time' 34 Label: 35 text: str(app.time) 36 37
また、StringPropertyを用いて5秒ごとに現在の時刻をTabbedPanel上に表示、更新させようとしていますが、参照している場所に問題があるのでしょう。これも上手くいきません。
頂いた回答により一部解決しました
AttributeError: 'Window' object has no attribute 'time'
補足情報
[Python] v3.6.3(32bit)
[kivy] v1.10.0
回答2件
あなたの回答
tips
プレビュー