回答編集履歴
2
test
CHANGED
@@ -17,6 +17,8 @@
|
|
17
17
|
|
18
18
|
def read(self):
|
19
19
|
if self.nth >= self.N:
|
20
|
+
if (self.ser.isOpen()):
|
21
|
+
self.ser.close()
|
20
22
|
return
|
21
23
|
global root, la
|
22
24
|
current_time = datetime.now()
|
@@ -43,6 +45,5 @@
|
|
43
45
|
serial_data = SerialData()
|
44
46
|
root.after(10, serial_data.read)
|
45
47
|
root.mainloop()
|
46
|
-
ser.close()
|
47
48
|
```
|
48
49
|
|
1
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
`r
|
1
|
+
`tkinter.Tk.after()` を利用して、`0.01`秒毎にシリアルポートからデータを読み込む方法が考えられます。(表示にはラベルではなく、`scrolledtext.ScrolledText` の方がよいかもしれません)
|
2
2
|
```python
|
3
3
|
import serial
|
4
4
|
import struct
|
@@ -6,6 +6,31 @@
|
|
6
6
|
import tkinter
|
7
7
|
from datetime import datetime
|
8
8
|
|
9
|
+
class SerialData:
|
10
|
+
def __init__(self):
|
11
|
+
self.measuretime = ['' for i in range(1, 6100)]
|
12
|
+
self.STATE = ['' for i in range(1, 6100)]
|
13
|
+
self.send4 = 'AAC81205112233445566778855'
|
14
|
+
self.ser = serial.Serial('COM3', 19200)
|
15
|
+
self.nth = 0
|
16
|
+
self.N = 60
|
17
|
+
|
18
|
+
def read(self):
|
19
|
+
if self.nth >= self.N:
|
20
|
+
return
|
21
|
+
global root, la
|
22
|
+
current_time = datetime.now()
|
23
|
+
self.measuretime[self.nth] = current_time.strftime("%Y/%m/%d %H:%M:%S.%f")[:-3]
|
24
|
+
self.ser.write(self.send4.encode())
|
25
|
+
recv4 = self.ser.read(13)
|
26
|
+
unrecv4 = struct.unpack('sssssssssssss', recv4)
|
27
|
+
state1 = (unrecv4[8])[-1] # 2進数化
|
28
|
+
state2 = bin(state1) # 2進数化
|
29
|
+
# update label text
|
30
|
+
la['text'] += state2[2:6] + '\n'
|
31
|
+
self.nth += 1
|
32
|
+
root.after(10, self.read)
|
33
|
+
|
9
34
|
# 画面作成
|
10
35
|
root = tkinter.Tk()
|
11
36
|
root.geometry('400x450') # 横x縦
|
@@ -14,27 +39,10 @@
|
|
14
39
|
la.place(x=100, y=350)
|
15
40
|
la.pack()
|
16
41
|
|
17
|
-
# read from serial port
|
42
|
+
# read data from serial port
|
18
|
-
measuretime = ['' for i in range(1, 6100)]
|
19
|
-
STATE = ['' for i in range(1, 6100)]
|
20
|
-
send4 = 'AAC81205112233445566778855'
|
21
|
-
ser = serial.Serial('COM3', 19200)
|
22
|
-
for k in range(60):
|
23
|
-
|
43
|
+
serial_data = SerialData()
|
24
|
-
measuretime[k] = current_time.strftime("%Y/%m/%d %H:%M:%S.%f")[:-3]
|
25
|
-
ser.write(send4.encode())
|
26
|
-
recv4 = ser.read(13)
|
27
|
-
unrecv4 = struct.unpack('sssssssssssss', recv4)
|
28
|
-
state1 = (unrecv4[8])[-1] # 2進数化
|
29
|
-
state2 = bin(state1) # 2進数化
|
30
|
-
# update label text
|
31
|
-
la['text'] += state2[2:6] + '\n'
|
32
|
-
# refresh
|
33
|
-
|
44
|
+
root.after(10, serial_data.read)
|
34
|
-
|
45
|
+
root.mainloop()
|
35
|
-
time.sleep(0.01)
|
36
46
|
ser.close()
|
37
|
-
|
38
|
-
root.mainloop()
|
39
47
|
```
|
40
48
|
|