-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_com.py
64 lines (56 loc) · 1.43 KB
/
test_com.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
import serial as sr
import timeit
from time import sleep
ser = sr.Serial('/dev/ttyACM0', 9600, timeout=1, parity=sr.PARITY_EVEN, rtscts=1)
sleep(1)
on = True
i = 0
reads = 0
writes = 0
def setLightMessage(light, intensity):
return 'set|assi_' + light + '|' + str(intensity)
def getStatusMessage():
return 'get|status'
def sendMessage(msg):
global reads, writes
msgOut = str(len(msg)) + ':' + msg + ';'
# print('sending', msgOut)
ser.write(str.encode(msgOut))
ser.flush()
writes+=1
msgIn = str(ser.read_until(str.encode(';')))
# print('response is ', msgIn, ' for ', msg)
if "ACK" in msgIn and msg in msgIn:
reads+=1
return True, msgIn
else:
return False, msgIn
def testLight():
global i
global on
if ser.writable() and ser.out_waiting == 0:
i+=10
if on:
on = False
if not sendMessage(setLightMessage('blue', i)):
print("Error")
else:
on = True
if not sendMessage(setLightMessage('blue', 0)):
print("Error")
else:
print("Not writable, nothing waiting")
def testStatus():
if ser.writable() and ser.out_waiting == 0:
result, reading = sendMessage(getStatusMessage())
print('status request ', result, '---', reading)
else:
print("Not writable, nothing waiting")
start = timeit.default_timer ()
while(True):
testStatus()
testLight()
end = timeit.default_timer ()
elapsed = end-start
print(reads, 'reads and ', writes, 'writes in ',elapsed, 'seconds', round(reads/elapsed), 'Hz communication')
ser.close()