/
write_raw.py
130 lines (111 loc) · 4.6 KB
/
write_raw.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from serial import Serial
from queue import Empty
import numpy as np
import s_mar_c
import mat_c
import time
def write(name_file, start_nm, end_nm, speed_nm, com, q_bar, q_str, q_stop, mode, direct):
print(direct)
# np.save(direct + '\\' + name_file, [1, 2, 3, 4, 5])
speed_nm1 = speed_nm
old_l = 0
stop = False
q_str.put('Scan starting')
speed_nm = speed_nm * 1.0625
s = Serial(com, baudrate = 500000, timeout = 1) ### Настройка сериал порта
time.sleep(4)
###############################################
time_scan = ((end_nm - start_nm) / speed_nm) * 60 ### Расчетное время сканирования
print(time_scan)
s.read_all() ### Считываем все из порта
time.sleep(4)
s.write(b"s") ### Отправка команды микрокотроллеру для старта
# started = s.readline().strip()
q_str.put('Get ready')
while True:
started = s.readline().strip()
print("started")
if started.endswith(b"started"):
print("started")
break
try:
stop = q_stop.get_nowait()
if stop == True:
print('Stop')
break
except Empty as end_scan:
print(end_scan)
############################################### Проверка ответной команды от микроконтроллера о страте
# if not started.endswith(b"started"):
# print(started)
# s.close()
# raise RuntimeError("Couldn't start reading!")
# else:
# print("Started")
###############################################
cur = b""
a = bytearray()
q_str.put('Scaning')
start_scan = time.time()
end_scan = 0
if stop != True:
while True:
try:
end_scan = time.time()
if end_scan-start_scan >= time_scan:
break
l = (int(end_scan-start_scan) * 100) / time_scan
if l != old_l:
q_bar.put(l)
cur = s.read(70)
a.extend(cur)
old_l = l
stop = q_stop.get_nowait()
if stop == True:
print('Stop')
break
except Empty as end_scan:
print(end_scan)
in_buf = s.in_waiting
print(in_buf)
cur = s.read(in_buf)
a.extend(cur)
print(s.in_waiting)
s.write(b"e") ### Отправка команды микрокотроллеру для завершения
stopped = s.readline().strip()
s.close()
############################################### Проверка ответной команды от микроконтроллера о заерщении
if stop != True:
if not stopped.endswith(b"stopped"):
print("Uncorrect stop!")
else:
print("Succesfully stopped.")
print("Elapsed", end_scan - start_scan)
q_str.put('Scan end, start correct')
print('Scan end, start correct')
############################################### Проверка на плохие(поломанные) данные, если нашли то "выбрасываем"
bytes_ = [0b00100010, 0b00100000, 0b00100100]
d = bytearray()
i = 0
while i < len(a):
pack = a[i:i+7]
if len(pack) == 7:
c = pack[0] in bytes_
if c == True:
d.extend(pack)
else:
while not a[i] in bytes_:
i+= 1
i+= 7
dtype = np.dtype([('signals','uint8'),('value', 'uint16'),('time','uint32')])
data = np.frombuffer(d, dtype)
diff = np.diff(data['time'])
ind = np.where(diff < np.median(diff)*2)
data = data[ind]
np.save(direct + '//' + name_file, data)
time.sleep(1)
q_str.put('Mathematical processing')
if mode == 'Dual beam mode':
mat_c.mat_calculations(start_nm, speed_nm1, name_file, q_str, direct)
if mode == 'Single beam mode':
s_mar_c.mat_calculations(start_nm, speed_nm1, name_file, q_str, direct)