-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance_bh3.py
83 lines (66 loc) · 2.03 KB
/
balance_bh3.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import time
import serial
import matplotlib.pyplot as plt
import datetime
# 與設備通訊
ser = serial.Serial(
port='COM5',
baudrate=600,
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 ------------------
# sample_name = input("Enter the sample name: ")
# filename = sample_name + '_sec.txt'
# with open(filename,'w') as f:
# f.write("program created by Walter!\n")
# f.close()
# while True:
# recorder = open(filename,'a')
# # ser.write('%RW\r'.encode('utf-8'))
# out = ser.read(15).decode('utf-8')
# data = out.replace(" ", "").replace("g","").replace("+","")
# recorder.write(data)
# print(data)
# # ---------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 + '.txt'
with open(filename,'w') as f:
f.write("program created by Walter!\n")
f.close()
while True:
recorder = open(filename,'a')
# countdown(t)
# ser.write('D05\r'.encode('utf-8'))
out = ser.read(15).decode('utf-8')
# print(f"{datetime.datetime.now()} {out}")
out_new = out.replace(" ", "").replace("g","").replace("+","") #移除+, 空格與文字g
recorder.write(f"{datetime.datetime.now()} {out_new}")
# recorder.write(out_new)
recorder.close()
# data.append(out_new)
data.append(float(out_new))
plt.plot(data, marker=".", linestyle="-")
plt.draw()
plt.pause(0.0001)
plt.savefig(filename.replace('txt', 'png'))
plt.clf()
# Reminder: restart the balance before taking any measurement in case the conflict from previous data