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

Commit 6cacdad

Browse files
Updating Pi Code to handle multiple Arduinos
- Still need to implement listening. Right now the code just opens and closes the ports needed to communicate with the boards.
1 parent 072e043 commit 6cacdad

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

raspberry_pi_controller/contoller.py

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,51 @@
44

55
#=====================================
66

7-
def sendToArduino(sendStr):
8-
ser.write(sendStr.encode())
9-
7+
def waitForArduino(port):
108

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+
12+
global startMarker, endMarker
13+
14+
msg = ""
15+
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)
1121
#======================================
1222

13-
def recvFromArduino():
23+
def recvFromArduino(port):
1424
global startMarker, endMarker
1525

1626
ck = ""
1727
x = "z" # any value that is not an end- or startMarker
1828
#byteCount = -1 # to allow for the fact that the last increment will be one too many
19-
x = ser.read()
29+
x = ser[port].read()
2030
x = x.decode("utf-8")
2131
#print(x)
2232
# wait for the start character
2333
while ord(x) != startMarker:
24-
x = ser.read()
34+
x = ser[port].read()
2535
x = x.decode("utf-8")
2636
#print(x)
2737
# save data until the end marker is found
2838
while ord(x) != endMarker:
2939
if ord(x) != startMarker:
3040
ck = ck + x
3141
#byteCount += 1
32-
x = ser.read()
42+
x = ser[port].read()
3343
x = x.decode("utf-8")
3444
#print(x)
3545
return(ck)
3646

47+
#=====================================
3748

38-
#============================
39-
40-
def waitForArduino():
41-
42-
# wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
43-
# it also ensures that any bytes left over from a previous message are discarded
44-
45-
global startMarker, endMarker
46-
47-
msg = ""
48-
while msg.find("Arduino is ready") == -1:
49-
50-
while ser.inWaiting() == 0:
51-
pass
52-
53-
msg = recvFromArduino()
49+
def sendToArduino(sendStr):
50+
ser.write(sendStr.encode())
5451

55-
print (msg)
56-
print ("")
57-
5852
#======================================
5953

6054
def runTest(td):
@@ -87,27 +81,37 @@ def runTest(td):
8781
import time
8882
import sys
8983

90-
print ("")
91-
print ("")
92-
93-
serPort = "/dev/cu.SLAB_USBtoUART"
94-
#serPort = "/dev/ttyUSB0"
95-
baudRate = 9600
96-
ser = serial.Serial(serPort, baudRate)
97-
print ("Serial port " + serPort + " opened Baudrate " + str(baudRate))
9884

85+
NUMBER_OF_SLAVES = 2
9986
startMarker = 60
10087
endMarker = 62
88+
baudRate = 9600
89+
serPort = ["/dev/cu.usbmodem1412101", "/dev/cu.usbmodem1412201"]
90+
#serPort = "/dev/cu.SLAB_USBtoUART"
91+
#serPort = "/dev/ttyUSB0"
92+
93+
ser = [None] * NUMBER_OF_SLAVES
94+
95+
for x in range(len(serPort)):
96+
ser[x] = serial.Serial(serPort[x], baudRate)
97+
# print(ser[x])
98+
print ("Serial port " + serPort[x] + " opened")
99+
100+
# Better Printout
101+
print("")
101102

102-
waitForArduino()
103+
for port in range(len(serPort)):
104+
waitForArduino(port)
103105

104-
while 1 :
105-
print ("===========")
106-
print ("")
107-
text = input("Up or Down?: ")
108-
text = "<" + text + ">"
109-
runTest(text)
110-
time.sleep(1)
106+
#while 1 :
107+
# print ("===========")
108+
# print ("")
109+
# text = input("Up or Down?: ")
110+
# text = "<" + text + ">"
111+
# runTest(text)
112+
# time.sleep(1)
111113

112-
ser.close
114+
for x in range(len(serPort)):
115+
print ("Serial port " + serPort[x] + " closed")
116+
ser[x].close
113117

0 commit comments

Comments
 (0)