-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoisture.py
66 lines (47 loc) · 1.24 KB
/
moisture.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import time
import serial
import matplotlib.pyplot as plt
import datetime
# 與設備通訊
ser = serial.Serial(
port='COM3',
baudrate=1200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
# 確認是否連上設備
if (ser.isOpen() == 0):
ser.open()
state = ser.isOpen()
print('state: ' + str(state))
print('connection succeeded.')
# --------------Test--------------------
# ser.write('TEMP\r'.encode('utf-8'))
# temp = ser.read(5).decode('utf-8')
# ser.write('D05\r'.encode('utf-8'))
# out = ser.read(12).decode('utf-8')
# print(temp)
# print(out)
# --------------Test--------------------
sample_name = input("Enter the sample name: ")
t = int(input("Enter time in second: "))
# 倒數時間/模組
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
# 數據讀取/作圖/儲存
data = {}
filename = sample_name + '_' +str(t) + '_sec.txt'
with open(filename,'w') as f:
pass
ser.write('START\r'.encode('utf-8'))
while True:
recorder = open(filename,'a')
out = ser.read().decode('utf-8')
recorder.write(out)
recorder.close()