This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserialMiddleware.py
113 lines (76 loc) · 2.66 KB
/
serialMiddleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
def sendToArduino(sendStr):
serialConnection.write(sendStr.encode('utf-8'))
def recvFromArduino():
global startMarker, endMarker
ck = ""
x = "z" # any value that is not an end- or startMarker
byteCount = -1 # to allow for the fact that the last increment will be one too many
# wait for the start character
while ord(x) != startMarker:
x = serialConnection.read()
# save data until the end marker is found
while ord(x) != endMarker:
if ord(x) != startMarker:
ck = ck + x.decode("utf-8") # change for Python3
byteCount += 1
x = serialConnection.read()
return(ck)
def waitForArduino():
# wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
global startMarker, endMarker
msg = ""
while msg.find("Arduino is ready") == -1:
while serialConnection.inWaiting() == 0:
pass
msg = recvFromArduino()
print(msg)
print()
def run(td):
numLoops = len(td)
waitingForReply = False
n = 0
while n < numLoops:
teststr = td[n]
if waitingForReply == False:
sendToArduino(teststr)
print("Sent from PC -- LOOP NUM " + str(n) + " STR Sent " + teststr)
waitingForReply = True
if waitingForReply == True:
while serialConnection.inWaiting() == 0:
pass
dataRecvd = recvFromArduino()
print("Reply Received " + dataRecvd)
n += 1
waitingForReply = False
print("===========")
time.sleep(5)
import serial, time
print()
print()
# NOTE the user must ensure that the serial port and baudrate are correct
serPort = "/dev/ttyUSB1"
# serPort = input("List the absolute path to the port like /dev/ttyUSB0 or /dev/cu.usbserial-0001:\n")
baudRate = 115200
arduino = serial.Serial(serPort, baudRate)
print("Serial port " + serPort + " opened Baudrate " + str(baudRate))
time.sleep(2)
while True:
concernLevel = input("Input concern Level: \n")
arduino.write("x" + (str)(concernLevel))
# data = arduino.read(1)
# if data:
# print data
# startMarker = 60
# endMarker = 62
# waitForArduino()
# testData = []
# concern = input("ConcernValue:\n")
# while (concern != 0) :
# testdata will send a string followed by the RGB values or the concern level
# for the testData to be valid correct fields needs to be setup on the arduino code
# to send New RGB Colors
# testData.append("<update, 40>")
# to send ConcernValue
# testData.append("<update," + (str)(concern) + ">")
# run(testData)
arduino.close