This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
279 lines (219 loc) · 7.7 KB
/
test.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import serial
import sys
import time
import json
import random
import threading
from multiprocessing import process
lock = threading.Lock()
#m- motion
#s- status
#o- output
class USB_serial(serial.Serial):
buf_m = []
buf_s = []
buf_o = []
def __init__(self):
sys.stdout.write("Test\n")
self.usb = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
print("Serial connection: ", self.usb.is_open)
print("Serial name: ", self.usb.name)
def __del__(self):
self.usb.close()
print("Serial comunication closed")
def send(self, data):
self.usb.write(data) #to musi byc stringS
def loopRecive(self):
while 1==1:
# lock.acquire(blocking=True, timeout=1)
print(threading.currentThread())
if self.usb.in_waiting != 0:
lock.acquire()
data = self.usb.readline()
lock.release()
first_char = data[:1].decode('utf-8')
print("check")
if first_char=='W' or first_char=='T' or first_char=='F':
self.buf_s.append(data)
elif first_char=='L' or first_char=='I' or first_char=='D':
self.buf_o.append(data)
elif first_char=='H' or first_char=='A' or first_char=='J' or first_char=='S':
self.buf_m.append(data)
elif first_char=='E':
self.buf_m.append(data)
print(data)
else:
print('Incorrect comand: ')
print(data.decode('utf-8'))
time.sleep(0.1)
class fileHandler:
# failed_tests = open('failed.txt', 'w')
# passed_tests = open('passed.txt', 'w')
def __init__(self):
global failed_tests
global passed_tests
def __del__(self):
self.failed_tests.close()
self.passed_tests.close()
def write_f(self, text):
print(text)
with open('failed.txt', 'a') as f:
f.write(text)
def write_p(self, text):
print(text)
with open('passed.txt', 'a') as f:
f.write(text)
def random_test(group, cmd, y):
x = random.randint(1,y)
name = ('tests/{}_{}_{}.json'.format(group, cmd, x))
print('Test name: {}'.format(name))
with open(name) as json_file:
data = json.load(json_file)
return data
def motion_test():
while 1==1:
# lock.acquire(blocking=True, timeout=10)
print(threading.currentThread())
cmd_list = ["H","A","J","S","Y"]
cmd = random.choice(cmd_list)
data = random_test('test_drive', cmd, 1)
test_list = data["CMDS"]
is_passed=0
for x in test_list:
if x["DIR"] == 'T':
cmd_to_send = x["CMD_TO_SEND"].encode('utf-8')
USB.send(cmd_to_send)
is_passed=1
elif x["DIR"] == 'R':
t = time.time()
while 1==1:
time.sleep(1)
print(t)
print(time.time())
if time.time() > t+(x["TIMEOUT"]/1000):
is_passed=0
break
if len(USB.buf_m) != 0:
answ = USB.buf_m.pop()
answ = answ.decode('utf-8')
if answ[1:] == x["CMD"] and answ[:2] == x["VAL"]:
is_passed = 1
print("OKEY")
break
else:
is_passed=0
print("NIEOKEY")
break
if is_passed==0: break
USB.buf_m.clear()
if is_passed==1:
FILE.write_p("{}==> PASSED\n".format(data['DESC']))
else:
FILE.write_f("{}==> FAILED\n".format(data['DESC']))
# lock.release()
time.sleep(0.1)
def state_test():
while 1==1:
# lock.acquire(blocking=True, timeout=10)
print(threading.currentThread())
cmd_list = ["W","T","F"]
cmd = random.choice(cmd_list)
data = random_test('test_state', cmd, 1)
test_list = data["CMDS"]
is_passed=0
for x in test_list:
if x["DIR"] == 'T':
cmd_to_send = x["CMD_TO_SEND"].encode('utf-8')
print(cmd_to_send)
USB.send(cmd_to_send)
is_passed=1
elif x["DIR"] == 'R':
t = time.time()
while 1==1:
time.sleep(1)
print(t)
print(time.time())
if time.time() > t+(x["TIMEOUT"]/1000):
is_passed=0
break
if len(USB.buf_s) != 0:
answ = USB.buf_s.pop()
answ = answ.decode('utf-8')
if answ[1:] == x["CMD"]:
is_passed = 1
print("OKEY")
break
else:
is_passed=0
print("NIEOKEY")
break
if is_passed==0: break
USB.buf_s.clear()
if is_passed==1:
FILE.write_p("{}==> PASSED\n".format(data['DESC']))
else:
FILE.write_f("{}==> FAILED\n".format(data['DESC']))
#lock.release()
time.sleep(0.1)
def output_test():
while 1==1:
# lock.acquire(blocking=True, timeout=10)
print(threading.currentThread())
cmd_list = ["L","I","R"]
cmd = random.choice(cmd_list)
data = random_test('test_out', cmd, 1)
test_list = data["CMDS"]
is_passed=0
for x in test_list:
if x["DIR"] == 'T':
cmd_to_send = x["CMD_TO_SEND"].encode('utf-8')
print(cmd_to_send)
USB.send(cmd_to_send)
is_passed=1
elif x["DIR"] == 'R':
t = time.time()
while 1==1:
time.sleep(1)
print(t)
print(time.time())
if time.time() > t+(x["TIMEOUT"]/1000):
is_passed=0
break
if len(USB.buf_o) != 0:
answ = USB.buf_o.pop()
answ = answ.decode('utf-8')
if answ[1:] == x["CMD"]:
is_passed = 1
print("OKEY")
break
else:
is_passed=0
print("NIEOKEY")
break
if is_passed==0: break
USB.buf_o.clear()
if is_passed==1:
FILE.write_p("{}==> PASSED\n".format(data['DESC']))
else:
FILE.write_f("{}==> FAILED\n".format(data['DESC']))
# lock.release()
time.sleep(0.1)
if __name__=='__main__':
USB = USB_serial()
FILE = fileHandler()
th1 = threading.Thread(target = USB.loopRecive)
# th1.daemon = True
th2 = threading.Thread(target = motion_test)
# th2.daemon = True
th3 = threading.Thread(target = state_test)
# th3.daemon = True
th1.start()
th2.start()
th3.start()
# th1.join()
# th2.join()
# print(data["BOUND"])
# y=json.dumps(data)
# print(y)
# USB.usb.write(b"start")
# print(usb_serial.name)