Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 97de165

Browse files
Python Formatting
1 parent 2bd5778 commit 97de165

File tree

1 file changed

+80
-74
lines changed

1 file changed

+80
-74
lines changed
Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,83 @@
1-
#=====================================
1+
# =====================================
22

33
# Function Definitions
44

5-
#=====================================
5+
# =====================================
6+
67

78
def waitForArduino(port):
89

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+
1213
global startMarker, endMarker
13-
14+
1415
msg = ""
1516
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+
2226

2327
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
4233
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):
6956
waitingForReply = False
70-
time.sleep(.1)
7157

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)
7277

73-
#======================================
74-
#======================================
78+
79+
# ======================================
80+
# ======================================
7581

7682
import serial
7783
import time
@@ -91,35 +97,35 @@ def run(td,port):
9197
threads = [None] * NUMBER_OF_SLAVES
9298

9399
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")
96102

97103
print("")
98104

99105
for port in range(len(serPort)):
100-
waitForArduino(port)
106+
waitForArduino(port)
101107

102-
while 1 :
103-
print ("===========")
104-
print ("")
108+
while 1:
109+
print("===========")
110+
print("")
105111
text = input("Enter Commands: ")
106-
parse_text = text.split(';')
107-
print (parse_text)
108-
112+
parse_text = text.split(";")
113+
print(parse_text)
114+
109115
# create threads
110116
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))
112118

113119
# start threads
114120
for x in range(len(parse_text)):
115-
threads[x].start()
121+
threads[x].start()
116122

117123
# wait for threads to finish
118124
for x in range(len(parse_text)):
119-
threads[x].join()
125+
threads[x].join()
126+
120127

121-
122128
# close all serial connections
123129
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

Comments
 (0)