1
- #=====================================
1
+ # =====================================
2
2
3
3
# Function Definitions
4
4
5
- #=====================================
5
+ # =====================================
6
+
6
7
7
8
def waitForArduino (port ):
8
9
9
- # wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
10
- # it also ensures that any bytes left over from a previous message are discarded
11
-
10
+ # wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
11
+ # it also ensures that any bytes left over from a previous message are discarded
12
+
12
13
global startMarker , endMarker
13
-
14
+
14
15
msg = ""
15
16
while msg .find ("Arduino is ready" ) == - 1 :
16
- while ser [port ].inWaiting () == 0 :
17
- pass
18
- msg = recvFromArduino (port )
19
- print ("Arduino Number:" , port , "Is Ready" )
20
- print ("Message from Arduino:" + msg )
21
- #======================================
17
+ while ser [port ].inWaiting () == 0 :
18
+ pass
19
+ msg = recvFromArduino (port )
20
+ print ("Arduino Number:" , port , "Is Ready" )
21
+ print ("Message from Arduino:" + msg )
22
+
23
+
24
+ # ======================================
25
+
22
26
23
27
def recvFromArduino (port ):
24
- global startMarker , endMarker
25
-
26
- ck = ""
27
- x = "z" # any value that is not an end- or startMarker
28
- #byteCount = -1 # to allow for the fact that the last increment will be one too many
29
- x = ser [port ].read ()
30
- x = x .decode ("utf-8" )
31
- #print(x)
32
- # wait for the start character
33
- while ord (x ) != startMarker :
34
- x = ser [port ].read ()
35
- x = x .decode ("utf-8" )
36
- #print(x)
37
- # save data until the end marker is found
38
- while ord (x ) != endMarker :
39
- if ord (x ) != startMarker :
40
- ck = ck + x
41
- #byteCount += 1
28
+ global startMarker , endMarker
29
+
30
+ ck = ""
31
+ x = "z" # any value that is not an end- or startMarker
32
+ # byteCount = -1 # to allow for the fact that the last increment will be one too many
42
33
x = ser [port ].read ()
43
- x = x .decode ("utf-8" )
44
- #print(x)
45
- return (ck )
46
-
47
- #======================================
48
-
49
- def run (td ,port ):
50
- waitingForReply = False
51
-
52
- if waitingForReply == False :
53
- ser [port ].write (td .encode ())
54
- print ("-> -> -> -> -> ->" )
55
- print ("Thread: " + str (port ) + " started" )
56
- print ("Message Sent:" )
57
- print ("PC: " + td )
58
- waitingForReply = True
59
-
60
- if waitingForReply == True :
61
-
62
- while ser [port ].inWaiting () == 0 :
63
- pass
64
-
65
- dataRecvd = recvFromArduino (port )
66
- print ("<- <- <- <- <- <-" )
67
- print ("Thread: " + str (port ) + " complete" )
68
- print ("Message Received: " + dataRecvd )
34
+ x = x .decode ("utf-8" )
35
+ # print(x)
36
+ # wait for the start character
37
+ while ord (x ) != startMarker :
38
+ x = ser [port ].read ()
39
+ x = x .decode ("utf-8" )
40
+ # print(x)
41
+ # save data until the end marker is found
42
+ while ord (x ) != endMarker :
43
+ if ord (x ) != startMarker :
44
+ ck = ck + x
45
+ # byteCount += 1
46
+ x = ser [port ].read ()
47
+ x = x .decode ("utf-8" )
48
+ # print(x)
49
+ return ck
50
+
51
+
52
+ # ======================================
53
+
54
+
55
+ def run (td , port ):
69
56
waitingForReply = False
70
- time .sleep (.1 )
71
57
58
+ if waitingForReply == False :
59
+ ser [port ].write (td .encode ())
60
+ print ("-> -> -> -> -> ->" )
61
+ print ("Thread: " + str (port ) + " started" )
62
+ print ("Message Sent:" )
63
+ print ("PC: " + td )
64
+ waitingForReply = True
65
+
66
+ if waitingForReply == True :
67
+
68
+ while ser [port ].inWaiting () == 0 :
69
+ pass
70
+
71
+ dataRecvd = recvFromArduino (port )
72
+ print ("<- <- <- <- <- <-" )
73
+ print ("Thread: " + str (port ) + " complete" )
74
+ print ("Message Received: " + dataRecvd )
75
+ waitingForReply = False
76
+ time .sleep (0.1 )
72
77
73
- #======================================
74
- #======================================
78
+
79
+ # ======================================
80
+ # ======================================
75
81
76
82
import serial
77
83
import time
@@ -91,35 +97,35 @@ def run(td,port):
91
97
threads = [None ] * NUMBER_OF_SLAVES
92
98
93
99
for x in range (len (serPort )):
94
- ser [x ] = serial .Serial (serPort [x ], baudRate )
95
- print ("Serial port " + serPort [x ] + " opened" )
100
+ ser [x ] = serial .Serial (serPort [x ], baudRate )
101
+ print ("Serial port " + serPort [x ] + " opened" )
96
102
97
103
print ("" )
98
104
99
105
for port in range (len (serPort )):
100
- waitForArduino (port )
106
+ waitForArduino (port )
101
107
102
- while 1 :
103
- print ("===========" )
104
- print ("" )
108
+ while 1 :
109
+ print ("===========" )
110
+ print ("" )
105
111
text = input ("Enter Commands: " )
106
- parse_text = text .split (';' )
107
- print (parse_text )
108
-
112
+ parse_text = text .split (";" )
113
+ print (parse_text )
114
+
109
115
# create threads
110
116
for x in range (len (parse_text )):
111
- threads [x ] = Thread (target = run , args = (parse_text [x ],x ))
117
+ threads [x ] = Thread (target = run , args = (parse_text [x ], x ))
112
118
113
119
# start threads
114
120
for x in range (len (parse_text )):
115
- threads [x ].start ()
121
+ threads [x ].start ()
116
122
117
123
# wait for threads to finish
118
124
for x in range (len (parse_text )):
119
- threads [x ].join ()
125
+ threads [x ].join ()
126
+
120
127
121
-
122
128
# close all serial connections
123
129
for x in range (len (serPort )):
124
- print ("Serial port " + serPort [x ] + " closed" )
125
- ser [x ].close
130
+ print ("Serial port " + serPort [x ] + " closed" )
131
+ ser [x ].close
0 commit comments