-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharduinoCom.py
More file actions
executable file
·123 lines (88 loc) · 2.81 KB
/
arduinoCom.py
File metadata and controls
executable file
·123 lines (88 loc) · 2.81 KB
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
import time
import serial
import serial.tools.list_ports
# global variables for module
startMarker = 60
endMarker = 62
startTime = 0
def valToArduino(emoStateTuple):
sendStr = "%s,%s,%s, %s, %s,%s,%s, %s, %s,%s,%s, %s" % emoStateTuple
print "SENDSTR %s" % (sendStr)
sendToArduino(sendStr)
# ========================
def valToArduinoSimple(emoState, state):
sendStr = "9;%s;" % (emoState)
print "================"
print "%s" % (state)
print "================"
sendToArduinoLeap(sendStr)
# ========================
def setupSerial():
global ser, startTime
# NOTE the user must ensure that the serial port and baudrate are correct
print "==================================================================="
# Find live ports
ports = list(serial.tools.list_ports.comports())
for p in ports:
print p
for index in range(0, 20):
str1 = "COM%d" % (index)
if str1 in p[1]:
serPort = str1
index += 1
baudRate = 9600
ser = serial.Serial(serPort, baudRate)
print "Serial port " + serPort + " opened Baudrate " + str(baudRate)
time.sleep(2);
# ========================
def closeSerial():
global ser
if 'ser' in globals():
ser.close()
print "Serial Port Closed"
else:
print "Serial Port Not Opened"
# ========================
def sendToArduino(sendStr):
global startMarker, endMarker, ser
ser.write(chr(startMarker))
ser.write(sendStr)
ser.write(chr(endMarker))
# ===========================
def sendToArduinoLeap(sendStr):
ser.write(sendStr)
waitForArduino()
print sendStr
# ===========================
def recvFromArduino(timeOut): # timeout in seconds eg 1.5
global startMarker, endMarker, ser
dataBuf = ""
x = "z" # any value that is not an end- or startMarker
byteCount = -1
# to allow for the fact that the last increment will be one too many
startTime = time.time()
# ~ print "Start %s" %(startTime)
# wait for the start marker
while ord(x) != startMarker:
if time.time() - startTime >= timeOut:
return('<<')
x = ser.read()
# save data until the end marker is found
while ord(x) != endMarker:
if time.time() - startTime >= timeOut:
return('>>')
if ord(x) != startMarker:
dataBuf = dataBuf + x
x = ser.read()
return(dataBuf)
# ============================
def waitForArduino():
# wait until the Arduino sends'Arduino Ready'-allows time for Arduino reset
# it also ensures that any bytes left over from a previous message are
# discarded
print "Waiting for Arduino to reset"
msg = ""
while msg.find("Arduino is ready") == -1:
msg = recvFromArduino(10)
print msg
print