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

Commit 9b690a8

Browse files
Got Multiboard Control Working!
Working multiboard control issues: - Code needs to be cleaned up - needs to be formatted
1 parent 6cacdad commit 9b690a8

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

raspberry_pi_controller/contoller.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,32 @@ def recvFromArduino(port):
4545
return(ck)
4646

4747
#=====================================
48+
# get rid of this function
4849

49-
def sendToArduino(sendStr):
50-
ser.write(sendStr.encode())
50+
def sendToArduino(sendStr, port):
51+
ser[port].write(sendStr.encode())
5152

5253
#======================================
5354

54-
def runTest(td):
55+
def run(td,port):
5556
waitingForReply = False
5657

5758
if waitingForReply == False:
58-
sendToArduino(td)
59+
sendToArduino(td, port)
5960
print ("-> -> -> -> -> ->")
6061
print ("Message Sent:")
6162
print ("PC: " + td)
6263
waitingForReply = True
6364

6465
if waitingForReply == True:
6566

66-
while ser.inWaiting() == 0:
67+
while ser[port].inWaiting() == 0:
6768
pass
6869

69-
dataRecvd = recvFromArduino()
70+
dataRecvd = recvFromArduino(port)
7071
print ("<- <- <- <- <- <-")
7172
print ("Message Received: " + dataRecvd)
7273
waitingForReply = False
73-
7474
time.sleep(.1)
7575

7676

@@ -80,38 +80,52 @@ def runTest(td):
8080
import serial
8181
import time
8282
import sys
83+
from threading import Thread
8384

84-
85+
# global variables
8586
NUMBER_OF_SLAVES = 2
8687
startMarker = 60
8788
endMarker = 62
8889
baudRate = 9600
90+
# alternative serPort = "/dev/ttyUSB0"
8991
serPort = ["/dev/cu.usbmodem1412101", "/dev/cu.usbmodem1412201"]
90-
#serPort = "/dev/cu.SLAB_USBtoUART"
91-
#serPort = "/dev/ttyUSB0"
9292

93+
# initialize serial variable array
9394
ser = [None] * NUMBER_OF_SLAVES
95+
threads = [None] * NUMBER_OF_SLAVES
9496

9597
for x in range(len(serPort)):
9698
ser[x] = serial.Serial(serPort[x], baudRate)
97-
# print(ser[x])
9899
print ("Serial port " + serPort[x] + " opened")
99100

100-
# Better Printout
101101
print("")
102102

103103
for port in range(len(serPort)):
104104
waitForArduino(port)
105105

106-
#while 1 :
107-
# print ("===========")
108-
# print ("")
109-
# text = input("Up or Down?: ")
110-
# text = "<" + text + ">"
111-
# runTest(text)
112-
# time.sleep(1)
106+
while 1 :
107+
print ("===========")
108+
print ("")
109+
text = input("Enter Commands: ")
110+
parse_text = text.split(';')
111+
print (parse_text)
112+
113+
# create threads
114+
for x in range(len(parse_text)):
115+
print (parse_text[x])
116+
print (x)
117+
threads[x] = Thread(target=run, args=(parse_text[x],x))
118+
119+
# start threads
120+
for x in range(len(parse_text)):
121+
threads[x].start()
113122

123+
# wait for threads to finish
124+
for x in range(len(parse_text)):
125+
threads[x].join()
126+
127+
128+
# close all serial connections
114129
for x in range(len(serPort)):
115130
print ("Serial port " + serPort[x] + " closed")
116131
ser[x].close
117-

0 commit comments

Comments
 (0)