|
4 | 4 |
|
5 | 5 | #=====================================
|
6 | 6 |
|
7 |
| -def sendToArduino(sendStr): |
8 |
| - ser.write(sendStr.encode()) |
9 |
| - |
| 7 | +def waitForArduino(port): |
10 | 8 |
|
| 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) |
11 | 21 | #======================================
|
12 | 22 |
|
13 |
| -def recvFromArduino(): |
| 23 | +def recvFromArduino(port): |
14 | 24 | global startMarker, endMarker
|
15 | 25 |
|
16 | 26 | ck = ""
|
17 | 27 | x = "z" # any value that is not an end- or startMarker
|
18 | 28 | #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() |
20 | 30 | x = x.decode("utf-8")
|
21 | 31 | #print(x)
|
22 | 32 | # wait for the start character
|
23 | 33 | while ord(x) != startMarker:
|
24 |
| - x = ser.read() |
| 34 | + x = ser[port].read() |
25 | 35 | x = x.decode("utf-8")
|
26 | 36 | #print(x)
|
27 | 37 | # save data until the end marker is found
|
28 | 38 | while ord(x) != endMarker:
|
29 | 39 | if ord(x) != startMarker:
|
30 | 40 | ck = ck + x
|
31 | 41 | #byteCount += 1
|
32 |
| - x = ser.read() |
| 42 | + x = ser[port].read() |
33 | 43 | x = x.decode("utf-8")
|
34 | 44 | #print(x)
|
35 | 45 | return(ck)
|
36 | 46 |
|
| 47 | +#===================================== |
37 | 48 |
|
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()) |
54 | 51 |
|
55 |
| - print (msg) |
56 |
| - print ("") |
57 |
| - |
58 | 52 | #======================================
|
59 | 53 |
|
60 | 54 | def runTest(td):
|
@@ -87,27 +81,37 @@ def runTest(td):
|
87 | 81 | import time
|
88 | 82 | import sys
|
89 | 83 |
|
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)) |
98 | 84 |
|
| 85 | +NUMBER_OF_SLAVES = 2 |
99 | 86 | startMarker = 60
|
100 | 87 | 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("") |
101 | 102 |
|
102 |
| -waitForArduino() |
| 103 | +for port in range(len(serPort)): |
| 104 | + waitForArduino(port) |
103 | 105 |
|
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) |
111 | 113 |
|
112 |
| -ser.close |
| 114 | +for x in range(len(serPort)): |
| 115 | + print ("Serial port " + serPort[x] + " closed") |
| 116 | + ser[x].close |
113 | 117 |
|
0 commit comments